1 数字(默认都是有符号,宽度指的是显示宽度,与存储无关)
tinyint int bigint:个数,年龄,id,qq号,手机号
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
unsigned用法:
=========有#默认为有符号,即数字前有正负号符号和无符号==========
=========alter table t1 modify id tinyint unsigned;==========
MariaDB [db1]> create table t1(x tinyint);
zerofill用法:
======用zerofill测试整数类型的显示宽度=============
MariaDB [db1]> create table t7(x int(3) zerofill);
alter table t1 modify id tinyint unsigned zerofill;
用zerofill约束之后 在字节不足的情况下用0补全
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 | #超过宽度限制仍然可以存
+------+
!!!注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下
其实我们完全没必要为整数类型指定显示宽度,使用默认的就可以了
默认的显示宽度,都是在最大值的基础上加1
int的存储宽度是4个Bytes,即32个bit,即2**32
无符号最大值为:4294967296-1
有符号最大值:2147483648-1
有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的
最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok
定点数类型 DEC等同于DECIMAL
浮点类型:FLOAT DOUBLE
作用:存储薪资、身高、体重、体质参数等
float:价格,身高,体重,余额
======================================
decimal[(m[,d])] [unsigned] [zerofill]
准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
特别的:对于精确数值计算时需要用此类型
decaimal能够存储精确值的原因在于其内部按照字符串存储。
======================================
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。
有符号:
-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是小数点后个数。
有符号:
-1.7976931348623157E+308 to -2.2250738585072014E-308
2.2250738585072014E-308 to 1.7976931348623157E+308
无符号:
2.2250738585072014E-308 to 1.7976931348623157E+308
**** 数值越大,越不准确 ****
验证:
MariaDB [db1]> create table t8(salary float(5,2)); #总共5位,小数部分占2位,因而整数部分最多3位
MariaDB [db1]> insert into t8 values
-> (3.3),
-> (7.33),
-> (9.335),
-> (1000.1);
MariaDB [db1]> select * from t8;
+--------+
| salary |
+--------+
| 3.30 |
| 7.33 |
| 9.34 | #4舍5入
| 999.99 | #小数最多2位,整数最多3位
+--------+
2 字符(宽度指的是字符个数):姓名,性别,职业,地址,职称,介绍
char:简单粗暴,不够则凑够固定长度存放起来,浪费空间,存取速度快
varchar:精准,计算出待存放的数据的长度,节省空间,存取速度慢
2.1 char与varchar测试()
create table t6(name char(4));
insert into t6 values('alexsb');
insert into t6 values('欧德博爱');
insert into t6 values('艾利克斯a');
create table t7(x char(5),y varchar(5));
#insert into t7 values('abcdef','abc');
#insert into t7 values('abc','abc');
#insert into t7 values('abc','abcdef');
insert into t7 values('abc','abc'); #char_length :查看字符的长度
insert into t7 values('你好啊','好你妹'); #char_length :查看字符的长度
#了解
insert into t7 values('你好啊','好你妹'); #length:查看字节的长度
#注意两点:
insert into t7 values('abc ','abc '); #length:查看字节的长度
select * from t7 where y='abc '; #去掉末尾的空格然后去比较
测试前了解两个函数
length:查看字节数
char_length:查看字符数
2.2.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)
2.2.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.
3 日期
DATE TIME DATETIME TIMESTAMP YEAR
3.1:日期类型
作用:存储用户注册时间,文章发布时间,员工入职时间,出生时间,过期时间等
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 年某时)
例
create table student(
id int,
name char(5),
born_date date,
born_year year,
reg_time datetime,
class_time time
);
insert into student values(1,'alex',now(),now(),now(),now());
insert into student values(1,'alex','2017-09-06','2017','2017-09-06 10:39:00','08:30:00');
#了解
insert into student values(1,'alex','2017-09-06',2017,'2017-09-06 10:39:00','08:30:00');
insert into student values(1,'alex','2017/09/06',2017,'2017-09-06 10:39:00','08:30:00');
insert into student values(1,'alex','20170906',2017,'20170906103900','083000');
============注意啦,注意啦,注意啦===========
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 2017-09-06 10:39:49
#出生年月日,开学时间
date:2017-09-06
#聊天记录,上课时间
time:10:39:49
#出生年
year:2017
4 枚举与集合
enum枚举:规定一个范围,可有多个值,但是为该字段传值时,只能取规定范围中的一个
set集合:规定一个范围,可有多个值,但是为该字段传值时,可以取规定范围中的一个或多个
字段的值只能在给定范围中选择,如单选框,多选框
enum 单选 只能在给定的范围内选一个值,如性别 sex 男male/女female
set 多选 在给定的范围内可以选择一个或一个以上的值(爱好1,爱好2,爱好3...)
枚举类型(enum)
An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
示例:
CREATE TABLE shirts (
name VARCHAR(40),
size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);
INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');
集合类型(set)
A SET column can have a maximum of 64 distinct members.
示例:
CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
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 | |
+------+--------+-------+------------+
create table student1(
id int primary key auto_increment,
name char(5),
sex enum('male','female'),
hobbies set('music','read','study','coding')
);
insert into student1(name,sex,hobbies) values('egon','None','asdfasdfasdf');
insert into student1(name,sex,hobbies) values('egon','male','music,read');
转载于:https://www.cnblogs.com/DE_LIU/p/7485479.html