Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the primary key from without using constraint name

CREATE TABLE Persons (
    ID int PRIMARY KEY,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int
);

How to remove the primary key as there is no constraint defined?

like image 671
Raghav Avatar asked Jun 03 '17 04:06

Raghav


People also ask

How can drop primary key in SQL Server without constraint name?

To drop a primary key from a table, use an ALTER TABLE clause with the name of the table (in our example, product ) followed by the clause DROP PRIMARY KEY . Since a table can have only one primary key, you don't need to specify the primary key column(s).

What happens when a primary key constraint is created without a constraint name?

Note, however, that if you don't specify the constraint name, SQL Server will generate one for you if you include a constraint definition.

How do I delete a primary key?

To delete a primary key constraint using Object Explorer In Object Explorer, expand the table that contains the primary key and then expand Keys. Right-click the key and select Delete. In the Delete Object dialog box, verify the correct key is specified and select OK.


1 Answers

"How to remove PK as there is no constraint defined?"

Actually it's every bit as simple as you might hope:

SQL> create table t23 (id number primary key);

Table created.

SQL> select constraint_name, constraint_type
  2  from user_constraints
  3  where table_name = 'T23'
  4  /

CONSTRAINT_NAME                C
------------------------------ -
SYS_C0034419                   P

SQL> alter table t23 drop primary key;

Table altered.

SQL> select constraint_name, constraint_type
  2  from user_constraints
  3  where table_name = 'T23'
  4  /

no rows selected

SQL> 
like image 186
APC Avatar answered Oct 06 '22 18:10

APC