#MySQL 数据类型精讲
#1、关于属性:character set name
show VARIABLES LIKE 'character_%';
#创建数据库时指明字符集
CREATE DATABASE IF NOT EXISTS dbtest12 CHARACTER SET 'utf8';
SHOW CREATE DATABASE dbtest12;
#创建表的时候,指明表的字符集
CREATE TABLE temp(
id INT
)CHARACTER SET 'utf8';
SHOW CREATE TABLE temp;
#创建表的字段时,可以指定字段的字符集
CREATE TABLE temp1(
id INT,
NAME VARCHAR(15) CHARACTER SET 'gbk'
);
SHOW CREATE TABLE temp1;
#2、整数型类型
USE dbtest12;
CREATE TABLE test_int1(
f1 TINYINT,
f2 SMALLINT,
f3 MEDIUMINT,
f4 INTEGER,
f5 BIGINT
);
DESC test_int1;
INSERT INTO test_int1(f1)
VALUES(12),(-12),(-128),(127);
SELECT * FROM test_int1;
#MySQL5.0版本
CREATE TABLE test_int2(
f1 INT,
f2 INT(5),
#① 显示宽度为5.当insert的值不足5位时,使用0填充。
#②当使用zerofill时,自动会添加UNSIGNED
f3 INT(5) ZEROFILL
);
#3、浮点类型
CREATE TABLE test_double1(
f1 FLOAT,
f2 FLOAT(5,2),
f3 DOUBLE,
f4 DOUBLE(5,2)
);
DESC test_double1;
INSERT INTO test_double1(f1,f2)
VALUES(123.45,123.45);
SELECT * FROM test_double1;
INSERT INTO test_double1(f3,f4)
VALUES(123.45,123.456);#存在四舍五入,但是整数位不行
#定点数类型
#比浮点类型更精准
CREATE TABLE test_decimall(
f1 DECIMAL,
f2 DECIMAL(5,2)
);
DESC test_decimall;
INSERT INTO test_decimall(f1)
VALUES (123),(123.45);
SELECT * FROM test_decimall;
INSERT INTO test_decimall(f2)
VALUES (67.567);#存在四舍五入
#Out of range value for column 'f2' at row 1
INSERT INTO test_decimall(f2)
VALUES (999.995);
# 位类型
CREATE TABLE test_bit1(
f1 BIT,
f2 BIT(5),
f3 BIT(64)
);
DESC test_bit1;
INSERT INTO test_bit1(f1)
VALUES(0),(1);
SELECT * FROM test_bit1;
INSERT INTO test_bit1(f2)
VALUES(31);
SELECT f1+0,f2+0
FROM test_bit1;
CREATE TABLE test_year(
f1 YEAR,
f2 year(4)
);
DESC test_year;
INSERT INTO test_year(f1)
VALUES('2021'),(2022);
SELECT * FROM test_year;
#Out of range value for column 'f1' at row 1
INSERT INTO test_year(f1)
VALUES ('2156');
INSERT INTO test_year(f1)
VALUES ('69'),('70');
INSERT INTO test_year(f1)
VALUES(0),('00');
CREATE TABLE test_date1(
f1 DATE
);
DESC test_date1;
INSERT INTO test_date1
VALUES('2020-10-01'),('20201001'),(20201001);
INSERT INTO test_date1
VALUES('00-10-01'),('001001'),('69-10-01'),('691001'),('70-10-01'),('701001');
INSERT INTO test_date1
VALUES(CURDATE()),(CURRENT_DATE()),(NOW());
SELECT * FROM test_date1;
CREATE TABLE test_time1(
f1 TIME
);
DESC test_time1;
INSERT INTO test_time1
values('2 12:30:29'),('12:35:29'),('12:40'),('2 12:40'),('1 05'),('45');
INSERT INTO test_time1
values('123520'),(124011),(1210);
SELECT * FROM test_time1;
CREATE TABLE test_datetime1(
dt DATETIME
);
DESC test_datetime1;
INSERT INTO test_datetime1
VALUES('2021-01-01 06:50:30'),('20210101065030');
INSERT INTO test_datetime1
VALUES('99-01-01 00:00:00'),('20-01-01 00:00:00');
INSERT INTO test_datetime1
VALUES(20200101000000),(200101000000),(19990101000000),(990101000000);
SELECT * FROM test_datetime1;
CREATE TABLE test_timestamp1(
ts TIMESTAMP
);
DESC test_timestamp1;
INSERT INTO test_timestamp1
VALUES('1999-01-01 03:04:50'),('19990101030405'),('99-01-01 03:04:05'),('990101030405');
INSERT INTO test_timestamp1
VALUES('2020@01@01@00@00@00'),('20@01@01@00@00@00');
SELECT * FROM test_timestamp1;
对比DATETIME 和 TIMESTAMP
CREATE TABLE temp_time(
d1 DATETIME,
d2 TIMESTAMP
);
INSERT INTO temp_time VALUES('2021-9-2 14:45:52','2021-9-2 14:45:52');
INSERT INTO temp_time VALUES(NOW(),NOW());
SELECT * FROM temp_time;
#修改当前的时区
SET time_zone = '+9:00';
SELECT * FROM temp_time;
CREATE TABLE test_char1(
c1 CHAR,
c2 CHAR(5)
);
DESC test_char1;
INSERT INTO test_char1(c1)
VALUES('a');
INSERT INTO test_char1(c2)
VALUES('hello');
SELECT * FROM test_char1;
VARCHAR
#错误: Column length too big for column 'NAME' (max = 21845); use BLOB or TEXT instead
CREATE TABLE test_varchar2(
NAME VARCHAR(65535)
);
CREATE TABLE test_varchar3(
NAME VARCHAR(5)
);
INSERT INTO test_varchar3
VALUES('地对地导弹');
SELECT * FROM test_varchar3;
CREATE TABLE test_text(
tx TEXT
);
INSERT INTO test_text
VALUES('hahahah ');
SELECT CHAR_LENGTH(tx)
FROM test_text; #10
CREATE TABLE test_enum(
season ENUM('春','夏','秋','冬','unknow')
);
INSERT INTO test_enum
VALUES('春'),('秋');
SELECT * FROM test_enum;
#Data truncated for column 'season' at row 1
INSERT INTO test_enum
VALUES('春,秋');
INSERT INTO test_enum
VALUES('UNKNOW'); #忽略大小写
#也可以使用索引进行枚举元素的调用
INSERT INTO test_enum
VALUES(1),('3'); # 春 秋
#没有限制非空的情况下,可以添加null值
INSERT INTO test_enum
VALUES(NULL);
SET类型
CREATE TABLE test_set(
s SET ('A','B','C')
);
INSERT INTO test_set (s) VALUES ('A'),('A,B');
#插入重复的SET类型成员时,MySQL会自动删除重复的成员
INSERT INTO test_set (s) VALUES ('A,B,C,A');
#向SET类型的字段插入SET成员中不存在的值时,MySQL会抛出错误
INSERT INTO test_set (s) VALUES ('A,B,C,D');
SELECT * FROM test_set;
CREATE TABLE temp_mul(
gender ENUM('男','女'),
hobby SET('吃饭','睡觉','打豆豆','写代码')
);
SELECT * FROM temp_mul;
INSERT INTO temp_mul
VALUES('男','睡觉,打豆豆');
#Data truncated for column 'gender' at row 1
INSERT INTO temp_mul
VALUES('男,女','睡觉,打豆豆');
二进制字符串类型和JSON类型讲解
BINARY与VARBINARY类型
CREATE TABLE test_binary1(
f1 BINARY,
f2 BINARY(3),
#f3 VARBINARY,
f4 VARBINARY(10)
);
DESC test_binary1;
INSERT INTO test_binary1(f1,f2)
VALUES('a','abc');
SELECT * FROM test_binary1;
#Data too long for column 'f1' at row 1
INSERT INTO test_binary1(f1)
VALUES('ab');
INSERT INTO test_binary1(f2,f4)
VALUES('ab','ab');
SELECT LENGTH(f2),LENGTH(f4)
FROM test_binary1;
CREATE TABLE test_blob1(
id INT,
img MEDIUMBLOB
);
INSERT INTO test_blob1(id)
VALUES(1001);
SELECT * FROM test_blob1;
CREATE TABLE test_json(
js json
);
INSERT INTO test_json(js)
VALUES ('{"name":"songhk","age":18,"address":{"province":"beijing","city":"beijing"}}');
SELECT * FROM test_json;
SELECT js ->'$.name' AS NAME,js -> '$.age' AS age , js -> '$.address.province' AS province, js -> '$.address.city' AS city
FROM test_json;