mysql约束条件&&数值类型的区别

datetime和timestamp的区别:
datetime  如果没有赋予值,则显示的数据为空

                  日期范围是1001-9999年,

                   使用8字节的存储空间       

                   DATETIME的默认值为null     
timestamp 如果没有赋予值,则会自动的变成系统的当前时间的值

                  日期范围是1970——2038年

                  依赖时区也显示时区 

                   TIMESTAMP的存储空间为4字节

char和varchar的区别

char是的长度是固定的,如果超过了char指定的宽带的时候则会自动报错。

varchar是可以变化的,但是每次的插入数据的时候,都需要内存读出插入数据的长度,所有是以速度换取空间的作法。

int(3) 显示宽度,当不够宽度的时候,默认是用空格补位的,如果够指定的位数的时候,则可以在int的范围内随意指定
char(4) 限制字符都长度

mysql> select year (now());    ----查看当前的年份
+--------------+
| year (now()) |
+--------------+
|         2018 |
+--------------+
1 row in set (0.00 sec)

mysql> select now()                         -----查看当前系统的时间
    -> ;
+---------------------+
| now()               |
+---------------------+
| 2018-07-23 12:59:22 |
+---------------------+
1 row in set (0.00 sec)

mysql> create table t12(
    -> name char(5),
    -> level int(3) zerofill,                  --------默认是用空格补齐,也可以换0来补充。
    -> money int(5) zerofill);
Query OK, 0 rows affected (0.33 sec)

mysql> insert into t12 values("swj",18,28)  ;
Query OK, 1 row affected (0.15 sec)

mysql> select * from t12;
+------+-------+-------+
| name | level | money |
+------+-------+-------+
| swj  |   018 | 00028 |
+------+-------+-------+
1 row in set (0.00 sec)

字段约束条件
NULL 默认是空,如果需要的时候则需要指定no null

+-------+--------------------------+------+-----+---------+-------+
| Field | Type                     | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| name  | char(5)                  | YES  |     | NULL    |       |
| level | int(3) unsigned zerofill | YES  |     | NULL    |       |
| money | int(5) unsigned zerofill | YES  |     | NULL    |       |
+-------+--------------------------+------+-----+---------+-------+


ysql> create table t14(  
    -> name char(5) not null,  设置不容许为空 
    -> level int(3) zerofill default 0,  把默认的补齐为空格改为0,并设置改字段默认设置为0
    -> money tinyint(2) zerofill default 0);   
Query OK, 0 rows affected (0.33 sec)


如下的设置当插入的name字段为空的时候,会默认的报错,即使设置了默认为NULL的字段,如果想要实现在不插入数据的时候字段name
为空的情况,则应该在应该设置default “” 设置默认值为""空,避免与NULL发生冲突。
mysql> create table t14(
    -> name char(5) not null,
    -> level int(3) zerofill default 0,
    -> money tinyint(2) zerofill default 0);
Query OK, 0 rows affected (0.33 sec)

mysql> desc t14
    -> ;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| name  | char(5)                      | NO   |     | NULL    |       |
| level | int(3) unsigned zerofill     | YES  |     | 000     |       |
| money | tinyint(2) unsigned zerofill | YES  |     | 00      |       |
+-------+------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改表结构

mysql> desc t14
    -> ;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| name  | char(5)                      | NO   |     | NULL    |       |
