drop:删除属性或约束,常见格式
add:添加属性或约束
modify:修改属性类型及约束
change:重命名属性修改类型及约束
constraint:手动指定约束名
create database test;
use test;
create table Students(
S_ID integer,
S_NAME varchar(10) not null,
S_AGE integer,
S_TEL varchar(11) null,
primary key(S_ID));
insert into Students values(1,'TOM',14,'13223616367'); #插入元组
insert into Students values(2,'JACK',14,'13245277581'); #插入元组
select * from Students; #Students表展示
#删除S_TEL属性
alter table Students drop column S_TEL;
select * from Students;
#添加S_TEL属性
alter table Students add column S_TEL varchar(11);
select * from Students;
#修改S_TEL类型以及约束
alter table Students modify column S_TEL varchar(15);
select * from Students;
#属性重命名,修改类型以及约束
alter table Students change column S_TEL S_phonenum varchar(11);
select * from Students;
#表重命名
alter table Students rename to Stu;
select * from Stu;
#删除主键
alter table Stu drop primary key;
select * from Stu;
#添加主键
alter table Stu add primary key(S_ID);
select * from Stu;
#添加唯一性约束
alter table Stu add constraint cons_0 unique index (S_phonenum);
select * from Stu;
#删除唯一性约束
alter table Stu drop index cons_0;
select * from Stu;