MySQL creates a primary key, a foreign key, and a composite primary key. For more information, see.
MySQL creates a primary key, a foreign key, and a composite primary key. For more information, see.
1. Create primary key syntax
Alter table table_name add constraint pk_name primary key (column name );
2. Create foreign key syntax
Alter table news_info [subtable name] add constraint FK_news_info_news_type [CONSTRAINT name] foreign key (info_id) [subtable column] REFERENCES news_type [primary TABLE name] (id) [primary TABLE column];
3. Use the primary key combination
If a column cannot uniquely distinguish records in a table, you can combine multiple columns to distinguish the uniqueness of table records.
① Creation: create table SC (
Studentno int,
Courseid int,
Score int,
Primary key (studentno, courseid ));
② Modification: alter table tb_name add primary key (Field 1, Field 2, Field 3 );