Wednesday, March 21, 2012

primary key fields in the database

Hello,
i need to query a database and get the list of all primary keys wrt all
the user tables in the database. I dont know as to what property field
of syscolumns mark a field as a primary key.
How do i know that the field in the table is a primary key '
thanksHere you go.
select c.column_name, object_name(o.parent_obj) as tableName
from information_schema.constraint_column_usage c
inner join sysobjects o on o.name = c.constraint_name
where o.xtype = 'PK'
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com
"ch8an" <chethan.shetty@.gmail.com> wrote in message
news:1124874929.246497.117660@.o13g2000cwo.googlegroups.com...
> Hello,
> i need to query a database and get the list of all primary keys wrt all
> the user tables in the database. I dont know as to what property field
> of syscolumns mark a field as a primary key.
> How do i know that the field in the table is a primary key '
> thanks
>|||A couple of partial solutions:
1)sp_help <table>
2)generating full script and then to identify PK and its associates fields
3)
select * from sysobjects inner join sysobjects s1
on sysobjects.id = s1.parent_obj
where s1.xtype = 'PK'
"ch8an" wrote:

> Hello,
> i need to query a database and get the list of all primary keys wrt all
> the user tables in the database. I dont know as to what property field
> of syscolumns mark a field as a primary key.
> How do i know that the field in the table is a primary key '
> thanks
>|||http://www.aspfaq.com/search.asp?q=schema%3A
http://www.aspfaq.com/2104
"ch8an" <chethan.shetty@.gmail.com> wrote in message
news:1124874929.246497.117660@.o13g2000cwo.googlegroups.com...
> Hello,
> i need to query a database and get the list of all primary keys wrt all
> the user tables in the database. I dont know as to what property field
> of syscolumns mark a field as a primary key.
> How do i know that the field in the table is a primary key '
> thanks
>

No comments:

Post a Comment