다음을 통해 공유


Key Object

Microsoft SQL Server의 이후 버전에서는 이 기능이 제거됩니다. 새 개발 작업에서는 이 기능을 사용하지 않도록 하고, 현재 이 기능을 사용하는 응용 프로그램은 수정하십시오.

The Key object exposes the attributes of Microsoft SQL Server table keys.

현재 개체를 보여 주는 SQL-DMO 개체 모델

주의

SQL Server tables can contain key constraints. The constraints apply declarative referential integrity to the data contained in the table. Keys can be primary or foreign. A single primary key can be defined on a table, though many foreign keys can be defined, constraining data for a column or columns to values existing as primary key values in other tables.

With the Key object, you can:

  • Define a PRIMARY KEY constraint for a SQL Server table.

  • Remove a PRIMARY KEY constraint from a SQL Server table.

  • Define a FOREIGN KEY constraint for a SQL Server table.

  • Remove a FOREIGN KEY constraint from a SQL Server table.

  • Rebuild the index used to maintain a PRIMARY KEY constraint on a table.

The Name property of a Key object uses the SQL Server data type sysname. The value of the Name property must be unique within a SQL Server database. The Name property is not required when using a Key object to define a new SQL Server PRIMARY or FOREIGN KEY constraint. When the Name property is not specified, SQL Distributed Management Objects (SQL-DMO) generates a name automatically.

To define a PRIMARY KEY constraint on a SQL Server table

  1. Create a Key object.

  2. Set the Type property to SQLDMOKey_Primary.

  3. Set the Clustered property to TRUE to create a SQL Server clustered index if clustering is a required attribute of the PRIMARY KEY constraint.

  4. Get the KeyColumnsNames collection from the Key object.

  5. Add the PRIMARY KEY column names to the Names collection. The order in which column names are added determines the order of column participation in the index maintaining the PRIMARY KEY constraint.

  6. Add the Key object to the Keys collection of a Table object that exposes the attributes of the SQL Server table.

To define a FOREIGN KEY constraint on a SQL Server table

  1. Create a Key object.

  2. Set the Type property to SQLDMOKey_Foreign.

  3. Get the KeyColumnsNames collection from the Key object.

  4. Add the FOREIGN KEY column names to the Names collection. The Names collection contains the names of the column or columns that make up the FOREIGN KEY constraint.

  5. Set the ReferencedTable property to the name of the SQL Server table containing the PRIMARY KEY constraint to be referenced by the FOREIGN KEY constraint.

  6. Get the ReferencedColumns Names collection from the Key object.

  7. Add the name of the columns participating in the PRIMARY KEY constraint of the specified table to the ReferencedColumns Namescollection.

  8. Add the Key object to the Keys collection of a Table object that exposes the attributes of the SQL Server table to receive the FOREIGN KEY constraint.