Key Object
В будущей версии Microsoft SQL Server эта возможность будет удалена. Избегайте использования этой возможности в новых разработках и запланируйте изменение существующих приложений, в которых она применяется.
The Key object exposes the attributes of Microsoft SQL Server table keys.
Properties
Замечания
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
Create a Key object.
Set the Type property to SQLDMOKey_Primary.
Set the Clustered property to TRUE to create a SQL Server clustered index if clustering is a required attribute of the PRIMARY KEY constraint.
Get the KeyColumnsNames collection from the Key object.
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.
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
Create a Key object.
Set the Type property to SQLDMOKey_Foreign.
Get the KeyColumnsNames collection from the Key object.
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.
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.
Get the ReferencedColumns Names collection from the Key object.
Add the name of the columns participating in the PRIMARY KEY constraint of the specified table to the ReferencedColumns Namescollection.
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.