Difference between revisions of "How can you determine the primary key using SQL?"
From SQLZOO
(Created page with "How can you determine the primary key using SQL? <div class='ht'> <div> If you have access to the SQL code which created the table the primary key can be seen easily. The prim...") |
|||
| (One intermediate revision by one user not shown) | |||
| Line 1: | Line 1: | ||
How can you determine the primary key using SQL? | How can you determine the primary key using SQL? | ||
<div class='ht'> | <div class='ht'> | ||
| + | <div class=params>schema:gisq</div> | ||
<div> | <div> | ||
If you have access to the SQL code which created the table the primary key can be seen easily. The primary key may be specified in one of two ways: | If you have access to the SQL code which created the table the primary key can be seen easily. The primary key may be specified in one of two ways: | ||
Latest revision as of 14:45, 12 July 2012
How can you determine the primary key using SQL?
schema:gisq
If you have access to the SQL code which created the table the primary key can be seen easily. The primary key may be specified in one of two ways:
CREATE TABLE cia (name VARCHAR(10) PRIMARY KEY,
population INTEGER)
or, where the primary key is composite:
CREATE TABLE casting(movieid INTEGER,
actorid INTEGER,
PRIMARY KEY (movieid, actorid)
)
If this is not possible then implementation specific commands may work.
SELECT conkey FROM pg_constraint JOIN pg_class ON pg_class.oid=conrelid WHERE contype='p' AND relname = 'casting'
SELECT a.owner, a.TABLE_NAME, b.column_name FROM all_constraints a, all_cons_columns b WHERE a.constraint_type='P' AND a.constraint_name=b.constraint_name AND a.TABLE_NAME = 'CASTING'
sp_pkeys @TABLE_NAME=casting
DESCRIBE castingHow can you determine the primary key using SQL?