数据类型
数值类型
整型
-TINYINT -SMALLINT -MEDIUMINT -INT -BIGINT
关键词:ZEROFILL(零填充) 、 UNSIGNED(无符号) 、 AUTO_INCREMENT(自动递增) 、 NOT NULL(非空) 、 PRIMARY KEY(主键) 、 UNIQUE(外键)
1)定义了ZEAROFILL的都自动添加unsigned 属性。
2)任何想要使用auto_increment的列,应定义NOT NULL ,并定义为 primary key或者是unique。
mysql> CREATE TABLE t1(
-> id1 INT,
-> id2 INT(5));
浮点数
浮点数和定点数
单精度:FLOAT(X,Y) 双精度:DOUBLE(X,Y) 定点:DECIMAL(X,Y)
浮点数如果不写精度,就按照输入的精度显示,定点数如果不写精度,就按照DECIMAL(10,0)
日期和时间类型
-DATE
-DATETIME
-TIMESTAMP
-TIME
-YEAR
DATETIME是DATE和TIME的组合
mysql> DROP TABLE t1;
mysql> CREATE TABLE t (
-> d date,
-> t time,
-> dt datetime);
mysql> DESC t;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d | date | YES | | NULL | |
| t | time | YES | | NULL | |
| dt | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
mysql> INSERT INTO t(d,t,dt) VALUES(NOW(),NOW(),NOW());
mysql> SELECT * FROM t;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2017-08-22 | 07:29:58 | 2017-08-22 07:29:58 |
+------------+----------+---------------------+
TIMESTAMP 时间戳
MySQL中只能有一列默认为CURRENT_TIMESTAMP;另外TIMESTAMP与时区有关,当插入时间时会先转换为本地时间存储,所以同一个时间在不同地区看到的时间可能不是一样的。
mysql> ALTER TABLE t ADD id1 TIMESTAMP;
mysql> SELECT * FROM t;
+------------+----------+---------------------+---------------------+
| d | t | dt | id1 |
+------------+----------+---------------------+---------------------+
| 2017-08-22 | 07:29:58 | 2017-08-22 07:29:58 | 2017-08-22 07:38:20 |
+------------+----------+---------------------+---------------------+
mysql> ALTER TABLE t ADD id2 TIMESTAMP;
mysql> SELECT * FROM t;
+------------+----------+---------------------+---------------------+---------------------+
| d | t | dt | id1 | id2 |
+------------+----------+---------------------+---------------------+---------------------+
| 2017-08-22 | 07:29:58 | 2017-08-22 07:29:58 | 2017-08-22 07:38:20 | 0000-00-00 00:00:00 |
+------------+----------+---------------------+---------------------+---------------------+
时区相关
创建表
mysql> CREATE TABLE t8(
-> id1 TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> id2 DATETIME DEFAULT NULL);
**查看当前时区**
mysql> SHOW VARIABLES LIKE "TIME_ZONE";
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| time_zone | SYSTEM |
+---------------+--------+
mysql> INSERT INTO t8 VALUES(NOW(),NOW());
mysql> SELECT * FROM t8;
+---------------------+---------------------+
| id1 | id2 |
+---------------------+---------------------+
| 2017-08-22 07:55:17 | 2017-08-22 07:55:17 |
+---------------------+---------------------+
**修改为东九区**
mysql> SET TIME_ZONE='+9:00';
mysql> SELECT * FROM t8;
+---------------------+---------------------+
| id1 | id2 |
+---------------------+---------------------+
| 2017-08-22 08:55:17 | 2017-08-22 07:55:17 |
+---------------------+---------------------+
字符串类型###
-CHAR
-VARCHAR
-BINARY
-VARBINARY
-BLOB
-TEXT
-ENUM
-SET
CHAR VARCHAR类型
NULL
BINARY VARBINARY类型
NULL
ENUM类型(枚举类型)
mysql> CREATE TABLE t(
-> id INT(10) AUTO_INCREMENT PRIMARY KEY,
-> sex ENUM("male","female")
-> );
mysql> DESC t;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| sex | enum('male','female') | YES | | NULL | |
+-------+-----------------------+------+-----+---------+----------------+
只允许从值的集合中选取值,而且不能一次取多个值
mysql> INSERT INTO t(sex) VALUES(NULL),("male"),("female"),("A");
ERROR 1265 (01000): Data truncated for column 'sex' at row 4
mysql> INSERT INTO t(sex) VALUES(NULL),("male"),("female");
mysql> SELECT * FROM t;
+----+--------+
| id | sex |
+----+--------+
| 1 | NULL |
| 2 | male |
| 3 | female |
+----+--------+
SET类型
为表t增加字段love
mysql> ALTER TABLE t ADD love SET("A","B","C");
mysql> UPDATE t SET love="a" WHERE id = 1;
mysql> SELECT * FROM t;
+----+--------+------+
| id | sex | love |
+----+--------+------+
| 1 | NULL | NULL |
| 2 | male | NULL |
| 3 | female | NULL |
+----+--------+------+
修改id=1的love的值
mysql> UPDATE t SET love="a" WHERE id = 1;
mysql> SELECT * FROM t;
+----+--------+------+
| id | sex | love |
+----+--------+------+
| 1 | NULL | A |
| 2 | male | NULL |
| 3 | female | NULL |
+----+--------+------+
love=2 的时候一样可以修改记录
mysql> UPDATE t SET love=2 WHERE id = 1;
mysql> SELECT * FROM t;
+----+--------+------+
| id | sex | love |
+----+--------+------+
| 1 | NULL | B |
| 2 | male | NULL |
| 3 | female | NULL |
+----+--------+------+