[英]mysql self relationship with 2 field
我有這樣的user
表:
id email
-------------------------
1 test@gmail.com
和這樣的menu
表:
id parent_id user_id name
-----------------------------------
1 NULL 1 tets
我想自我關系parent_id
AND user_id
。
如果黑客想添加這條記錄 mysql 忽略了這個動作,因為user_id
2不是 owner parent menu 1 !!! :
id parent_id user_id name
-----------------------------------
2 1 2 tets
1.使用以下查詢創建表用戶
create table User(id int primary key,email varchar(30));
2.使用以下查詢創建表菜單
create table Menu(id,int,u_id int,p_id int,name varchar(10),primary key(u_id,p_id),
constraint user_fk foreign key(id) references User(id)
on delete cascade);
當您輸入表 User 中不存在的值時,將拋出以下錯誤
Cannot add or update a child row: a foreign key constraint fails
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.