SQL 语法错误 (phpMyAdmin)

发布于 2024-11-09 13:20:17 字数 513 浏览 0 评论 0原文

此 SQL 语句有什么问题:

ALTER TABLE `tbl_issue` 
ADD CONSTRAINT `FK_issue_requester` 
  FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

我在名为 trackstar_dev 的数据库中有名为 tbl_issue 和 tbl_user 的表。

phpMyAdmin 说:

#1005 - Can't create table 'trackstar_dev.#sql-1a4_9d' (errno: 121) (<a href="server_engines.php?engine=InnoDB&amp;page=Status&amp;token=fdfsdghrw222323hndgsf">Details...</a>)

What's wrong with this SQL statement:

ALTER TABLE `tbl_issue` 
ADD CONSTRAINT `FK_issue_requester` 
  FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

I have tables called tbl_issue and tbl_user within a database called trackstar_dev.

phpMyAdmin said:

#1005 - Can't create table 'trackstar_dev.#sql-1a4_9d' (errno: 121) (<a href="server_engines.php?engine=InnoDB&page=Status&token=fdfsdghrw222323hndgsf">Details...</a>)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ㄟ。诗瑗 2024-11-16 13:20:17

出现此错误的最常见原因是外键约束与另一个表中的名称相同。外键的名称在数据库中必须是唯一的(不仅仅是在表级别)。您数据库的另一个表中是否有 requester_id

The most common reason for this error is that the foreign key constraint have the same name as in another table. Foreign keys' names must be unique in the database (not just on table level). Do you have requester_id in another table in your database?

沩ん囻菔务 2024-11-16 13:20:17

如果您尝试添加名称已在其他地方使用的约束,您将收到此消息,如果您尝试添加名称已在其他地方使用的约束,您将收到此消息在其他地方使用过。改一下就可以了:)

you will get this message if you're trying to add a constraint with a name that's already used somewhere else, c you will get this message if you're trying to add a constraint with a name that's already used somewhere else . change it and it will be ok :)

从﹋此江山别 2024-11-16 13:20:17

我也有同样的问题。删除数据库中的所有表,然后使用下面的 SQL 代码重新创建它们。我假设您正在使用 Yii 1.1 和 php 5 进行敏捷 Web 开发中的示例

- Disable foreign keys
SET FOREIGN_KEY_CHECKS = 0 ;# MySQL returned an empty result set (i.e. zero rows).


-- Create tables section -------------------------------------------------

-- Table tbl_project

CREATE TABLE IF NOT EXISTS `tbl_project` (
`id` INTEGER NOT NULL auto_increment,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`create_time` DATETIME default NULL,
`create_user_id` INTEGER default NULL,
`update_time` DATETIME default NULL,
`update_user_id` INTEGER default NULL,
 PRIMARY KEY  (`id`)
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).


-- DROP TABLE IF EXISTS `tbl_issue` ;

CREATE TABLE IF NOT EXISTS `tbl_issue` 
( 
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` varchar(2000), 
`project_id` INTEGER,
`type_id` INTEGER,
`status_id` INTEGER,
`owner_id` INTEGER,
`requester_id` INTEGER,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER  
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).


-- DROP TABLE IF EXISTS `tbl_user` ;

-- Table User

CREATE TABLE IF NOT EXISTS `tbl_user` 
(
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`email` Varchar(256) NOT NULL,
`username` Varchar(256),
`password` Varchar(256),
`last_login_time` Datetime,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).


-- DROP TABLE IF EXISTS `tbl_project_user_assignment` ;

-- Table User

CREATE TABLE IF NOT EXISTS `tbl_project_user_assignment`
(
`project_id` Int(11) NOT NULL,
`user_id` Int(11) NOT NULL,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER,
PRIMARY KEY (`project_id`,`user_id`)
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).



-- The Relationships 
ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_project` FOREIGN KEY (`project_id`)       REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an  empty result set (i.e. zero rows).


ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_owner` FOREIGN KEY (`owner_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_requester` FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_user_project` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


-- Insert some seed data so we can just begin using the database
INSERT INTO `tbl_user` 
(`email`, `username`, `password`) 
VALUES 
('[email protected]','Test_User_One', MD5('test1')),
('[email protected]','Test_User_Two', MD5('test2'))    
;# 2 rows affected.


-- Enable foreign keys
SET FOREIGN_KEY_CHECKS = 1 ;# MySQL returned an empty result set (i.e. zero rows).

I had this same problem. Drop all the tables in your database and then use the SQL code below to recreate them. I assume you are following the example in the agile web development with Yii 1.1 and php 5

- Disable foreign keys
SET FOREIGN_KEY_CHECKS = 0 ;# MySQL returned an empty result set (i.e. zero rows).


-- Create tables section -------------------------------------------------

-- Table tbl_project

CREATE TABLE IF NOT EXISTS `tbl_project` (
`id` INTEGER NOT NULL auto_increment,
`name` varchar(128) NOT NULL,
`description` text NOT NULL,
`create_time` DATETIME default NULL,
`create_user_id` INTEGER default NULL,
`update_time` DATETIME default NULL,
`update_user_id` INTEGER default NULL,
 PRIMARY KEY  (`id`)
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).


-- DROP TABLE IF EXISTS `tbl_issue` ;

CREATE TABLE IF NOT EXISTS `tbl_issue` 
( 
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` varchar(2000), 
`project_id` INTEGER,
`type_id` INTEGER,
`status_id` INTEGER,
`owner_id` INTEGER,
`requester_id` INTEGER,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER  
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).


-- DROP TABLE IF EXISTS `tbl_user` ;

-- Table User

CREATE TABLE IF NOT EXISTS `tbl_user` 
(
`id` INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`email` Varchar(256) NOT NULL,
`username` Varchar(256),
`password` Varchar(256),
`last_login_time` Datetime,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).


-- DROP TABLE IF EXISTS `tbl_project_user_assignment` ;

-- Table User

CREATE TABLE IF NOT EXISTS `tbl_project_user_assignment`
(
`project_id` Int(11) NOT NULL,
`user_id` Int(11) NOT NULL,
`create_time` DATETIME,
`create_user_id` INTEGER,
`update_time` DATETIME,
`update_user_id` INTEGER,
PRIMARY KEY (`project_id`,`user_id`)
) ENGINE = InnoDB
;# MySQL returned an empty result set (i.e. zero rows).



-- The Relationships 
ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_project` FOREIGN KEY (`project_id`)       REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an  empty result set (i.e. zero rows).


ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_owner` FOREIGN KEY (`owner_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


ALTER TABLE `tbl_issue` ADD CONSTRAINT `FK_issue_requester` FOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_user_project` FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;# MySQL returned an empty result set (i.e. zero rows).


-- Insert some seed data so we can just begin using the database
INSERT INTO `tbl_user` 
(`email`, `username`, `password`) 
VALUES 
('[email protected]','Test_User_One', MD5('test1')),
('[email protected]','Test_User_Two', MD5('test2'))    
;# 2 rows affected.


-- Enable foreign keys
SET FOREIGN_KEY_CHECKS = 1 ;# MySQL returned an empty result set (i.e. zero rows).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文