且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在一条语句中添加多个约束

更新时间:2021-09-16 04:34:30

您遇到三个问题:


  1. 在第二行代码的末尾使用; 终止语句。

  2. 上一条语句中有 FOR MEMBER_ID ,可能应该是 FOR Sys_date

  3. 您重复 ADD ,但不要

  1. You terminate the statement with the ; at the end of your second line of code.
  2. You have FOR MEMBER_ID in the last statement, which should probably be FOR Sys_date.
  3. You repeat ADD but don't have to.

假定此表结构为:

CREATE TABLE Member (MEMBER_ID BIGINT NOT NULL, Sys_date DATETIME);

此DDL将起作用:

ALTER TABLE MEMBER
ADD CONSTRAINT U_MEMBERID UNIQUE(MEMBER_ID), primary key (MEMBER_ID),
    CONSTRAINT Sys_date DEFAULT GETDATE() FOR Sys_date;

请参见此sqlfiddle

可以理论上也可以在 ALTER TABLE 上的MSDN页面,尽管我会很容易地承认那些规格可能很难阅读。以下是他们对其解释的一种刺探:

You can theoretically also see this on the MSDN's page on ALTER TABLE, though I'd readily admit those specs can be hard to read. Here's a stab at how they explain it:

ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name 
{ 
    ALTER COLUMN column_name 
    -- Omitted....
    | ADD 
    { 
        <column_definition>
      | <computed_column_definition>
      | <table_constraint> 
      | <column_set_definition> 
    } [ ,...n ]
    -- Omitted....

ADD 关键字出现一次,并且} [,... n] 位告诉您可以在{括号} n 次之间重复位,并用隔开。

The ADD keyword occurs once, and the } [ ,...n ] bit tells you that you can repeat the bit between {brackets} n times, separated by a ,.