MySQL 创建主键,外键和复合主键的方法,需要的朋友可以参考下。
1.创建主键语法文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
ALTER TABLE table_name ADD CONSTRAINT pk_name PRIMARY KEY(列名);文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
2.创建外键语法文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
ALTER TABLE news_info[子表名] ADD CONSTRAINT FK_news_info_news_type[约束名] FOREIGN KEY (info_id)[子表列] REFERENCES news_type[主表名] (id)[主表列] ;文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
3.使用组合主键文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
如果一列不能唯一区分一个表里的记录时,可以考虑多个列组合起来达到区分表记录的唯一性,形式文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
①创建时:create table sc (文章源自很文博客https://www.hinwi.com/很文博客-https://www.hinwi.com/47937.html
studentno int,
courseid int,
score int,
primary key (studentno,courseid) );
②修改时:alter table tb_name add primary key (字段1,字段2,字段3);
评论