V. database (table integrity constraints)

A. Complete constraint on a table

1. Introduction Constraints

                            

 2. NOT NULL (non-empty)

Whether empty, null represents the empty, non-string 

not null - not empty 

null - can be empty
mysql> create table t12 (id int not null);
Query OK, 0 rows affected (0.02 sec)

mysql> select * from t12;
Empty set (0.00 sec)

mysql> desc t12;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
row in set (0.00 sec)

#不能向id列插入空元素。 
mysql> insert into t12 values (null);
ERROR 1048 (23000): Column 'id' cannot be null

mysql> insert into t12 values (1);
Query OK, 1 row affected (0.01 sec)

2. DEFAULT + NOT NULL (default non-null value +)

We are bound by a column is not empty, if often have duplicate content of this column, we need to frequently inserted, this will give our operations bring new burdens, so it was a concept of default. 

The default value may specify the default column was created, if not the active set, the default value is automatically added when inserting data
MySQL> Create Table T13 (ID1 int Not null, ID2 int Not null default 222 ); 
Query the OK, 0 rows affected ( 0.01 sec) 

MySQL > desc T13;
 + ------- + ------- - + ------ + ----- + --------- + ------- + 
| Field, | Type | Null | Key | the Default | Extra | 
+ - + --------- + ------ + ----- + ----- + --------- + ------- 
| id1 | int (. 11) | NO | | NULL | | 
| ID2 | int (. 11) | NO | | 222 | | 
+ ------- + --------- + ------ --------- + ------- + ----- + + 
rows in SET (0.01 sec) 

# only add to the value field id1, id2 will find you use the default value field filled 
mysql > insert into t13 (id1) values (111); 
Query the OK, 1 Row affected (0.00 sec) 

MySQL > the SELECT * from T13;
 + ----- + ----- + 
| id1 | id2 | 
+ ----- + ----- + 
| 111 | 222 | 
+ ----- + ----- + 
Row in SET (0.00 sec) 

# ID1 field can not be blank, it is not alone to fill value id2 field; 
MySQL> INSERT iNTO T13 (id2) values (223 ); 
ERROR 1364 (HY000): Field, ' ID1 ' doesn ' T have have a default value 

# to ID1, id2 packed data, respectively, padding data id2 may override the default values 
mysql> insert into t13 (id1, id2) values (112,223 ); 
Query the OK, 1 Row affected (0.00 sec)

mysql> select * from t13;
+-----+-----+
| id1 | id2 |
+-----+-----+
| 111 | 222 |
| 112 | 223 |
+-----+-----+
rows in set (0.00 sec)
not null not take effect

setting strict mode: do not support the insertion of null values for not null fields are not supported for self-growth field into the "Value does not support text fields have default values take effect directly (restart failure) in mysql: mysql
> the SET sql_mode ="STRICT_TRANS_TABLES , NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION"; profile adds (permanent failure): SQL-mode ="the STRICT_TRANS_TABLES, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION"

3. UNIQUE (unique constraint specifies a column or combination of columns can not be repeated)

UNIQUE constraint uniquely identifies each record in the database table. 

UNIQUE and PRIMARY KEY constraints are column or set of columns to provide a guarantee of uniqueness. PRIMARY KEY constraint automatically has a UNIQUE definition .

Note that each table can have multiple UNIQUE constraints, but each table can have only one PRIMARY KEY constraint

方法一:
create table department1(
id int,
name varchar(20) unique,
comment varchar(100)
);


方法二:
create table department2(
id int,
name varchar(20),
comment varchar(100),
unique(name)
);


mysql> insert into department1 values(1,'IT','技术');
Query OK, 1 row affected (0.00 sec)
mysql> insert into department1 values(1,'IT','技术');
ERROR 1062 (23000): Duplicate entry 'IT' for key 'name
UNIQUE + NOT NULL 结合

mysql> create table t1(id int not null unique); Query OK, 0 rows affected (0.02 sec) mysql> desc t1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ row in set (0.00 sec)
UNIQUE combined unique (represented in the field data table to add only repeated addition being given)
Create Table-Service ( ID int Primary Key AUTO_INCREMENT, name VARCHAR (
20 is), Host VARCHAR (15)Notnull, Port intNotnull, UNIQUE (Host, Port)#United unique ); MySQL>INSERT INTO-Service values -> (. 1,'Nginx','192.168.0.10', 80), -> (2,'HAProxy','192.168.0.20', 80), -> (3,'mysql','192.168.0.30',3306) -> ; Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> insert into service(name,host,port) values('nginx','192.168.0.10',80); ERROR 1062 (23000): Duplicate entry '192.168.0.10-80' for key 'host'

 4.  a PRIMARY KEY (in order to ensure that the primary key field in each data table is a table similar to a unique value and unique)

 

Guess you like

Origin www.cnblogs.com/Sup-to/p/11229436.html