赵娜0418

2020-08-27   阅读量: 821

Mysql

mysql如何增加和删除主键外键?

扫码加入数据分析学习群

增删主键:


alter table 表名 modify 主键列名 新列类型(不含auto_increment); --删除自增,才能删除主键


alter table 表名 drop primary key; --删除主键


alter table 表名 add primary key(列名); --添加主键


增删外键:


alter table 子表名 add [constraint fk_sno] foreign key(子表的外键名称) references 父表名(父表的主键名称);


--添加外键约束。fk_sno为外键ID名,若不添加,系统会自动配一个。


alter table 子表名 drop foreign key fk_sno;


--删除外键约束。fk_sno为外键ID名, 若不知,可查询建表明细(show create table 表名)。


36.9535 5 0 关注作者 收藏

评论(0)


暂无数据

推荐课程