如何创建带有外键的表?

时间:2020-04-28 21:15:25

标签: sql-server

我正在建立一个数据库数据库,并绘制出here的表格示意图。我已经创建了所有不依赖于任何其他表格的表格。

USE master
GO

/******Check to see if database exists******/
IF DB_ID('SET2133810') IS NOT NULL
    DROP DATABASE SET2133810
GO

/******Object: Database SET2133810******/
CREATE DATABASE SET2133810
GO

USE SET2133810
GO

/******Object: table*****/
CREATE TABLE BookList(
    BookID int IDENTITY(1,1) NOT NULL,
    BookISBN varchar(20) NOT NULL,
    BookTitle varchar(50) NOT NULL,
    BookAuthor varchar(50) NOT NULL,
    BookPublisher varchar(50) NOT NULL,
    BookGenre varchar(50) NULL,
 CONSTRAINT PK_BookList PRIMARY KEY CLUSTERED (
    BookID ASC
 )
)
GO

/******Object: Table*****/
CREATE TABLE MembersList(
    MemberID int IDENTITY(1,1) NOT NULL,
    MemberLName varchar(50) NOT NULL,
    MemberFName varchar(50) NOT NULL,
    MemberAddress varchar(50) NULL,
    MemberCity varchar(50) NOT NULL,
    MemberState char(2) NOT NULL,
    MemberZipCode varchar(20) NOT NULL,
    MemberPhone varchar(50) NOT NULL,
    MemberEmail varchar(50) NOT NULL,
 CONSTRAINT PK_MembersList PRIMARY KEY CLUSTERED(
    MemberID ASC
 )
)
GO

/******Object: table*****/
CREATE TABLE VendorsList(
    VendorID int IDENTITY(1,1) NOT NULL,
    VendorName varchar(50) NOT NULL,
    VendorPhone varchar(50) NULL,
    VendorContactLName varchar(50) NULL,
    VendorContactFName varchar(50) NULL,
    VendorAddress varchar(50) NULL,
    VendorCity varchar(50) NOT NULL,
    VendorState char(2) NOT NULL,
    VendorZipCode varchar(20) NOT NULL,
 CONSTRAINT PK_VendorsList PRIMARY KEY CLUSTERED (
    VendorID ASC
 )
)
GO

/******Object: Table*****/
CREATE TABLE InvoicesList(
    InvoiceID int IDENTITY(1,1) NOT NULL,
    VendorID int NOT NULL,
    InvoiceNumber varchar(50) NOT NULL,
    InvoiceDate smalldatetime NOT NULL,
    InvoiceTotal money NOT NULL,
    PaymentTotal money NOT NULL,
    PaymentDate smalldatetime NULL,
 CONSTRAINT PK_InvoicesList PRIMARY KEY CLUSTERED(
    InvoiceID ASC
 )
)
GO

我对如何对booksIssued表和InvoiceItems表进行编码感到困惑,因为它们中有外键,因此我认为需要某种显式代码来说明这种关系。

通过从我的教科书中复制一个创建类似数据库的脚本,我可以做到这一点。我直接整理了几张桌子。您会在下面的课本代码中看到它们已经用外键创建了一些表。我只是不确定什么代码做什么?以及在创建表时标识这些关系的代码的位置。

USE master
GO

/****** Object:  Database AP     ******/
IF DB_ID('AP') IS NOT NULL
    DROP DATABASE AP
GO

CREATE DATABASE AP
GO 

USE AP
GO

/****** Object:  Table ContactUpdates  ******/   
CREATE TABLE ContactUpdates(
    VendorID int IDENTITY(1,1) NOT NULL,
    LastName varchar(50) NULL,
    FirstName varchar(50) NULL
)
GO

/****** Object:  Table GLAccounts     ******/
CREATE TABLE GLAccounts(
    AccountNo int NOT NULL,
    AccountDescription varchar(50) NOT NULL,
 CONSTRAINT PK_GLAccounts PRIMARY KEY CLUSTERED (
    AccountNo ASC
 )
)
GO

