Oracle数据库基础

创建表空间

  注意:创建表空间需要使用有超级管理员权限的用户才能进行操作,例如SYSTEM用户

# 创建一个名为test的表空间
create tablespace test datafile 'D:\oracle\test.dbf' size 100M autoextend on next 10M;			
# datafile 指定数据持久化的路径
# size 设置数据文件的默认大小
# autoextend on 设置自增长值,在达到最大文件大小时会自动对文件进行扩容

# 删除表空间
drop tablespace test

控制用户权限

  和MySQL不同的是,Oracle的管理单位是用户,所以在实际开发中,基本是一个项目一个用户,就和MySQL中一个项目一个数据库一样。注:以下命令只能在具有DBA权限的用户下执行

# 创建用户
create user test identified by test default tablespace test;	
# identified by 用户密码
# default tablespace 默认表空间

# 用户授权
# Oracle的常用角色:connect-连接角色,基本角色;  resource-开发者角色;   dba-超级管理员角色
grant resource to test;
# 当然也可以授予指定的权限,Oracle常用的权限:
# create session 创建会话权限,允许用户登录客户端
# create table 创建表权限
# create sequence 创建序列权限
# create view 创建视图权限
# create procedure 创建过程权限
grant create table, create view to test;

# 指定用户可以操作的表空间
# quota 可以操作的表空间大小,unlimited 没有限制,或者是具体的值
alter user test quota unlimited on tablespace;

# 修改用户密码
alter user test identified by password

# Oracle除了默认角色,我们还可以自己创建角色并分配权限
create role my_role;
# 赋予角色权限
grant create session,create table,create view to my_role;
# 赋予用户角色
grant my_role to test;

# 对象权限,可以将自己某张表的某些权限给其他用户
grant select,update,insert,delete on USER.table to USER;

# 权限授予后也可以收回
revoke select,update,insert,delete on USER.table from USER;

数据库权限
Oracle数据库基础_第1张图片

Oracle数据类型
  Varchar varchar2:表示一个可变长度的字符串,可以定义最大字符串长度
  char:定长字符串
  number:有两种形式,number(n)和number(m,n),前者表示一个长度为n的整数,后者表示长度为m,小数位为n的小数
  data:日期类型,精确到年月日时分秒
  clob:大文本数据类型,最大可存4GB
  blob:二进制数据,最大可存4GB

创建表格

# 创建person表
# 方式一:直接创建
create table person (
       pid number(20)primary key,
       pname varchar2(10)
)
# 方式二:依赖已经有的表来创建,注意会将已有的表的数据复制到新表,如果想创建空表,可以加一个不存在的条件
create table table
as
select column1, column2, column3, column4
from target_table where 1 = 2;

# 添加注释
comment on column person.pid is '主键' 

# 添加列
alter table person add (gender number(1),phone varchar2(11))

# 修改列类型
alter table person modify (gender char(1))

# 修改列名称
alter table person rename column gender to sex

# 删除一列
alter table person drop column sex

# 删除表
drop table person

# 清空表
truncate table person (不可回滚)
delete from person (可以回滚)

# 修改表名
rename person to new_person

SELECT 查询语句

# 语法
select ... from ...

# 查询表中所有的数据
select * from table

# 查询表中所有的列
desc table

# 查询表中某一列
select column1,column2,column3 from table

# 别名,将查询的列起一个新的名字,as 可以省略
select column1 as 别名 from table

# 连接符||,将查询的字段拼接成一句话
select column1 || ' 这里可以添加自己的字符 ' || column2 from table

# distinct去重查询
select distinct column from table

# where 条件查询
select * from table where 条件
# between and 条件,满足在条件值范围内的数据
select * from table where column between 1 and 6
# in 条件,满足条件值的数据
select * from table where column in (1,6,9)
# like 模糊查询,使用%进行模糊匹配,或者使用_进行占位匹配
# 示例1:查询以a结尾的数据
select * from table where column like '%a'
# 示例2:查询以a开始的数据
select * from table where column like 'a%'
# 示例3:查询包含a的数据
select * from table where column like '%a%'
# 示例4:查询第3个字符是a的数据,用_进行占位
select * from table where column like '__a%'
# 示例5:查询包含_的数据
select * from table where column like '%\_%' escape '\'
# is null 空值查询,非空加not
select * from table where column is null
# 其他的例如 < > >= <= <> != and or not 就不在进行演示了

