首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能在MySQL中添加外键约束:错误3780

问未能在MySQL中添加外键约束:错误3780
EN

Stack Overflow用户
提问于 2022-11-13 21:11:47
回答 2查看 36关注 0票数 -1

我发现了一个错误:

错误代码: 3780。在外键约束'category_id‘中引用列’类别‘和引用列'product_ibfk_1’是不兼容的。

代码语言:javascript
复制
drop table if exists Provider;
drop table if exists Category;
drop table if exists Product;

create table Provider
 (
 privider_id serial not null primary key, 
 login_password varchar(20) not null
        constraint passrule3 check(login_password sounds like '[A-Za-z0-9]{6,20}'),
 fathersname varchar(20) not null,
 name_of_contact_face varchar(10) not null,
 surname varchar(15), 
 e_mail varchar(25) unique
        constraint  emailrule2 check(e_mail sounds like  '[A-Za-z0-9]{10,10})\@gmail.com\s?')
 );
 
create table Category
(
title varchar(20),
category_id serial not null primary key
);
create table Product
(
 barecode serial not null primary key, 
 provider_id bigint not null, 
 manufacturer varchar(25) not null, 
 category_id bigint not null, 
 dimensions varchar(10) not null, 
 amount int not null,
 date_of_registration datetime not null,
 #constraint 'provider_for_product'
 foreign key (provider_id) references Provider (provider_id) on delete restrict on update cascade,
 foreign key (category_id) references Category (category_id) on delete restrict on update cascade
);
EN

回答 2

Stack Overflow用户

发布于 2022-11-13 21:34:44

在外键约束中引用的两列的数据类型需要匹配。

票数 0
EN

Stack Overflow用户

发布于 2022-11-13 22:22:24

在MySQL中,SERIAL是BIGINT UNSIGNED AUTO_INCREMENT的别名。

若要创建引用此列的外键,必须是BIGINT UNSIGNED,而不是签名的BIGINT。

您可能希望查看我所做贡献的外键错误清单:https://stackoverflow.com/a/4673775/20860

我还在我的书“SQL Antipatterns第1卷:避免数据库编程的缺陷”的一章中更详细地介绍了外键错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74424811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档