思维导图
一.create语句
语句格式:
create table 表名
(
列1名 列类型 约束,--约束有5种primary key,not null,unique,check,foreign key
列2名 列类型 约束,
.
.
constraint 约束名 约束类型
)
可以直接定义表的结构:表名,列名,列类型,默认值,约束
create table student
(
sno number(10),
sname varchar2(10) not null,
ssex varchar2(2) not null,
sdream varchar2(10) default 'no' unique,
constraint student_pk primary key(sno),
constraint student_sdream check(sdream is not null),
constraint student_sno foreign key(sno) references student_no(sno)
);
也可以通过已有的表建立新表
create table student2 as (select * from student);
二.alter语句
语句格式:
alter table 表名
[add 列名 列类型 默认值 约束 | --添加列
modify 列名 列类型 默认值 约束 | --修改列
rename column 列名 to 新列名 | --重命名列
drop column 列名 | --删除列
add constraint 约束名 约束类型 | --添加约束
drop constraint 约束名 | --删除约束
enable constraint 约束名 | --启用约束
disable constraint 约束名 | --停用约束
]
添加一列
alter table student add sclass varchar2(10) default 'no' not null;
修改一列
alter table student modify sclass number(10) default 10;
改列名
alter table student rename column sclass to class;
删除一列
alter table student drop column class;
添加约束
alter table student add constraint studnet_sno primary key(sno);
删除约束
alter table student drop constraint studnet_sno;
使约束生效
alter table student enable constraint student_sno;
使约束失效
alter table student disable constraint student_sno;
三.drop语句
语句格式:
drop table 表名 (cascade constraints) --级联删除,删除这个表的同时删除和该表有关系的其他对象
删除表
drop table student cascade constraints;