目录
一、修改表结构
(用TS_USER表展示)
1.删除列
alter table TS_USER drop column userName;
2.添加列
alter table TS_USER add userName varchar2(100);
3.修改字段的数据类型
//TS_USER表中keyWord 列没有数据
alter table TS_USER alter column keyWord varchar(100);
//TS_USER表中keyWord 列中有数据
alter table TS_USER drop column keyWord;
alter table TS_USER add keyWord varchar(100);
二、添加约束
1.主键约束
alter table TS_USER add constraint PK_TS_USER_userId primary key(userId)
2.唯一性约束
alter table TS_USER add constraint UK_TS_USER_userId unique(userId)
3.check约束
alter table TS_USER add constraint CK_TS_USER_age
check(age>=18 and age<=50)
4.非空约束(修改列的数据类型)
alter table TS_USER alter column userName Varchar2(100) not null
5.外键约束
alter table TS_USER add constraint FK_TS_USER_deptId
foreign key(deptId) references TS_Dept(deptId)
6.删除约束
//删除单条约束
alter table TS_USER drop constraint Fk_TS_USER_userId
//删除多条约束
alter table TS_USER drop constraint Fk_TS_USER_userId,CK_TS_USER_deptId
7.一条语句添加多个约束
alter table TS_USER add
constraint FK_TS_USER_deptId foreign key(deptId) references TS_DEPT(deptId)
constraint PK_TS_USER_stuId primary key(stuId)
三、删除数据库数据
1.删除数据,不删除表结构
//删除全部数据
delete from TS_USER;
//删除全部数据(速度快)
truncate table TS_USER;
//删除单条数据
delete TS_USER where userId = '';
2.删除数据,并删除表结构
drop table TS_USER;