Database CHECK constraint exists

Source: Internet
Author: User

Http://stackoverflow.com/questions/2499332/how-to-check-if-a-constraint-exists-in-sql-server

Easiest-to-check for the existence of a constraint (and then do something such as drop it if it exists) are to use the OBJECT_ID () function ...

  if object_id  ( dbo.[ Ck_constraintname] ' ,  ' C ' )  is notnull alter table Dbo.[ tablename] drop constraint ck_constraintname         

Object_ID can is used without the second parameter (' C ' for check constraints only) and that could also work, but if your Constraint name matches the name of the other objects in the database to get unexpected results.

  if object_id  ( dbo.[ Ck_constraintname] ' )  isnot null alter table Dbo.[ tablename] drop constraint ck_constraintname         

OBJECT_ID can also is used with other "constraints" such as Foreign key constraints or Primary key constraints, etc. For best results, always include the appropriate object type as the second parameter for the OBJECT_ID function:

Constraint Object Types:

    • C = CHECK Constraint
    • D = DEFAULT (constraint or stand-alone)
    • F = FOREIGN KEY constraint
    • PK = PRIMARY KEY constraint
    • R = Rule (Old-style, stand-alone)
    • UQ = UNIQUE Constraint

Also Note that the schema is often required. The schema of constraints generally takes the schema of the parent table.

Failure to put your constraints (or whatever is checking) in brackets when using the This method may also cause a false n Egative--If your object uses unusual characters (such as A.), the brackets is required.

Database CHECK constraint exists

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.