SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL 2005 - List Primary Key Columns


SQL 2005 - List Primary Key Columns

Author
Message
Sandeep Saretha
Sandeep Saretha
SSC-Addicted
SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)SSC-Addicted (411 reputation)

Group: General Forum Members
Points: 411 Visits: 389
Comments posted to this topic are about the item SQL 2005 - List Primary Key Columns
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)SSC Guru (100K reputation)

Group: General Forum Members
Points: 100166 Visits: 18616
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

below86
below86
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2579 Visits: 2982
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
below86
below86
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2579 Visits: 2982
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
Dave Vroman
Dave Vroman
Old Hand
Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)

Group: General Forum Members
Points: 393 Visits: 727
Nice idea. I tested it on 2000, 2005, and 2008. It works well on all of them.
danaanderson
danaanderson
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 195
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
below86
below86
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2579 Visits: 2982
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
danaanderson
danaanderson
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 195
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
below86
below86
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2579 Visits: 2982
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
danaanderson
danaanderson
Old Hand
Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)Old Hand (306 reputation)

Group: General Forum Members
Points: 306 Visits: 195
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search