首頁>Program>source
CREATE TABLE [dbo].[user](
        [userID] [int] IDENTITY(1,1) NOT NULL,
        [fcode] [int] NULL,
        [scode] [int] NULL,
        [dcode] [int] NULL,
        [name] [nvarchar](50) NULL,
        [address] [nvarchar](50) NULL,
     CONSTRAINT [PK_user_1] PRIMARY KEY CLUSTERED 
    (
        [userID] ASC
    )
    ) ON [PRIMARY]
    GO

如何為 fcode, scode, dcode列添加唯一約束 与 t-sql 和/或 management studiofcode, scode, dcode 在一起必须是唯一的。

最新回復
  • 5月前
    1 #

    通過在表建立時使用約束定義,可以指定一个或多个跨越多个列的約束.从technet的文件中簡化的語法為:

    CONSTRAINT constraint_name UNIQUE [ CLUSTERED | NONCLUSTERED ] 
    (
        column [ ASC | DESC ] [ ,...n ]
    )
    

    因此,重排表定義為:

    CREATE TABLE [dbo].[user](
        [userID] [int] IDENTITY(1,1) NOT NULL,
        [fcode] [int] NULL,
        [scode] [int] NULL,
        [dcode] [int] NULL,
        [name] [nvarchar](50) NULL,
        [address] [nvarchar](50) NULL,
        CONSTRAINT [PK_user_1] PRIMARY KEY CLUSTERED 
        (
            [userID] ASC
        ),
        CONSTRAINT [UQ_codes] UNIQUE NONCLUSTERED
        (
            [fcode], [scode], [dcode]
        )
    ) ON [PRIMARY]
    

  • 5月前
    2 #

    如果已经在資料庫中建立了表,則可以稍後使用此SQL查詢添加唯一約束:

    ALTER TABLE dbo.User
      ADD CONSTRAINT ucCodes UNIQUE (fcode, scode, dcode)
    

  • 5月前
    3 #

    這也可以在GUI中完成.這是一个向現有表添加多列唯一約束的示例。

    在表格下,右键單击索引->單击/悬停新索引->單击非聚集索引...

    將提供預設的索引名稱,但您可能需要更改它.選中Unique複選框,然後單击Add ...按钮

    檢查要包含的列

    在每个視窗中單击確定,您就完成了。

  • 5月前
    4 #

    USE [TSQL2012]
    GO
    /****** Object:  Table [dbo].[Table_1]    Script Date: 11/22/2015 12:45:47 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    CREATE TABLE [dbo].[Table_1](
        [seq] [bigint] IDENTITY(1,1) NOT NULL,
        [ID] [int] NOT NULL,
        [name] [nvarchar](50) NULL,
        [cat] [nvarchar](50) NULL,
     CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED 
    (
        [ID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
     CONSTRAINT [IX_Table_1] UNIQUE NONCLUSTERED 
    (
        [name] ASC,
        [cat] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    

  • docker主機操作系統和容器基础映像操作系統之間是什麼關係?
  • ios:仪器分配跟蹤使用者定義類的物件的分配和取消分配