How to Get Primary Key of Table

SQL Server: Get table primary key using sql query

Found another one:

SELECT 
KU.table_name as TABLENAME
,column_name as PRIMARYKEYCOLUMN
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC

INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
AND KU.table_name='YourTableName'

ORDER BY
KU.TABLE_NAME
,KU.ORDINAL_POSITION
;

I have tested this on SQL Server 2003/2005

SQL query to find Primary Key of a table?

For Oracle, you can look it up in the ALL_CONSTRAINTS table:

SELECT a.COLUMN_NAME
FROM all_cons_columns a INNER JOIN all_constraints c
ON a.constraint_name = c.constraint_name
WHERE c.table_name = 'TBL'
AND c.constraint_type = 'P';

DEMO.

For SQL Server, it was already answered here, and for MySQL check @ajon's answer.

MySQL - How to get primary key of a table?

Try this:

Solution 1:

This is from Ajons Answer

SELECT k.COLUMN_NAME
FROM information_schema.table_constraints t
LEFT JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
AND t.table_schema=DATABASE()
AND t.table_name='owalog';

Solution 2:

This is from alexn

SHOW KEYS FROM tablename WHERE Key_name = 'PRIMARY'

Solution 3:

SHOW INDEX FROM presort.final_conf_score_mld_run2 
WHERE Key_name = 'PRIMARY';

How to get primary key of table?

A better way is to use SHOW KEYS since you don't always have access to information_schema. The following works:

SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'

Column_name will contain the name of the primary key.

How to get primary key column in Oracle?

SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'TABLE_NAME'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;

Make sure that 'TABLE_NAME' is in upper case since Oracle stores table names in upper case.

How to get the primary key column name of a specific table in MySQL

You can get the details from information_schema database. Use the following query:

SELECT COLUMN_NAME 
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_NAME = 'Your_table_name'
AND CONSTRAINT_NAME = 'PRIMARY'

How do you list the primary key of a SQL Server table?

SELECT Col.Column_Name from 
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type = 'PRIMARY KEY'
AND Col.Table_Name = '<your table name>'

How to get the primary keys column of a table in SQLite

The function that you should use is pragma_table_info():

SELECT * 
FROM pragma_table_info('Evenements')

You will get info about all the columns of the table Evenements.

If you want info only for the primary key add a WHERE clause:

WHERE pk 

Show the primary key of a table using SQL+ in Oracle?

You can show the prymary key of a specific table with the following query:

SELECT constraint_name
FROM user_constraints
WHERE table_name = 'TABLE_NAME'
AND constraint_type = 'P'

How to get PRIMARY KEY column name of a table

this should be your query. You are missing single quotes on your table name. Tested and works fine.

string sql = "SELECT ColumnName = col.column_name 
FROM information_schema.table_constraints tc
INNER JOIN information_schema.key_column_usage col
ON col.Constraint_Name = tc.Constraint_Name
AND col.Constraint_schema = tc.Constraint_schema
WHERE tc.Constraint_Type = 'Primary Key' AND col.Table_name = '" + _lstview_item + "'";


Related Topics



Leave a reply



Submit