1. 添加列级约束

alter table 表名 modify column 字段名 字段类型 新约束

例如:alter table student modify column age int not null;

添加字段唯一:alter table student modify column stu_nu varchar(20) unique;

2. 添加表级约束

alter table 表名 add [constraint 约束名] 约束类型(字段名) [外键的引用]

例如添加外键:alter table student add constraint fk_stu_class foreign key(class_id) references class(id);

添加主键:alter table student add constraint pk_stu primary key(id);

添加字段唯一:alter table student add unique(stu_nu);

3.删除列级约束

例如删除非空约束

alter table student modify column age int null;

删除主键:

alter table student drop primary key;

删除唯一键(可以使用show index from student查看唯一)

alter table student drop index index_name;

删除外键:

alter table student drop foreign key fk_name;

 

相关文章:

  • 2021-04-11
  • 2021-08-15
  • 2021-11-10
  • 2022-12-23
  • 2021-07-18
  • 2021-10-09
  • 2021-09-15
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2021-08-11
  • 2021-05-02
  • 2022-02-11
  • 2022-12-23
相关资源
相似解决方案