SQL Server错误:引入外键约束

时间:2016-07-25 03:31:33

标签: sql-server

我一直收到以下错误:

  

介绍FOREIGN KEY约束' FK_Zipcode2'在桌子上Employee'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

我做错了什么?这是我的代码:

USE Master

/*If a "Richardson_LAR" database already exists, drop it.*/

IF EXISTS(SELECT * FROM sysdatabases WHERE name = 'Richardson_LAR')
DROP DATABASE Richardson_LAR

/*Create a database named "Richardson_LAR"*/

GO

CREATE DATABASE Richardson_LAR

ON PRIMARY

(
NAME = 'Richardson_LAR',
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Richardson_LAR.mdf',
SIZE = 10MB,
MAXSIZE = 50MB,
FILEGROWTH = 10%
)

LOG ON

(
NAME = 'Richardson_LAR_Log',
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\Richardson_LAR.ldf',
SIZE = 1000KB,
MAXSIZE = 5MB,
FILEGROWTH = 500KB
)

/*Create tables named Appointment, CreditCard, Customer, Employee, Payment, Receipt, Spa, SpaPackage, SpaService, and Zip.*/

GO

USE Richardson_LAR

CREATE TABLE SpaService
(
SpaServiceID            smallint        NOT NULL    IDENTITY(1, 1), 
SpaServiceName          varchar(30)     NOT NULL,
SpaServiceCategory      char(1)         NOT NULL,
SpaServicePrice         smallmoney      NOT NULL,
SpaServiceDescription   varchar(200)    NOT NULL
)

CREATE TABLE Zip
(
Zipcode                 char(10)        NOT NULL,
City                    varchar(20)     NOT NULL,
State                   char(2)         NULL,
Country                 varchar(20)     NOT NULL
)

CREATE TABLE Appointment
(
AppointmentID           smallint        NOT NULL    IDENTITY(1, 1),
CustomerID              smallint        NOT NULL,
EmployeeID              smallint        NOT NULL,
AppointmentDateTime     smalldatetime   NOT NULL,
AppointmentStatus       char(1)         NOT NULL,
AppointmentComments     varchar(200)    NULL
)

CREATE TABLE CreditCard
(
CreditCardID            smallint        NOT NULL    IDENTITY(1, 1),
CustomerID              smallint        NOT NULL,
CCType                  varchar(5)      NOT NULL,
CCNumber                varchar(16)     NOT NULL,
CCCompany               varchar(40)     NOT NULL,
CCCardHolder            varchar(40)     NOT NULL,
CCExpiration            smalldatetime   NOT NULL
)

CREATE TABLE Customer
(
CustomerID              smallint        NOT NULL    IDENTITY(1000, 1),
CustomerFirstName       varchar(20)     NOT NULL,
CustomerLastName        varchar(20)     NOT NULL,
CustomerAddress1        varchar(30)     NOT NULL,
CustomerAddress2        varchar(10)     NULL,
Zipcode                 char(10)        NOT NULL,
CustomerPhone           varchar(20)     NOT NULL,
CustomerEmail           varchar(30)     NULL,
CustomerComments        varchar(200)    NULL,
)

CREATE TABLE Employee
(
EmployeeID              smallint        NOT NULL    IDENTITY(2000, 1),
EmployeeGender          char(1)         NOT NULL,
EmployeeFirstName       varchar(20)     NOT NULL,
EmployeeLastName        varchar(20)     NOT NULL,
EmployeeAddress1        varchar(30)     NOT NULL,
EmployeeAddress2        varchar(10)     NULL,
Zipcode                 char(10)        NOT NULL,
EmployeePhone           varchar(20)     NOT NULL,
EmployeeEmail           varchar(30)     NULL,
SpaID                   smallint        NOT NULL,
EmployeeTitle           varchar(30)     NOT NULL,
EmployeeHireDate        date            NOT NULL,
EmployeeStartDate       date            NOT NULL
)

CREATE TABLE Hotel
(
HotelID                 smallint        NOT NULL,
HotelName               varchar(30)     NOT NULL,
HotelAddress            varchar(30)     NOT NULL,
Zipcode                 char(10)        NOT NULL,
HotelStarRating         char(1)         NULL,
HotelPictureLink        varchar(100)    NULL,
TaxLocationID           smallint        NOT NULL
)

CREATE TABLE Payment
(
PaymentID               smallint        NOT NULL    IDENTITY(3000, 1),
AppointmentID           smallint        NOT NULL,
PaymentDate             date            NOT NULL,
PaymentAmount           smallmoney      NOT NULL,
PaymentMethod           char(1)         NOT NULL,
PaymentComments         varchar(200)    NULL
)

CREATE TABLE Receipt
(
ReceiptID               smallint        NOT NULL    IDENTITY(1, 1),
AppointmentID           smallint        NOT NULL,
SpaPackageID            smallint        NOT NULL,
SpaServiceID            smallint        NOT NULL,
ReceiptAmount           smallmoney      NOT NULL,
ReceiptItemQty          tinyint         NOT NULL,
ReceiptDate             smalldatetime   NOT NULL
)