# order by 排序,desc 倒叙,asc 顺序,使用逗号拼接多层排序
select * from table order by column1 desc,column2 asc

单行函数

# 大小写控制函数
# lower 转换为小写
# upper 转换为大写
# initcap 每个单词的首字母大写
select lower('HELLO WORLD'),upper('Hello World'),initcap('HelloWorld') from dual;
# 示例:例如我们要查询某个英文字符,但是不确定是大写还是小写
select * from table where lower(column) = 'string'

# 字符控制函数
# concat(str1,str2) 将两个字符拼接成一个
# substr(str,index,lenght) 截取字符串,下标从1开始
# length(str) 字符串的长度
select concat('hello','world'),substr('helloworld',3,2),length('hello world') from dual;
# instr(str,char) 字符在字符串首次出现的下标,返回0表示当前字符不在字符串中
select instr('hello world','or') from dual;
# lpad(str,lenght,'*') 右对齐,如果字符串的长度小于lenght,则在左边以*好补齐,大于lenght会截取lenght长度的字符串
select lpad('hello',10,'*') from dual
# rpad(str,lenght,'*') 左对齐,和右对齐相反
select rpad('hello',10,'*') from dual;
# trim(str1 from str2) 从str2从去除str1,只能去掉首尾的字符
select trim('o' from 'oheollo oworldo') from dual;
#replace(str,str1,str2) 将str中的str1替换成str2,替换所有的
select replace('hello word','o','l') from dual;

# 数字函数
# round(num,n) 四舍五入,n默认0不保留小数,为正数表示小数点后第n位四舍五入,负数表示小数点前n位四舍五入
select round(12.23),round(23.55),round(458.56,2),round(459.45,-2) from dual;
# trunc(num,n) 截断,n默认0不保留小数,为正数表示小数点n位后截取,为负数表示小数点前n位截取
select trunc(12.23),trunc(23.55),trunc(458.565,2),trunc(459.45,-2) from dual;
# mod 求余
select mod(521,7) from dual;

# 日期函数
# 对日期进行加减
select sysdate,sysdate+1,sysdate-3 from dual;
# 日期差(不能求日期和)
select sysdate-date from table;
# months_between(date,data) 两个日期间隔的月数
select months_between(sysdate,date) from table;
# add_months(date,n) 向指定的日期加上月数
select add_months(sysdate,2) from dual;
# next_day(date,'星期*') 指定日期的下一个星期几对应的日期
select next_day(sysdate,'星期五') from dual;
# last_day 本月的最后一天
select last_day(sysdate) from dual;

# 转换函数
# oracle 隐式转转可以自动进行转换,转换规则:
# date <---> varchar2 <---> number
# 显示转换函数
# to_date(column,格式)  
# to_number(column,格式) 转换为数字
select to_number('$256325540','$999999999999') from dual;
# to_char(column,格式) 转换为字符串
# 示例1:日期转字符串,注意格式,如果有时分秒,oracle中使用HH24:mi:ss
select to_char(sysdate,'yyyy/mm/dd') from dual;
# 示例2:数字转字符,注意格式中只能是0或9,并且数字长度必须小于格式长度格式为0表示会补全长度,9不会补全,格式前也可以加货币符号,加L表示当地的货币符号
select to_char(5248566,'999,999,999,999'),to_char(5248566,'000,000,000,000'),,to_char(5248566,'L000,000,000,000')from dual;

# 通用函数,适用于任何数据类型,包括空值
# nvl(expr1,expr2) 当expr1为空时用expr2代替,注意两个类型必须相同
select nvl(column,0) from table;
# nvl2(expr1,expr2,expr3) expr1不为null返回expr2,为null返回expr3
select nvl2(column,column + 1,100) from table;