| level | int(3) unsigned zerofill     | YES  |     | 000     |       |
| money | tinyint(2) unsigned zerofill | YES  |     | 00      |       |
+-------+------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table t14 add email varchar(30) default "[email protected]",add tel char(11);   -----修改表的默认值
Query OK, 0 rows affected (0.48 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc t14;
+-------+------------------------------+------+-----+-------------+-------+
| Field | Type                         | Null | Key | Default     | Extra |
+-------+------------------------------+------+-----+-------------+-------+
| name  | char(5)                      | NO   |     | NULL        |       |
| level | int(3) unsigned zerofill     | YES  |     | 000         |       |
| money | tinyint(2) unsigned zerofill | YES  |     | 00          |       |
| email | varchar(30)                  | YES  |     | [email protected] |       |
| tel   | char(11)                     | YES  |     | NULL        |       |
+-------+------------------------------+------+-----+-------------+-------+
5 rows in set (0.00 sec)


mysql> alter table t14 add sex enum("boy","girl","no") default "no" after name;   -----更改字段的排序
Query OK, 0 rows affected (0.52 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t14;
+--------+------------------------------+------+-----+-------------+-------+
| Field  | Type                         | Null | Key | Default     | Extra |
+--------+------------------------------+------+-----+-------------+-------+
| stu_id | char(9)                      | YES  |     | NULL        |       |
| name   | char(5)                      | NO   |     | NULL        |       |
| sex    | enum('boy','girl','no')      | YES  |     | no          |       |
| level  | int(3) unsigned zerofill     | YES  |     | 000         |       |
| money  | tinyint(2) unsigned zerofill | YES  |     | 00          |       |
| email  | varchar(30)                  | YES  |     | [email protected] |       |
| tel    | char(11)                     | YES  |     | NULL        |       |
+--------+------------------------------+------+-----+-------------+-------+
7 rows in set (0.00 sec)

modify 改类型和约束
当数据中有数据的时候,改约束条件的时候则会发生冲突报错,在修改约束条件的时候,如果没有把原有的约束条件删除,则会恢复默认的设置。
mysql> alter table t14 modify stu_id char(9) not null;          -------修改字段的默认条件设置
Query OK, 0 rows affected (0.48 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t14;
+--------+------------------------------+------+-----+-------------+-------+
| Field  | Type                         | Null | Key | Default     | Extra |
+--------+------------------------------+------+-----+-------------+-------+
| stu_id | char(9)                      | NO   |     | NULL        |       |
| name   | char(5)                      | NO   |     | NULL        |       |
| sex    | enum('boy','girl','no')      | YES  |     | no          |       |
| level  | int(3) unsigned zerofill     | YES  |     | 000         |       |
| money  | tinyint(2) unsigned zerofill | YES  |     | 00          |       |
| email  | varchar(30)                  | YES  |     | [email protected] |       |
| tel    | char(11)                     | YES  |     | NULL        |       |
+--------+------------------------------+------+-----+-------------+-------+
7 rows in set (0.00 sec)

mysql> alter table t14 modify tel char(11) after sex;           ----------修改字段的排序,把tel放在sex后面
Query OK, 0 rows affected (0.51 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t14;
+--------+------------------------------+------+-----+-------------+-------+
| Field  | Type                         | Null | Key | Default     | Extra |
+--------+------------------------------+------+-----+-------------+-------+
| stu_id | char(9)                      | NO   |     | NULL        |       |
| name   | char(9)                      | YES  |     | NULL        |       |
| sex    | enum('boy','girl','no')      | YES  |     | no          |       |
| tel    | char(11)                     | YES  |     | NULL        |       |
| level  | int(3) unsigned zerofill     | YES  |     | 000         |       |
| money  | tinyint(2) unsigned zerofill | YES  |     | 00          |       |
| email  | varchar(30)                  | YES  |     | [email protected] |       |
+--------+------------------------------+------+-----+-------------+-------+
7 rows in set (0.00 sec)

mysql> alter table t14 drop stu_id,drop tel;                -----      删除相应的字段
Query OK, 0 rows affected (0.66 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t14;
+-------+------------------------------+------+-----+-------------+-------+
| Field | Type                         | Null | Key | Default     | Extra |
+-------+------------------------------+------+-----+-------------+-------+
| name  | char(9)                      | YES  |     | NULL        |       |
| sex   | enum('boy','girl','no')      | YES  |     | no          |       |
| level | int(3) unsigned zerofill     | YES  |     | 000         |       |
| money | tinyint(2) unsigned zerofill | YES  |     | 00          |       |
| email | varchar(30)                  | YES  |     | [email protected] |       |
+-------+------------------------------+------+-----+-------------+-------+
5 rows in set (0.00 sec)
 

猜你喜欢

转载自blog.csdn.net/zhydream77/article/details/81174475