MySQL如何创建主键,外键和复合主键

如题所述

courseidint,
scoreint,primarykey(studentno) );
②修改时:ALTER TABLE table_name ADD CONSTRAINT pk_name PRIMARY KEY(列名);
前提是原先没有设置主键。
2.外键语法①创建时:createtablesc (studentnoint,
courseidint,
scoreint,foreignkey(courseid) );②修改时:ALTER TABLE news_info[子表名] ADD CONSTRAINT FK_news_info_news_type[约束名] FOREIGN KEY (info_id)[子表列] REFERENCES news_type[主表名] (id)[主表列] ;
3.使用组合主键
如果一列不能唯一区分一个表里的记录时,可以考虑多个列组合起来达到区分表记录的唯一性,形式①创建时:createtablesc (studentnoint,
courseidint,
scoreint,primarykey(studentno,courseid) );
②修改时:alter table tb_name add primary key (字段1,字段2,字段3);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-03-31

MySQL云数据库创建、配置与使用教程,五秒钟创建属于自己的mysql云数据库,申请地址:xmjar.cn

相似回答