# 条件表达式,当满足第一个条件返回的值,否者满足第二个条件返回值,一次类推
# case条件表达式
select column1,column2,column3,column4,
		case column1 when 10 then column4 * 1.1	
                   	when 20 then column4 * 1.2
                   else column4 * 1.3 
                     end  new_column4
from table
where column1 in (10,20,30)
# decode函数
select column1,column2,column3,column4,decode(column1,10,column4* 1.1,
                                                      20,column4* 1.2,
                                                      column4* 1.3) new_column4
from table
where column1in (10,20,30)

多表查询

# 内连接
# 等值连接,多个表中关联的字段值相等
select table1.column, table2.column FROM table1, table2 WHERE table1.table2_id= table2.id;
# 非等值连接,表一的字段值在表二的某个范围内
select table1.column1,table1.column2,table2.column1 FROM table1,table2 WHERE table1.column BETWEEN table2.column1 AND table2.column2

# 外连接
# 左外连接,左表中即使不满足条件也会全部查询
select table1.column, table2.column FROM table1, table2 WHERE table1.table2_id= table2.id(+);
# 右外连接,右表中即使不满足条件也会全部查询
select table1.column, table2.column FROM table1, table2 WHERE table1.table2_id(+)= table2.id;

# 自然连接,不用自己定义查询条件,根据表中相同的字段自动进行条件判断,并且会自动判断字段在那张表中
select column1,column2 from table1 natural join table2;
# 当然也可以选择其中的某一个字段作为判断条件,这个条件字段必须在两个表中都存在
select column1,column2 from table1 join table2 using (column3);
# 最后还是使用on来连接查询条件   join    on,如果有相同的字段需要指定这个字段从哪个表中获取
select table1.column1,column2 from table1 join table2 on table1.table2_id = table2.id;
# join on 可以实现左、右、满外连接,具体的语法如下(outer可以省略)
# 左外连接:left outer join on
# 右外连接:right outer join on
# 满外连接:full outer join on

# 自连接,一个表中的两个字段相互关联,可以看成两个相同的表进行查询
select e.column1,m.column1,m.column2,m.column3 from table e ,table m where e.id= m.m_id

分组函数

# 组函数
# avg 平均值,只能是number类型
# count 计数,没有类型的限制,但只会统计非空的值
# max 最大值,没有类型的限制
# min 最小值,没有类型的限制
# sum 求和,只能是number类型
select avg(column),max(column),min(column),sum(column),count(column) from table;
# 如果不想统计重复的可以加distinct 关键字
select count(distinct column) from table;

# group by 分组,注意select列表中所有未包含在组函数中的列都应该包含在group by 子句中
select column from table group by column;

# having 条件
# 组函数查询条件过滤使用having而不是使用where
select colum1n,avg(column2) from table having avg(column2) > 6000 group by column1;

子查询

# 单行子查询
# 一条sql查询的结果是另一条sql的条件,这种情况我们就可以使用子查询
select column1, column2 from table where column2 > (select column2 from table where lower(column1) = 'str');

# 多行子查询
# 子查询sql查询到的值有多个,并不是唯一的,这种情况可以使用多行子查询,关键字:
# in 等于列表中任意一个,即条件字段只要等于列表中的任意一个就成立
SELECT column1, column2, column3
FROM table
WHERE column2 != 'str'
  AND column3 in (select column3
                    from table
                    where column2= 'str')
# any 和列表中的某一个值比较,如果条件字段比列表中任一一个的值都大或小条件成立,即比较最值
SELECT column1, column2, column3
FROM table
WHERE column2 != 'str'
  AND column3 < ANY (select column3
                    from table
                    where column2= 'str')
# all 和列表中的所有值比较,如果条件字段比列表中所有的值都大或小条件成立,即比较最值
SELECT column1, column2, column3
FROM table
WHERE column2 != 'str'
  AND column3 < all (select column3
                    from table
                    where column2= 'str')

数据处理(增删改)

