7
我想創建一個跨數據庫外鍵。當我在同一個數據庫上運行下面的代碼Mysql跨數據庫外鍵
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`int_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `int_id` (`int_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
但是,當一個DATABSE(D1)創建T1,然後運行在第二DATABSE(D2)下面的代碼,我收到的一般錯誤:# 1005 - 無法創建表 'userdata.t2'(錯誤:150)
CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ext_id` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `ext_id` (`ext_id`),
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`ext_id`) REFERENCES `d1.t1` (`int_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
任何幫助將不勝感激。謝謝!
非常真棒!非常感謝。 – sagibb 2013-03-21 11:18:13