基础
Sql*plus
set linesize 设置行宽度 set pagesize 设置页面长度
edit 路径 打开路径所在文档
spool 路径 select…… spool off 将查询结果保存在指定路径 clear 清屏
show error 查看错误
set serveroutput on 打开输出
dbms_output.put_line(“”); 输出数据
desc 表名 查看表结构
用户管理
conn 用户名/密码 连接数据库 disc 断开连接 show user 查看用户 password 用户名 修改密码
create user 用户名 indetified by 密码 创建用户
create role 角色 not indetifid/indetified by 密码 创建角色(不需要验证/验证) 角色是一组权限(系统权限、对象权限)的集合
grant 系统权限 to 用户名/角色[with admin option]
将权限授予用户或角色[可以将权限授予别人,当回收本级权限时不会级联回收] grant 对象权限 to 用户名/角色[with grant option]
将对象权限授予用户或角色[可以将权限授予别人,当回收本级时会级联回收] revoke 权限/角色 from 用户名/角色 回收权限
alter user 用户名 account lock 锁定用户(该用户无法使用,当用户所属的对象可以使用) alter user 用户名 account unlock 解除锁定
drop user 用户名[cascade] 删除用户[将用户所属对象一并删除]
drop role 角色 删除角色,拥有该角色的用户将不再有该角色下的权限
conn 用户名/密码 as sysdba 以sys用户权限登陆(前提该操作系统用户属于dba组)
基本语法 ddl
数据定义语言(create alter drop) ? create
create table 表名(
字段1 数据类型(长度), ……
字段n 数据类型(长度) )
create table 表名 as select…… select * into 新表from 旧表 ? alter
alter table 表名
add 字段 数据类型(长度) 增加字段
modify column字段 字段类型(长度) 修改字段 drop 字段 删除字段
rename 旧字段名 to 新字段名 rename 旧表名 to 新表名 ? drop
drop table 表名 删除表
dml
数据操纵语言(insert update delete select) ? insert
insert into 表名 values(字段1,……字段n); 按表的字段依次顺序插入 insert into 表名 (字段1,字段2) values(值1,值2) 按字段插入 insert into 表名(字段……) select…… 将查询结果插入 ? update
update 表名 set 字段=值 [where 条件] ? delete
delete from 表名 [where 条件]
trunc table 表名(不可回退的删除 速度快) ? select
select [distinct] 字段
from 表名 where 条件 group by 分组
having 分组后筛选条件 order by 排序
进阶
复杂查询 子查询
? 单列查询
select * from 表名 where 字段=(select 字段 from 表名 where 条件) select * from 表名 where 字段 in (select 字段 from 表名 where 条件) ? 多列查询
select * from 表名 where(字段1,字段2)=(select 字段1,字段2 from 表名where 条件) ? all
select * from 表名 where 字段 表达式 all (select ……) ? any
select * from 表名 where 字段 表达式 any (select ……) ? 子表 select *
from t1,(select ……)t2
连接查询
? 自然连接
select * from t1,t2 where t1.z=t2.z
select * from t1 inner join t2 on t1.z=t2.z ? 左连接(左表全部加右表符合条件的) select * from t1 left join t2 on t1.z=t2.z select * from t1,t2 where t1.z=t2.z(+) ? 右连接
select * from t1 right t2 on t1.z=t2.z select * from t1,t2 where t1.z(+)=t2.z ? 全连接
select * from t1 full out join t2 on t1.z=t2.z
分页查询
select * from (select t1.*,rownum num from (select语句)t1 where num<=最大值)t where num>=最小值
合并查询
union 去重复联合查询 union all不去重复 intersect 取交集 minus 取左表有右表没有的值
函数(常用)
单行函数:查多行只出一行(多用于分组) 多行函数:查几行出几行 ? 聚合函数
sum() avg() max() min() count() avg会忽略null值 ? 转换函数
convert(数据类型,字段,格式) cast(字段 as 数据类型) ? 数值函数
abs()绝对值 floor()小于或等于的最大正式 ceil() 大于等于的最小整数 round() 四舍五入 trunc()截取小数 mod(值1,值2)取余 ? 日期函数 add_mouths() ? 字符串函数
substr(字段,起始位置,长度) lower() 将英文换为小写 upper()将英文换为大写 length()查看长度
replace(字段,原值,替换为值) ? 其他函数
case 字段 when 值 then … when 值 then… else … end case when 字段=? then … else … end
decode( 字段,条件1,值1,条件2.值2……) dump(字段) 查看字段类型
nvl(字段,值) 若字段为null则值为值
事务
数据操纵的一组集合
savepoint 保存点名字 设置保存点
……dml
rollback to 保存点名字 回退到保存点,取消设置保存点之后的操作 commit 提交 rollback 回退
?可以设置多个保存点,但保存点只可以回退一次,提交之后不可回退
完整性
? 基本
not null 非空 只能在行级定义
unique 唯一 不可以重复,但可以为null 并且可以多处为null check 检查约束 default 默认
primary key 主键 非空的唯一标示 可以自动创建索引 foreign key 外键 跟主表的主键或者唯一约束所在列对应
建表的时候先建主表再建从表,删除的时候先删从表再删主表 ? 定义
行级定义:在字段名之后直接定义
表级定义:建完字段后再用constraint定义 constraint 约束名 约束类型 字段 ? 修改
alter table 表名
add constraint 约束名 约束类型 字段 modify constraint 约束名 约束类型 字段 drop constraint 约束名
序列
create sequence 序列名 start with 起始值 increment by 步长 maxval 最大值 minval 最小值
cycle/nocycle 循环、不循环 nocache/cache 缓存
序列名.currval 当前值 序列名.nextval 下一个值