# 插入数据
# 插入一条数据,注意如果不写要插入的列名,默认插入全部列并且顺序按照表中的列顺序
insert into table (column1,column2) values (value1,value2)
# 从其他表中引入数据,如果不加where条件会将所有的数据都插入到新表中
insert into target_table (column1,column2)
select column1,column2
from source_table
where id = 80;

# 修改数据,注意添加where条件,否则会修改全部
update table set column1 = value1, column2 = value2 where id = 1;

# 删除数据,如果没有where条件会将所有的数据都删除
delete from table where id = 1

数据库事务

# 事务操作仅支持对增删改的操作
# 提交,将数据写到磁盘上,事务提交后就不能进行回滚
commit ;
# 回滚,将数据回滚到上一次执行commit之后的数据
rollback ;

# 控制事务,指定回滚的位置
sql1;
savepoint A; # 设置保存点A
sql2;
savepoint B; # 设置保存点B
sql3;
savepoint C; # 设置保存点C
rollback to savepoint B;  # 回滚到指定的点

# 注意:事务没有提交前,其他用户是不能看到当前用户所做的改变,只有提交后才能看到

约束

# 五种约束条件
# not null 非空,只能定义在列上
# unique 唯一,插入的数据不能重复,可以是空值
# primary key 主键,不能插入空值,并且唯一
# foreign key 外键,表中的字段关联另一张表的字段,外键关联的字段
# check 检查
# 在创建表示创建约束条件
create table tableName
(
	# 方式一:直接在列级定义
    id            number(20)   primary key, # 主键
    name          varchar2(20) not null, # 非空
    salary        number(10, 2) check ( salary > 1500 and salary < 30000), # 检查,salary 在1500-30000范围内才能插入成功
    dept_id        number(10) unique, # 唯一约束
    t_id number(10) references table2 (id), # 外键关联
    # 方式二:在表级定义
    # 外键,注意可以设置父表删除时子表的操作,on delete cascade:父表删除时子表也删除  on delete set null:附表删除时子表设为空
    constraint t_id_fk foreign key (t_id) references table2 (id) on delete cascade, --on delete set null
    # 唯一约束
    constraint t_dept_id unique (dept_id),
    # 主键
    constraint t_id primary key (id)
);
# 在已有的表中添加约束
# 添加非空约束
alter table table1 modify (name varchar2(10) not null);
# 删除约束
alter table table1 drop constraint 约束名;
# 添加唯一约束
alter table table1 add unique (job_id);
# 约束不生效
alter table table1 disable constraint 约束名;
# 约束失效后在重新启用
alter table table1 enable constraint 约束名;

# 查询表的约束条件
select * from USER_CONSTRAINTS where TABLE_NAME = 'table';
# 查询约束定义的列
select * from USER_CONS_COLUMNS where TABLE_NAME = 'table';

视图

  视图是从表中抽出的逻辑上相关的数据集合,是一种虚表, 它建立在已有的表的基础上,它是向用户提供基表数据的另一种形式,可以控制数据访问,简化查询,避免重复访问相同的数据

# 创建视图
create view tableview
as
select column1, column2, column3
from table
where column1 = 80;
# 视图和基表中的数据是相互关联的,即只要一个表改变,另一个表也会进行相同的改变

# 修改视图 create or replace
create or replace view tableview
as
select column1, column2, column3, column4
from table
where column1 = 80;

# 视图只读
create view tableview
as
select column1, column2, column3
from table
where column1 = 80
with read only;

# 视图的数据操作和表的操作是一样的,就不在详说了
# 复杂视图,上面的视图都是简单视图,但视图查询语句中有组函数时就是复杂视图了
# 复杂视图不能进行增删改操作,只能进行查询,但是可以对基表进行修改

# 删除视图
drop view tableview;

Top-N 分析

  Top-N 分析 查询一个列中最大或最小的n个值,首先需要知道一个伪列—ROWNUM,表示每一行的行数,它有一个默认排序是按照主键的顺序排序,也就是说在查询是默认是按照主键的顺序查询,所以order by 就不会生效,可以将查询到的数据当成一个表在进行查询

select column1, column2, column3
from (select column1, column2, column3
      from table
      order by column3 desc)
where ROWNUM <= 10;

