SQL Delete Constraint

Source: Internet
Author: User

In SQL database, what should you do if you need to delete a table constraint? Here's how you can remove the SQL table constraint for your reference and hopefully help you.

1) Disable SQL for all table constraints
Select ' ALTER TABLE ' +name+ ' nocheck constraint all ' from sysobjects where type= ' U '

2) Delete SQL for all table data
Select ' TRUNCATE TABLE ' +name from sysobjects where type= ' U '

3) Restore SQL for all table constraints
Select ' ALTER TABLE ' +name+ ' check constraint all ' from sysobjects where type= ' U '

4) Delete a constraint for a field
DECLARE @name varchar (100)
--DF is the constraint name prefix
Select @name =b.name from syscolumns a,sysobjects b where a.id=object_id (' table name ') and B.id=a.cdefault and a.name= ' field name ' and B.N Ame like ' df% '
--Delete Constraint
ALTER TABLE name drop constraint @name
--Add new default values and constraints for the field
ALTER TABLE name ADD CONSTRAINT @name DEFAULT (0) for [field name] changes to field constraints
--Delete Constraint
ALTER TABLE TableName
Drop CONSTRAINT Constraint name
--Modify the properties of a column that already exists in the table (excluding constraints, but can be primary or incremented or unique)
ALTER TABLE TableName
ALTER column name int NOT NULL
--Adding a column constraint
ALTER TABLE TableName
ADD CONSTRAINT df_tablename_ Column name DEFAULT (0) for column name
--Add scope constraints
ALTER TABLE tablename Add check (Gender in (' M ', ' F '))

SQL Delete Constraint

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.