How to drop a primary key in microsoft sql server
Upcoming SlideShare
Loading in...5
×
 

How to drop a primary key in microsoft sql server

on

  • 656 views

name of constraints and indexes that you’re dropping. This isn’t a problem when ...

name of constraints and indexes that you’re dropping. This isn’t a problem when
you’re working with the management studio, because it looks at the database
metadata and generates code like
ALTER TABLE [dbo].[someTable] DROP CONSTRAINT
[PK__someTabl__3214EC07271AA44F]
It’s less convenient, however, when you’re writing a set of migration scripts in SQL to
implement changes that you make over the database in time. Specifically, if you
create the table twice in two different databases, the hexadecimal string in the name
of the key will be different — the ALTER TABLE statement will fail when you try to
drop the index later, since the name of the key won’t match.
Here’s a stored procedure that looks up the name of the primary key

Statistics

Views

Total Views
656
Views on SlideShare
656
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

How to drop a primary key in microsoft sql server How to drop a primary key in microsoft sql server Document Transcript

  • Generation 5 » How to Drop A Primary Key in Microsoft SQL Server  Subscribe to our RSS Feed | About Us How to Drop A Primary Key in Microsoft SQL Server Search for: One of my pet peeves with Microsoft’s SQL Server is that it requires you to know the name of constraints and indexes that you’re dropping. This isn’t a problem when you’re working with the management studio, because it looks at the database metadata and generates code like ALTER TABLE [dbo].[someTable] DROP CONSTRAINT [PK__someTabl__3214EC07271AA44F] It’s less convenient, however, when you’re writing a set of migration scripts in SQL to implement changes that you make over the database in time. Specifically, if you create the table twice in two different databases, the hexadecimal string in the name of the key will be different — the ALTER TABLE statement will fail when you try to drop the index later, since the name of the key won’t match. Here’s a stored procedure that looks up the name of the primary key in the system catalog and uses dynamic SQL to drop the index: CREATE PROCEDURE [dbo].[DropPrimaryKey] @tableName Varchar(255) AS /* Drop the primary key on @TableName http://gen5.info/q/ Version 1.1 June 9, 2008 */ Archives June 2012 (1) August 2010 (1) May 2010 (1) June 2009 (2) April 2009 (1) March 2009 (1) February 2009 (3) January 2009 (3) November 2008 (1) August 2008 (2) July 2008 (5) June 2008 (5) May 2008 (2) April 2008 (6) March 2008 (8) June 2006 (1) February 2006 (1) Categories DECLARE @pkName Varchar(255) SET @pkName= ( SELECT [name] FROM sysobjects WHERE [xtype] = 'PK' AND [parent_obj] = OBJECT_ID(N'[dbo].['+@tableName+N']') ) DECLARE @dropSql varchar(4000) SET @dropSql= 'ALTER TABLE [dbo].['+@tableName+'] DROP CONSTRAINT ['+@PkName+']' EXEC(@dropSql) GO Once you've loaded this stored procedure, Search you can write EXEC [dbo].[DropPrimaryKey] @TableName='someTable' It’s just that simple. Similar stored procedures can be written to convert fields from NOT NULL to NULL and do other operation which required a named constraint. Paul Houle on June 6th 2008 in SQL Comments (2) Comments (2) Login Sort by: Date Rating Last Activity Adrian Banks · 292 weeks ago +1 A better query to find the primary key for the table would be: SELECT [name] FROM sysobjects WHERE [xtype] = 'PK' AND [parent_obj] = OBJECT_ID(N'[dbo].[' + @tableName + ']') This doesn't rely on a naming convention of primary keys' names starting with PK, but instead uses SQL Server's own data on whether the index is a primary key. Reply http://gen5.info/q/2008/06/06/how-to-drop-a-primary-key-in-microsoft-sql-server/[1/16/2014 3:53:20 PM] AJAX (2) Asynchronous Communications (16) Biology (1) Books (1) Design (1) Distributed (1) Exceptions (2) Functional Programming (1) GIS (1) Ithaca (1) Japan (1) Math (1) Media (3) Nature (1) Semantic Web (3) Tools (28) CRUD (1) Dot Net (17) Freebase (2) GWT (9) Java (7) Linq (2) PHP (6) Server Frameworks (1) Silverlight (12) SQL (5) Uncategorized (1) Web (2) Analytics (1)
  • Generation 5 » How to Drop A Primary Key in Microsoft SQL Server admin · 292 weeks ago -1 Thanks Adrian! I tested this and then updated the script. It's nice to see what a wealth of information is in the system catalog tables. Reply Post a new comment Enter text right here! Comment as a Guest, or login: Name Email Website (optional) Displayed next to your comments. Not displayed publicly. If you have a website, link to it here. None Subscribe to None Submit Comment Copyright © 2013 Generation 5. WordPress Theme design. http://gen5.info/q/2008/06/06/how-to-drop-a-primary-key-in-microsoft-sql-server/[1/16/2014 3:53:20 PM]