-- 创建用户
create user "jestc"@"localhost" identified by "123456";
create user "jestc"@"localhost" identified by "123456";
-- 修改密码
set password for "jestc"@"localhost" = PASSWORD("jestc");
set password for "jestc"@"localhost" = PASSWORD("jestc");
-- 授权操作
grant update, insert, select, create on *.* to "jestc"@"localhost";
grant update, insert, select, create on *.* to "jestc"@"localhost";
-- 登录数据库
mysql -u jestc -p
mysql -u jestc -p
-- 查看数据库
show databases;
show databases;
![](https://i-blog.csdnimg.cn/blog_migrate/2a46161e73827fb8fc94be5d7bec6a4f.jpeg)
-- 创建数据库
create database xxxxxx;
create database xxxxxx;
-- 删除数据库
drop database xxxxxx;
drop database xxxxxx;
-- 使用数据库
use mysql;
use mysql;
![](https://i-blog.csdnimg.cn/blog_migrate/eeaaf82c06630393209eece5e1420d9d.jpeg)
-- 查看表
show tables;
show tables;
-- 创建一张表
create table student(
id int unsigned not null primary key auto_increment,
name varchar(10) not null,
age tinyint not null
)charset utf8;
create table student(
id int unsigned not null primary key auto_increment,
name varchar(10) not null,
age tinyint not null
)charset utf8;
-- 查看表结构
describe student;
describe student;
![](https://i-blog.csdnimg.cn/blog_migrate/c4ac0f1c2218e9dc016390c4e29aabb3.jpeg)
-- 修改表结构
alter table student add birthday date;
alter table student add tel char(11) default "-";
alter table drop id;
alter table student add id int unsigned not null auto_increment primary key first;
alter table student rename stu;
alter table student add birthday date;
alter table student add tel char(11) default "-";
alter table drop id;
alter table student add id int unsigned not null auto_increment primary key first;
alter table student rename stu;
-- 插入数据
insert into student (id, name, age, birthday, tel) values (1, "张三", 20, "1997-1-1", "12345678900");
insert into student (name, age, birthday) values ("李四", 21, "1997-1-2");
insert into student values
(3, "王五", 22, "1997-1-3", "12345678902"),
(4, "aa", 23, "1997-1-4", "12345678903");
-- 删除表
drop table stu;
drop table stu;
-- 查询数据
select * from student;
select * from student;
![](https://i-blog.csdnimg.cn/blog_migrate/0932969534bf06594a40094b44fe0694.jpeg)
select name from student where age > 21;
![](https://i-blog.csdnimg.cn/blog_migrate/0f51f166ce45e5b787c39b4ecee39851.jpeg)
-- 修改数据
update stu set tel = "11111111111" where id = 2;
-- 删除数据
delete from stu where id = 4;
delete from stu where age = 20;
delete from stu where id = 4;
delete from stu where age = 20;
针对表中有关联的修改遇到意外情况无法恢复的问题,引进事务这一概念。
-- 新建一张表
create table my_account(
num char(16) not null unique,
name varchar(8) not null,
money decimal(10,2) default 0.00
)charset utf8;
create table my_account(
num char(16) not null unique,
name varchar(8) not null,
money decimal(10,2) default 0.00
)charset utf8;
![](https://i-blog.csdnimg.cn/blog_migrate/ee83e7c66e192af57ede5d88016c16b1.jpeg)
-- 插入数据
insert into my_account values
("0000000000000001","张三",1000.00),
("0000000000000002","李四",2000.00);
insert into my_account values
("0000000000000001","张三",1000.00),
("0000000000000002","李四",2000.00);
![](https://i-blog.csdnimg.cn/blog_migrate/3ad702e8eb8e9c5a0dfd3cef40ad4e05.jpeg)
-- 修改
alter table my_account add id int unsigned auto_increment primary key first;
alter table my_account add id int unsigned auto_increment primary key first;
![](https://i-blog.csdnimg.cn/blog_migrate/79275076ba790aa52d1cc3e87d92c6ec.jpeg)
-- 操作
update my_account set money = money - 1000 where id = 2; -- 扣除1000
update my_account set money = money - 1000 where id = 2; -- 扣除1000
![](https://i-blog.csdnimg.cn/blog_migrate/afb6fdb07cf043e71cd6c14c9d6e04fc.jpeg)
-- 断电(退出),出现问题!
-- 事务操作 1、自动提交 2、手动提交
-- 开启事务
start transaction;
start transaction;
![](https://i-blog.csdnimg.cn/blog_migrate/f1a8e0b6775d1cf675743435ab95febd.jpeg)
-- 一系列操作
update my_account set money = money - 1000 where id = 2; -- 扣除1000
update my_account set money = money + 1000 where id = 1; -- 增加1000
update my_account set money = money - 1000 where id = 2; -- 扣除1000
update my_account set money = money + 1000 where id = 1; -- 增加1000
![](https://i-blog.csdnimg.cn/blog_migrate/3e954379e3e8ff813749d7d0495883ce.jpeg)
此时通过打开cmd运行,查看
![](https://i-blog.csdnimg.cn/blog_migrate/61e8862203f793c5acb05bcad684f550.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/68b451a1715f72223d539ebe35077a2f.jpeg)
-- 提交结束
commit;
-- 回滚到初始状态
rollback;
rollback;
![](https://i-blog.csdnimg.cn/blog_migrate/29c82903502083db5fc22f9c1f4411f5.jpeg)
-- 回滚点
-- 开启事务
start transaction;
-- 开启事务
start transaction;
-- 一系列操作
update my_account set money = money - 500 where id = 1;
update my_account set money = money - 500 where id = 1;
-- 设置回滚点
savepoint sp1; -- 设置回滚点名字
savepoint sp1; -- 设置回滚点名字
![](https://i-blog.csdnimg.cn/blog_migrate/de0def98cef5ca6ce74a670e9d2e8f73.jpeg)
update my_account set money = money + 500 where id = 2;
-- 操作完发现上一条错误,回滚
rollback to sp1;
rollback to sp1;