我正在尝试为足球注册应用程序创建数据库。我现在正试图保持简单,一旦我感到舒服,那就让它更加细致。我正在尝试运行我的sql,但我一直收到这些错误。我想知道是否有人可以查看我的sql,因为我不相信有外键问题(但显然有)
Msg 3726,Level 16,State 1,Line 23 无法删除对象'TShirtSizes',因为它是由FOREIGN KEY约束引用的。 Msg 3726,Level 16,State 1,Line 26 无法删除对象'TGenders',因为它是由FOREIGN KEY约束引用的。 Msg 2714,Level 16,State 6,Line 39 数据库中已经有一个名为“TGenders”的对象。
IF OBJECT_ID('TFields') IS NOT NULL DROP TABLE TFields
IF OBJECT_ID('TAgeGroups') IS NOT NULL DROP TABLE TAgeGroups
IF OBJECT_ID('TReferees') IS NOT NULL DROP TABLE TReferees
IF OBJECT_ID('TTeamCoaches') IS NOT NULL DROP TABLE TTeamCoaches
IF OBJECT_ID('TCoaches') IS NOT NULL DROP TABLE TCoaches
IF OBJECT_ID('TStates') IS NOT NULL DROP TABLE TStates
IF OBJECT_ID('TSockSizes') IS NOT NULL DROP TABLE TSockSizes
IF OBJECT_ID('TPantSizes') IS NOT NULL DROP TABLE TPantSizes
IF OBJECT_ID('TShirtSizes') IS NOT NULL DROP TABLE TShirtSizes
IF OBJECT_ID('TTeamPlayers') IS NOT NULL DROP TABLE TTeamPlayers
IF OBJECT_ID('TPlayers') IS NOT NULL DROP TABLE TPlayers
IF OBJECT_ID('TGenders') IS NOT NULL DROP TABLE TGenders
IF OBJECT_ID('TTeams') IS NOT NULL DROP TABLE TTeams
------------------------------------------------------------------------------------
-- create tables
------------------------------------------------------------------------------------
create table TTeams
(
intTeamID INTEGER NOT NULL
,strTeam VARCHAR(50) NOT NULL
,CONSTRAINT TTeams_PK PRIMARY KEY ( intTeamID )
)
CREATE TABLE TGenders
(
intGenderID INTEGER NOT NULL
,strGender VARCHAR(10) NOT NULL
,CONSTRAINT TGenders_PK PRIMARY KEY ( intGenderID )
)
CREATE TABLE TPlayers
(
intPlayerID INTEGER NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strLastName VARCHAR(50) NOT NULL
,strEmail VARCHAR(50) NOT NULL
,intShirtSizeID INTEGER NOT NULL
,intPantSizeID INTEGER NOT NULL
,intSockSizeID INTEGER NOT NULL
,strCity VARCHAR(50) NOT NULL
,intStateID INTEGER NOT NULL
,intGenderID INTEGER NOT NULL
,intAgeGroupID INTEGER NOT NULL
,CONSTRAINT TPlayers_PK PRIMARY KEY ( intPlayerID )
)
CREATE TABLE TTeamPlayers
(
intTeamPlayerID INTEGER NOT NULL
,intTeamID INTEGER NOT NULL
,intPlayerID INTEGER NOT NULL
,CONSTRAINT TTeamPlayers_PK PRIMARY KEY ( intTeamPlayerID )
)
CREATE TABLE TShirtSizes
(
intShirtSizeID INTEGER NOT NULL
,strShirtSize VARCHAR(50) NOT NULL
,CONSTRAINT TShirtSizes_PK PRIMARY KEY ( intShirtSizeID )
)
CREATE TABLE TPantSizes
(
intPantSizeID INTEGER NOT NULL
,strPantSize VARCHAR(50) NOT NULL
,CONSTRAINT TPantSizes_PK PRIMARY KEY ( intPantSizeID )
)
CREATE TABLE TSockSizes
(
intSockSizeID INTEGER NOT NULL
,strSockSize VARCHAR(50) NOT NULL
,CONSTRAINT TSockSizes_PK PRIMARY KEY ( intSockSizeID )
)
CREATE TABLE TStates
(
intStateID INTEGER NOT NULL
,strState VARCHAR(50) NOT NULL
,CONSTRAINT TStates_PK PRIMARY KEY ( intStateID )
)
CREATE TABLE TCoaches
(
intCoachID INTEGER NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strLastName VARCHAR(50) NOT NULL
,strCity Varchar(50) not null
,intStateID integer not null
,strPhoneNumber varchar(50) not null
,CONSTRAINT TCoaches_PK PRIMARY KEY ( intCoachID )
)
CREATE TABLE TTeamCoaches
(
intTeamCoachID INTEGER NOT NULL
,intTeamID INTEGER NOT NULL
,intCoachID INTEGER NOT NULL
,CONSTRAINT TTeamCoaches_PK PRIMARY KEY ( intTeamCoachID )
)
CREATE TABLE TReferees
(
intRefereeID INTEGER NOT NULL
,strFirstName VARCHAR(50) NOT NULL
,strLastName VARCHAR(50) NOT NULL
,CONSTRAINT TReferees_PK PRIMARY KEY ( intRefereeID )
)
CREATE TABLE TAgeGroups
(
intAgeGroupID INTEGER NOT NULL
,strAge VARCHAR(10) NOT NULL
,CONSTRAINT TAgeGroups_PK PRIMARY KEY ( intAgeGroupID )
)
CREATE TABLE TFields
(
intFieldID INTEGER NOT NULL
,strFieldName VARCHAR(50) NOT NULL
,intTeamID INTEGER NOT NULL
,intRefereeID INTEGER NOT NULL
,CONSTRAINT TFields_PK PRIMARY KEY ( intFieldID )
)
-- --------------------------------------------------------------------------------
-- Step #1 & @: Identify and Create Foreign Keys
-- --------------------------------------------------------------------------------
--
-- # Child Parent Column(s)
-- - ----- ------ ---------
-- 1 TTeamPlayers TPlayers intPlayerID
-- 2 TPlayers TShirtSizes intShirtSizeID
-- 3 TPlayers TPantSizes intPantSizeID
-- 4 TPlayers TSockSizes intSockSizeID
-- 5 TPlayers TStates intStateID
-- 6 TPlayers TGenders intGenderID
-- 7 TPlayers TAgeGroups intAgeGroupID
-- 8 TTeamCoaches TCoaches intCoachID
-- 9 TFields TTeams intTeamID
-- 10 TFields TReferees intRefereeID
-- 1
ALTER TABLE TTeamPlayers ADD CONSTRAINT TTeamPlayers_TPlayers_FK
FOREIGN KEY ( intPlayerID ) REFERENCES TPlayers ( intPlayerID )
-- 2
ALTER TABLE TPlayers ADD CONSTRAINT TPlayers_TShirtSizes_FK
FOREIGN KEY ( intShirtSizeID ) REFERENCES TShirtSizes ( intShirtSizeID )
-- 3
ALTER TABLE TPlayers ADD CONSTRAINT TPlayers_TPantSizes_FK
FOREIGN KEY ( intPantSizeID ) REFERENCES TPantSizes ( intPantSizeID )
-- 4
ALTER TABLE TPlayers ADD CONSTRAINT TPlayers_TSockSizes_FK
FOREIGN KEY ( intSockSizeID ) REFERENCES TSockSizes ( intSockSizeID )
-- 5
ALTER TABLE TPlayers ADD CONSTRAINT TPlayers_TStates_FK
FOREIGN KEY ( intStateID ) REFERENCES TStates ( intStateID )
-- 6
ALTER TABLE TPlayers ADD CONSTRAINT TPlayers_TGenders_FK
FOREIGN KEY ( intGenderID ) REFERENCES TGenders ( intGenderID )
-- 7
ALTER TABLE TPlayers ADD CONSTRAINT TPlayers_TAgeGroups_FK
FOREIGN KEY ( intAgeGroupID ) REFERENCES TAgeGroups ( intAgeGroupID )
-- 8
ALTER TABLE TTeamCoaches ADD CONSTRAINT TTeamCoaches_TCoaches_FK
FOREIGN KEY ( intCoachID ) REFERENCES TCoaches ( intCoachID )
-- 9
ALTER TABLE TFields ADD CONSTRAINT TFields_TTeams_FK
FOREIGN KEY ( intTeamID ) REFERENCES TTeams ( intTeamID )
-- 10
ALTER TABLE TFields ADD CONSTRAINT TFields_TReferees_FK
FOREIGN KEY ( intRefereeID ) REFERENCES TReferees ( intRefereeID )
答案 0 :(得分:0)
尝试按以下顺序进行操作。首先您应该删除具有FK的表,以便FK约束也将被删除,然后您可以删除子表。
IF OBJECT_ID('TTeamPlayers') IS NOT NULL DROP TABLE TTeamPlayers
IF OBJECT_ID('TPlayers') IS NOT NULL DROP TABLE TPlayers
IF OBJECT_ID('TTeamCoaches') IS NOT NULL DROP TABLE TTeamCoaches
IF OBJECT_ID('TFields') IS NOT NULL DROP TABLE TFields
IF OBJECT_ID('TAgeGroups') IS NOT NULL DROP TABLE TAgeGroups
IF OBJECT_ID('TReferees') IS NOT NULL DROP TABLE TReferees
IF OBJECT_ID('TCoaches') IS NOT NULL DROP TABLE TCoaches
IF OBJECT_ID('TStates') IS NOT NULL DROP TABLE TStates
IF OBJECT_ID('TSockSizes') IS NOT NULL DROP TABLE TSockSizes
IF OBJECT_ID('TPantSizes') IS NOT NULL DROP TABLE TPantSizes
IF OBJECT_ID('TShirtSizes') IS NOT NULL DROP TABLE TShirtSizes
IF OBJECT_ID('TGenders') IS NOT NULL DROP TABLE TGenders
IF OBJECT_ID('TTeams') IS NOT NULL DROP TABLE TTeams
答案 1 :(得分:0)
首先尝试禁用外键约束然后删除表。最后,再次启用约束和触发器。
EXEC sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?'
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
IF OBJECT_ID('TFields') IS NOT NULL DROP TABLE TFields
IF OBJECT_ID('TAgeGroups') IS NOT NULL DROP TABLE TAgeGroups
IF OBJECT_ID('TReferees') IS NOT NULL DROP TABLE TReferees
IF OBJECT_ID('TTeamCoaches') IS NOT NULL DROP TABLE TTeamCoaches
IF OBJECT_ID('TCoaches') IS NOT NULL DROP TABLE TCoaches
IF OBJECT_ID('TStates') IS NOT NULL DROP TABLE TStates
IF OBJECT_ID('TSockSizes') IS NOT NULL DROP TABLE TSockSizes
IF OBJECT_ID('TPantSizes') IS NOT NULL DROP TABLE TPantSizes
IF OBJECT_ID('TShirtSizes') IS NOT NULL DROP TABLE TShirtSizes
IF OBJECT_ID('TTeamPlayers') IS NOT NULL DROP TABLE TTeamPlayers
IF OBJECT_ID('TPlayers') IS NOT NULL DROP TABLE TPlayers
IF OBJECT_ID('TGenders') IS NOT NULL DROP TABLE TGenders
IF OBJECT_ID('TTeams') IS NOT NULL DROP TABLE TTeams
GO
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'ENABLE TRIGGER ALL ON ?'
GO