MySQL约束笔记

MySQL 查看约束,添加约束,删除约束 添加列,修改列,删除列
· 查看表的字段信息:desc 表名;
· 查看表的所有信息:show create table 表名;
添加主键约束:alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);
如:ALTER TABLE business ADD CONSTRAINT pk_bid PRIMARYK KEY business bid

设置主键自增长
ALTER TABLE business MODIFY bid INT AUTO_INCREMENT
· 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段); · 删除主键约束:alter table 表名 drop primary key; · 删除外键约束:alter table 表名 drop foreign key 外键(区分大小写); · 修改表名:alter table t_book rename to bbb; · 添加列:alter table 表名 add column 列名 varchar(
30); · 删除列:alter table 表名 drop column 列名; · 修改列名MySQL: alter table bbb change nnnnn hh int; · 修改列名SQLServer:exec sp_rename't_student.name','nn','column'; · 修改列名Oracle:alter table bbb rename column nnnnn to hh int; · 修改列属性:alter table t_book modify name varchar(22); 主键约束 create table member5( id int(11), --添加主键自增长约束  可直接在字段后面跟上PRIMARY KEY AUTO_INCREMENT username varchar(32) not null, age int(11) , birthday date , email varchar(32) unique, constraint pk_id primary key (id), constraint ck_age check (age between 0 and 150) ); 外键约束 create table book( id int(11), bookname varchar(32) not null, mid int(11), constraint fk_mid foreign key(mid) references member(id) ) 修改约束 alter table book drop primary key; alter table book drop foreign key fk_book ; alter table book add constraint pk_book primary key (id); alter table book add constraint fk_book foreign key (mid) references member(id);
原文地址:https://www.cnblogs.com/StanLong/p/6867536.html