Mysql field modifier (constraints)

(1).null和not null

  not null can not insert null, but can insert a null value.

  Numeric, character, date can be inserted null, but only be inserted null character.

  Use as follows:

MySQL> Create Database TEST_DB; 
Query the OK,. 1 Row affected (0.00 sec) 

MySQL> use TEST_DB; 
Database changed 
MySQL> Create Table `notnull_table` (name VARCHAR (20 is) Not null); 
Query the OK, 0 rows affected (0.02 sec) 

mysql> insert into notnull_table values (null ); // set is not null columns not permit insertion of null 
ERROR 1048 (23000): the column 'name' CAN bE Not null 
MySQL> iNSERT INTO notnull_table values ( ''); // but inserting nulls can 
Query the OK,. 1 Row affected (0.00 sec) 

MySQL> iNSERT INTO notnull_table values ( 'Jack'); 
Query the OK,. 1 Row affected (0.01 sec)

  The difference between the null and null: not null space, but null space (1 byte calculation).

  not null null higher than the efficiency of. This is because the null value is not empty, take up space, so, when compared to the field, relatively null fields will participate, some impact on efficiency. And the index does not store null values, efficiency index will drop a lot.

(2).default

  defalut, default values ​​can only be used for character.

mysql> create table default_table(id int not null,name varchar(20) not null default 'test');
Query OK, 0 rows affected (0.03 sec)

mysql> insert into default_table(id) values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from default_table;
+----+------+
| id | name |
+----+------+
|  1 | test |
+----+------+
1 row in set (0.00 sec)

  Note: If the character field is not set default, can be null, the default is null; is not null, error. enum treatment alone, the default is the first element. Time fields are not default, default to the current time.

(3).auto_increment

  AUTO_INCREMENT, automatically increase automatically incremented by 1, for only numeric. But it seems to have to spend auto_increment primary key, or mysql error message will be reported in 1075.

mysql> create table auto_table(id bigint auto_increment primary key,name varchar(20));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into auto_table(name) values('name');
Query OK, 1 row affected (0.00 sec)

mysql> select * from auto_table;
+----+------+
| id | name |
+----+------+
|  1 | name |
+----+------+
1 row in set (0.00 sec)

(4) Extension: Clear table data, including the value of auto_increment

  In general, the data table will be deleted using the delete command, but there is no way to clear delete auto_increment value. as follows:

mysql> select * from auto_table;
+----+------+
| id | name |
+----+------+
|  1 | name |
+----+------+
1 row in set (0.00 sec)

mysql> delete from auto_table;
Query OK, 1 row affected (0.03 sec)

mysql> insert into auto_table(name) values('name2');
Query OK, 1 row affected (0.01 sec)

mysql> select * from auto_table;
+----+-------+
| id | name  |
+----+-------+
|  2 | name2 |
+----+-------+
1 row in set (0.00 sec)

  This time you need to use truncate commands. as follows:

mysql> select * from auto_table;
+----+-------+
| id | name  |
+----+-------+
|  2 | name2 |
+----+-------+
1 row in set (0.00 sec)

mysql> truncate table auto_table;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into auto_table(name) values('name3');
Query OK, 1 row affected (0.01 sec)

mysql> select * from auto_table; 
+----+-------+
| id | name  |
+----+-------+
|  1 | name3 |
+----+-------+
1 row in set (0.00 sec)

  Note: truncate will erase all data tables. If you want to clear auto_increment value, do not use this command.

Guess you like

Origin www.cnblogs.com/diantong/p/10983825.html