Defines a foreign key on a table or view in the current database.
sp_foreignkey tabname, pktabname, col1 [, col2] ... [, col8]
is the name of the table or view that contains the foreign key to be defined.
is the name of the table or view that has the primary key to which the foreign key applies. The primary key must already be defined.
is the name of the first column that makes up the foreign key. The foreign key must have at least one column and can have a maximum of eight columns.
The primary key of the publishers table is the pub_id column. The titles table also contains a pub_id column, which is a foreign key of publishers:
sp_foreignkey titles, publishers, pub_id
The primary key of the parts table has been defined with sp_primarykey as the partnumber and subpartnumber columns. The orders table contains the columns part and subpart, which make up a foreign key of parts:
sp_foreignkey orders, parts, part, subpart
sp_foreignkey adds the key to the syskeys table. Keys make explicit a logical relationship that is implicit in your database design.
sp_foreignkey does not enforce referential integrity constraints; use the foreign key clause of the create table or alter table command to enforce a foreign key relationship.
The number and order of columns that make up the foreign key must be the same as the number and order of columns that make up the primary key. The datatypes (and lengths) of the primary and foreign keys must agree, but the null types need not agree.
The installation process runs sp_foreignkey on the appropriate columns of the system tables.
To display a report on the keys that have been defined, execute sp_helpkey.
You cannot use a Java datatype with sp_foreignkey.
Only the owner of the table or view can execute sp_foreignkey.
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_helpjoins, sp_helpkey, sp_primarykey