Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 getting the primary key

Author  Topic 

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-16 : 11:58:24
Hello , how you all doing?


1. Im using the INFORMATION_SCHEMA stuff.
2. I want to figure out which column of a table is the primary key.
3. How do i do that?

SELECT * FROM information_schema.columns doesnt tell med which one is the primary key..

any help would be much much appreciated..

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-06-16 : 12:47:30
one way using info_schema views:


select column_name as 'primary_key'
from information_schema.table_constraints pk
inner join information_schema.key_column_usage c on c.table_name = pk.table_name
and c.constraint_name = pk.constraint_name
where pk.table_name = <your_table_name>
and constraint_type = 'primary key'


or using object property:

select column_name --, constraint_name
from information_schema.constraint_column_usage
where objectproperty(object_id(constraint_name),'isprimarykey') = 1
and table_name = <your_table_name>
Go to Top of Page

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-16 : 14:45:30
Aah, thanks dude.

Go to Top of Page

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-16 : 17:51:00
Hello again.

This was really hard to do *pheew*

But this is what i have come up with so far:
SELECT
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
,INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
,INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
,INFORMATION_SCHEMA.COLUMNS.COLUMN_DEFAULT
,INFORMATION_SCHEMA.COLUMNS.IS_NULLABLE
,INFORMATION_SCHEMA.COLUMNS.DATA_TYPE
,INFORMATION_SCHEMA.COLUMNS.CHARACTER_MAXIMUM_LENGTH
,INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.COLUMNS

LEFT OUTER JOIN information_schema.KEY_COLUMN_USAGE
ON
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME=information_schema.KEY_COLUMN_USAGE.TABLE_NAME
AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME=information_schema.KEY_COLUMN_USAGE.COLUMN_NAME
AND INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION=information_schema.KEY_COLUMN_USAGE.ORDINAL_POSITION

LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS
ON
information_schema.KEY_COLUMN_USAGE.TABLE_NAME=information_schema.TABLE_CONSTRAINTS.TABLE_NAME
AND information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME=information_schema.TABLE_CONSTRAINTS.CONSTRAINT_NAME


--WHERE information_schema.COLUMNS.TABLE_NAME='orders'
ORDER BY
INFORMATION_schema.COLUMNS.TABLE_NAME
,information_schema.COLUMNS.ORDINAL_POSITION


Will display this result:

 
TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CONSTRAINT_TYPE
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ---------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ------------------------ ---------------
customers customersID 1 NULL NO int NULL PRIMARY KEY
customers customername 2 NULL NO varchar 50 NULL
orderdetails orderdetailsID 1 NULL NO int NULL PRIMARY KEY
orderdetails ordersID 2 NULL YES int NULL NULL
orderdetails productsID 3 NULL YES int NULL NULL
orders ordersID 1 NULL NO int NULL PRIMARY KEY
orders customersID 2 NULL YES int NULL NULL
orders ordername 3 NULL NO varchar 50 NULL
orders orderdate 4 NULL NO smalldatetime NULL NULL
products productsID 1 NULL NO int NULL PRIMARY KEY
products productname 2 NULL NO varchar 50 NULL
sysdiagrams name 1 NULL NO nvarchar 128 NULL
sysdiagrams principal_id 2 NULL NO int NULL NULL
sysdiagrams diagram_id 3 NULL NO int NULL NULL
sysdiagrams version 4 NULL YES int NULL NULL
sysdiagrams


But as you can see in the "CONSTRAINT_TYPE" column, "UNIQUE" AND "FOREIGN KEY" is missing , only the "PRIMARY KEY" constraint is showing..i want to include every type of constraint ..
some more help would be extremely appreciated
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-06-16 : 18:13:58
Review this join criteria:

AND INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION=information_schema.KEY_COLUMN_USAGE.ORDINAL_POSITION


Run this SELECT and youll see what Im getting at:


select 	constraint_name, 
ordinal_position
from information_schema.KEY_COLUMN_USAGE
where table_name = 'orders'


Go to Top of Page

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-16 : 18:19:26
hmm so i cant include the ordinal_position?. dont sure what you mean. but i included that one just in case so the SQL-server wont mix up columns with other columns..
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-06-16 : 18:28:37
You are joining on table_name and column_name already, which is unique. You cannot have duplicate column_name within the same table.

The ORDINAL_POSITION in the KEY_COLUMN_USAGE table is referring to the column on which the constraint resides.

So yea, just remove that from the join
Go to Top of Page

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-16 : 18:39:12
Ahh, I see

i removed the "ORDINAL_POSITION" and it worked.. thanks again dude!
Go to Top of Page

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-17 : 07:12:52
Hello.. me again

Im now trying to extend my information_schema so it also includes which tablecolumns the FOREIGN KEY references to..

SELECT
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
,INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
,INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
,INFORMATION_SCHEMA.COLUMNS.COLUMN_DEFAULT
,INFORMATION_SCHEMA.COLUMNS.IS_NULLABLE
,INFORMATION_SCHEMA.COLUMNS.DATA_TYPE
,INFORMATION_SCHEMA.COLUMNS.CHARACTER_MAXIMUM_LENGTH
,INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE
,INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME
,information_schema.REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME
,information_schema.REFERENTIAL_CONSTRAINTS.MATCH_OPTION
,information_schema.REFERENTIAL_CONSTRAINTS.UPDATE_RULE
,information_schema.REFERENTIAL_CONSTRAINTS.DELETE_RULE

