MySQL常用命令
创建数据库:
命令:create database 数据库名 [库选项];
示例:create database text3 charset utf8;
查看所有数据库:
命令:Show databases;
查看数据库格式:
命令:Show create database 数据库名;
实例:Show create database text3;
修改库选项:
命令:Alter database 数据库库名 库选项;
实例:Alter database text3 charset utf8;
修改库选项:
命令:Use 库名;
示例:Use text3;
删除数据库:
命令:drop database 数据库名;
示例:drop database text3;
新建表格:
命令:create table 表名(
列名1 数据类型,
列名2 数据类型
) ;
示例:create table student(
username varchar(10),
age int
)charset utf8;
删除表格:
命令:drop table 表名
示例:drop table student;
修改表结构:
插入(新增)字段:
命令:alter table 表名 add 新字段名 数据类型
示例:alter table student add class varchar(10);
删除字段
命令:alter table 表名 drop column 字段名
示例:alter table student drop column class
查询表内容
完整命令:Select 字段名1,字段名2 from 数据源 where 条件 group by 需要分组字段1 having 条件 order by排序 limit
联合查询
命令:Select 语句 + union (union选项) + select 语句
Union选项:与select选项基本一致
Distinct: 去重 去掉完全重复的数据 (默认的)
示例:获取男生身高升序 女生身高降序
(select * from my_student where sex = '男' order by heigh asc limit 10)
union
(select * from my_student where sex = '女' order by heigh desc limit 10);
连接查询
1.交叉连接(笛卡尔积,避免)
命令:select from 表1 cross 表2
示例:select * from my_student CROSS join my_class;
2.内连接
命令:表1 (inner) join 表2 on 匹配条件
示例:查询学生表的所有信息包含班级信息
select my_student.*,my_class.name from my_student inner join my_class on my_student.class_id = my_class.id;
3.外连接
左连接命令:主表 left join 从表 on 连接条件;
示例:select my_student.*,my_class.name from my_student LEFT JOIN my_class on my_student.class_id = my_class.id;
右连接命令:从表 right join 主表 on 连接条件;
示例:select my_student.*,my_class.name from my_student right JOIN my_class on my_student.class_id = my_class.id;
子查询
1.标量子查询
命令:select * from 数据源 where 条件判断 = /<> (select 字段名 from 数据源 where 条件判断)
示例:
知道一个学生的名字rose,得到他所在的班级名字
(1)通过学生表获取他所在班级的class_id
(2)通过班级的ID 找到对应的班级名字
select name from my_class where class_id =
(select class_id from my_student where stu_name = 'rose');
2.列子查询
命令:主查询 where 条件 in (列子查询)
示例:
想获取已有学生在班的所有班级名字
(1)找出学生表中所有的班级ID
(2)找出班级表中对应的班级名字
select name from my_class where class_id in
(select class_id from my_student);
3.行子查询
命令:主查询 where 条件 (构造行元素) = 行子查询;
示例:
求出班级上年龄最大 且身高最高的学生
(1)求出班上最大年龄值
(2)求出班级身高最高值
(3)求出对应的学生
select * from my_student where (age,heigh) =
(SELECT max(age),max(heigh) from my_student);
表子查询
命令:select 字段表 from (表子查询)as 别名 where、group by、having、order by limit;
示例:获取每个班最高的学生
1.获得每个班最高的学生降序排序 最高的在第一个 order by
2.再针对结果进行group by 保留每组第一个
select * from (select * from my_student order by heigh desc) a GROUP BY a.class_id;
exists子查询
语法:where exists (查询语句) :exists就是根据查询得到的结果进行判定,如果结果存在,那么返回1,否则返回0
示例:
想获取已有学生在班的所有班级名字
select * from my_class as c where EXISTS
(select stu_id from my_student s where s.class_id=c.class_id);
增加表中数据
命令:insert into 表名 (字段1,字段2) values (值1,值2),(值1,值2);
示例:insert into student(username, age)values('张三',16),('李四',17);
删除表中数据
命令:delete from 表名 where 字段=条件;
示例:delete from student where username='张三';
删除表
命令:drop table <表名>;
示例:drop table student;
更新表中数据
命令:update 表 set 字段1=新值, 字段2=值2 … where 列名=条件; – -仅更新符合条件的记录
示例:update student set age=20 where username='张三';
流程控制语句
1. 简单if语句
命令:if(条件,为真结果,为假结果)
示例:求学生表年龄大于20的学生
select * ,if(age>20,'符合','不符合') as judge from my_student; --as为取别名
2.复杂if语句
命令:
If 条件表达式 then
满足条件要执行的语句
Else
不满足条件要执行的语句
//如果还有其他分支
If 表达式 then
满足条件要执行的语句
End if;
End if;
while语句
命令:
While 条件 do
要循环执行的代码;
End while;
函数调用
命令:select 函数名 (参数列表)
示例:select Char_length('abcd'); -- 判断字符串的字符数
自定义函数
命令:
修改语句结束符
Create function 函数名(参数) returns 返回值类型
Begin
//函数体
End
语句结束符 $$
修改语句结束符(改回来)
示例:调用函数返回10
delimiter $$
create FUNCTION my_func1() returns int
BEGIN
return 10;
end
$$
delimiter ;
删除函数
命令:Drop function 函数名;
示例:Drop function a1;
创建过程
命令:
Create procedure 过程名字(参数列表(可以没有))
Begin
过程体
End
结束符
示例:求1-100和的存储过程
delimiter $$ create PROCEDURE my_pro4() begin DECLARE i int DEFAULT 1; --声明局部变量 给出默认值 set @sum = 0; --声明会话变量 while i<101 do --开启循环 求结果 set @sum = @sum+i; set i = i+1; end while; --结束循环 select @sum; --查询结果 END $$ delimiter ; call my_pro4();
调用过程
命令:call 过程名
示例:call my_pro4();
查看过程
Show procedure status;
删除过程
命令:Drop procedure 过程名字;
示例:Drop procedure my_pro4();
创建触发器
命令:
Create trigger 触发器名字 触发时机 触发条件 on 表 for each row
Begin
触发器内容
End
示例:商品自动扣除库存
create table my_goods( id int PRIMARY key auto_increment, name varchar(20) not null, inv int )charset utf8; create table my_orders( id int PRIMARY key auto_increment, goods_id int not null, goods_num int not null )charset utf8;
insert into my_goods VALUES (1,'手机',100),(2,'电脑',1000),(3,'ipad',500); create trigger after_insert_order_t after insert on my_orders for each row begin select inv from my_goods where id = new.goods_id into @inv; update my_goods set inv =inv-new.goods_num where id = new.goods_id; if @inv < new.goods_num then insert into xxx VALUES ('xxx'); end if; end $$ delimiter ; insert into my_orders VALUES (null,2,999); select * from my_orders; select * from my_goods;
开启事物
start transaction
提交事务
确认提交:commit 写入到表,清空事务日志
回滚操作:rollback 清空事务日志
示例:
start TRANSACTION; -- 开启事务 insert into my_class VALUES (113,'警察班'); -- 插入数据 ROLLBACK; -- 回滚操作 最终数据没有插入成功
回滚点
增加回滚点:save point 回滚点名字
回到回滚点: rollback to 回滚点名字
示例:
start TRANSACTION; -- 再次开启事务 insert into my_class VALUES (112,'警察班'); -- 插入数据 SAVEPOINT sp1; -- 增加回滚点 update my_student set class_id = 110 where stu_id = '111'; -- 出现错误步骤 修改错了人 ROLLBACK to sp1; -- 回滚到回滚点 commit; -- 提交
创建视图
命令:create view 视图名字 as select指令
示例:
create view student_class_v as select s.*,c.name from my_student as s LEFT JOIN my_class as c on s.class_id = c.class_id;
使用视图
命令:select 字段列表 from 视图名字(各种子句)
示例:select * from student_class_v where stu_name = 'rose';
desc student_class_v;
修改视图
修改视图:本质修改视图对应查询语句
命令:alter view 视图名字 as select指令;
删除视图
命令:drop view 视图名字;

浙公网安备 33010602011771号