sp_primarykey

Defines a primary key on a table or view.

Syntax

sp_primarykey tabname, col1 [, col2, col3, ..., col8]

Parameters

Examples

Usage

There are additional considerations when using sp_primarykey:
  • 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.

See also alter table, create table, create trigger in Reference Manual: Commands.

Permissions

You must be the table owner to execute sp_primarykey. Permission checks do not differ based on the granular permissions settings.

Auditing

Values in event and extrainfo columns from the sysaudits table are:

InformationValues
Event

38

Audit option

exec_procedure

Command or access audited

Execution of a procedure

Information in extrainfo
  • Roles – Current active roles

  • Keywords or options – NULL

  • Previous value – NULL

  • Current value – NULL

  • Other information – All input parameters

  • Proxy information – Original login name, if set proxy in effect

Related reference
sp_commonkey
sp_dropkey
sp_foreignkey
sp_helpjoins
sp_helpkey