FROM INFORMATION_SCHEMA.COLUMNS

LEFT OUTER JOIN information_schema.KEY_COLUMN_USAGE
ON
INFORMATION_SCHEMA.COLUMNS.TABLE_NAME=information_schema.KEY_COLUMN_USAGE.TABLE_NAME
AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME=information_schema.KEY_COLUMN_USAGE.COLUMN_NAME
--AND INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION=information_schema.KEY_COLUMN_USAGE.ORDINAL_POSITION

LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS
ON
information_schema.KEY_COLUMN_USAGE.TABLE_NAME=information_schema.TABLE_CONSTRAINTS.TABLE_NAME
AND information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME=information_schema.TABLE_CONSTRAINTS.CONSTRAINT_NAME

LEFT OUTER JOIN information_schema.REFERENTIAL_CONSTRAINTS
ON
information_schema.REFERENTIAL_CONSTRAINTS.CONSTRAINT_NAME=information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME
--information_schema.REFERENTIAL_CONSTRAINTS.UNIQUE_CONSTRAINT_NAME=information_schema.KEY_COLUMN_USAGE.CONSTRAINT_NAME


WHERE information_schema.COLUMNS.TABLE_NAME='orders'
ORDER BY information_schema.COLUMNS.ORDINAL_POSITION


Givs me this output:

TABLE_NAME                                                                                                                       COLUMN_NAME                                                                                                                      ORDINAL_POSITION COLUMN_DEFAULT                                                                                                                                                                                                                                                   IS_NULLABLE DATA_TYPE                                                                                                                        CHARACTER_MAXIMUM_LENGTH CONSTRAINT_TYPE CONSTRAINT_NAME                                                                                                                  UNIQUE_CONSTRAINT_NAME                                                                                                           MATCH_OPTION UPDATE_RULE DELETE_RULE
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ---------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ------------------------ --------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------------ ----------- -----------
orders ordersID 1 NULL NO int NULL PRIMARY KEY PK__orders__7E6CC920 NULL NULL NULL NULL
orders customersID 2 NULL YES int NULL FOREIGN KEY FK__orders__customer__7F60ED59 PK__customers__7C8480AE SIMPLE NO ACTION CASCADE
orders ordername 3 NULL NO varchar 50 NULL NULL NULL NULL NULL NULL
orders orderdate 4 NULL NO smalldatetime NULL NULL NULL NULL NULL NULL NULL



I would like to include which table,columns the FOREIGN KEY references to.. but, ah man its tricky

like this:


TABLE_NAME COLUMN_NAME ORDINAL_POSITION COLUMN_DEFAULT IS_NULLABLE DATA_TYPE CHARACTER_MAXIMUM_LENGTH CONSTRAINT_TYPE CONSTRAINT_NAME UNIQUE_CONSTRAINT_NAME MATCH_OPTION UPDATE_RULE DELETE_RULE FOREIGN_KEY_REFERENCES_TABLE_NAME FOREIGN_KEY_REFERENCES_COLUMN_NAME
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ---------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- -------------------------------------------------------------------------------------------------------------------------------- ------------------------ --------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------------ ----------- ----------- --------------------------------- ----------------------------------
orders ordersID 1 NULL NO int NULL PRIMARY KEY PK__orders__7E6CC920 NULL NULL NULL NULL NULL NULL
orders customersID 2 NULL YES int NULL FOREIGN KEY FK__orders__customer__7F60ED59 PK__customers__7C8480AE SIMPLE NO ACTION CASCADE NULL NULL
orders ordername 3 NULL NO varchar 50 NULL NULL NULL NULL NULL NULL NULL NULL
orders orderdate 4 NULL NO smalldatetime NULL NULL NULL NULL NULL NULL NULL NULL NULL

Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-06-17 : 11:24:29
Join again to key_column_refernces and table_constraints with modified join criteria to get the foreign specific info.

And introducing, the "Table Alias"



select	 c.table_name
,c.column_name
,c.ordinal_position
,c.column_default
,c.is_nullable
,c.data_type
,c.character_maximum_length
,tc.constraint_type
,tc.constraint_name
,rc.unique_constraint_name
,rc.match_option
,rc.update_rule
,rc.delete_rule
,tc_fk.table_name 'fk_table'
,kcu_fk.column_name 'fk_column'
from information_schema.columns c
left outer join information_schema.key_column_usage kcu
on c.table_name = kcu.table_name
and c.column_name = kcu.column_name
left outer join information_schema.table_constraints tc
on kcu.table_name = tc.table_name
and kcu.constraint_name = tc.constraint_name
left outer join information_schema.referential_constraints rc
on rc.constraint_name = kcu.constraint_name

left join information_schema.key_column_usage kcu_fk
on rc.constraint_name = kcu_fk.constraint_name
left join information_schema.table_constraints tc_fk
on rc.unique_constraint_name = tc_fk.constraint_name


where c.table_name = 'orders'
order by c.ordinal_position
Go to Top of Page

natas
Yak Posting Veteran

51 Posts

Posted - 2005-06-17 : 12:36:57
Aahh, perfect , thanks!, your far too kind.
Go to Top of Page
   

- Advertisement -