Difference between revisions of "How can you determine the primary key using SQL?"

From SQLZOO
Jump to: navigation, search
(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...")
 
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:scott</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:

Revision as of 10:16, 12 July 2012

How can you determine the primary key using SQL?

schema:scott

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 casting
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense