如果有人可以帮助我的话,我试图找出我的掉落表和创建表格的顺序

时间:2018-03-15 20:46:32

标签: sql sql-server create-table alter-table drop-table

我无法弄清楚我的丢弃表和创建表格的顺序。我正在创建一个仿冒足球注册数据库。最初我很好但是想让它变得更复杂,从那以后我就无法弄清楚应该是什么样的顺序。我知道这听起来很愚蠢,但我还是一个学生正试图自己创造一些东西来了解更多。下面是我的drop table和create table现在和我的alter table的顺序。我非常感谢你们的帮助!

-- --------------------------------------------------------------------------------
-- Drop Tables
-- --------------------------------------------------------------------------------
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
IF OBJECT_ID('Z_TeamPlayers') IS NOT NULL DROP TABLE Z_TeamPlayers
IF OBJECT_ID('Z_TeamCoaches') IS NOT NULL DROP TABLE Z_TeamCoaches
IF OBJECT_ID('Z_Teams') IS NOT NULL DROP TABLE Z_Teams
IF OBJECT_ID('Z_Players') IS NOT NULL DROP TABLE Z_Players
IF OBJECT_ID('Z_Coaches') IS NOT NULL DROP TABLE Z_Coaches
IF OBJECT_ID('TUsers') IS NOT NULL DROP TABLE TUsers
IF OBJECT_ID('TRoles') IS NOT NULL DROP TABLE TRoles
IF OBJECT_ID('TLogins') IS NOT NULL DROP TABLE TLogins
IF OBJECT_ID('TFieldGames') IS NOT NULL DROP TABLE TFieldGames
------------------------------------------------------------------------------------
-- 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 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 TReferees
(
    intRefereeID    INTEGER         NOT NULL
    ,strFirstName   VARCHAR(50)     NOT NULL
    ,strMiddleName  VARCHAR(50)     NOT NULL
    ,strLastName    VARCHAR(50)     NOT NULL
    ,strEmail       VARCHAR(50)     NOT NULL
    ,dtmDateOfBirth DATE            NOT NULL
    ,strCity        VARCHAR(50)     NOT NULL
    ,intStateID     INTEGER         NOT NULL
    ,strAddress     varchar(50)     not null
    ,strZip         varchar(50)     not null
    ,strPhoneNumber 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
    ,CONSTRAINT TFields_PK PRIMARY KEY ( intFieldID )
)

