The SQL statement (eight) that must be the integrity constraint of the database

Source: Internet
Author: User

Entity integrity

 1. Defining a primary key when building a tableCreate Table Table name   (Sno int identity (+),Sname nvarchar (),--Set Primary keyPrimary Key (Sno)   )2. Add a primary keyALTER TABLE table nameadd constraint pk_ table name _snoprimary KEY (ID) Referential Integrity1. Define a foreign key when building a tableCREATE TABLE table name  (Sno int Identity (primary) key,CNO int not NULL,foreign Key (CNO) ReferencesTable Name 2 (Cno)On Delete cascade --Cascade Delete On update Cascade --Cascade update --On delete control on action Delete  )2. Add a foreign keyALTER TABLE table nameadd constraint Fk_ table name _ Table Name 2Foreign Key (CID) references table Name 2 (CID)  user-defined integrity1. Non-null constraintsALTER TABLE table nameALTER COLUMN name varchar (NOT NULL)2. Unique ConstraintsALTER TABLE table nameadd constraint uq_ table name _ Column name unique (column)3. Check ConstraintsALTER TABLE table nameadd constraint Ck_ table name _ Column name Check (age>5)4. Default ConstraintsALTER TABLE table nameadd constraint df_ table name _ Column name default (' Male ')For gender Delete Constraint--delete constraintALTER TABLE name drop constraint df_ table name _ column

The SQL statement (eight) that must be the integrity constraint of the database

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.