I am looking for a query to get the PrimaryKey Name, primary key column
for a particular table.
The follwing query will give me the Key Name and the Table Name but not
the column Name. Can I get the column name also in this query.
select A.ID,A.Name as PrimaryKey, B.Name as MasterTable from sysobjects
A INNER JOIN sysobjects B
on A.Parent_obj= B.ID
where A.xType = 'PK'
Thanks in AdvanceWhat about
SELECT * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE
HTH, Jens Suessmeyer.|||SQL novice
SELECT rc.constraint_name,
ccu_prim.TABLE_NAME AS prim_table,
ccu_prim.COLUMN_NAME AS prim_column,
ccu_for.TABLE_NAME AS for_table,
ccu_for.COLUMN_NAME AS for_column
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu_prim
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
ON ccu_prim.CONSTRAINT_CATALOG = rc.UNIQUE_CONSTRAINT_CATALOG
AND ccu_prim.CONSTRAINT_SCHEMA = rc.UNIQUE_CONSTRAINT_SCHEMA
AND ccu_prim.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu_for
ON ccu_for.CONSTRAINT_CATALOG = rc.CONSTRAINT_CATALOG
AND ccu_for.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
AND ccu_for.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
ORDER BY rc.constraint_name, for_table, prim_column
"SQL novice" <balacr@.gmail.com> wrote in message
news:1132221814.286212.141430@.g14g2000cwa.googlegroups.com...
>I am looking for a query to get the PrimaryKey Name, primary key column
> for a particular table.
> The follwing query will give me the Key Name and the Table Name but not
> the column Name. Can I get the column name also in this query.
> select A.ID,A.Name as PrimaryKey, B.Name as MasterTable from sysobjects
> A INNER JOIN sysobjects B
> on A.Parent_obj= B.ID
> where A.xType = 'PK'
>
> Thanks in Advance
>|||you could use 'sp_help <nametable>' which will inform you about that.
"SQL novice" wrote:
> I am looking for a query to get the PrimaryKey Name, primary key column
> for a particular table.
> The follwing query will give me the Key Name and the Table Name but not
> the column Name. Can I get the column name also in this query.
> select A.ID,A.Name as PrimaryKey, B.Name as MasterTable from sysobjects
> A INNER JOIN sysobjects B
> on A.Parent_obj= B.ID
> where A.xType = 'PK'
>
> Thanks in Advance
>|||Thanks Jens and Uri,
This was very helpful.
No comments:
Post a Comment