gpt4 book ai didi

php - mysql自身与2个字段的关系

转载 作者:行者123 更新时间:2023-11-28 23:32:57 25 4
gpt4 key购买 nike

我有这样的 user 表:

  id          email
-------------------------
1 test@gmail.com

menu 表格如下:

  id   parent_id   user_id   name
-----------------------------------
1 NULL 1 tets

我想 self 关系 parent_iduser_id
如果黑客想添加这条记录 mysql 会忽略此操作,因为 user_id 2 不是 owner parent menu 1!!! :

  id   parent_id   user_id   name
-----------------------------------
2 1 2 tets

最佳答案

1.使用下面的查询创建表User

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);

当您输入一个不在用户表中的值时,将抛出以下错误

Cannot add or update a child row: a foreign key constraint fails

关于php - mysql自身与2个字段的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36857506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com