# 可以用Top-N 来进行分页,ROWNUM 是虚列,并且是有< 和<= 操作,所以没办法直接对它就行比较,我们可以将虚列转为实列,将查询到ROWNUM 起个别名在重新进行查询就可以比较操作了,可用于分页
select column1, column2, column3
from (select ROWNUM rn, column1, column2, column3
      from table
      order by column3 desc)
where rn <=10 and rn > 5;

序列

  在Oracle没有自增的概念,但是有序列,序列就是提供有规律的唯一数值,例如我们的主键需要自增时,可以在赋值时使用序列

# 创建序列
create sequence seq_id
	increment by 1 # 每次增长值
	start with 1 # 初始值
	maxvalue 99999999 # 最大值
	minvalue 1 # 最小值
	cycle / nocycle # 是否循环
	nocache / cache # 是否缓存

# 查询序列的值,nextval下一个值,currval当前值
select seq_id.nextval, seq_id.currval from DUAL;
select * from USER_SEQUENCES;

# 修改序列,只能修改序列的增量,最大值,最小值,循环,缓存,不能修改序列的初始值
alter sequence seq_id increment by 2 nocycle;

# 删除序列
drop sequence seq_id;

# 注意,如果事务回滚、系统异常、多表使用同一个序列,序列会出现裂缝,就是值不连续,中间有断层

索引

  索引可以加快查询速度

# 创建索引
# 主键primary key 和唯一约束unique 在创建时会自动创建索引
# 手动创建
create index table_id_index on table(column);

# 删除索引
drop index emp01_id_index;

Set 运算符

# 常用的Set 运算符有一下集中:
# union/union all 并集,注意union会去掉重复的数据,而union all不会去掉重复的数据
select column1,column2 from table1 union all select column1,column2 from table2;
# intersect 交集
select column1,column2 from table1 intersect select column1,column2 from table2;
# minus 差集
select column1,column2 from table1 minus select column1,column2 from table2;

# 注意:两个表查询的数据进行set运算时,查询的列名和表达式在数量和数据类型上要相对应

高级子查询

# 多列子查询,子查询会返回多个查询结果,where判断的条件字段必须和子查询返回的字段一一对应
select column1, column2, column3
from table
where (column4, column5) in (select column4, column5 from table where id in (141, 174))

# 在from子句中使用子查询,from中的查询条件当成一个新的表进行操作
select column1, column2, column3, T2.AVG_1
FROM TABLE T1,
     (SELECT column2, AVG(column3) AVG_1 FROM TABLE GROUP BY ID) T2
WHERE T1.column2 = T2.column2

# 相关子查询,按照一行接一行的顺序执行,主查询的每一行都执行一次子查询
select column1, column2, column3
from TABLE1 T1
where 2 <= (select count(*) from TABLE2 where ID = T1.ID);

# EXISTS 操作符,检查在子查询中是否存在满足条件的行,如果满足就不在进行查找并返回true,不满足就继续查找返回false,注意,子查询查询的字段可以使任意的东西
select column1, column2, column3
from TABLE1 T1
where exists (select 1 from TABLE2 T2 where T1.id = T2.id)

# NOT EXISTS 操作符,与EXISTS 相反
select column1, column2, column3
from TABLE1 T1
where not exists (select 1 from TABLE2 T2 where T1.id = T2.id)

# 相关更新,使用相关子查询依据一个表中的数据更新另一个表的数据
update TABLE1 T1
set column1 = (select column1 from TABLE2 T2 where T2.ID = T1.ID)

# 相关删除,使用相关子查询依据一个表中的数据删除另一个表的数据
delete from TABLE1 T1
where ID in (select ID from TABLE2 T2 where T2.ID = T1.ID);

# with子句,将该字句中的语句块执行一次并存储到用户的临时表空间中,避免在select语句中重复书写相同的语句块,并且可以提高查询效率,多个with块用逗号隔开
with test_with as (select ID from TABLE)
select column1, column2, column3
from TABLE
where ID IN (select ID from test_with);

你可能感兴趣的:(Oracle,oracle,数据库)