我在VARCHAR列上获取外键时遇到了困难。
理想情况下,我希望将它作为复合FK(从我尝试添加它时可以看到)。
在本地,在MySQL 5.6上运行此脚本我收到错误#1215。 在CloudBees上运行脚本时,我得到错误150
我在这里看到有关数据类型完全相同的其他问题,我已经验证了,你可以从脚本中看到。
/* Start of REGION table */
CREATE TABLE REGION (
ID INTEGER NOT NULL AUTO_INCREMENT, /* Unique Identifier */
RORDER INTEGER, /* Order to be displayed by (currently unused) */
LANG VARCHAR(3) DEFAULT 'en' , /* Language of this entry */
NAME VARCHAR(30) NOT NULL, /* Region Name */
DESCR VARCHAR(50) DEFAULT '' NOT NULL, /* Region Desc */
ACTIVE INTEGER DEFAULT '1' NOT NULL, /* Is item active or not */
CDATE TIMESTAMP DEFAULT 0, /* Create Date */
MODDATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, /* Last Update Date */
INSUSER VARCHAR(20) DEFAULT 'SYSTEM' NOT NULL, /* User that inserted this entry */
MODUSER VARCHAR(20), /* User that last modified this entry */
PRIMARY KEY(id)
) engine InnoDB;
COMMIT;
ALTER TABLE REGION ADD CONSTRAINT UNIQUE (LANG, NAME);
/* End of REGION table */
/* Start of LOCATION table */
CREATE TABLE LOCATION (
ID INTEGER NOT NULL AUTO_INCREMENT, /* Unique Identifier */
REGIONNAME VARCHAR(30) NOT NULL, /* FK - REGION.ID */
LORDER INTEGER, /* Order to be displayed by (currently unused) */
NAME VARCHAR(30) DEFAULT '' NOT NULL, /* Location Name (FK Key) */
LANG VARCHAR(3) DEFAULT 'en' , /* Language of this entry */
DESCR VARCHAR(50) DEFAULT '' NOT NULL, /* Location Desc */
ACTIVE INTEGER DEFAULT '1' NOT NULL, /* Is item active or not */
CDATE TIMESTAMP DEFAULT 0, /* Create Date */
MODDATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, /* Last Update Date */
INSUSER VARCHAR(20) DEFAULT 'SYSTEM' NOT NULL, /* User that inserted this entry */
MODUSER VARCHAR(20), /* User that last modified this entry */
PRIMARY KEY(id)
) engine InnoDB;
ALTER TABLE LOCATION ADD CONSTRAINT FK_LOCATION_REGION FOREIGN KEY (REGIONNAME, LANG) REFERENCES REGION(NAME, LANG) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE LOCATION ADD CONSTRAINT UNIQUE (LANG, NAME);
COMMIT;
/* End of LOCATION table */
答案 0 :(得分:0)
它需要REGION表中列的索引。
解决方案:
创建REGION但在创建LOCATION之前,添加:
CREATE INDEX REG_NAME_LANG ON REGION (NAME, LANG);