Megosztás a következőn keresztül:


REFERENTIAL_CONSTRAINTS (Transact-SQL)

Returns one row for each FOREIGN KEY constraint in the current database. This information schema view returns information about the objects to which the current user has permissions.

To retrieve information from these views, specify the fully qualified name of INFORMATION_SCHEMA.view_name.

Column name

Data type

Description

CONSTRAINT_CATALOG

nvarchar(128)

Constraint qualifier.

CONSTRAINT_SCHEMA

nvarchar(128)

Name of schema that contains the constraint.

Important noteImportant
Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of an object is to query the sys.objects catalog view or OBJECT_SCHEMA_NAME function.

CONSTRAINT_NAME

sysname

Constraint name.

UNIQUE_CONSTRAINT_CATALOG

nvarchar(128)

UNIQUE constraint qualifier.

UNIQUE_CONSTRAINT_SCHEMA

nvarchar(128)

Name of schema that contains the UNIQUE constraint.

Important noteImportant
Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of an object is to query the sys.objects catalog view or OBJECT_SCHEMA_NAME function.

UNIQUE_CONSTRAINT_NAME

sysname

UNIQUE constraint.

MATCH_OPTION

varchar(7)

Referential constraint-matching conditions. Always returns SIMPLE. This means that no match is defined. The condition is considered a match when one of the following is true:

  • At least one value in the foreign key column is NULL.

  • All values in the foreign key column are not NULL, and there is a row in the primary key table that has the same key.

UPDATE_RULE

varchar(11)

Action taken when a Transact-SQL statement violates the referential integrity that is defined by this constraint.

Returns one of the following:

  • NO ACTION

  • CASCADE

  • SET NULL

  • SET DEFAULT

If NO ACTION is specified on ON UPDATE for this constraint, the update of the primary key that is referenced in the constraint will not be propagated to the foreign key. If such an update of a primary key will cause a referential integrity violation because at least one foreign key contains the same value, SQL Server will not make any change to the parent and referring tables. SQL Server also will raise an error.

If CASCADE is specified on ON UPDATE for this constraint, any change to the primary key value is automatically propagated to the foreign key value.

DELETE_RULE

varchar(11)

Action taken when a Transact-SQL statement violates referential integrity defined by this constraint.

Returns one of the following:

  • NO ACTION

  • CASCADE

  • SET NULL

  • SET DEFAULT

If NO ACTION is specified on ON DELETE for this constraint, the delete on the primary key that is referenced in the constraint will not be propagated to the foreign key. If such a delete of a primary key will cause a referential integrity violation because at least one foreign key contains the same value, SQL Server will not make any change to the parent and referring tables. SQL Server also will raise an error.

If CASCADE is specified on ON DELETE on this constraint, any change to the primary key value is automatically propagated to the foreign key value.

Examples

The following example returns information about each FOREIGN KEY constraint in the Sales schema of the database.

USE AdventureWorks;
GO

SELECT CONSTRAINT_SCHEMA, CONSTRAINT_NAME, UNIQUE_CONSTRAINT_SCHEMA, UNIQUE_CONSTRAINT_NAME, 
    MATCH_OPTION, UPDATE_RULE, DELETE_RULE
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = 'Sales';