gpt4 book ai didi

mysql - 错误 150 : table couldn't be created

转载 作者:行者123 更新时间:2023-11-29 06:19:17 24 4
gpt4 key购买 nike

我哪里错了?我收到错误 150 debit_card 表无法创建。做一些我知道的外键,只是不知道到底是什么。帮忙?

create table customer(
cust_id int auto_increment,
first_name varchar(25),
last_name varchar(25),
street varchar(25),
city varchar(25),
state char(2),
zip varchar(10),
CONSTRAINT customer_pk primary key(cust_id)
);

create table card_account(
acct_no char(16),
exp_date date,
card_type ENUM("Debit","Credit") not null,
CONSTRAINT card_account_pk primary key(acct_no,exp_date)
);

create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no) references card_account(acct_no) on delete cascade,
CONSTRAINT debit_card_exp_date_fk foreign key(exp_date) references card_account(exp_date) on delete cascade
);

最佳答案

您的card_account 表有一个复合主键。您不能在 foreign key 约束中单独使用这些列。

但是您可以添加一个引用复合主键外键:

create table debit_card(
bank_no char(9) not null,
acct_no char(16),
exp_date date,
CONSTRAINT debit_card_pk primary key(acct_no,exp_date),
CONSTRAINT debit_card_acct_no_fk foreign key(acct_no,exp_date) references card_account(acct_no,exp_date) on delete cascade
);

关于mysql - 错误 150 : table couldn't be created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34194429/

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