1. 數據類型分類
2. 數據類型
可能在我們平時練習的時候,我們一般插入的數據也比較小,一般不會超過數據類型所規定的範圍,但大家還是有必要記住它們的範圍。
看個錯誤的例子把:
mysql> create table tt1(num tinyint);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into tt1 values(1);
Query OK, 1 row affected (0.00 sec)
這樣是正確的,如果我們插入一個129呢
mysql> insert into tt1 values(128); -- 越界插入,報錯
ERROR 1264 (22003): Out of range value for column 'num' at row 1 --由於我們越界插入,就會報錯
ps:
- 在MYSQL中,整數還可以指定爲無符號類型的,默認是有符號的
- 可以通過UNSIGNED指定某個字段爲無符號類型
- 儘量不使用無符號類型,有符號的放不下,無符號的也可能仍然放不下,還不如設計時把它設計爲BIGINT
無符號案例:
mysql> create table tt2(num tinyint unsigned);
mysql> insert into tt2 values(-1); -- 無符號,範圍是: 0 - 255
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into tt2 values(255);
Query OK, 1 row affected (0.02 sec)
mysql> select * from tt2;
+------+
| num |
+------+
| 255 |
+------+
1 row in set (0.00 sec)
關於bit類型:
bit[(M)]:位字段類型,M表示每個值的位數,範圍從0~63,不寫M時,默認是1
舉個栗子:
mysql> create table tt4 ( id int, a bit(8));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into tt4 values(10, 10);
Query OK, 1 row affected (0.01 sec)
mysql> select * from tt4; --很奇怪,a的數據10沒有出現
+------+------+
| id | a |
+------+------+
| 10 | |
+------+------+
1 row in set (0.00 sec)
這是因爲bit字段在顯示時,是按照ASCII碼對應的值顯示。
通過查表,ascii值爲10(十進制),這個是不可顯示字符,因此什麼都不顯示。
float[(m, d)] [unsigned] : M指定顯示長度,d指定小數位數,佔用空間4個字節
如float(4,2)表示的範圍是-99.99 ~ 99.99,MySQL在保存值時會進行四捨五入。
mysql> create table tt6(id int, salary float(4,2));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into tt6 values(100, -99.99);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tt6 values(101, -99.991); #多的這一點被拿掉了
Query OK, 1 row affected (0.00 sec)
mysql> select * from tt6;
+------+--------+
| id | salary |
+------+--------+
| 100 | -99.99 |
| 101 | -99.99 |
+------+--------+
2 rows in set (0.00 sec)
如果定義的是float(4,2) unsigned 這時,因爲把它指定爲無符號的數,範圍是 0 ~ 99.99
mysql> create table tt7(id int, salary float(4,2) unsigned);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into tt7 values(100, -0.1);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+-------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------+
| Warning | 1264 | Out of range value for column 'salary' at row 1 |
+---------+------+-------------------------------------------------+
1 row in set (0.00 sec)
mysql> insert into tt7 values(100, -0);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tt7 values(100, 99.99);
Query OK, 1 row affected (0.00 sec)
3. 字符串類型
char(L): 固定長度字符串,L是可以存儲的長度,單位爲字符,最大長度值可以爲255
mysql> create table tt9(id int, name char(2));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into tt9 values(100, 'ab');
Query OK, 1 row affected (0.00 sec)
mysql> insert into tt9 values(101, '中國');
Query OK, 1 row affected (0.00 sec)
mysql> select * from tt9;
+------+--------+
| id | name |
+------+--------+
| 100 | ab |
| 101 | 中國 |
+------+--------+
varchar(L): 可變長度字符串,L表示字符長度,最大長度65535個字節
mysql> create table tt10(id int ,name varchar(6)); --表示這裏可以存放6個字符
mysql> insert into tt10 values(100, 'hello');
mysql> insert into tt10 values(100, '我愛你,中國');
mysql> select * from tt10;
+------+--------------------+
| id | name |
+------+--------------------+
| 100 | hello |
| 100 | 我愛你,中國 |
+------+--------------------+
如何選擇定長或變長字符串?
如果數據確定長度都一樣,就使用定長(char),比如:身份證,手機號
如果數據長度有變化,就使用變長(varchar), 比如:名字,地址,但是你要保證最長的能存的進去。
定長的磁盤空間比較浪費,但是效率高。
變長的磁盤空間比較節省,但是效率低。