You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
show schemas; -- schemas 同 database
show databases;
select database();
createdatabaseif not exists `java_basic` charset utf8mb4;
use `java_basic`;
show create database `java_basic`;
dropdatabase if exists `java_basic`;
use `java_basic`;
show tables;
createtableif not exists `student`
(
`id`int auto_increment primary keynot null comment '编号',
`name`varchar(32) not null comment '姓名',
`gender` tinyint unsigned default 0not null comment '性别'
) comment '学生表';
desc`student`;
show create table `student`;
droptable if exists `student`;
类型
大小
描述
tinyint
1
极小整形
smallint
2
小整形
mediumint
3
中整形
int / integer
4
大整形
bigint
8
极大整形
float
4
单精度浮点数
double
8
双精度浮点数
decimal
取决于精度(M)和标度(D)
精确定点数
[unsigned]
-
数值后可加unsigned
char
0-255
定长字符串
varchar
0-65535
变长字符串
tinyblob
0-255
短二进制数据
tinytext
0-255
短文本
blob
0-65535
二进制数据
text
0-65535
文本
mediumblob
0-16777215
中等二进制数据
mediumtext
0-16777215
中等文本
longblob
0-4294967295
极大二进制数据
longtext
0-4294967295
极大文本
-
-
-
date
3
YYYY-MM-DD
time
3
HH:MM:SS
year
1
YYYY
datetime
8
YYYY-MM-DD HH:MM:SS
timestamp
4
YYYY-MM-DD HH:MM:SS
use `java_basic`;
altertable`student`
add `age` tinyint unsigned comment '年龄';
desc`student`;
altertable`student`
modify gender tinyint unsigned comment '性别';
desc`student`;
altertable`student`
change gender sex tinyint unsigned not null comment '性别';
desc`student`;
altertable`student`
drop age;
desc`student`;
altertable`student` rename to tb_student;
show tables;
truncate table `tb_student`;
show tables;
droptable if exists `tb_student`;
show tables;
-- 准备测试数据dropdatabase if exists `java_basic`;
createdatabaseif not exists `java_basic` charset utf8mb4;
use `java_basic`;
createtableif not exists `student`
(
`id`int auto_increment primary keynot null comment '编号',
`name`varchar(32) not null comment '姓名',
`gender` tinyint unsigned not null comment '性别',
`age` tinyint unsigned not null comment '年龄',
`entry_time` datetime not null comment '入学时间'
) comment '学生表';
drop procedure if exists add_student;
create procedure add_student(in num int)
begin
declare
i int default 0;
while i != num
do
set i := i +1;
insert into`student` (`name`, `gender`, `age`, `entry_time`)
values (concat('student', i),
if(rand() <0.5, 1, 2),
18+ rand() *2,
date_add(now(), interval rand() *-30 day));
end while;
end;
call add_student(10);
altertable`student` rename to `stu_big`;
show tables;
select*from`student`;
use `mysql`;
select*from`user`;
createuser `java_basic`@`localhost` identified by '123456';
grant all on`java_basic`.* to `java_basic`@`localhost`;
createuser `any`@`%` identified by '123456';
select*from user;
alteruser'any'@'%' identified with mysql_native_password by '123';
dropuser'any'@'%';
dropuser'java_basic'@'localhost';
select*from user;
createuser `any`@`%` identified by '123456';
show grants for 'any'@'%';
grant all on*.* to `any`@`%`;
revoke all on*.*from`any`@`%`;