在SQL Server中创建添加约束和默认值的表

时间:2014-03-07 01:46:22

标签: sql sql-server database-table

您好我是SQL服务器的新手,我正在创建表格我得到了以下ERD和约束我不确定我的代码是否与erd中的表匹配任何建议将不胜感激

Table 

Customer

CustomeNumber    Int  (PK)  
LastName         VarChar (30)  
FirstName        VarChar (30)  
Address(o)       VarChar (40)  
City(o)          VarChar (30)  
Province(o)      Char(2)  
PostalCode(o)    Char(6)  
HomePhone(o)     Char(10)  

Constraints  
Table----------Column -------Check------------Default    
Customer-----PostalCode-------L0L0L0     
Customer-----Province--------------------------------AB

我的创建表格中有以下内容

    CREATE TABLE Customer
    (
        CustomerNumber int Constraint PK_Customer_CustomerNumber Primary Key Clustered,
        FirstName   varchar(30)         Constraint Customer_FirstName       not null,
        LastName    varchar(30)         Constraint Customer_LastName        not null,
        Address     varchar(40)         Constraint Customer_Address         not null,
        City        varchar(30)         Constraint Customer_City            not null,
        Province    char(2)             Constraint Customer_Province        not null,
        PostalCode  char(6)             Constraint Customer_PostalCode      not null,
        HomePhone   char(10)            Constraint Customer_HomePhone       not null,
    )

1 个答案:

答案 0 :(得分:0)

试试这个......以获得基本的CREATE TABLE语句

CREATE TABLE Customer
(
CustomerNumber int      NOT NULL  Primary Key Clustered,
FirstName   varchar(30) not null,
LastName    varchar(30) not null,
Address     varchar(40) not null,
City        varchar(30) not null,
Province    char(2)     not null,
PostalCode  char(6)     not null,
HomePhone   char(10)    not null,
)
GO

请阅读此处以了解有关Default ConstraintsCheck Constraints

的更多信息