数据类型和运算符(一)
来源:互联网 发布:混合云 数据复制 编辑:程序博客网 时间:2024/05/19 19:39
Mysql支持多种数据类型:包括数值类、日期/日期类型、字符串类型
数值数据类型:整数类型 tinyint smallint mediumint int bigint 、浮点小数类型 float double 、定点小数类型 decimal
日期/时间类型:year time date datetime timestamp
字符串类型: char varchar binary varbinary blob text enum set
整数类型的属性字段可以添加auto_increment自增约束条件
mysql中整数的数据类型的存储大小为:
类型名字 存储需求tinyint 1个字节smallint 2个字节mediumint 3个字节int(integer) 4个字节bigint 8个字节
tinyint的最大值(无符号)2^8-1
mysql> desc tb8;+-------+---------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| id | int(11) | NO | PRI | NULL | || dp | int(11) | YES | MUL | NULL | |+-------+---------+------+-----+---------+-------+
上面的int(11)里面的11代表的是该数据类型指定的显示的宽度,指定讷讷够显示的数值中数字的个数。
如 year int(4)表示的是在year字段中的数据一般只显示4位数字的宽度
在这里要注意的是::显示宽度和数据类型的取值范围是无关的。显示宽度只是指明mysql最大可能显示的数字个数,数值的位数小于指定的宽度时会由空格来填充;如果插入了大于显示宽度的值,只要这个值不超过该类型整数的取值范围,数值仍然可以显示出来。
如果不指定数据类型,系统会为整数类型指定默认的宽度
mysql> create table tb7 -> ( -> a tinyint, -> b smallint, -> c mediumint, -> d int, -> e bigint -> );Query OK, 0 rows affected (0.13 sec)mysql> desc tb7;+-------+--------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+--------------+------+-----+---------+-------+| a | tinyint(4) | YES | | NULL | || b | smallint(6) | YES | | NULL | || c | mediumint(9) | YES | | NULL | || d | int(11) | YES | | NULL | || e | bigint(20) | YES | | NULL | |+-------+--------------+------+-----+---------+-------+5 rows in set (0.01 sec)
这里的tinyint为什么是4呢?因为要显示的数据是从-128到127的,加上-号就是4位来显示
类型名称 存储需求float 4个字节double 8个字节decimal(M,D),dec M+2个字节
其中M称为精度,表示总共的位数,N称为标度,表示小数的位数
不论是定点还是浮点,如果用户指定的精度超出精度范围,就会四舍五入进行处理
mysql> create table tb9 -> ( -> x float(5,1), -> y double(5,1), -> z decimal(5,1) -> );Query OK, 0 rows affected (0.05 sec)mysql> insert into tb9 values(5.12,5.15,5.123);Query OK, 1 row affected, 1 warning (0.22 sec)mysql> show warnings;+-------+------+----------------------------------------+| Level | Code | Message |+-------+------+----------------------------------------+| Note | 1265 | Data truncated for column 'z' at row 1 |+-------+------+----------------------------------------+1 row in set (0.00 sec)mysql> select * from tb9;+------+------+------+| x | y | z |+------+------+------+| 5.1 | 5.2 | 5.1 |+------+------+------+1 row in set (0.05 sec)
float和double在不指定精度的时候,默认会按照实际的精度,decimal如果不指定精度会默认为(10,0)
日期与时间类型
类型名称 日期范围 存储需求(字节)year 1901-2155 1time -838:59:59~838:59:59 3date 1000-01-01~9999-12-3 3datetime 1000-01-01 00:00:00~9999-12-31 23:59:59 8
year:
以2位字符串格式表示的year,范围从00到99,00到69表示2000-2069,70-99表示1970-1999, 0与00相同,插入超过取值范围的值将会被转换为2000
以2位数字表示的year,范围为1—99(和上面的相同),但是0在这里代表的却是0000,而不是2000
mysql> create table tb10 -> ( -> y year -> );Query OK, 0 rows affected (0.08 sec)mysql> insert into tb10 values(2010);Query OK, 1 row affected (0.03 sec)mysql> select * from tb10;+------+| y |+------+| 2010 |+------+1 row in set (0.00 sec)mysql> insert into tb10 values('2010');Query OK, 1 row affected (0.03 sec)mysql> insert into tb10 values(2156);ERROR 1264 (22003): Out of range value for column 'y' at row
下面是一些需要主要的输入
mysql> insert into tb10 values(0),('0'),('00'),('12'),('99');Query OK, 5 rows affected (0.05 sec)Records: 5 Duplicates: 0 Warnings: 0mysql> select * from tb10;+------+| y |+------+| 2010 || 2010 || 0000 || 2000 || 2000 || 2012 || 1999 |+------+//这里要注意当输入数字0,字符0,数字和字符1-99的情况
time
需要3个字节,格式:hh:mm:ss 范围 -838:59:59—–838:59:59
指定格式:
1 D HH:MM:SS D可以取0-34之间的值
2 HHMMSS 要求是这个时间要有意义 129912没有意义
mysql> create table tb11 -> ( -> t time -> );Query OK, 0 rows affected (0.06 sec)mysql> insert into tb11 values('10:05:05'),('23:23'),('2 10:10') -> ,('3 02'),('12');Query OK, 5 rows affected (0.05 sec)Records: 5 Duplicates: 0 Warnings: 0mysql> select * from tb11;+----------+| t |+----------+| 10:05:05 || 23:23:00 || 58:10:00 || 74:00:00 || 00:00:12 |+----------+5 rows in set (0.00 sec)
在使用’D HH‘格式的时候,小时一定要使用双位数值,如果是小于10的要在前面加上0
向表中插入系统当前时间
mysql> insert into tb11 values(current_time),(now());Query OK, 2 rows affected (0.08 sec)Records: 2 Duplicates: 0 Warnings: 0//两者都是插入系统当前时间,是相同的
date
mysql> create table tb12 -> ( -> d date -> );Query OK, 0 rows affected (0.06 sec)mysql> insert into tb12 values('1994-02-28'),('20001010');Query OK, 2 rows affected (0.02 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> select * from tb12;+------------+| d |+------------+| 1994-02-28 || 2000-10-10 |+------------+2 rows in set (0.00 sec)
向表中插入系统当前时间
mysql> insert into tb12 values(current_date),(now());Query OK, 2 rows affected, 1 warning (0.01 sec)Records: 2 Duplicates: 0 Warnings: 1mysql> select * from tb12;+------------+| d |+------------+| 1994-02-28 || 2000-10-10 || 2015-07-08 || 2015-07-08 |+------------+4 rows in set (0.00 sec)
current_date只是返回当前日期值,不包括时间部分,但是now()函数返回日期和时间值,在保存到数据库的时候,只是保留了日期部分
mysql允许不严格的语法:任何标点符号都可以用作日期部分之间的分隔符。 例如‘98-11-31’‘98.11.31’‘98@11@31’这些都是等价的
datetime以及timestamp暂时不考虑
- 数据类型和运算符(一)
- 数据类型和运算符(一)
- 数据类型和运算符(一)
- C语言:(一)数据类型、运算符和表达式
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- 数据类型和运算符
- SQL 合并两个结果集 一个倒序,一个正序
- 用mysql编译:Java Web开发实战经典(基础篇)课后题答案 060501_〖第05章:JSP基础语法〗_第01题
- POJ1113(Wall) && Hdu1392(Surround the Trees) (计算几何--凸包)
- mysql相关时间函数
- /dev/fb0入门练习(linux FrameBuffer)
- 数据类型和运算符(一)
- [LeetCode]String to Integer (atoi)
- tomcat远程调试
- Android底部Tab页基于ViewPager的实现
- cout的输出格式初探
- 软件开发模型
- 爱奇艺算法工程师面试题(2015年)
- Activity
- OC 面试中的消息机制:KVO,NSNotification,Delegate,Protocol