Does anyone know how I can see which are the primary & foreign keys in a table?
EDIT: Thanks for all the responses. I was looking for a SQL Query to do that. Right now I'm playing around with writing a tool which can list me all Tables of a DB and show the columns. I'd like to display also which of the keys are primary keys.
This is how I read out the Table Catalog:
const string sqlSelectTable = "SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE " +
"FROM INFORMATION_SCHEMA.TABLES " +
"WHERE TABLE_TYPE = 'BASE TABLE' " +
"ORDER BY TABLE_TYPE,TABLE_NAME";
And this is how I get the Infos about a Column:
const string sqlSelectTable =
"SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH " +
"FROM INFORMATION_SCHEMA.COLUMNS " +
"WHERE (TABLE_NAME = #TABLE_NAME) " +
"ORDER BY ORDINAL_POSITION";
Would I have to create a Inner-Join so see which of the Columns are Primary Key?
Cheers
For the primary key on each table, you can use this query:
SELECT
kc.name,
c.NAME
FROM
sys.key_constraints kc
INNER JOIN
sys.index_columns ic ON kc.parent_object_id = ic.object_id
INNER JOIN
sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
kc.type = 'PK'
and for the foreign key, I believe this query should get you the necessary information:
SELECT
OBJECT_NAME(parent_object_id) 'Parent table',
c.NAME 'Parent column name',
OBJECT_NAME(referenced_object_id) 'Referenced table',
cref.NAME 'Referenced column name'
FROM
sys.foreign_key_columns fkc
INNER JOIN
sys.columns c
ON fkc.parent_column_id = c.column_id
AND fkc.parent_object_id = c.object_id
INNER JOIN
sys.columns cref
ON fkc.referenced_column_id = cref.column_id
AND fkc.referenced_object_id = cref.object_id
Marc
see the Querying the SQL Server System Catalog FAQ, How do I find the columns of a primary key for a specified table? and How do I find the columns of a foreign key for a specified table?
EDIT: if you want to do it programmatically (which is not clear from your question), that is.
In Management Studio, expand the table and then expand the Columns item. The primary key(s) has a key icon next to them.
To see the foreign keys, expand the Constraints item.
You can start with:
SELECT
Table_Name as [TableName],
Column_Name as [ColumnName],
Constraint_Name as [Constraint],
Table_Schema as [Schema]
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
ORDER BY
[TableName],
[ColumnName]
(you can filter then by tableName)
SELECT
OBJECT_NAME(parent_object_id) 'Parent table',
c.NAME 'Parent column name',
OBJECT_NAME(referenced_object_id) 'Referenced table',
cref.NAME 'Referenced column name'
FROM
sys.foreign_key_columns fkc
INNER JOIN
sys.columns c
ON fkc.parent_column_id = c.column_id
AND fkc.parent_object_id = c.object_id
INNER JOIN
sys.columns cref
ON fkc.referenced_column_id = cref.column_id
AND fkc.referenced_object_id = cref.object_id where OBJECT_NAME(parent_object_id) = 'tablename'
If you want to get the foreign key relation of all the tables exclude the where clause else write your tablename instead of tablename
Related
I would like to drop a constraint on column in more than one database. I do not know exact name of the constraint on a column.
Is it possible to Drop a constraint without knowing its name.
Ex:
ALTER TABLE TempTable DROP CONSTRAINT IF EXISTS ON Columnname
you can retrieve constraint information from a table like this
select t.Name as TableName,
ccd.Name as ColumnName_default_constraint,
dc.Name as default_constraint,
ccc.Name as ColumnName_check_constraint,
cc.Name as check_constraint
from sys.tables t
inner join sys.default_constraints dc on t.object_id = dc.parent_object_id
inner join sys.check_constraints cc on t.object_id = cc.parent_object_id
inner join sys.columns ccd on dc.parent_object_id = ccd.object_id
and ccd.column_id = dc.parent_column_id
inner join sys.columns ccc on cc.parent_object_id = ccc.object_id
and ccc.column_id = cc.parent_column_id
where t.Name = 'your table name'
order by t.Name
from this you can build scripts like this
select 'alter table ' + t.Name +' drop constraint ' + dc.Name,
'alter table ' + t.Name +' drop constraint ' + cc.Name
from sys.tables t
inner join sys.default_constraints dc on t.object_id = dc.parent_object_id
inner join sys.check_constraints cc on t.object_id = cc.parent_object_id
where t.Name = 'your table name'
order by t.Name
How can I find all the tables of db that contains primary key or particular column of a single table in Postgresql Database....Means a column of a perticular table that is included in many tables either as foreign key or non - foreign key...column can be primary key or a non - primary key....
Use below Query to find particular column of a single table:
SELECT table_name
FROM information_schema.columns
WHERE table_schema = 'public'
AND column_name = 'YOUR_COLUMN_NAME'
Use below Query to find List of all the tables of db that contains primary key
SELECT t.table_catalog,
t.table_schema,
t.table_name,
kcu.constraint_name,
kcu.column_name,
kcu.ordinal_position
FROM INFORMATION_SCHEMA.TABLES t
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
ON tc.table_catalog = t.table_catalog
AND tc.table_schema = t.table_schema
AND tc.table_name = t.table_name
AND tc.constraint_type = 'PRIMARY KEY'
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON kcu.table_catalog = tc.table_catalog
AND kcu.table_schema = tc.table_schema
AND kcu.table_name = tc.table_name
AND kcu.constraint_name = tc.constraint_name
WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY t.table_catalog,
t.table_schema,
t.table_name,
kcu.constraint_name,
kcu.ordinal_position;
Use below Query to find List of table having Foreign Key in another Table
SELECT t.table_name as FK_Table, tc.constraint_name,tc.constraint_type,ccu.table_name as PK_Table
FROM INFORMATION_SCHEMA.TABLES t
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
ON tc.table_catalog = t.table_catalog
AND tc.table_schema = t.table_schema
AND tc.table_name = t.table_name
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
ON ccu.constraint_name = tc.constraint_name
/* Remove comments for check based on CONSTRAINT'name OR 'PRIMARY_KEY' name */
--and ccu.constraint_name = 'CONSTRAINT_NAME'
--and AND ccu.column_name = 'COLUMN_NAME'
WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
I try to get info of all column in a table
Example info about type, name column, especially info about primary key but it missing
I use
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='tablename'
It's not contain info about primary key. How to get that thanks
How about something like
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND table_name = 'TableName'
Also maybe try
SELECT i.name AS IndexName,
OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName,
c.*
FROM sys.indexes AS i INNER JOIN
sys.index_columns AS ic ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id INNER JOIN
sys.columns c ON ic.object_id = c.object_id
AND ic.column_id = c.column_id
WHERE i.is_primary_key = 1
try with sp_help [tablename] it will give you primary key details
sp_help [tablename]
sp_help
Or if you need only details about primary key column try with Mr.Astander answer
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND table_name = 'TableName'
You could retrieve primary key information and join it to INFORMATION_SCHEMA.COLUMNS and get primary key information with other columns. Please see the below query.
SELECT c.*,
CASE WHEN keys.COLUMN_NAME IS NULL
THEN 0
ELSE 1
END AS is_primary
FROM
INFORMATION_SCHEMA.COLUMNS c
LEFT OUTER JOIN (
SELECT col.COLUMN_NAME,col.TABLE_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tab
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
ON Col.Constraint_Name = tab.Constraint_Name
AND Col.Table_Name = tab.Table_Name
WHERE Constraint_Type = 'PRIMARY KEY') keys
ON c.COLUMN_NAME=keys.COLUMN_NAME AND c.TABLE_NAME=keys.TABLE_NAME
ORDER BY c.TABLE_NAME
Try:
SELECT TC.TABLE_NAME, CU.CONSTRAINT_NAME, C.DATA_TYPE
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU
ON TC.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON CU.TABLE_NAME = C.TABLE_NAME AND CU.TABLE_SCHEMA = C.TABLE_SCHEMA AND C.COLUMN_NAME = CU.COLUMN_NAME
WHERE TC.TABLE_NAME = 'tablename' AND CONSTRAINT_TYPE = 'PRIMARY KEY'
I want to find all the matching list of tables where the columnname = 'Letter'?
Thanks
SELECT TABLE_SCHEMA,
TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'Letter'
select TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'Letter'
select
type_desc,
sch.name as 'schema name',
obj.name as 'table',
col.name as columnname,
ty.name as columntype,
col.max_length as columnmaxlength,
is_identity
from sys.objects obj
inner join sys.all_columns col
on obj.object_id = col.object_id
inner join sys.schemas sch
on obj.schema_id = sch.schema_id
inner join sys.types ty
on col.system_type_id = ty.system_type_id
where type ='U'
and col.name like '%letter%'
order by type_desc,sch.name, obj.name, col.name
Above code will get you TABLES as well as VIEWS
To rule out VIEWS
SELECT C.TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS C,INFORMATION_SCHEMA.TABLES T
WHERE C.TABLE_NAME=T.TABLE_NAME AND C.COLUMN_NAME = 'YOUR_COLUMN_NAME'
AND T.TABLE_TYPE='BASE TABLE'
Is this the best way to - Get a List of all Primary Keys in a Database - or is there something better?
SELECT
KCU.TABLE_NAME AS Table_Name,
KCU.CONSTRAINT_NAME AS Constraint_Name,
KCU.COLUMN_NAME AS COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA
AND KCU.TABLE_NAME = TC.TABLE_NAME
WHERE
TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY
KCU.TABLE_SCHEMA, KCU.TABLE_NAME, KCU.CONSTRAINT_NAME
USE databasename;
GO
SELECT i.name AS IndexName, OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
INNER JOIN sys.index_columns AS ic
ON i.OBJECT_ID = ic.OBJECT_ID
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1
This query will extract the all primary key constraints from the database...
u just need to execute this query and type the database name in first line
The following syntax give you all constraints in database in use.
select * from sys.key_constraints;
If you want the data type information as well:
SELECT
so.name 'Table Name',
c.name 'Column Name',
t.Name 'Data type',
c.max_length 'Max Length',
c.precision ,
c.scale ,
c.is_nullable,
ISNULL(i.is_primary_key, 0) 'Primary Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
INNER JOIN
sysobjects so ON c.object_id = so.id
WHERE
i.is_primary_key = 1 and
so.xtype = 'U'
Order By 'Table Name', 'Column Name'
look on link
EXEC sp_pkeys '<tablename>'
EXEC sp_helpconstraint '<tablename>'
sp_pkeys will return a row for each
column that participates in the
primary key for . The
columns you are likely most interested
in are COLUMN_NAME and PK_NAME.
sp_helpconstraint will list all
constraints for , including
foreign keys that reference the table.
In the first recordset, there will
only be a column called Object Name
(kind of useless, since that's what
you passed in). In the second
resultset, there will be the following
columns: constraint_type,
constraint_name, and constraint_keys.
If you are doing this from java you can also use the getPrimaryKeys method in the databasemetadata object. Perhaps other languages have similar ways to do it.