【问题标题】:MySQL error:1251 cannot add foreign keyMySQL 错误:1251 无法添加外键
【发布时间】:2018-05-27 17:00:15
【问题描述】:

前 4 个表创建良好,事务表遇到问题。我收到 1215 错误:无法添加外键。我检查并重新检查了数据类型,并确保所有 FK 都是他们自己的表的 PK。这里有什么问题?

CREATE SCHEMA FinalDB;

CREATE TABLE `User` (
    userId int not null auto_increment primary key,
    first_name varchar(255) not null,
    last_name varchar(255) not null,
    address varchar(255) null,
    DOB date not null,
    availableBalance int not null default 0,
    currency varchar(20)
);

CREATE TABLE Verifications(
    userId int not null primary key,
    passport int null,
    ssn int null,
    license int null,
    constraint
    foreign key (userId)
    references User(userId)
);

CREATE TABLE Linked_Account(
    account_Id int not null, 
    userId int not null,
    routing int null,
    swift int null,
    primary key (userId, account_Id),
    constraint
    foreign key (userId) 
    references User(userId)
);

CREATE TABLE Wallet (
    userId int not null,
    walletId varchar(5) not null,
    coinAmount int not null default 0,
    netWorth int not null default 0,
    primary key(userId, walletId),
    constraint
    foreign key (userId)
    references `User`(userId)
);

CREATE TABLE Transactions (
    transactionId int not null primary key auto_increment, 
    userId int not null,
    type varchar(30) not null,
    walletId varchar(5) not null,
    payment_method int null, #optional
    total int null, #optional
    quantity int not null,
    fee int null, #optional
    `date` date not null,
    sender varchar(50) null, #optional
    reciever varchar(50) null, #optional
    status varchar(20) not null,
    notes varchar(200) null, #optional
    constraint 
    foreign key (userId)
    references `User`(userId) 
    ON DELETE CASCADE ON UPDATE CASCADE,
    constraint 
    foreign key (walletId)
    references Wallet(walletId)
    ON DELETE CASCADE ON UPDATE CASCADE,
    constraint
    foreign key (payment_method)
    references Linked_Account(account_id)

);

CREATE TABLE TransactionsExchange(
    transactionId int not null auto_increment primary key,
    userId int not null,
    currencyFrom int not null,
    currencyFromAmount int not null,
    currencyInto int not null,
    currencyIntoEquivalent int not null,
    notes varchar(200) null,
    `date` date not null,
    constraint
    foreign key (userId)
    references User(userId),
    constraint
    foreign key (currencyFrom)
    references Wallet(walletId),
    constraint
    foreign key (currencyInto)
    references Wallet(walletId)
);

我在网上寻找可能的答案,但通常与不一致的数据类型或未声明的 PK 有关。我基本上是在尝试制作一个事务表来记录不同组合中的各种不同数据。除了一些默认值之外,使用后端逻辑来处理需要和不需要的内容。

【问题讨论】:

    标签: mysql sql foreign-keys mysql-error-1064


    【解决方案1】:

    要将复合主键用作外键,您必须添加 具有相同数据类型的相同列数(组成 PK) 子表,然后使用这些列的组合 外键定义。

    在此处查看相关帖子https://stackoverflow.com/a/10566463/4904726

    试试这个“交易”表创建查询:

     CREATE TABLE Transactions (
                transactionId int not null primary key auto_increment, 
                userId int not null,
                type varchar(30) not null,
                walletId varchar(5) not null,
                payment_method int null, #optional
                total int null, #optional
                quantity int not null,
                fee int null, #optional
                `date` date not null,
                sender varchar(50) null, #optional
                reciever varchar(50) null, #optional
                status varchar(20) not null,
                notes varchar(200) null, #optional
                constraint 
                foreign key (userId)
                references `User`(userId) 
                ON DELETE CASCADE ON UPDATE CASCADE,
                constraint
                foreign key (userId, walletId)
                references Wallet(userId, walletId)
                ON DELETE CASCADE ON UPDATE CASCADE,
                constraint
                foreign key (userId, payment_method)
                references Linked_Account(userId, account_id)
    
            );
    

    【讨论】:

    • 效果很好!到目前为止,我从来没有将复合键作为外键导入。我只是假设每个键都必须单独声明。谢谢!
    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2014-09-15
    • 2013-06-02
    • 2018-04-28
    相关资源
    最近更新 更多