Search code examples
t-sqlsql-server-2000constraintsinformation-schema

Removing All Primary Keys


This is going to sound like a crazy request. The databases that I report from do not have any foreign keys, and every single primary key is an identity_column. This makes using tools such as TOAD difficult because the Intellisense works by reading the PK and FK relationships.

Anyone have a script to remove the primary keys from every table in the database so I can replace them with "correct" PK and add FK’s to assist in reporting?

To head off the avalanch of "Don't do it!!!" responses, let me make it clear that I am not going to do this to my production database, but copy of it on another server.

Any advice would be appreciated.

------- EDIT This is updated with correct information. ----------------

Thanks guys, but I realized I've made a mistake. Almost every single table has an "identity_column" with the property of identity. That identity is a clustered index. However, it is not designated as a primary key.

First, what is the difference between a primary key and a clustered index?

Second, how can I script out all the clustered indexes? Would this work?

SELECT 
  'ALTER TABLE ' + OBJECT_NAME(OBJECT_ID) + ' DROP CONSTRAINT ' + name 
FROM sys.indexes WHERE type_desc = 'CLUSTERED'

Thanks for your patience


Solution

  • How about something like this?

    -- Helper Procedure 
    CREATE PROC #DropConstraints 
      @tableSchema nvarchar(max), 
      @tableName nvarchar(max), 
      @constraintType nvarchar(20) 
    AS 
    BEGIN 
      DECLARE @cName nvarchar(max); 
    
      DECLARE constraint_cursor CURSOR FOR 
        SELECT CONSTRAINT_NAME  
        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
        WHERE  
          CONSTRAINT_TYPE = @constraintType 
          AND TABLE_NAME = @tableName 
          AND TABLE_SCHEMA = @tableSchema 
    
      OPEN constraint_cursor 
    
      FETCH NEXT FROM constraint_cursor INTO @cName 
      WHILE @@FETCH_STATUS = 0 
      BEGIN 
        EXEC ('ALTER TABLE ' + @tableSchema + '.' + @tableName + ' DROP CONSTRAINT ' + @cName); 
        FETCH NEXT FROM constraint_cursor INTO @cName 
      END 
    
      CLOSE constraint_cursor 
      DEALLOCATE constraint_cursor 
    END 
    GO
    
    BEGIN TRANSACTION
    
      -- Setup Cursor for looping 
      DECLARE table_cursor SCROLL CURSOR FOR 
        SELECT TABLE_SCHEMA, TABLE_NAME  
        FROM INFORMATION_SCHEMA.TABLES 
    
      OPEN table_cursor
    
      -- Declare Variables
    
      DECLARE 
        @tableSchema nvarchar(max), 
        @tableName nvarchar(max) 
    
     -- Drop Primary Keys 
    
      FETCH FIRST FROM table_cursor INTO @tableSchema, @tableName 
      WHILE @@FETCH_STATUS = 0 
      BEGIN 
        EXEC #DropConstraints @tableSchema, @tableName, 'PRIMARY KEY'; 
    
        FETCH NEXT FROM table_cursor INTO @tableSchema, @tableName 
      END 
    
      -- Cleanup 
      CLOSE table_cursor 
      DEALLOCATE table_cursor
    
    COMMIT TRANSACTION 
    GO
    
    DROP PROCEDURE #DropConstraints;
    GO