第一部分:基础知识 3. 数据类型 --[MySQL轻松入门教程]
MySQL 支持多种数据类型,这些数据类型可以分为几大类:数值类型、字符串类型、日期和时间类型、二进制类型以及枚举和集合。每种类型都有其特定的用途和存储需求。以下是 MySQL 中常用的数据类型的详细介绍:
1. 数值类型
在MySQL中,数值类型用于存储整数和浮点数。以下是一些常见的数值类型及其示例,包括如何创建表、插入数据以及查询数据。
1. 整数类型
TINYINT
- 范围:-128 到 127(有符号),0 到 255(无符号)
- 大小:1 字节
CREATE TABLE TinyIntExample (
id INT AUTO_INCREMENT PRIMARY KEY,
tinyint_col TINYINT
);
INSERT INTO TinyIntExample (tinyint_col) VALUES (100);
INSERT INTO TinyIntExample (tinyint_col) VALUES (-100);
INSERT INTO TinyIntExample (tinyint_col) VALUES (255); -- 无符号时有效
-- 查询所有 TinyIntExample 表中的数据
SELECT * FROM TinyIntExample;
mysql> create database taoyuan;
Query OK, 1 row affected (0.01 sec)
mysql> use taoyuan;
Database changed
mysql> CREATE TABLE TinyIntExample (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> tinyint_col TINYINT
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO TinyIntExample (tinyint_col) VALUES (100);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO TinyIntExample (tinyint_col) VALUES (-100);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO TinyIntExample (tinyint_col) VALUES (255);
ERROR 1264 (22003): Out of range value for column 'tinyint_col' at row 1
mysql> select * from TinyIntExample;
+----+-------------+
| id | tinyint_col |
+----+-------------+
| 1 | 100 |
| 2 | -100 |
+----+-------------+
2 rows in set (0.00 sec)
mysql>
SMALLINT
- 范围:-32768 到 32767(有符号),0 到 65535(无符号)
- 大小:2 字节
CREATE TABLE SmallIntExample (
id INT AUTO_INCREMENT PRIMARY KEY,
smallint_col SMALLINT
);
INSERT INTO SmallIntExample (smallint_col) VALUES (10000);
INSERT INTO SmallIntExample (smallint_col) VALUES (-10000);
INSERT INTO SmallIntExample (smallint_col) VALUES (65535); -- 无符号时有效
-- 查询所有 SmallIntExample 表中的数据
SELECT * FROM SmallIntExample;
mysql> CREATE TABLE SmallIntExample (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> smallint_col SMALLINT
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO SmallIntExample (smallint_col) VALUES (10000);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO SmallIntExample (smallint_col) VALUES (-10000);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO SmallIntExample (smallint_col) VALUES (65535);
ERROR 1264 (22003): Out of range value for column 'smallint_col' at row 1
mysql> select * from SmallIntExample;
+----+--------------+
| id | smallint_col |
+----+--------------+
| 1 | 10000 |
| 2 | -10000 |
+----+--------------+
2 rows in set (0.00 sec)
mysql>
MEDIUMINT
- 范围:-8388608 到 8388607(有符号),0 到 16777215(无符号)
- 大小:3 字节
CREATE TABLE MediumIntExample (
id INT AUTO_INCREMENT PRIMARY KEY,
mediumint_col MEDIUMINT
);
INSERT INTO MediumIntExample (mediumint_col) VALUES (1000000);
INSERT INTO MediumIntExample (mediumint_col) VALUES (-1000000);
INSERT INTO MediumIntExample (mediumint_col) VALUES (8388608); -- 无符号时有效
INSERT INTO MediumIntExample (mediumint_col) VALUES (8388607);
-- 查询所有 MediumIntExample 表中的数据
SELECT * FROM MediumIntExample;
mysql> CREATE TABLE MediumIntExample (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> mediumint_col MEDIUMINT
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> INSERT INTO MediumIntExample (mediumint_col) VALUES (1000000);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO MediumIntExample (mediumint_col) VALUES (-1000000);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO MediumIntExample (mediumint_col) VALUES (8388608);
ERROR 1264 (22003): Out of range value for column 'mediumint_col' at row 1
mysql> INSERT INTO MediumIntExample (mediumint_col) VALUES (8388607);
Query OK, 1 row affected (0.01 sec)
mysql> select * from MediumIntExample;
+----+---------------+
| id | mediumint_col |
+----+---------------+
| 1 | 1000000 |
| 2 | -1000000 |
| 3 | 8388607 |
+----+---------------+
3 rows in set (0.00 sec)
mysql>
INT 或 INTEGER
- 范围:-2147483648 到 2147483647(有符号),0 到 4294967295(无符号)
- 大小:4 字节
CREATE TABLE IntExample (