March 23, 2008 at 8:25 am
Comments posted to this topic are about the item SQL 2005 - List Primary Key Columns
January 4, 2010 at 5:31 pm
Nice idea and this is a nice foundation script.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
June 29, 2012 at 7:08 am
I like the ides of this, I would add ordering the columns as they are defined in the key. And for the programmers in my area I would add a statement in the where clause so they only got the table they need instead of every table on the database.
SELECT p.TABLE_NAME, c.CONSTRAINT_NAME, c.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS p
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE c on
c.TABLE_NAME = p.TABLE_NAME
AND c.CONSTRAINT_NAME = p.CONSTRAINT_NAME
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND p.TABLE_NAME = 'name_here'
ORDER by c.TABLE_NAME, c.ordinal_position
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 29, 2012 at 8:25 am
Here is a query to find all the indexes on a table.
select r.[name] as table_name, i.[name] as index_name, c.[name] as column_name
FROM sys.index_columns l
inner join sys.tables r on
l.object_id = r.object_id
inner join sys.indexes i on
l.object_id = i.object_id
and l.index_id = i.index_id
inner join sys.columns c on
l.object_id = c.object_id
and l.column_id = c.column_id
where r.[name] = 'table_name_here'
order by r.[name], l.index_id, l.key_ordinal
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 29, 2012 at 8:36 am
Nice idea. I tested it on 2000, 2005, and 2008. It works well on all of them.
June 29, 2012 at 10:03 am
I ran this and got an empty results set. Is there something that needs to be done to populate these schema views? This is a totally new subject for me. I was under the impression that these views would be already defined in the system and can just be queried as in this article.
Dana
June 29, 2012 at 10:11 am
It could be either your table does not have an index set up, or the database doesn't have any tables that have indexes.
You need to be connected to the database where the table exists for either of these to work.
I don't think anything needs to be done for this to work, but then I'm not a DBA.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 29, 2012 at 12:28 pm
I have plenty of tables, I've been using it for a while. And there are indexes. And stored procedures and functions. But still, the script returns an empty results set. It seems that something must be done to populate these views. I'm no DBA either, except by necessity. The database is on my shared hosting, so maybe there are some constraints there. I know there are other things that I do not have access to. For example, there have been scripts that access certain system stored procedures and are meant to help with optimizing and finding where things could be sped up. They don't work, either. But in those cases I get an error message that states that I don't have privileges to those procedures. This doesn't complain, it just returns and empty set. *shrug*
Dana
June 29, 2012 at 12:41 pm
I talked to my DBA and he said these are automatically updated. So it must be a permissions issue that is stopping you from seeing anything.
-------------------------------------------------------------
we travel not to escape life but for life not to escape us
Don't fear failure, fear regret.
June 29, 2012 at 1:01 pm
belowery (6/29/2012)
I talked to my DBA and he said these are automatically updated. So it must be a permissions issue that is stopping you from seeing anything.
Thank you. At least I know. I suppose I can get along without them. I have thus far. 😉
Dana
May 5, 2015 at 2:36 am
Hi Sandeep,
Many thanks for this script. I have just the spent the last hour deep in BOL, sys.objects, sys.key_constraints and friends, trying to identify which tables in the DB have composite primary keys.
Kind regards,
Sean Redmond
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply