Constraints table in the database

1.NULL / NOT NULL

NULL (represents the default), the default database fields are basically field is empty.

NOT NULL (means no empty)

--NULL data and any arithmetic operation, the result is NULL

2.default (default value)

All the students are female sex, you can attach to the gender column when girls students in the class, such as creating tables when you create a table defaults 'female'; when you insert data, not to gender assignment, the default value.

mysql> create table student(id int,name varchar(32),sex varchar(32) default '女');
Query OK, 0 rows affected (0.49 sec)
mysql> insert into student(id,name)values(1,'huahua'),(2,'haha');
Query OK, 2 rows affected (0.19 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from student;
+------+--------+------+
| id   | name   | sex  |
+------+--------+------+
|    1 | huahua | 女   |
|    2 | haha   | 女   |
+------+--------+------+
2 rows in set (0.00 sec)
mysql> insert into student values(3,'hh','男');
Query OK, 1 row affected (0.14 sec)
 
mysql> select * from student;
+------+--------+------+
| id   | name   | sex  |
------ + ------ + -------- + +
| 1 | huahua | F |
| 2 | haha | F |
| 3 | HH | M |
+ ---- - + -------- + ------ +
3 rows in the SET (0.00 sec)
3. column description (comment comment: There is no impact on the program)

MySQL> Create Table Student (
    ID int Comment 'student number',
    name VARCHAR (32) Comment 'name',
    Sex VARCHAR (32) Comment 'Sex' default 'F');
Query the OK, 0 rows affected (0.49 sec)
. 4 .zerofill (if the width is less than the width of the set autofill 0)

mysql> create table zerofilltest(a int(5));
Query OK, 0 rows affected (0.80 sec)
mysql> insert into zerofilltest values(1);
Query OK, 1 row affected (0.17 sec)
mysql> select * from zerofilltest;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
mysql> alter table zerofilltest modify a int(5) zerofill;
Query OK, 1 row affected (1.75 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> select * from zerofilltest;
+-------+
| a     |
+-------+
| 00001 |
+-------+
5.主键(primary key)

Unique primary key constraint of the field for which data can not be duplicated, can not be empty, a table can have at most one primary key, where the primary key columns are often integer type.

Add a primary key ways
I. added directly after the primary key column name.

MySQL> Create Table pritest (int Primary Key A, B int);
Query the OK, 0 rows affected (0.63 sec)
 
MySQL> desc pritest;
+ ------- + --------- + - --------- + + ----- + ----- + -------
| Field, | Type | Null | Key | the Default | Extra |
+ ------ - + --------- + ------ + ----- + --------- + ------- +
| A | int (11) | NO | the PRI | NULL | |
| B | int (. 11) | YES | | NULL | |
+ ------- + --------- + ------ + - + --------- + ------- + ---
2 rows in the SET (0.11 sec)
II. added using alter table table name add primary key (field names) after the table is created successfully primary key

mysql> create table pritest1(a int,b int);
Query OK, 0 rows affected (0.65 sec)
 
mysql> desc pritest1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a     | int(11) | YES  |     | NULL    |       |
| b     | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--添加主键
mysql> alter table pritest1 add primary key(a);
Query OK, 0 rows affected (1.12 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc pritest1;
+-------+---------+------+-----+---------+-------+
| Field, | Type | Null | Key | the Default | Extra |
+ ------- + --------- + ------ + ----- + ----- + ------- + ----
| A | int (. 11) | NO | the PRI | NULL | |
| B | int (. 11) | YES | | NULL | |
+ ------- ------ + ----- + --------- + --------- + ------- + +
2 rows in SET (0.02 sec)
III. use composite primary key, primary key used when creating the table (field 1, field 2)

MySQL> Create Table pritest2 (A int, int B, Primary Key (A, B));
Query the OK, 0 rows affected (0.71 sec)
 
MySQL> desc pritest2;
+ ------- + ----- + ------ + ----- + ---- + --------- + -------
| Field, | Type | Null | Key | the Default | Extra |
+ + --------- + ------ + ------- ----- + --------- + ------- +
| A | int (. 11) | NO | the PRI | NULL | |
| B | int (. 11) | NO | the PRI | NULL | |
+ ------- + --------- + - + ----- + --------- + ---- ------- +
2 in rows SET (0.00 sec)
*** a value corresponding to the main key can not be repeated, once inserted duplicate value, the insert fails
MySQL> iNSERT INTO pritest values (1,1);
Query the OK,. 1 Row affected (0.08 sec)
MySQL> iNSERT INTO pritest values (1,2);
ERROR 1062 (23000): duplicate entry ' 1 'for key' PRIMARY '
删除主键:用alter table 表名 drop primary key;
 
mysql> alter table pritest drop primary key;
Query OK, 1 row affected (1.51 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
mysql> insert into pritest values(1,2);
Query OK, 1 row affected (0.23 sec)
6.自增长(auto_increment)

When the corresponding field is not a value, the system will automatically be triggered, the system will already have the maximum value +1 from the current field, to give a new different value

    

mysql> create table incrementtest(id int primary key auto_increment,age int);
Query OK, 0 rows affected (0.57 sec)
mysql> insert into incrementtest (age)values(15),(16);
Query OK, 2 rows affected (0.13 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from incrementtest;
+----+------+
| id | age  |
+----+------+
|  1 |   15 |
|  2 |   16 |
+----+------+
2 rows in set (0.00 sec)
    自增长的特点:

        Any field do self growth, provided that in itself is an index (key column has the value) 

        Since growth field must be an integer 

        A table can have a self-growth

7. unique key (unique key)

The only key to solve the problem in a number of fields table requires a unique constraint. Unique key allows empty, may be a plurality of empty, empty fields are not uniquely comparison.

8. A foreign key (foreign key)

Is used to define foreign key relationship between the primary and the tables: foreign key constraint is defined in the primary table, the primary table must be a unique primary key constraint or constraints. When the foreign key is defined, the required data must be included in the foreign key column is null or the presence of the primary key in the primary table.

Foreign key Syntax: foreign key (field names) references the primary table (column)
 

Published 28 original articles · won praise 24 · views 1073

Guess you like

Origin blog.csdn.net/qq_42305209/article/details/104262798