[Reading notes] SQL constraints

Source: Internet
Author: User

Purpose: To ensure that data conforms to some data integrity rule by setting constraints at the column or table level

Implementation: The database proactively checks the integrity of the maintenance data

means: Constraints, data types, triggers

--------------------------------------------------------------------------------------------------------------- -------------------------------------

Three types of constraints:

Entity constraints, domain constraints, referential integrity constraints

Specific implementation methods:

Primary KEY constraints, FOREIGN KEY constraints, unique constraints,CHECK constraints, default Constraints, rules, defaults

Classified

Domain constraints: For columns, ensure that the data for the column meets a specific standard. CHECK constraints, default constraints, rules, and Defaults

Entity constraints: For rows, only specific rows are concerned, and specific columns for each row have unique values. PRIMARY KEY constraint, UNIQUE constraint

Referential integrity constraint: The value of a column must match the value of another column. FOREIGN KEY constraints

--------------------------------------------------------------------------------------------------------------- -------------------------------------

Difference constraints and rules, default values:

Rules and defaults are applied earlier than CHECK and default constraints, which are part of older SQL fallback constraints and are used primarily for backwards compatibility.

Constraint: is a table of characteristics, itself does not exist form;

Rules, default values: are defined separately, and then bound to the table.

--------------------------------------------------------------------------------------------------------------- -------------------------------------

Syntax

Add constraint--use ALTER TABLE <table name>

ALTER TABLE <table name>

ADD CONSTRAINT <constraint name>

<constraint type> <constraint parameters...>

FOREIGN KEY constraints

<column Name><data type><nullability>

FOREIGN KEY REFERENCES <table name> (<column name>)

[on DELETE {cascade|no action| SET null| SET DEFAULT}]

[on UPDATE {cascade|no action| SET null| SET DEFAULT}]

ALTER TABLE <table name>

ADD CONSTRAINT <constraint name>

FOREIGN KEY (<column name>) REFERENCES <table name> (<column name>)

Unique constraint

<column name> <data type> <nullabiliby>

UNIQUE

ALTER TABLE <table name>

ADD CONSTRAINT <constraint name>

UNIQUE (<column name>)

CHECK Constraint

ALTER TABLE <table name>

ADD CONSTRAINT <constraint name>

CHECK

(<rule...>)

DEFAULT Constraint

<column name> <data type> <nullability>

DEFAULT <default value>

ALTER TABLE <table name>

ADD CONSTRAINT <default name>

DEFAULT <value> for <column name>

Ignore invalid data when creating constraints

When you create a constraint with SQL Server, the constraint cannot be created if the existing data does not meet the constraint requirements. There are two workarounds:1) Modify the existing data to conform to the constraint requirements;2) Add the WITH NOCHECK option in the ALTER statement :

ALTER TABLE <table name>

With NOCHECK

ADD CONSTRAINT <constraint name>

<constraint Type>

Temporarily disable a constraint that already exists

ALTER TABLE <table name>

NOCHECK

CONSTRAINT <constraint name>

To enable a forbidden constraint

ALTER TABLE <table name>

CHECK

CONSTRAINT <constraint name>

Delete Constraint

ALTER TABLE <tablename>
Drop constraint <constraintname>

modifying constraints

Delete the constraint before adding

[Reading notes] SQL constraints

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.