Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how get primary key in any database


how get primary key in any database

Author
Message
Furanshizuku
Furanshizuku
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 994
Comments posted to this topic are about the item how get primary key in any database
toniupstny
toniupstny
SSC Veteran
SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)

Group: General Forum Members
Points: 248 Visits: 940
This information can be gotten despite how the constraint is named this way:


SELECT Table_name, constraint_name
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE constraint_type = 'PRIMARY KEY'



then you can use the table_name and constraint_name to do what you wish.

Toni
toniupstny
toniupstny
SSC Veteran
SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)SSC Veteran (248 reputation)

Group: General Forum Members
Points: 248 Visits: 940
To be clearer, as you can name the primary key anything you want the statements from the article would miss any that do not begin with 'PK'.

declare @name_pk varchar(255)

select @name_pk = name from sysindexes where id in (select id from sysobjects where name = 'table_name') and name like 'PK%'

execute ('alter table table_name drop constraint ' + @name_pk)


So to get the name of the Primary Key Constraint without this restriction, using the same 'table_name' substitution:

declare @name_pk varchar(255)

SELECT @name_pk = constraint_name
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE constraint_type = 'PRIMARY KEY' and Table_name = 'table_name'

Print 'pk ' + @name_pk -- show the results

/* or use the execute as in the original */
-- execute ('alter table table_name drop constraint ' + @name_pk)


Toni
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