2013-04-08 80 views
0

我有引用該表一樣通過持有一個列表(種)表形成一個sql文件,然後一個主表(動物):復位數據庫無法刪除或更新父行,外鍵約束失敗

DROP TABLE IF EXISTS `tbl_species`; 
CREATE TABLE `tbl_species` (
    specie     VARCHAR(10) PRIMARY KEY 
) ENGINE=InnoDB    DEFAULT CHARSET=utf8; 
INSERT INTO `tbl_species` VALUES ('dog'); 
INSERT INTO `tbl_species` VALUES ('cat'); 
INSERT INTO `tbl_species` VALUES ('bird'); 

DROP TABLE IF EXISTS `tbl_animal`; 
CREATE TABLE `tbl_animal` (
    id_animal INTEGER  NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name  VARCHAR(25) NOT NULL DEFAULT "no nombre", 
    specie  VARCHAR(10) NOT NULL DEFAULT "dog", 
    FOREIGN KEY (specie)  REFERENCES `tbl_species`  (specie), 
    CONSTRAINT `uc_Info_Animal` UNIQUE (`id_animal`)   
) ENGINE=InnoDB     DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

這部作品sqlfiddle沒有問題,但是當我執行服務器上 我收到以下錯誤的SQL文件:

Cannot delete or update a parent row: a foreign key constraint fails 
Statement: 
DROP TABLE IF EXISTS `tbl_species` 

你能告訴怎麼解決這個問題,目前我不得不刪除數據庫並再次創建它...所以drop語句導致問題...

回答

5

你得到Cannot delete or update錯誤的原因是tbl_animal表已經存在。該表需要在創建相反的順序進行刪除:

SET FOREIGN_KEY_CHECKS=0; 

這將禁用外鍵檢查當前會話:

DROP TABLE IF EXISTS `tbl_animal`; 
DROP TABLE IF EXISTS `tbl_species`; 

CREATE TABLE `tbl_species` (
    specie     VARCHAR(10) PRIMARY KEY 
) ENGINE=InnoDB    DEFAULT CHARSET=utf8; 
INSERT INTO `tbl_species` VALUES ('dog'); 
INSERT INTO `tbl_species` VALUES ('cat'); 
INSERT INTO `tbl_species` VALUES ('bird'); 

CREATE TABLE `tbl_animal` (
    id_animal INTEGER  NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name  VARCHAR(25) NOT NULL DEFAULT "no nombre", 
    specie  VARCHAR(10) NOT NULL DEFAULT "dog", 
    FOREIGN KEY (specie)  REFERENCES `tbl_species`  (specie), 
    CONSTRAINT `uc_Info_Animal` UNIQUE (`id_animal`)   
) ENGINE=InnoDB     DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 

或者,你可以與前言中的命令。

要重新啓用外鍵檢查,之後添加下面的命令:

SET FOREIGN_KEY_CHECKS=1; 

看到http://sqlfiddle.com/#!2/b6727

1

工作的例子你要麼有下降的從屬表之前刪除外鍵表,或降並在每次刪除表時重新創建外鍵。

試試這個,它會刪除並重新創建表每次:(我的SQL Server的版本是2012)

IF OBJECT_ID('dbo.tbl_animal', 'U') IS NOT NULL 
    DROP TABLE dbo.tbl_animal 
IF OBJECT_ID('dbo.tbl_species', 'U') IS NOT NULL 
    DROP TABLE dbo.tbl_species 

CREATE TABLE [dbo].[tbl_species](
    [specie] [varchar](10) NOT NULL, 
CONSTRAINT [PK_tbl_species] PRIMARY KEY CLUSTERED 
(
    [specie] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

INSERT INTO tbl_species VALUES ('dog'); 
INSERT INTO tbl_species VALUES ('cat'); 
INSERT INTO tbl_species VALUES ('bird'); 

CREATE TABLE [dbo].[tbl_animal](
    [id_animal] [int] IDENTITY(1,1) NOT NULL, 
    [name] [varchar](25) NOT NULL, 
    [specie] [varchar](10) NOT NULL, 
CONSTRAINT [PK_tbl_animal] PRIMARY KEY CLUSTERED 
(
    [id_animal] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
ALTER TABLE [dbo].[tbl_animal] ADD CONSTRAINT [DF_tbl_animal_name] DEFAULT ('no nombre') FOR [name] 
GO 
ALTER TABLE [dbo].[tbl_animal] ADD CONSTRAINT [DF_tbl_animal_specie] DEFAULT ('dog') FOR [specie] 
GO 
ALTER TABLE [dbo].[tbl_animal] WITH CHECK ADD CONSTRAINT [FK_tbl_animal_tbl_species] FOREIGN KEY([specie]) 
REFERENCES [dbo].[tbl_species] ([specie]) 
GO 
ALTER TABLE [dbo].[tbl_animal] CHECK CONSTRAINT [FK_tbl_animal_tbl_species] 
GO 

如果你想給它一個去,你在這裏可以使用腳本(當表已經存在): 降約束,則表:

ALTER TABLE [dbo].[tbl_animal] drop CONSTRAINT [FK_tbl_animal_tbl_species] 
GO 
IF OBJECT_ID('dbo.tbl_species', 'U') IS NOT NULL 
    DROP TABLE dbo.tbl_species 
go 

刪除約束,然後重新創建它:

ALTER TABLE [dbo].[tbl_animal] drop CONSTRAINT [FK_tbl_animal_tbl_species] 
GO 
ALTER TABLE [dbo].[tbl_animal] WITH CHECK ADD CONSTRAINT [FK_tbl_animal_tbl_species] FOREIGN KEY([specie]) 
REFERENCES [dbo].[tbl_species] ([specie]) 
GO 

用這些你可以修改腳本,所以你不必每次都放棄動物表。

相關問題