Oracle基础教程

体系结构

数据库

一个操作系统仅有一个数据库

实例

拥有一系列后台进程和存储结构,一个数据库可拥有一个或多个实例,一般只有1个实例

数据文件(.dbf/.ora)

数据文件是数据库的物理存储单元,一个表空间由一个或多个数据文件组成,一个数据文件只能属于一个表空间。当某个数据文件被加到某个表空间后,就不能删除这个文件,如果要删除某个数据文件,只能删除其所属的表空间

表空间

实质上是对数据文件的一种逻辑映射,大小由所包含的数据文件所定义,每个数据库至少有一个表空间

用户

用户在表空间下创建

oracle数据库相当于房间,表空间相当于房间内的空间,可以自由分配,数据文件相当于空间内堆放的箱子,表则为箱子内放的物件。

表空间的相关操作

查看表空间

sql 复制代码
select * from dba_tablespaces;
select tablespace_name from dba_tablespaces;
desc dba_tablespaces;

创建表空间

sql 复制代码
create tablespace 表空间的名字
datafile 'XXX.dbf' # 指定表空间的数据文件
size XXXm # 指定数据文件的大小
autoextend on next YYm # 开启自动增长,指定每次扩容的大小
maxsize unlimited; # 限制数据文件最大的大小,unlimited时,最大为32G

查看数据文件

sql 复制代码
select * from dba_data_files;
select file_name from dba_data_files;

更改表空间

扩容1:增加数据文件大小

sql 复制代码
alter database datafile 'XXX.dbf' resize YYYm;

扩容2:增加数据文件数量

sql 复制代码
alter tablespace 表空间的名字 add datafile '' size XXXm autoextend on next YYm maxsize unlimited;

删除数据文件

sql 复制代码
alter tablespace 表空间的名字 drop datafile 'xxx.dbf';

删除表空间

sql 复制代码
drop tablespace 表空间的名字;

用户相关操作

角色,默认的有connect、resource、dba

sql 复制代码
select * from dba_role_privs where grantee = '用户名大写';

权限

sql 复制代码
select * from system_privilege_map; # 查看所有系统权限
select distinct privilege from dba_tab_privs; # 查看所有对象权限

select * from dba_sys_privs where grantee = 'CONNECT';
select * from dba_sys_privs where grantee = 'RESOURCE';
select * from dba_sys_privs where grantee = 'DBA';

select * from dba_sys_privs where grantee = '角色名称';

查看所有用户

sql 复制代码
select * from dba_users;
select * from all_users;
select * from user_users; # 查看当前用户信息

查看用户拥有的表

sql 复制代码
select * from all_tables where owner = '用户名大写';

切换用户

sql 复制代码
conn 用户名 # scott 用户默认密码为:tiger

修改账户状态

sql 复制代码
alter user 用户名 account lock|unlock;

创建用户

sql 复制代码
create user 用户名
identified by 密码
default tablespace 表空间名
alter user 用户名 identified by 密码 # 修改密码

用户系统权限赋予与撤回

sql 复制代码
grant XXX to 用户名
grant XXX to 用户名 with admin option # 可以把自己的权限赋予其他用户
revoke XXX from 用户名

用户对象权限赋予与撤回

赋予、撤回某项权限

sql 复制代码
grant XXX on USERNAME.TABLENAME to 用户名;
revoke XXX on USERNAME.TABLENAME from 用户名;

赋予、撤回所有权限

sql 复制代码
grant all on USERNAME.TABLENAME to 用户名;
revoke all on USERNAME.TABLENAME from 用户名;

删除用户

sql 复制代码
drop user 用户名
drop user 用户名 cascade # 删除用户及下属所有表

角色相关操作

创建角色

sql 复制代码
create role 角色名称

赋予与撤回权限

sql 复制代码
grant XXX to 角色名称
revoke XXX from 角色名称

删除角色

sql 复制代码
drop role 角色名称 

表相关操作

建表

sql 复制代码
create table 表名(
字段名 字段类型(长度) 限制,
字段名 字段类型(长度) 限制,
字段名 字段类型(长度) 限制,

constraint 限制名称 cons,
constraint 限制名称 cons,
constraint 限制名称 cons
) tablespace 表空间名;

