茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671280 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00183.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

DB研討會 : [轉貼]建立唯一的條件約束

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]建立唯一的條件約束

建立唯一的條件約束 Create Unique Constraints

您可以使用 SQL Server 2017 SQL Server 2017Transact-SQL SQL Server Management Studio ,在 Transact-SQL Transact-SQL 中建立唯一條件約束,確保在沒有參與主索引鍵之特定資料行中輸入的值不會重複。 You can create a unique constraint in SQL Server 2017 SQL Server 2017 by using Transact-SQL SQL Server Management Studio or Transact-SQL Transact-SQL to ensure no duplicate values are entered in specific columns that do not participate in a primary key. 建立唯一條件約束會自動建立對應的唯一索引。 Creating a unique constraint automatically creates a corresponding unique index.

開始之前 Before You Begin

安全性 Security

權限 Permissions

需要資料表的 ALTER 權限。 Requires ALTER permission on the table.

使用 SQL Server Management Studio Using SQL Server Management Studio

若要建立唯一條件約束 To create a unique constraint

  1. 物件總管中,以滑鼠右鍵按一下要加入唯一條件約束的資料表,然後按一下 [設計]。 In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.

  2. 在 [資料表設計工具] 功能表上,按一下 [索引/索引鍵]。 On the Table Designer menu, click Indexes/Keys.


  3. 在 [索引/索引鍵] 對話方塊中,按一下 [加入]。 In the Indexes/Keys dialog box, click Add.

  4. 在 [一般] 底下的方格中,按一下 [類型],然後從屬性右邊的下拉式清單方塊中選擇 [唯一索引鍵]。 In the grid under General, click Type and choose Unique Key from the drop-down list box to the right of the property.

  5. 在 [檔案] 功能表上,按一下 [儲存 <資料表名稱>]。 On the File menu, click Save table name.

使用 Transact-SQL Using Transact-SQL

若要建立唯一條件約束 To create a unique constraint

  1. [物件總管] 中,連接到 Database Engine Database Engine的執行個體。
    In Object Explorer, connect to an instance of Database Engine Database Engine.

  2. 在標準列上,按一下 [新增查詢] On the Standard bar, click New Query.

  3. 複製下列範例並將其貼到查詢視窗中,然後按一下 [執行] Copy and paste the following example into the query window and click Execute. 此範例會建立 TransactionHistoryArchive4 資料表並且在 TransactionID資料行上建立唯一條件約束。 The example creates the table TransactionHistoryArchive4 and creates a unique constraint on the column TransactionID.


    USE AdventureWorks2012;
    GO
    CREATE TABLE Production.TransactionHistoryArchive4
    (
    TransactionID int NOT NULL,
    CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
    );
    GO

若要在現有的資料表上建立唯一條件約束 To create a unique constraint on an existing table

  1. [物件總管] 中,連接到 Database Engine Database Engine的執行個體。 In Object Explorer, connect to an instance of Database Engine Database Engine.

  2. 在標準列上,按一下 [新增查詢] On the Standard bar, click New Query.

  3. 複製下列範例並將其貼到查詢視窗中,然後按一下 [執行] Copy and paste the following example into the query window and click Execute. 此範例會在 PasswordHash 資料表中的 PasswordSaltPerson.Password資料行上建立唯一條件約束。
    The example creates a unique constraint on the columns PasswordHash and PasswordSalt in the table Person.Password.


    USE AdventureWorks2012;
    GO
    ALTER TABLE Person.Password
    ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt);
    GO

若要在新的資料表中建立唯一條件約束 To create a unique constraint in an new table

  1. [物件總管] 中,連接到 Database Engine Database Engine的執行個體。 In Object Explorer, connect to an instance of Database Engine Database Engine.

    在標準列上,按一下 [新增查詢] On the Standard bar, click New Query.

  2. 複製下列範例並將其貼到查詢視窗中,然後按一下 [執行] Copy and paste the following example into the query window and click Execute. 此範例會建立資料表並且在 TransactionID 資料行上定義唯一條件約束。 The example creates a table and defines a unique constraint on the column TransactionID.


    USE AdventureWorks2012;
    GO
    CREATE TABLE Production.TransactionHistoryArchive2
    (
    TransactionID int NOT NULL,
    CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
    );
    GO

    如需詳細資訊,請參閱 ALTER TABLE (Transact-SQL)CREATE TABLE (Transact-SQL)table_constraint (Transact-SQL) For more information, see ALTER TABLE (Transact-SQL), CREATE TABLE (Transact-SQL), and
    table_constraint (Transact-SQL).


    原文出處:建立唯一的條件約束 | Microsoft Docs
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]SQL UNIQUE Constraint
SQL UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:

SQL Server / Oracle / MS Access:
CREATE TABLE Persons (
    ID int NOT NULL UNIQUE,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int
);

MySQL:
CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    UNIQUE (ID)
);

To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CONSTRAINT UC_Person UNIQUE (ID,LastName)
);

SQL UNIQUE Constraint on ALTER TABLE

To create a UNIQUE constraint on the "ID" column when the table is already created, use the following SQL:

MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD UNIQUE (ID);

To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);

DROP a UNIQUE Constraint

To drop a UNIQUE constraint, use the following SQL:

MySQL:
ALTER TABLE Persons
DROP UNIQUE UC_Person;

SQL Server / Oracle / MS Access:
ALTER TABLE Persons
DROP CONSTRAINT UC_Person;


原文出處:SQL UNIQUE Constraint
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|