/****** Object:  Table InvoiceArchive     ******/
CREATE TABLE InvoiceArchive(
    InvoiceID int NOT NULL,
    VendorID int NOT NULL,
    InvoiceNumber varchar(50) NOT NULL,
    InvoiceDate smalldatetime NOT NULL,
    InvoiceTotal money NOT NULL,
    PaymentTotal money NOT NULL,
    CreditTotal money NOT NULL,
    TermsID int NOT NULL,
    InvoiceDueDate smalldatetime NOT NULL,
    PaymentDate smalldatetime NULL
) 
GO

/****** Object:  Table InvoiceLineItems     ******/
CREATE TABLE InvoiceLineItems(
    InvoiceID int NOT NULL,
    InvoiceSequence smallint NOT NULL,
    AccountNo int NOT NULL,
    InvoiceLineItemAmount money NOT NULL,
    InvoiceLineItemDescription varchar(100) NOT NULL,
CONSTRAINT PK_InvoiceLineItems PRIMARY KEY CLUSTERED (
    InvoiceID ASC,
    InvoiceSequence ASC
 ) 
)
GO

/****** Object:  Table Invoices     ******/
CREATE TABLE Invoices(
    InvoiceID int IDENTITY(1,1) NOT NULL,
    VendorID int NOT NULL,
    InvoiceNumber varchar(50) NOT NULL,
    InvoiceDate smalldatetime NOT NULL,
    InvoiceTotal money NOT NULL,
    PaymentTotal money NOT NULL,
    CreditTotal money NOT NULL,
    TermsID int NOT NULL,
    InvoiceDueDate smalldatetime NOT NULL,
    PaymentDate smalldatetime NULL,
 CONSTRAINT PK_Invoices PRIMARY KEY CLUSTERED (
    InvoiceID ASC
 )
)
GO

/****** Object:  Table Terms    ******/
CREATE TABLE Terms(
    TermsID int IDENTITY(1,1) NOT NULL,
    TermsDescription varchar(50) NOT NULL,
    TermsDueDays smallint NOT NULL,
 CONSTRAINT PK_Terms PRIMARY KEY CLUSTERED (
    TermsID ASC
 )
) 
GO

/****** Object:  Table Vendors    ******/
CREATE TABLE Vendors(
    VendorID int IDENTITY(1,1) NOT NULL,
    VendorName varchar(50) NOT NULL,
    VendorAddress1 varchar(50) NULL,
    VendorAddress2 varchar(50) NULL,
    VendorCity varchar(50) NOT NULL,
    VendorState char(2) NOT NULL,
    VendorZipCode varchar(20) NOT NULL,
    VendorPhone varchar(50) NULL,
    VendorContactLName varchar(50) NULL,
    VendorContactFName varchar(50) NULL,
    DefaultTermsID int NOT NULL,
    DefaultAccountNo int NOT NULL,
 CONSTRAINT PK_Vendors PRIMARY KEY CLUSTERED(
    VendorID ASC
 )
) 
GO

1 个答案:

答案 0 :(得分:0)

您以与CREATE TABLE语句内联的主键约束相似的方式添加外键约束

CREATE TABLE Invoices(
    InvoiceID int IDENTITY(1,1) NOT NULL,
    VendorID int NOT NULL,
    InvoiceNumber varchar(50) NOT NULL,
    InvoiceDate smalldatetime NOT NULL,
    InvoiceTotal money NOT NULL,
    PaymentTotal money NOT NULL,
    PaymentDate smalldatetime NULL,
 CONSTRAINT PK_Invoices 
    PRIMARY KEY CLUSTERED(InvoiceID ASC),
 CONSTRAINT FK_Invoices_Vendors 
    FOREIGN KEY (VendorID) REFERENCES Vendors(VendorID)
 )

或者在使用ALTER TABLE创建表之后

 ALTER TABLE Invoices ADD CONSTRAINT FK_Invoices_Vendors
    FOREIGN KEY (VendorID) references Vendors(VendorID)

请注意将List作为后缀或tbl_前缀添加到表的不良做法。单数或复数名词以及名词短语都是可以接受的。尽管您不应该在数据库中混合使用单数和复数。

以全小写或全大写键入SQL关键字也是可以接受的,但再次选择一个并保持一致。