CREATE TABLE TCoaches
(
    intCoachID      INTEGER         NOT NULL
    ,strFirstName   VARCHAR(50)     NOT NULL
    ,strMiddleName  varchar(50)     not null
    ,strLastName    VARCHAR(50)     NOT NULL
    ,intShirtID     INTEGER         NOT NULL
    ,dtmDateOfBirth DATE            NOT NULL
    ,strCity        Varchar(50)     not null
    ,intStateID     integer         not null
    ,strPhoneNumber varchar(50)     not null
    ,strEmail       VARCHAR(50)     NOT NULL
    ,strAddress     varchar(50)     not null
    ,strZip         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 TPlayers
(
    intPlayerID     INTEGER         NOT NULL
    ,strFirstName   VARCHAR(50)     NOT NULL
    ,strMiddleName  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
    ,strAddress     varchar(50)     not null
    ,strZip         varchar(50)     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 Z_TeamPlayers
(
    intTeamPlayerAuditID    INTEGER     IDENTITY    NOT NULL
    ,intTeamPlayerID        INTEGER         NOT NULL
    ,intTeamID              INTEGER         NOT NULL
    ,intPlayerID            INTEGER         NOT NULL
    ,UpdatedBy              VARCHAR(128)        NOT NULL
    ,UpdatedOn              DATETIME            NOT NULL
    ,strAction              VARCHAR(1)          NOT NULL
    ,strModified_Reason     VARCHAR(1000)
    ,CONSTRAINT Z_TeamPlayers_PK PRIMARY KEY ( intTeamPlayerAuditID )
)

CREATE TABLE Z_TeamCoaches
(
    intTeamCoachAuditID INTEGER IDENTITY    NOT NULL
    ,intTeamCoachID INTEGER         NOT NULL
    ,intTeamID      INTEGER         NOT NULL
    ,intCoachID     INTEGER         NOT NULL
    ,CONSTRAINT Z_TeamCoaches_PK PRIMARY KEY ( intTeamCoachAuditID )
)

CREATE TABLE Z_Teams
(
    intTeamAuditID  INTEGER IDENTITY        NOT NULL
    ,intTeamID      INTEGER         NOT NULL
    ,strTeam        VARCHAR(50)     NOT NULL
    ,UpdatedBy      VARCHAR(128)        NOT NULL
    ,UpdatedOn      DATETIME            NOT NULL
    ,strAction              VARCHAR(1)          NOT NULL
    ,strModified_Reason     VARCHAR(1000)
    ,CONSTRAINT Z_Teams_PK PRIMARY KEY ( intTeamAuditID )
)

CREATE TABLE Z_Players
(
    intPlayerAuditID    INTEGER IDENTITY    NOT NULL
    ,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
    ,UpdatedBy      VARCHAR(128)        NOT NULL
    ,UpdatedOn      DATETIME            NOT NULL
    ,strAction              VARCHAR(1)          NOT NULL
    ,strModified_Reason     VARCHAR(1000)
    ,CONSTRAINT Z_Players_PK PRIMARY KEY ( intPlayerAuditID )
)

CREATE TABLE Z_Coaches
(
    intAuditCoachID     INTEGER IDENTITY        NOT NULL
    ,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
    ,UpdatedBy      VARCHAR(128)        NOT NULL
    ,UpdatedOn      DATETIME            NOT NULL
    ,strAction              VARCHAR(1)          NOT NULL
    ,strModified_Reason     VARCHAR(1000)
    ,CONSTRAINT Z_Coaches_PK PRIMARY KEY ( intAuditCoachID )
)

CREATE TABLE TUsers
(
    intUserID       integer         not null
    ,intLoginID     integer         not null
    ,intRoleID      integer         not null
    ,CONSTRAINT TUsers_PK PRIMARY KEY ( intUserID )
)

CREATE TABLE TRoles
(
    intRoleID       integer         not null
    ,strRole            VARCHAR(50)     NOT NULL
    ,CONSTRAINT TRoles_PK PRIMARY KEY ( intRoleID )
)

CREATE TABLE TLogins 
(
    intLoginID      integer         not null
    ,strLoginName   varchar(50)     not null
    ,strPassword    VARCHAR(50)     NOT NULL
    ,CONSTRAINT TLogins_PK PRIMARY KEY ( intLoginID )
)

CREATE TABLE TFieldGames
(
    intFieldGameID  INTEGER         NOT NULL
    ,intFieldID     INTEGER         NOT NULL
    ,intTeamID      INTEGER         NOT NULL
    ,intRefereeID   INTEGER         NOT NULL
    ,CONSTRAINT TFieldGames_PK PRIMARY KEY ( intFieldGameID )
)


-- 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 TFieldGames ADD CONSTRAINT TFieldGames_TFields_FK
FOREIGN KEY ( intFieldID ) REFERENCES TFields ( intFieldID )

-- 10
ALTER TABLE TFieldGames ADD CONSTRAINT TFieldGames_TReferees_FK
FOREIGN KEY ( intRefereeID ) REFERENCES TReferees ( intRefereeID )

--11
ALTER TABLE TUsers ADD CONSTRAINT TUsers_TLogins_FK
FOREIGN KEY ( intLoginID ) REFERENCES TLogins ( intLoginID )

-- 12
ALTER TABLE TUsers ADD CONSTRAINT TUsers_TRoles_FK
FOREIGN KEY ( intRoleID ) REFERENCES TRoles ( intRoleID )


-- 13
ALTER TABLE TTeamPlayers ADD CONSTRAINT TTeamPlayers_TTeams_FK
FOREIGN KEY ( intTeamID ) REFERENCES TTeams ( intTeamID )

-- 14
ALTER TABLE TTeamCoaches ADD CONSTRAINT TTeamCoaches_TTeams_FK
FOREIGN KEY ( intTeamID ) REFERENCES TTeams ( intTeamID )

-- 15
ALTER TABLE TCoaches ADD CONSTRAINT TCoaches_TShirtSizes_FK
FOREIGN KEY ( intShirtSizeID ) REFERENCES TShirtSizes ( intShirtSizeID )

--16
ALTER TABLE TCoaches ADD CONSTRAINT TCoaches_TStates_FK
FOREIGN KEY ( intStateID ) REFERENCES TStates ( intStateID )

2 个答案:

答案 0 :(得分:3)

由于您的方案中没有外键,因此顺序无关紧要。这就是那些外键的用途:了解关系并保护您的结构。如果没有外键,数据库就无法了解结构,也不会让您远离破坏结构。在添加这些外键之前,您必须处理结构。

答案 1 :(得分:0)

由于您正在学习,最简单的方法是SSMS用于编写脚本的技术。首先,将外键放在一个批处理中(example)。然后放下桌子。目前还不确定你学习的重点是什么,但是在尝试以任何复杂程度编写DDL脚本之前,你应该先掌握tsql。鉴于您所描述的内容,您可能已经过头了,应该专注于首先使用设计良好的数据库获得技能/经验 - 例如,AdventureWorks或全球进口商。