字段类型

  1. number(n, m): 数字类型,n代表数字范围为n(包含m),m指定小数点后精度,默认n=18
  2. char(n): 字符串类型,长度固定,用空格扩充,最大存储2000B
  3. varchar2(n): 字符串类型,长度可变,最大存储4000B
  4. long : 存储4G
  5. date:日期类型的数据,sysdate
  6. clob:存储字符,最大4G
  7. blob:存储图像、声音等二进制数据,最大4G

限制

  1. primary key:主键,可以唯一标识一行数据的字段

  2. not null:要求字段非空

  3. unique:要求字段不重复

  4. default VAL:设定字段的默认值

  5. constraint TBALE_PK primary key(COL, COL);

  6. constraint TBALE_FK foreign key(COL) references TABLE_NAME(col); # 参考其他表的主键

  7. constraint AGE_CK check((age>0) and (age<200));

  8. constraint GENDER_CK check(GENDER in ('M', 'F', 'O'));

操作约束
查看
sql 复制代码
select constraint_name, constraint_type from user_constraints where table_name = '';
constraint_type
  1. C : check
  2. P : primary key
  3. U : unique
  4. R : foreign key
其他
sql 复制代码
alter table 表名 add  constraint 限制名称 cons # 添加约束
alter table 表名 disable constraint 限制名称 # 关闭约束
alter table 表名 enable constraint 限制名称 # 开启约束
alter table 表名 drop constraint 限制名称 # 删除约束

示例

sql 复制代码
create table class(
 cid number(4) primary key,
 teacher varchar2(20) not null,
 phone char(4) unique
);

create table student(
 sid number(4) primary key,
 sname varchar2(20) not null,
 gender char not null,
 phone char(4) unique,
 age number(3),
 cid number(4),
 constraint AGE_CK check((age>0) and (age<150)),
 constraint GENDER_CK check(GENDER in ('M', 'F', 'O')),
 constraint STU_FK foreign key(cid) references class(cid)
);

插入数据

sql 复制代码
insert into 表名(字段名, 字段名,....)  values(值1, 值2, .....);
insert into 表名 values(值1, 值2, .....);

修改数据

sql 复制代码
update 表名 set 字段名=值 where.....

删除数据

sql 复制代码
delete from 表名 where....
delete from 表名
truncate table 表名

修改表

sql 复制代码
rename 旧表名 to 新表名

增加字段

sql 复制代码
alter table 表名 add(
字段名 字段类型(长度) 限制,
字段名 字段类型(长度) 限制,
字段名 字段类型(长度) 限制
);

修改已有字段类型

sql 复制代码
alter table 表名 modify(
字段名 字段类型(长度) 限制,
字段名 字段类型(长度) 限制,
字段名 字段类型(长度) 限制
);

修改字段名称

sql 复制代码
alter table 表名 rename column 旧字段名 to 新字段名;

删除字段

sql 复制代码
alter table 表名 drop column 字段名;

删除表

sql 复制代码
drop table 表名;

查询

sql 复制代码
select * from 表名; # 查询所有数据
select 字段名,字段名, from 表名;

单表查询

条件查询

sql 复制代码
select * from 表名 where ... 
select * ftom 表名 where 字段=值;
模糊查询:%用于匹配0-n个字符,_用于匹配一个字符
sql 复制代码
where 字段 like  '%KEY%' # 含有KEY的记录
where 字段 like '%KEY' # 以KEY结尾的
where 字段 like 'KEY%' # 以KEY开始的
where 字段 like '_KEY_' # KEY前后各有一个字符的
范围查询
sql 复制代码
where 字段 >|>=|<|<= 值
where 字段 between lo and hi # lo和hi范围之间,包含lo和hi
where 字段 in (值) # 选择
where 字段 not in (值)
空值查询
sql 复制代码
where 字段 is null
where 字段 is not null
查询结果去重
sql 复制代码
select distinct 字段
查询结果排序
sql 复制代码
select * from 表名 oder by 字段 [asc]# 默认升序
select * from 表名 oder by 字段 desc# 降序
聚合统计
sql 复制代码
select sum(字段) from
select max(字段) from
select min(字段) from
select avg(字段) from
select count(*) from
分组统计
sql 复制代码
select 字段,聚合统计 from 表名 group by 字段 # select后面只可以跟聚合函数或者是分组聚合的条件
select 字段,聚合统计 from 表名 group by 字段 having count(*)  > 值 #
练习
  1. 查询每个dept部门分别有多少员工、平均工资是多少

    sql 复制代码
    select deptno, count(*), avg(sal) from emp group by deptno;
  2. 查询每个dept部门、job岗位分别有多少员工、工资最高是多少

    sql 复制代码
    select deptno, job, count(*), max(sal) from emp group by deptno,job;
  3. 查询comm为0的有多少员工

    sql 复制代码
    select count(*) from emp where comm=0 or comm is null;
  4. 查询comm不为0的员工数量及平均工资

    sql 复制代码
    select count(*), avg(sal) from emp where comm is not null and comm > 0;
  5. 查询sal工资在[1200, 3000]内的员工数量

    sql 复制代码
    select count(*) from emp where sal between 1200 and 3000;
    select count(*) from emp where sal>=1200 and sal<=3000;
  6. 查询各岗位的最高工资,并按最高工资降序排序

    sql 复制代码
    select job, max(sal) from emp group by job order by -max(sal);
  7. 查询最低工资小于1500的岗位

    sql 复制代码
    select job, min(sal) from emp group by job having min(sal)<1500;
