gpt4 book ai didi

MYSQL建表、约束、外键

转载 作者:行者123 更新时间:2023-11-29 02:43:07 25 4
gpt4 key购买 nike

我正在学习有关 SQL 的 UDEMY 类(class)。讲师在postgresql工作。我在 MySQL 工作台工作。我们公司的开发是在AWS平台上的MySQL 5.6。所以我想学习MySQL。

有一个关于“创建表”以及使用约束和外键命令链接两个不同表中的主键的讲座。这是我想用我的数据做的事情,所以挑战是相关的。讲师的代码不适合我编译。我收到“错误代码 1215。无法添加外键约束”。

我在这里读到变量必须具有完全相同的定义。所以我把讲师的表从“serial”改成了int。我仍然收到错误。我试过简化代码。我仍然没有取得任何进展。谁能帮我理解为什么它没有执行?

create table if not exists accounts(

user_id INT auto_increment not null,
username varchar(50) unique not null,
password varchar(50) not null,
email varchar(350) unique not null,
created_on timestamp not null,
last_login timestamp,
primary key (user_id)
);


create table if not exists role(
role_id INT auto_increment not null,
role_name varchar(250) unique not null,
PRIMARY KEY (role_id)
);

create table accounts_role(
user_id INT,
role_id INT,
grant_date timestamp,
primary key (user_id, role_id),

constraint accounts_role_role_id_fkey foreign key (role_id)
references role(role_id) match simple
on update no action
on delete no action,

constraint accounts_role_user_id_fkey foreign key (user_id)
references account (user_id) MATCH SIMPLE
on update no action
on delete no action
);

最佳答案

天啊。
谢谢 Caius,我正在实现你的建议,我找到了问题的答案。

我意识到我的表被命名为“accounts”并且我在“account”上创建了一个约束......

create table accounts_role(
user_id INT,
role_id INT,
grant_date timestamp,
primary key (user_id, role_id),

constraint accounts_role_role_id_fkey foreign key (role_id)
references role(role_id) match simple
on update no action
on delete no action,

constraint accounts_role_user_id_fkey foreign key (user_id)
references accounts(user_id) MATCH SIMPLE
on update no action
on delete no action
);

关于MYSQL建表、约束、外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363006/

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