基本数据类型
int的存储宽度是4个Bytes,即32个bit,即2**32 无符号最大值为:4294967296-1 有符号最大值:2147483648-1 有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的 最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok 来自 <http://www.cnblogs.com/linhaifeng/articles/7233411.html> 3、测试:默认整型都是有符号的 create table t1(x tinyint); insert into t1 values(128),(-129); create table t2(x tinyint unsigned); insert into t2 values(-1),(256); create table t3(x int unsigned); #4294967295 insert into t3 values(4294967296); create table t4(x int(12) unsigned); insert into t4 values(4294967296123); 4,强调:对于整型来说 ,数据类型后的宽度并不是存储限制,而是显示限制 所以在创建表示,如果字段采用的是整型类型,完全无需指定显示宽度, 默认的显示宽度,足够显示完整当初存放的数据 显示时,不够8位用0填充,如果超出8位则正常显示 create table t5(x int(8) unsigned zerofill); insert into t5 values(4294967296123); insert into t5 values(1); 补充一点 # 查看sql_mode mysql> show variables like "%sql_mode%"; +----------------------------+---------------------+ | Variable_name | Value | +----------------------------+---------------------+ | binlogging_impossible_mode | IGNORE_ERROR | | block_encryption_mode | aes-128-ecb | | gtid_mode | OFF | | innodb_autoinc_lock_mode | 1 | | innodb_strict_mode | OFF | | pseudo_slave_mode | OFF | | slave_exec_mode | STRICT | | sql_mode | STRICT_TRANS_TABLES | +----------------------------+---------------------+ 8 rows in set (0.00 sec) #修改sql_mode为严格模式:在该模式下,如果插入的数据超过限制,则会立即报错 mysql> set global sql_mode="strict_trans_tables";
#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能够存储精确值的原因在于其内部按照字符串存储。
create table t9(x float(255,30)); create table t10(x double(255,30)); create table t11(x decimal(65,30)); insert into t9 values(1.111111111111111111111111111111); insert into t10 values(1.111111111111111111111111111111); insert into t11 values(1.111111111111111111111111111111); mysql> select * from t9; +----------------------------------+ | x | +----------------------------------+ | 1.111111164093017600000000000000 | +----------------------------------+ 1 row in set (0.00 sec) mysql> select * from t10; +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111200000000000000 | +----------------------------------+ 1 row in set (0.00 sec) mysql> select * from t11; +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111111111111111111 | +----------------------------------+ 1 row in set (0.00 sec)
3、测试:字符的宽度限制单位是字符个数 create table t12(x char(4)); # 超出4个字符则报错,不够4个字符则用空格补全成4个字符 create table t13(y varchar(4));# 超出4个字符则报错,不够4个字符那么字符有几个就存几个 insert into t12 values('hello'); insert into t13 values('hello'); insert into t12 values('a'); #'a ' insert into t13 values('a'); #'a' 看长度 set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH"; select char_length(x) from t12; #4 select char_length(y) from t13; #1 注意: 针对char类型,mysql在存储时会将数据用空格补全存放到硬盘中 但会在读出结果时自动去掉末尾的空格,因为末尾的空格在以下场景中是无用 mysql> select * from t14 where name="lxx"; # name字段明确地等于一个值,该值后填充空格是没有用 mysql> select * from t14 where name like "lxx"; # name字段模糊匹配一个值,该值后填充空格是有用的 对比char与varchar name char(5) # 缺点:浪费空间 # 优点:存取速度都快 egon alex lxx wxx yx name varchar(5) # 缺点:存取速度都慢 # 优点:节省空间 (1bytes+egon)(1bytes+alex)(1bytes+lxx) 官方图 推荐使用char
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 | +------+------+-----------+------------+------------+---------------------+ 在实际应用的很多场景中,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语句中没有指定该列的更新值,则默认更新为当前时间。
create table student( id int, name char(16), born_year year, birth date, class_time time, reg_time datetime ); insert into student values(1,'egon','2000','2000-01-27','08:30:00','2013-11-11 11:11:11'); http://www.cnblogs.com/linhaifeng/articles/7233411.html 日期相关了解
测试 create table teacher( id int, name char(16) sex enum('male','female','others'), hobbies set('play','read','music','piao') );