前文-mysql权限
Mysql数据类型
Int 类型
有无符号
在项目中使用 BIGINT,而且是有符号的。
演示:
create table test_unsigned(a int unsigned, b int unsigned);
insert into test_unsigned values(1, 2);
select b - a from test_unsigned;
select a - b from test_unsigned; #运行出错
INT(N)是什么?
演示
create table test_int_n(a int(4) zerofill);
insert into test_int_n values(1);
insert into test_int_n values(123456);
- int(N)中的 N 是显示宽度, 不表示 存储的数字的 长度 的上限。
- zerofill 表示当存储的数字 长度 < N 时,用 数字 0 填充左边,直至补满长度 N
- 当存储数字的长度 超过 N 时 ,按照 实际存储 的数字显示
自动增长的面试题
这列语法有错误吗?
create table test_auto_increment(a int auto_increment);
应该要加上 主键(只有主键才允许自增长)
create table test_auto_increment(a int auto_increment primary key);
以下结果是什么?
insert into test_auto_increment values(NULL);
insert into test_auto_increment values(0);
insert into test_auto_increment values(-1);
insert into test_auto_increment values(null),(100),(null),(10),(null)
字符类型
排序规则
select 'a' = 'A';
create table test_ci (a varchar(10), key(a));
insert into test_ci values('a');
insert into test_ci values('A');
select * from test_ci where a = 'a'; #结果是什么?
set names utf8mb4 collate utf8mb4_bin
时间类型
# datatime 与 timestamp 区别
create table test_time(a timestamp, b datetime);
insert into test_time values (now(), now());
select * from test_time; select @@time_zone;
set time_zone='+00:00'; select * from test_time;
timestamp 会有一个时区的概念