MySQL data constraint

Source: Internet
Author: User

Non-null constraint
CREATE TABLE USER (    INTnotNULL,--  non-null constraint    VARCHAR(10  ),    VARCHAR(DEFAULT' Shandong province '  --  default value )
Unique constraint
CREATE TABLE USER (    INTunique,--  Unique constraint    VARCHAR() ,    VARCHAR(theDEFAULT' Shandong province '–  Default value )
--PRIMARY KEY constraint--NOTE:--1) Typically, each table will have a primary key field set. Used to mark the uniqueness of each record in a table. ---2) It is recommended that you do not select a field that contains business meaning for the table as the primary key, and it is recommended that you design an ID field for each table that is non-business meaning independently.
CREATE TABLE USER(UIDINT PRIMARY KEY,--PRIMARY KEY constraint non-null + uniqueUnameVARCHAR(Ten), AddressVARCHAR( -)DEFAULT 'Shandong Province' --Default Value)INSERT  into USER(Uid,uname)VALUES('Zhang San');
Self-growth constraints
CREATE TABLE USER(UIDINT PRIMARY KEYAuto_increment,--the self-growth constraint must be a primary keyUnameVARCHAR(Ten), AddressVARCHAR( -)DEFAULT 'Shandong Province' --Default Value)INSERT  into USER(uname)VALUES('Zhang San');
0 Filling
CREATE TABLE USER(UIDINT(5) ZerofillPRIMARY KEYAuto_increment,--self-growth constraint must be primary key, 0 paddingUnameVARCHAR(Ten), AddressVARCHAR( -)DEFAULT 'Shandong Province' --Default Value)INSERT  into USER(uname)VALUES('Zhang San');
--Delete from empty table can not delete constraint--tauncate table clears the constraint

FOREIGN key
CREATE TABLEDept (DeptIDINT PRIMARY KEYauto_increment, DeptnameVARCHAR(Ten))SELECT *  fromDept;SELECT *  fromEMP;INSERT  intoDept (Deptname)VALUES('Software Development Department');INSERT  intoDept (Deptname)VALUES('Software Testing Department');CREATE TABLEEMP (EidINT PRIMARY KEYauto_increment, enameVARCHAR(5), DeptIDINT,    CONSTRAINTEmp_dept_fkFOREIGN KEY(DeptID)REFERENCESDept (deptid))INSERT  intoEMP (Ename,deptid)VALUES('Zhang San',1);

1) The constrained table is called the secondary table, the table that constrains others is called the main table, and the foreign key is set on the secondary table!!!

2) Main Table Reference field universal primary key!

3) Add data: First add the Main table, then add the secondary table

4) Modify the data: Modify the secondary table first, then modify the main table

5) Delete the data: Delete the secondary table before deleting the main table

MySQL data 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.