无法添加外键约束

时间:2015-08-30 10:55:37

标签: mysql

CREATE TABLE tblTransaction (
    strTransCode VARCHAR(50) NOT NULL,
    dtmTransDate datetime,
    strOwnCode VARCHAR(50) NOT NULL,
    strOwnName VARCHAR(50) NOT NULL,
    strTransDesc VARCHAR(50) NOT NULL,
    dblTransAmt double,
    strAcctCode VARCHAR(50) NOT NULL,
    strEntryCode VARCHAR(50) NOT NULL,
    FOREIGN KEY (strEntryCode) REFERENCES tblEntry (strEntryCode) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (strAcctCode) REFERENCES tblAccount (strAcctCode) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (strTransCode)
)ENGINE=InnoDB;
 CREATE TABLE tblVoucher (
    intVoucRefCode INT,
    strVoucRefDesc VARCHAR(50) NOT NULL,
   dtmVoucDate datetime,
   strOwnCode VARCHAR(50) NOT NULL,
   strOwnVoucCode VARCHAR(50) NOT NULL,
   FOREIGN KEY (strOwnVoucCode) REFERENCES tblTransaction(strOwnCode) ON DELETE RESTRICT ON UPDATE CASCADE,
   PRIMARY KEY (intVoucRefCode)
)ENGINE=InnoDB;

无法添加外键约束帮助 无法添加外键约束帮助 无法添加外键约束帮助 无法添加外键约束帮助

1 个答案:

答案 0 :(得分:1)

要从表strOwnCode创建tblTransaction的外键,您需要将strOwnCode定义为primary key

CREATE TABLE tblTransaction (
    strTransCode VARCHAR(50) NOT NULL,
    dtmTransDate datetime,
    strOwnCode VARCHAR(50) NOT NULL,
    strOwnName VARCHAR(50) NOT NULL,
    strTransDesc VARCHAR(50) NOT NULL,
    dblTransAmt double,
    strAcctCode VARCHAR(50) NOT NULL,
    strEntryCode VARCHAR(50) NOT NULL,
    PRIMARY KEY (strOwnCode)
)ENGINE=InnoDB;

DEMO