存儲引擎決定了表的類型,而表內存放的數據也要有不一樣的類型,每種數據類型都有本身的寬度,但寬度是可選的html
詳細參考:mysql
- http://www.runoob.com/mysql/mysql-data-types.html
- http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html
mysql經常使用數據類型概覽sql
#1. 數字: 整型:tinyinit int bigint 小數: float :在位數比較短的狀況下不精準 double :在位數比較長的狀況下不精準 0.000001230123123123 存成:0.000001230000 decimal:(若是用小數,則用推薦使用decimal) 精準 內部原理是以字符串形式去存 #2. 字符串: char(10):簡單粗暴,浪費空間,存取速度快 root存成root000000 varchar:精準,節省空間,存取速度慢 sql優化:建立表時,定長的類型往前放,變長的日後放 好比性別 好比地址或描述信息 >255個字符,超了就把文件路徑存放到數據庫中。 好比圖片,視頻等找一個文件服務器,數據庫中只存路徑或url。 #3. 時間類型: 最經常使用:datetime #4. 枚舉類型與集合類型
enum('male','female')
set('play','music','read','study')
一、整數類型
整數類型:TINYINT SMALLINT MEDIUMINT INT BIGINT數據庫
做用:存儲年齡,等級,id,各類號碼等服務器
規則: ======================================== tinyint[(m)] [unsigned] [zerofill] 小整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -128 ~ 127 無符號: 0 ~ 255 PS: MySQL中無布爾值,使用tinyint(1)構造。 ======================================== int[(m)][unsigned][zerofill] 整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -2147483648 ~ 2147483647 無符號: 0 ~ 4294967295 ======================================== bigint[(m)][unsigned][zerofill] 大整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -9223372036854775808 ~ 9223372036854775807 無符號: 0 ~ 18446744073709551615 ========================================
驗證:ide
=========有符號和無符號tinyint========== #tinyint默認爲有符號 MariaDB [db1]> create table t1(x tinyint); #默認爲有符號,即數字前有正負號 MariaDB [db1]> desc t1; MariaDB [db1]> insert into t1 values -> (-129), -> (-128), -> (127), -> (128); MariaDB [db1]> select * from t1; +------+ | x | +------+ | -128 | #-129存成了-128 | -128 | #有符號,最小值爲-128 | 127 | #有符號,最大值127 | 127 | #128存成了127 +------+ #設置無符號tinyint MariaDB [db1]> create table t2(x tinyint unsigned); MariaDB [db1]> insert into t2 values -> (-1), -> (0), -> (255), -> (256); MariaDB [db1]> select * from t2; +------+ | x | +------+ | 0 | -1存成了0 | 0 | #無符號,最小值爲0 | 255 | #無符號,最大值爲255 | 255 | #256存成了255 +------+ ============有符號和無符號int============= #int默認爲有符號 MariaDB [db1]> create table t3(x int); #默認爲有符號整數 MariaDB [db1]> insert into t3 values -> (-2147483649), -> (-2147483648), -> (2147483647), -> (2147483648); MariaDB [db1]> select * from t3; +-------------+ | x | +-------------+ | -2147483648 | #-2147483649存成了-2147483648 | -2147483648 | #有符號,最小值爲-2147483648 | 2147483647 | #有符號,最大值爲2147483647 | 2147483647 | #2147483648存成了2147483647 +-------------+ #設置無符號int MariaDB [db1]> create table t4(x int unsigned); MariaDB [db1]> insert into t4 values -> (-1), -> (0), -> (4294967295), -> (4294967296); MariaDB [db1]> select * from t4; +------------+ | x | +------------+ | 0 | #-1存成了0 | 0 | #無符號,最小值爲0 | 4294967295 | #無符號,最大值爲4294967295 | 4294967295 | #4294967296存成了4294967295 +------------+ ==============有符號和無符號bigint============= MariaDB [db1]> create table t6(x bigint); MariaDB [db1]> insert into t5 values -> (-9223372036854775809), -> (-9223372036854775808), -> (9223372036854775807), -> (9223372036854775808); MariaDB [db1]> select * from t5; +----------------------+ | x | +----------------------+ | -9223372036854775808 | | -9223372036854775808 | | 9223372036854775807 | | 9223372036854775807 | +----------------------+ MariaDB [db1]> create table t6(x bigint unsigned); MariaDB [db1]> insert into t6 values -> (-1), -> (0), -> (18446744073709551615), -> (18446744073709551616); MariaDB [db1]> select * from t6; +----------------------+ | x | +----------------------+ | 0 | | 0 | | 18446744073709551615 | | 18446744073709551615 | +----------------------+ ======用zerofill測試整數類型的顯示寬度============= MariaDB [db1]> create table t7(x int(3) zerofill); MariaDB [db1]> insert into t7 values -> (1), -> (11), -> (111), -> (1111); MariaDB [db1]> select * from t7; +------+ | x | +------+ | 001 | | 011 | | 111 | | 1111 | #超過寬度限制仍然能夠存 +------+
注意:int(3)表示的是顯示寬度,不是存儲寬度,本身沒有必要指定,其餘全部類型指定的寬度表示存儲寬度! 函數
二、浮點型
定點數類型 DEC等同於DECIMAL性能
浮點類型:FLOAT DOUBLE測試
做用:存儲薪資、身高、體重、體質參數等優化
====================================== #FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] 定義: 單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。m最大值爲255,d最大值爲30 有符號: -3.402823466E+38 to -1.175494351E-38, 1.175494351E-38 to 3.402823466E+38 無符號: 1.175494351E-38 to 3.402823466E+38 精確度: **** 隨着小數的增多,精度變得不許確 **** ====================================== #DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 定義: 雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。m最大值爲255,d最大值爲30 有符號: -1.7976931348623157E+308 to -2.2250738585072014E-308 2.2250738585072014E-308 to 1.7976931348623157E+308 無符號: 2.2250738585072014E-308 to 1.7976931348623157E+308 精確度: ****隨着小數的增多,精度比float要高,但也會變得不許確 **** ====================================== decimal[(m[,d])] [unsigned] [zerofill] 定義: 準確的小數值,m是數字總個數(負號不算),d是小數點後個數。 m最大值爲65,d最大值爲30。 精確度: **** 隨着小數的增多,精度始終準確 **** 對於精確數值計算時須要用此類型 decaimal可以存儲精確值的緣由在於其內部按照字符串存儲。
驗證:
mysql> create table t1(x float(256,31)); ERROR 1425 (42000): Too big scale 31 specified for column 'x'. Maximum is 30. mysql> create table t1(x float(256,30)); ERROR 1439 (42000): Display width out of range for column 'x' (max = 255) mysql> create table t1(x float(255,30)); #建表成功 Query OK, 0 rows affected (0.02 sec) mysql> create table t2(x double(255,30)); #建表成功 Query OK, 0 rows affected (0.02 sec) mysql> create table t3(x decimal(66,31)); ERROR 1425 (42000): Too big scale 31 specified for column 'x'. Maximum is 30. mysql> create table t3(x decimal(66,30)); ERROR 1426 (42000): Too-big precision 66 specified for 'x'. Maximum is 65. mysql> create table t3(x decimal(65,30)); #建表成功 Query OK, 0 rows affected (0.02 sec) mysql> show tables; +---------------+ | Tables_in_db1 | +---------------+ | t1 | | t2 | | t3 | +---------------+ rows in set (0.00 sec) mysql> insert into t1 values(1.1111111111111111111111111111111); #小數點後31個1 Query OK, 1 row affected (0.01 sec) mysql> insert into t2 values(1.1111111111111111111111111111111); Query OK, 1 row affected (0.00 sec) mysql> insert into t3 values(1.1111111111111111111111111111111); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> select * from t1; #隨着小數的增多,精度開始不許確 +----------------------------------+ | x | +----------------------------------+ | 1.111111164093017600000000000000 | +----------------------------------+ row in set (0.00 sec) mysql> select * from t2; #精度比float要準確點,但隨着小數的增多,一樣變得不許確 +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111200000000000000 | +----------------------------------+ row in set (0.00 sec) mysql> select * from t3; #精度始終準確,d爲30,因而只留了30位小數 +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111111111111111111 | +----------------------------------+ row in set (0.00 sec)
三、日期類型
create table student( id int, name char(6), born_year year, birth_date date, class_time time, reg_time datetime ); insert into student values (1,'egon',now(),now(),now(),now()); insert into student values (2,'alex',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12");
DATE TIME DATETIME TIMESTAMP YEAR 做用:存儲用戶註冊時間,文章發佈時間,員工入職時間,出生時間,過時時間等
YEAR YYYY(1901/2155)# 範圍 DATE YYYY-MM-DD(1000-01-01/9999-12-31) TIME HH:MM:SS('-838:59:59'/'838:59:59') DATETIME YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y) TIMESTAMP YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)
驗證:
============year=========== MariaDB [db1]> create table t10(born_year year); #不管year指定何種寬度,最後都默認是year(4) MariaDB [db1]> insert into t10 values -> (1900), -> (1901), -> (2155), -> (2156); MariaDB [db1]> select * from t10; +-----------+ | born_year | +-----------+ | 0000 | | 1901 | | 2155 | | 0000 | +-----------+ ============date,time,datetime=========== MariaDB [db1]> create table t11(d date,t time,dt datetime); MariaDB [db1]> desc t11; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | d | date | YES | | NULL | | | t | time | YES | | NULL | | | dt | datetime | YES | | NULL | | +-------+----------+------+-----+---------+-------+ MariaDB [db1]> insert into t11 values(now(),now(),now()); MariaDB [db1]> select * from t11; +------------+----------+---------------------+ | d | t | dt | +------------+----------+---------------------+ | 2017-07-25 | 16:26:54 | 2017-07-25 16:26:54 | +------------+----------+---------------------+ ============timestamp=========== MariaDB [db1]> create table t12(time timestamp); MariaDB [db1]> insert into t12 values(); MariaDB [db1]> insert into t12 values(null); MariaDB [db1]> select * from t12; +---------------------+ | time | +---------------------+ | 2017-07-25 16:29:17 | | 2017-07-25 16:30:01 | +---------------------+ ============注意啦,注意啦,注意啦=========== 1. 單獨插入時間時,須要以字符串的形式,按照對應的格式插入 2. 插入年份時,儘可能使用4位值 3. 插入兩位年份時,<=69,以20開頭,好比50, 結果2050 >=70,以19開頭,好比71,結果1971 MariaDB [db1]> create table t12(y year); MariaDB [db1]> insert into t12 values -> (50), -> (71); MariaDB [db1]> select * from t12; +------+ | y | +------+ | 2050 | | 1971 | +------+ ============綜合練習=========== MariaDB [db1]> create table student( -> id int, -> name varchar(20), -> born_year year, -> birth date, -> class_time time, -> reg_time datetime); MariaDB [db1]> insert into student values -> (1,'alex',"1995","1995-11-11","11:11:11","2017-11-11 11:11:11"), -> (2,'egon',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"), -> (3,'wsb',"1998","1998-01-01","13:13:13","2017-01-01 13:13:13"); MariaDB [db1]> select * from student; +------+------+-----------+------------+------------+---------------------+ | id | name | born_year | birth | class_time | reg_time | +------+------+-----------+------------+------------+---------------------+ | 1 | alex | 1995 | 1995-11-11 | 11:11:11 | 2017-11-11 11:11:11 | | 2 | egon | 1997 | 1997-12-12 | 12:12:12 | 2017-12-12 12:12:12 | | 3 | wsb | 1998 | 1998-01-01 | 13:13:13 | 2017-01-01 13:13:13 | +------+------+-----------+------------+------------+---------------------+
datetime與timestamp的區別
在實際應用的不少場景中,MySQL的這兩種日期類型都可以知足咱們的須要,存儲精度都爲秒,但在某些狀況下,會展示出他們各自的優劣。 下面就來總結一下兩種日期類型的區別。 1.DATETIME的日期範圍是1001——9999年,TIMESTAMP的時間範圍是1970——2038年。 2.DATETIME存儲時間與時區無關,TIMESTAMP存儲時間與時區有關,顯示的值也依賴於時區。在mysql服務器, 操做系統以及客戶端鏈接都有時區的設置。 3.DATETIME使用8字節的存儲空間,TIMESTAMP的存儲空間爲4字節。所以,TIMESTAMP比DATETIME的空間利用率更高。 4.DATETIME的默認值爲null;TIMESTAMP的字段默認不爲空(not null),默認值爲當前時間(CURRENT_TIMESTAMP), 若是不作特殊處理,而且update語句中沒有指定該列的更新值,則默認更新爲當前時間。
四、字符類型
#官網:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括號內的參數指的都是字符的長度 #char類型:定長,簡單粗暴,浪費空間,存取速度快 字符長度範圍:0-255(一箇中文是一個字符,是utf8編碼的3個字節) 存儲: 存儲char類型的值時,會往右填充空格來知足長度 例如:指定長度爲10,存>10個字符則報錯,存<10個字符則用空格填充直到湊夠10個字符存儲 檢索: 在檢索或者說查詢時,查出的結果會自動刪除尾部的空格,除非咱們打開pad_char_to_full_length SQL模式(SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH';) #varchar類型:變長,精準,節省空間,存取速度慢 字符長度範圍:0-65535(若是大於21845會提示用其餘類型 。mysql行最大限制爲65535字節,字符編碼爲utf-8:https://dev.mysql.com/doc/refman/5.7/en/column-count-limit.html) 存儲: varchar類型存儲數據的真實內容,不會用空格填充,若是'ab ',尾部的空格也會被存起來 強調:varchar類型會在真實數據前加1-2Bytes的前綴,該前綴用來表示真實數據的bytes字節數(1-2Bytes最大表示65535個數字,正好符合mysql對row的最大字節限制,即已經足夠使用) 若是真實的數據<255bytes則須要1Bytes的前綴(1Bytes=8bit 2**8最大表示的數字爲255) 若是真實的數據>255bytes則須要2Bytes的前綴(2Bytes=16bit 2**16最大表示的數字爲65535) 檢索: 尾部有空格會保存下來,在檢索或者說查詢時,也會正常顯示包含空格在內的內容
char:定長 varchar:變長 #寬度指的是字符的個數 create table t13(name char(5)); create table t14(name varchar(5)); insert into t13 values('李傑 '); #'李傑 ' insert into t14 values('李傑 '); #'李傑 ' select char_length(name) from t13; #5 select char_length(name) from t14; #3 select name from t13 where name='李傑'; select name from t13 where name like '李傑'; name char(5) egon |alex |wxx | name varchar(5) 1bytes+egon|1bytes+alex|1bytes+wxx| 4+egon|4+alex|3+wxx|
測試前瞭解兩個函數:
length:查看字節數
char_length:查看字符數
1. char填充空格來知足固定長度,可是在查詢時卻會很不要臉地刪除尾部的空格(裝做本身好像沒有浪費過空間同樣),而後修改sql_mode讓其現出原形
mysql> create table t1(x char(5),y varchar(5)); Query OK, 0 rows affected (0.26 sec) #char存5個字符,而varchar存4個字符 mysql> insert into t1 values('你瞅啥 ','你瞅啥 '); Query OK, 1 row affected (0.05 sec) mysql> SET sql_mode=''; Query OK, 0 rows affected, 1 warning (0.00 sec) #在檢索時char很不要臉地將本身浪費的2個字符給刪掉了,裝的好像本身沒浪費過空間同樣,而varchar很老實,存了多少,就顯示多少 mysql> select x,char_length(x),y,char_length(y) from t1; +-----------+----------------+------------+----------------+ | x | char_length(x) | y | char_length(y) | +-----------+----------------+------------+----------------+ | 你瞅啥 | 3 | 你瞅啥 | 4 | +-----------+----------------+------------+----------------+ row in set (0.00 sec) #略施小計,讓char現出原形 mysql> SET sql_mode = 'PAD_CHAR_TO_FULL_LENGTH'; Query OK, 0 rows affected (0.00 sec) #這下子char原形畢露了...... mysql> select x,char_length(x),y,char_length(y) from t1; +-------------+----------------+------------+----------------+ | x | char_length(x) | y | char_length(y) | +-------------+----------------+------------+----------------+ | 你瞅啥 | 5 | 你瞅啥 | 4 | +-------------+----------------+------------+----------------+ row in set (0.00 sec) #char類型:3箇中文字符+2個空格=11Bytes #varchar類型:3箇中文字符+1個空格=10Bytes mysql> select x,length(x),y,length(y) from t1; +-------------+-----------+------------+-----------+ | x | length(x) | y | length(y) | +-------------+-----------+------------+-----------+ | 你瞅啥 | 11 | 你瞅啥 | 10 | +-------------+-----------+------------+-----------+ row in set (0.00 sec)
2. 雖然 CHAR 和 VARCHAR 的存儲方式不太相同,可是對於兩個字符串的比較,都只比 較其值,忽略 CHAR 值存在的右填充,
即便將 SQL _MODE 設置爲 PAD_CHAR_TO_FULL_ LENGTH 也同樣,,但這不適用於like
Values in CHAR and VARCHAR columns are sorted and compared according to the character set collation assigned to the column. All MySQL collations are of type PAD SPACE. This means that all CHAR, VARCHAR, and TEXT values are compared without regard to any trailing spaces.
「Comparison」 in this context does not include the LIKE pattern-matching operator, for which trailing spaces are significant. For example: mysql> CREATE TABLE names (myname CHAR(10)); Query OK, 0 rows affected (0.03 sec) mysql> INSERT INTO names VALUES ('Monty'); Query OK, 1 row affected (0.00 sec) mysql> SELECT myname = 'Monty', myname = 'Monty ' FROM names; +------------------+--------------------+ | myname = 'Monty' | myname = 'Monty ' | +------------------+--------------------+ | 1 | 1 | +------------------+--------------------+ row in set (0.00 sec) mysql> SELECT myname LIKE 'Monty', myname LIKE 'Monty ' FROM names; +---------------------+-----------------------+ | myname LIKE 'Monty' | myname LIKE 'Monty ' | +---------------------+-----------------------+ | 1 | 0 | +---------------------+-----------------------+ row in set (0.00 sec)
3. 總結
#經常使用字符串系列:char與varchar 注:雖然varchar使用起來較爲靈活,可是從整個系統的性能角度來講,char數據類型的處理速度更快,有時甚至能夠超出varchar處理速度的50%。
所以,用戶在設計數據庫時應當綜合考慮各方面的因素,以求達到最佳的平衡 #其餘字符串系列(效率:char>varchar>text) TEXT系列 TINYTEXT TEXT MEDIUMTEXT LONGTEXT BLOB 系列 TINYBLOB BLOB MEDIUMBLOB LONGBLOB BINARY系列 BINARY VARBINARY text:text數據類型用於保存變長的大字符串,能夠組多到65535 (2**16 − 1)個字符。 mediumtext:A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. longtext:A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters.
枚舉類型與集合類型
字段的值只能在給定範圍中選擇,如單選框,多選框
enum 單選 只能在給定的範圍內選一個值,如性別 sex 男male/女female
set 多選 在給定的範圍內能夠選擇一個或一個以上的值(愛好1,愛好2,愛好3...)
MariaDB [db1]> create table consumer( -> name varchar(50), -> sex enum('male','female'), -> level enum('vip1','vip2','vip3','vip4','vip5'), #在指定範圍內,多選一 -> hobby set('play','music','read','study') #在指定範圍內,多選多 -> ); MariaDB [db1]> insert into consumer values -> ('egon','male','vip5','read,study'), -> ('alex','female','vip1','girl'); MariaDB [db1]> select * from consumer; +------+--------+-------+------------+ | name | sex | level | hobby | +------+--------+-------+------------+ | egon | male | vip5 | read,study | | alex | female | vip1 | | +------+--------+-------+------------+