If you need to return all foreign keys that reference a given table in SQL Server, try one of the following methods.
The first method queries the sys.foreign_keys
system view. The second method executes the sp_fkeys
system stored procedure.
If you need to return all foreign keys that reference a given table in SQL Server, try one of the following methods.
The first method queries the sys.foreign_keys
system view. The second method executes the sp_fkeys
system stored procedure.
You can use the OBJECTPROPERTY()
function in SQL Server to check whether or not a table is referenced by a foreign key.
To do this, pass the table’s object ID as the first argument, and TableHasForeignRef
as the second argument. The function returns a 1
or a 0
depending on whether or not it is referenced by a foreign key.
A return value of 1
means that the table is referenced by a foreign key, and a value of 0
means that it’s not.
Note that the examples presented here don’t list out the foreign keys or their tables or anything like that. They simply return a true/false value that you can use to test whether or not a table is referenced by a foreign key. If you need to list out all foreign keys that reference a given table, see Return All Foreign Keys that Reference a Given Table in SQL Server. The examples in that article list out each foreign key, as well as the foreign key table/s, and the primary key table.
You can use the OBJECTPROPERTY()
function in SQL Server to see whether or not a table has a DEFAULT constraint.
To do this, pass the table’s object ID as the first argument, and TableHasDefaultCnst
as the second argument. The function returns a 1
or a 0
depending on whether or not it has a DEFAULT constraint.
A return value of 1
means that the table has a DEFAULT constraint, and a value of 0
means that it’s not.
You can use the OBJECTPROPERTY()
function in SQL Server to check whether an object is a user-defined table or not.
To do this, pass the object ID as the first argument, and IsUserTable
as the second argument. The function returns a 1
or a 0
depending on whether or not it’s a user-defined table.
A return value of 1
means that it is a user-defined table, and a value of 0
means that it’s not.
You can use the OBJECTPROPERTY()
function in SQL Server to check whether an object is a table-valued function or not.
To do this, pass the object ID as the first argument, and IsTableFunction
as the second argument. The function returns a 1
or a 0
depending on whether or not it’s a table-valued function.
A return value of 1
means that it is a table-valued function, and a value of 0
means that it’s not.
The OBJECTPROPERTY()
function in SQL Server enables you to check an object for a specific property.
You can use this function to check whether an object is a system table or not. To do this, pass the object ID as the first argument, and IsSystemTable
as the second argument. The function returns a 1
or a 0
depending on whether or not it’s a system table (1
means that it is a system table, and 0
means that it’s not).
You can use the OBJECTPROPERTY()
function in SQL Server to check whether or not an object is a primary key.
To find out whether an object is a primary key, pass the object ID as the first argument, and IsPrimaryKey
as the second argument. The function returns a 1
or a 0
depending on whether or not it’s a primary key.
A return value of 1
means that it is a primary key, and a value of 0
means that it’s not.
You can use the OBJECTPROPERTY()
function in SQL Server to find out whether or not an object is a foreign key.
To find out whether an object is a foreign key, pass the object ID as the first argument, and IsForeignKey
as the second argument. The function returns a 1
or a 0
depending on whether or not it’s a foreign key.
A return value of 1
means that it is a foreign key, and a value of 0
means that it isn’t.
You can use the OBJECTPROPERTY()
function in SQL Server to find out whether or not an object is a view.
This function accepts two parameters: the object ID, and the property for which you’re checking it for.
Therefore, you can pass the object ID as the first argument, and IsView
as the second, and the function will return either a 1
or a 0
depending on whether or not it’s a view.
A return value of 1
means that it is a view, and a value of 0
means that it’s not.
In SQL Server you can use the OBJECTPROPERTY()
function to find out whether or not an object is a stored procedure.
This function accepts two parameters: the object ID, and the property for which you’re checking it for.
Therefore, you can pass the object ID as the first argument, and IsProcedure
as the second, and the function will return either a 1
or a 0
depending on whether or not it’s a stored procedure.
A return value of 1
means that it is a stored procedure, and a value of 0
means that it’s not.