伪列查询
sql 复制代码
select rowid, t.* from emp t;

select rownum, t.* from emp t; # 在生成查询记录时赋值rownum,从1开始计数
select rownum, t.* from emp t where rownum > 0;
select rownum, t.* from emp t where rownum >= 1;
select rownum, t.* from emp t where rownum <= n; # 前n条记录
select rownum, t.* from emp t where rownum != n; #前n-1条记录

多表连接查询

内连接

sql 复制代码
select * from emp e, dept d;
select e.ename, d.dname, d.loc from emp e, dept d where e.deptno=d.deptno;

左外连接

sql 复制代码
select d.dname, e.ename from dept d left join emp e on d.deptno = e.deptno;
select d.dname, e.ename from dept d, emp e where d.deptno=e.deptno(+);

select d.dname, count(*) from dept d left join emp e on d.deptno = e.deptno group by d.dname;
select d.dname, count(e.ename) from dept d left join emp e on d.deptno = e.deptno group by d.dname;

右外连接

sql 复制代码
select e1.ename as directboss, e2.ename as clerk from emp e1 right join emp e2 on e1.empno=e2.mgr;
select e1.ename as directboss, e2.ename as clerk from emp e1, emp e2 where e1.empno(+)=e2.mgr;

子查询

where子句中加入子查询

单行
sql 复制代码
select ename, sal from emp where sal > (select avg(sal) from emp);
多行
sql 复制代码
select ename, sal from emp where ename in (select ename from emp where ename like '%S');

from子句中加入子查询

sql 复制代码
select * from (select deptno, count(*) as cnt from emp group by deptno) where cnt > 3;

select子句中加入子查询

sql 复制代码
select e.ename, e.sal, (select grade from salgrade s where e.sal between s.losal and s.hisal) salgrade from emp e;

select e.ename, e.sal, g.grade from emp e, salgrade g where e.sal between g.losal and g.hisal;

数据导出与导入

cmd下,文件保存在cmd当前目录

整库导出与导入

shell 复制代码
exp system/orcl file=data.dmp full=y
imp system/orcl file=data.dmp full=y

按用户导出与导入

shell 复制代码
exp system/orcl file=scott.dmp owner='SCOTT'
imp system/orcl file=scott.dmp fromuser='SCOTT'

按表导出与导入

shell 复制代码
exp scott/tiger file=emp.dmp tables='EMP'
imp scott/tiger file=emp.dmp tables='EMP'
相关推荐
huaqianzkh1 小时前
了解MySQL 高可用架构:主从备份
数据库·mysql·架构
向往风的男子2 小时前
【mysql】mysql之读写分离以及分库分表
数据库·mysql
阳光开朗_大男孩儿2 小时前
DBUS属性原理
linux·服务器·前端·数据库·qt
挠背小能手3 小时前
达梦数据库SCHEMA使用初探
数据库·oracle
楠神说软件测试3 小时前
接口自动化框架入门(requests+pytest)
运维·数据库·自动化
惟长堤一痕3 小时前
医学数据分析实训 项目一 医学数据采集
数据库
xuan哈哈哈3 小时前
web基础—dvwa靶场(八)SQL Injection(Blind)
数据库·web安全·网络安全
Lill_bin3 小时前
Lua编程语言简介与应用
开发语言·数据库·缓存·设计模式·性能优化·lua
终末圆4 小时前
MyBatis动态SQL中的`if`标签使用【后端 19】
java·数据结构·数据库·sql·算法·spring·mybatis
拾伍廿肆4 小时前
Django ORM(多表)
数据库·django