SQL Server removes the primary key constraint that is thought of

Source: Internet
Author: User

The following code has been found on the Internet:

DECLARE @Pk varchar (100);
Select @Pk =name from sysobjects where parent_obj=object_id (' table name ') and xtype= ' Pk ';
If @Pk is not null
Begin
EXEC (' Alter table name Drop ' + @Pk)--Delete the primary key
End

The point here is that the table Sysobjects,sysobjects table is a system table, each object created in the database (constraints, defaults, logs, rules, stored procedures, etc.) in the table for a row, the above xtype is the type, such as PK is the primary key, F is a foreign key, that is, Delete the foreign key, you just need to change the PK to F can be, the same as the default constraints D and so on.

EXEC (' SQL statement ') is a stored procedure that executes SQL statements

In fact, the use of good sysobjects this table, we can make a lot of articles, such as looking at an object (table, database, stored procedures, etc.) exist.

SQL Server removes the primary key constraint that is thought of

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.