Skip to main content
remove social & meta commentary, improve language & format
Source Link
philipxy
  • 15.2k
  • 6
  • 44
  • 101

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

egE.g., using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'.

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects  
where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT
 (select name 
   from sysobjects 
  where xtype = 'PK' 
    and parent_obj = object_id('categories') ) 

Can anyone show me where IWhere am I going wrong?

Many thanks,

Bob

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT
 (select name 
   from sysobjects 
  where xtype = 'PK' 
    and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name.

E.g., using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'.

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can get the primary key name for the table by table name:

select name from sysobjects 
where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT
 (select name 
   from sysobjects 
  where xtype = 'PK' 
    and parent_obj = object_id('categories') ) 

Where am I going wrong?

text formatting
Source Link
Marcello Miorelli
  • 3.8k
  • 6
  • 53
  • 79

HI,

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT

 
 (select name 
   from sysobjects 
  where xtype = 'PK' 
    and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

HI,

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT

 
(select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT
 (select name 
   from sysobjects 
  where xtype = 'PK' 
    and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

added 3 characters in body; edited title
Source Link
marc_s
  • 759.6k
  • 185
  • 1.4k
  • 1.5k

ms sql SQL Server: drop table primary key, without knowing its name

HI,

Using: MSSQLSQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT


(select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

ms sql drop table primary key, without knowing its name

HI,

Using: MSSQL Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT


(select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

SQL Server: drop table primary key, without knowing its name

HI,

Using: SQL Server Database: Northwind

I'd like to drop a table primary key, without knowing the PK constraint name..

eg, using the Categories table in the Northwind Sample database, the primary key column is 'CategoryId', and the primary key name is 'PK_Categories'

I can drop the primary key while knowing the primary key name:

ALTER TABLE categories DROP CONSTRAINT PK_Categories;

And I can also get the primary key name for the table by table name:

select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories')

However, I cannot put them together to delete a table's primary key, without first knowing the primary key name.

I am trying:

ALTER TABLE categories DROP CONSTRAINT


(select name from sysobjects where xtype = 'PK' and parent_obj = object_id('categories') ) 

Can anyone show me where I am going wrong?

Many thanks,

Bob

Source Link
Bob
Bob
Loading