(我使用工作台)我有questions
表id, user_id, text
和表users
表id, name
我需要联系这两张桌子!
我写下以下内容:
ALTER TABLE `mydb_development`.`questions`
ADD CONSTRAINT fk_QueUsers_1
FOREIGN KEY (`user_id`)
REFERENCES `mydb_development`.`users`(`id`);
但我明白了:
ERROR 1046: No database selected
SQL Statement:
ALTER TABLE `questions`
ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
ERROR: Error when running failback script. Details follow.
ERROR 1046: No database selected
SQL Statement:
CREATE TABLE `questions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`text` text NOT NULL,
`security_token` varchar(40) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=373 DEFAULT CHARSET=utf8
ERROR 1046: No database selected
SQL Statement:
ALTER TABLE `questions`
ADD FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
ERROR: Error when running failback script. Details follow.
.....................
修改 我试着这样做:
USE `mydb_development`;
ALTER TABLE `mydb_development`.`questions`
ADD CONSTRAINT `fk_QueUsers_1`
FOREIGN KEY (`user_id`)
REFERENCES `mydb_development`.`users`(`id`);
我收到错误:
Error Code: 1005
Can't create table 'survey_development.#sql-4ad_45' (errno: 150)
不明白:S
修改
我的用户表:
DROP TABLE IF EXISTS `mydb_development`.`users`;
CREATE TABLE `mydb_development`.`users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
答案 0 :(得分:2)
答案 1 :(得分:0)
输入:
use `mydb_development`;
将约束名称'user_id'更改为其他内容,例如'fk_user_id'
答案 2 :(得分:0)
尝试选择您的数据库以查看它是否有任何区别
USE `mydb_development`;
答案 3 :(得分:0)
首先,您要摆脱ERROR 1046: No database selected
错误。
为此,请确保您:
USE mydb_development;
ALTER/CREATE
语句以包含数据库。例如。 ALTER TABLE mydb_development.questions