CREATE TABLE Spa
(
SpaID                   char(5)         NOT NULL,
HotelID                 smallint        NOT NULL,
SpaWeekdayHours         smalldatetime   NOT NULL,
SpaWeekendHours         smalldatetime   NOT NULL,
SpaAddress              varchar(30)     NOT NULL,
Zipcode                 char(10)        NOT NULL,
SpaPhone                varchar(20)     NOT NULL,
SpaEmail                varchar(30)     NOT NULL
)

CREATE TABLE SpaPackage
(
SpaPackageID            smallint        NOT NULL    IDENTITY(1, 1),
SpaPackageName          varchar(30)     NOT NULL,
SpaServiceID            smallint        NOT NULL,
SpaPackagePrice         smallmoney      NOT NULL,
SpaPackageDescription   varchar(200)    NOT NULL
)

/*Alter the Appointment, CreditCard, Customer, Employee, Payment, Zipcode, Receipt, Spa, SpaPackage, and SpaService tables by adding primary keys.*/

GO

ALTER TABLE SpaService
    ADD CONSTRAINT PK_SpaServiceID
    PRIMARY KEY (SpaServiceID)

ALTER TABLE Zip
    ADD CONSTRAINT PK_Zipcode
    PRIMARY KEY (Zipcode)

ALTER TABLE Appointment
    ADD CONSTRAINT PK_AppointmentID
    PRIMARY KEY (AppointmentID)

ALTER TABLE CreditCard
    ADD CONSTRAINT PK_CreditCardID
    PRIMARY KEY (CreditCardID)

ALTER TABLE Customer
    ADD CONSTRAINT PK_CustomerID
    PRIMARY KEY (CustomerID)

ALTER TABLE Employee
    ADD CONSTRAINT PK_EmployeeID
    PRIMARY KEY (EmployeeID)

ALTER TABLE Hotel
    ADD CONSTRAINT PK_HotelID
    PRIMARY KEY (HotelID)

ALTER TABLE Payment
    ADD CONSTRAINT PK_PaymentID
    PRIMARY KEY (PaymentID)

ALTER TABLE Receipt
    ADD CONSTRAINT PK_ReceiptID
    PRIMARY KEY (ReceiptID)

ALTER TABLE Spa
    ADD CONSTRAINT PK_SpaID
    PRIMARY KEY (SpaID)

ALTER TABLE SpaPackage
    ADD CONSTRAINT PK_SpaPackageID
    PRIMARY KEY (SpaPackageID)

/*Alter the Appointment, CreditCard, Customer, Employee, Payment, Receipt, Spa, and SpaPackage tables by adding foreign keys.*/

GO

ALTER TABLE Appointment
    ADD

    CONSTRAINT FK_CustomerID1
    FOREIGN KEY (CustomerID) REFERENCES Customer (CustomerID)
    ON UPDATE Cascade
    ON DELETE Cascade,

    CONSTRAINT FK_EmployeeID
    FOREIGN KEY (EmployeeID) REFERENCES Employee (EmployeeID)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE CreditCard
    ADD

    CONSTRAINT FK_CustomerID2
    FOREIGN KEY (CustomerID) REFERENCES Customer (CustomerID)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE Customer
    ADD

    CONSTRAINT FK_Zipcode1
    FOREIGN KEY (Zipcode) REFERENCES Zip (Zipcode)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE Employee
    ADD

    CONSTRAINT FK_Zipcode2
    FOREIGN KEY (Zipcode) REFERENCES Zip (Zipcode)
    ON UPDATE Cascade
    ON DELETE Cascade,

    CONSTRAINT FK_SpaID
    FOREIGN KEY (SpaID) REFERENCES Spa (SpaID)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE Hotel
    ADD

    CONSTRAINT FK_Zipcode3
    FOREIGN KEY (Zipcode) REFERENCES Zip (Zipcode)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE Payment
    ADD

    CONSTRAINT FK_AppointmentID1
    FOREIGN KEY (AppointmentID) REFERENCES Appointment (AppointmentID)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE Receipt
    ADD

    CONSTRAINT FK_AppointmentID2
    FOREIGN KEY (AppointmentID) REFERENCES Appointment (AppointmentID)
    ON UPDATE Cascade
    ON DELETE Cascade,

    CONSTRAINT FK_SpaPackageID
    FOREIGN KEY (SpaPackageID) REFERENCES SpaPackage (SpaPackageID)
    ON UPDATE Cascade
    ON DELETE Cascade,

    CONSTRAINT FK_SpaServiceID1
    FOREIGN KEY (SpaServiceID) REFERENCES SpaService (SpaServiceID)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE Spa
    ADD

    CONSTRAINT FK_HotelID
    FOREIGN KEY (HotelID) REFERENCES Hotel (HotelID)
    ON UPDATE Cascade
    ON DELETE Cascade

ALTER TABLE SpaPackage
    ADD

    CONSTRAINT FK_SpaServiceID2
    FOREIGN KEY (SpaServiceID) REFERENCES SpaService (SpaServiceID)
    ON UPDATE Cascade
    ON DELETE Cascade

0 个答案:

没有答案