记录---Oracle命令整理

–使用子查询复制表数据

--只能复制表结构和表数据,不能复制约束
create table 表名 as select * from emp;

–添加列

alter table 表名 add (列名1 数据类型,列名2 数据类型...);

–修改列类型

alter table 表名 modify 列名 新数据类型;

–修改列名

alter table 表名 rename column 列名 to 新列名;

–删除列

alter table 表名 drop column 列名;

–修改表名

rename 表名 to 新表名;

–删除表

--删除表结构及数据
drop table 表名;
drop table category cascade constraint;--强制删除,先断开约束,再删除category表数据,product表中相关数据还在(不推荐)
--只删除表数据
delete from table 表名;--逐条删除,可加条件删除,支持事务
truncate table 表名;--先删除表再创建表,删除速度快,不能加条件,不支持事务

–单表约束

create table stu(
	stuid number primary key,	--主键约束
	name varchar2(10) unique,	--唯一约束
	age varchar2(10) not null,	--非空约束
	gender varchar2(4) check(gender in ('男','女'))	--检查约束
);

–添加外键约束

--category主表,product从表,插入数据先插主表,删除数据先删从表
alter table product add foreign key(cno) references category(cid);
alter table product add foreign key(cno) references category(cid) on delete cascade;--添加外键约束时设置级联删除,当delete from category where cid='2'时,会将从表中与约束关联的数据一并删除

–使用子查询插入数据

insert into 表名1 select * from 表名2 [where 条件];  

–事务

--事务的隔离级别:read uncommitted,read committed,repeatable read,serializable
--Oracle支持的隔离级别:read committed,serilizable(read only不确定是否支持)
--插入数据,如果有异常,则回滚到保存点处,保存点之前的数据仍执行成功
declare
begin
insert into 表名 values(...);
insert into 表名 values(...);
savepoint 保存点名;--添加保存点或回滚点
insert into 表名 values(...);
commit;
exception
	when others then
		rollback to 保存点名;
		commit;
end;

–创建视图

create [or replace] view 视图名 as select... [with read only];--修改视图会连带修改表中数据

–创建同义词

create synonym 同义词名 for 对象名;

–创建序列

create sequence 序列名
start with 开始数字
increment by 增长数字
maxvalue 最大数字 | nomaxvalue
minvalue 最小数字 | nominvalue
cycle | nocycle   ----是否循环
cache 缓存数量 | nocache  --是否缓存
;
--查看序列号
select 序列名.nextval from dual;--当前序列号+1
select 序列名.currval from dual;--当前序列号,需要先执行nextval

–创建索引

create index 索引名 on 表名(列名);--主键约束自带主键索引,唯一约束自带唯一索引;索引原理是有索引的表以平衡树结构存储,提高查询速度,但插入数据会破坏树结构,导致速度变慢,需要重构索引
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值