Mysql 建立表外來鍵

2021-07-10 15:42:41 字數 733 閱讀 6437

我自己的步驟:

1.定義表,可以指定主鍵

create table sc (sno char(9),courseno char(10),grade smallint,

primary key (sno,courseno));

2.插入資料

insert into sc values ('31101','01',89);

insert into sc values ('31101','02',99);

insert into sc values ('31102','04',90);

insert into sc values ('31103','01',85);

insert into sc values ('31104','04',79);

insert into sc values ('31105','02',99);

insert into sc values ('31105','04',80);

3.新增外來鍵

alter table sc add foreign key (sno) references student (sno);

alter table sc add foreign key (courseno) references course (courseno);

如果在定義表時指定外來鍵,插入資料會出現error1025,1452之類的錯誤。(在workbench中操作的)

mysql建立外來鍵報錯 Mysql表建立外來鍵報錯

資料庫表a create table task desc tab id int 11 primary key not null comment 自增主鍵 auto increment,taskname varchar 200 not null comment 任務名字 sqlname varchar...

mysql建立外來鍵

建立外來鍵的前提 本表的列必須與外來鍵型別相同 外來鍵必須是外表主鍵 外來鍵作用 使兩張表形成關聯,外來鍵只能引用外表中的列的值!指定主鍵關鍵字 foyunmkreign key 列名 引用外來鍵關鍵字 references 外來鍵表名 外來鍵列名 事件觸發限制 on delete和on updat...

MYSQL資料表建立外來鍵

mysql建立關聯表可以理解為是兩個表之間有個外來鍵關係,但這兩個表必須滿足三個條件 1.兩個表必須是innodb資料引擎 2.使用在外鍵關係的域必須為索引型 index 3.使用在外鍵關係的域必須與資料型別相似 例如 1 建立s user表 create table s user u id int...