我需要您的协助条件!我无法确定问题是什么...... 我认为一切都是正确的,但MySQL抛出错误1005,SQL状态HY000:无法创建表'cpis.cpis_sudent_profile'(错误号:150)
DROP TABLE IF EXISTS `cpis_users`;
CREATE TABLE `cpis_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(128) NOT NULL,
`email` varchar(128) NOT NULL,
`activkey` varchar(128) NOT NULL DEFAULT '',
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lastvisit_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`status` boolean NOT NULL DEFAULT false,
`user_type` varchar(128) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `cpis_countries`;
CREATE TABLE `cpis_countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(3) NOT NULL,
`title_ru` varchar(100) DEFAULT NULL,
`title_en` varchar(100) DEFAULT NULL,
`title_cz` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `cpis_programs`;
CREATE TABLE `cpis_programs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title_ru` varchar(100) DEFAULT NULL,
`description_ru` text DEFAULT NULL,
`title_en` varchar(100) DEFAULT NULL,
`description_en` text DEFAULT NULL,
`title_cz` varchar(100) DEFAULT NULL,
`description_cz` text DEFAULT NULL,
`publicated` boolean DEFAULT false,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `cpis_sudent_profile`;
CREATE TABLE `cpis_sudent_profile` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`fname` varchar(100) NOT NULL,
`pname` varchar(100) NOT NULL,
`lname` varchar(100) NOT NULL,
`birthday` date NOT NULL,
`citizenship` int(11) NOT NULL,
`sex` varchar(30) NOT NULL,
`program_id` varchar(100) NOT NULL,
`owner` int(11) DEFAULT NULL,
`member` int(11) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
CONSTRAINT `spfk_1` FOREIGN KEY (`user_id`) REFERENCES `cpis_users` (`id`),
CONSTRAINT `spfk_2` FOREIGN KEY (`citizenship`) REFERENCES `cpis_countries` (`id`),
CONSTRAINT `spfk_3` FOREIGN KEY (`program_id`) REFERENCES `cpis_programs` (`id`),
CONSTRAINT `spfk_4` FOREIGN KEY (`owner`) REFERENCES `cpis_users` (`id`),
CONSTRAINT `spfk_5` FOREIGN KEY (`member`) REFERENCES `cpis_users` (`id`)
) ENGINE=InnoDB CHARSET=utf8;
答案 0 :(得分:1)
program_id varchar(100)
应与cpis_programs (id int(11))
具有相同的数据类型。通过具有不同类型的数据,无法建立约束。示例SQL Fiddle。
...
DROP TABLE IF EXISTS `cpis_sudent_profile`;
CREATE TABLE `cpis_sudent_profile` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`fname` varchar(100) NOT NULL,
`pname` varchar(100) NOT NULL,
`lname` varchar(100) NOT NULL,
`birthday` date NOT NULL,
`citizenship` int(11) NOT NULL,
`sex` varchar(30) NOT NULL,
/*`program_id` varchar(100) NOT NULL,*/
`program_id` int(11) NOT NULL,
`owner` int(11) DEFAULT NULL,
`member` int(11) DEFAULT NULL,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
CONSTRAINT `spfk_1` FOREIGN KEY (`user_id`) REFERENCES `cpis_users` (`id`),
CONSTRAINT `spfk_2` FOREIGN KEY (`citizenship`) REFERENCES `cpis_countries` (`id`),
CONSTRAINT `spfk_3` FOREIGN KEY (`program_id`) REFERENCES `cpis_programs` (`id`),
CONSTRAINT `spfk_4` FOREIGN KEY (`owner`) REFERENCES `cpis_users` (`id`),
CONSTRAINT `spfk_5` FOREIGN KEY (`member`) REFERENCES `cpis_users` (`id`)
) ENGINE=InnoDB CHARSET=utf8;