无法在MySQL中添加外键约束:错误3780

egdjgwm8  于 2022-11-21  发布在  Mysql
关注(0)|答案(2)|浏览(255)

我收到错误:
错误代码:3780。外键约束条件'product_ibfk_1'中的引用列'category'和被引用列'category_id'不兼容。

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

4ngedf3f1#

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

dfddblmv

dfddblmv2#

在MySQL中,SERIALBIGINT UNSIGNED AUTO_INCREMENT的别名。
若要生成引用此列的外键,它必须是BIGINT UNSIGNED,而不是带符号的BIGINT
您可能希望查看我所造成的外键错误的清单:https://stackoverflow.com/a/4673775/20860
我还在我的书SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming的一章中更详细地介绍了外键错误。

相关问题