Defines a primary key on a table or view.
sp_primarykey tabname, col1 [, col2, col3, ..., col8]
is the name of the table or view on which to define the primary key.
is the name of the first column that makes up the primary key. The primary key can consist of from one to eight columns.
Defines the au_id field as the primary key of the table authors:
sp_primarykey authors, au_id
Defines the combination of the fields lastname and firstname as the primary key of the table employees:
sp_primarykey employees, lastname, firstname
Executing sp_primarykey adds the key to the syskeys table. Only the owner of a table or view can define its primary key. sp_primarykey does not enforce referential integrity constraints; use the primary key clause of the create table or alter table command to enforce a primary key relationship.
Define keys with sp_primarykey, sp_commonkey, and sp_foreignkey to make explicit a logical relationship that is implicit in your database design. An application program can use the information.
A table or view can have only one primary key. To display a report on the keys that have been defined, execute sp_helpkey.
The installation process runs sp_primarykey on the appropriate columns of the system tables.
You must be the table owner to execute sp_primarykey. Permission checks do not differ based on the granular permissions settings.
Values in event and extrainfo columns from the sysaudits table are:
Event |
Audit option |
Command or access audited |
Information in extrainfo |
---|---|---|---|
38 |
exec_procedure |
Execution of a procedure |
|
Commands alter table, create table, create trigger
System procedures sp_commonkey, sp_dropkey, sp_foreignkey, sp_helpjoins, sp_helpkey