Mysql常用sql总结(SQL学习思维导图)

基本SQL语句

  1. 查看数据库:show databases;
  2. 使用数据库: use 数据库名;
  3. 创建数据库:create database 数据库名
  4. 查看所有表名: show tables;
  5. 删除数据库 drop database 数据库名
  6. 删除表 : drop table t_student
  7. 查看表结构:desc 表名
  8. 查看数据库版本:select version();
  9. 查看表索引:show index from 表名
  10. 索引的创建与使用
  11. 查看系统事物隔离级别: select @@global.tx_isolation;
  12. 查看当前会话事物隔离级别:select @@tx_isolation;
  13. 设置当前会话事物隔离级别: set session transaction isolatin level repeatable read;
  14. 设置系统当前隔离级别:set global transaction isolation level repeatable read;
  15. 开始事物: start transaction
  16. 提交事物:commit
  17. 回滚事物:rollback
  18. 常用函数
  19. 创建存储过程
create procedure 存储过程名字(参数)
BEGIN
存储过程体
END
  1. 执行存储过程:call 存储过程名(参数)
  2. 删除存储过程:DROP PROCEDURE IF EXISTS 存储过程名

创建表:

drop table if EXISTS`t_student`;
create table t_student
(
-- 设置stuid为主键,自动增加
	stuid int primary key auto_increment COMMENT  '主键',
-- 设置约束不能为空
	stuname char(20) not null COMMENT  '姓名',
	stuage int  COMMENT  '年龄',
-- 设置stuphone 唯一
	stuphone char(11) unique,
-- 设置stusex 默认为男
	stusex char(4) default '男',
-- 自动获取系统时间
    writerDate TIMESTAMP  default CURRENT_TIMESTAMP,
-- 设置外键为 t_teacher.tid
	tid int references t_teacher.tid
);
  • 创建teacher表
drop table if EXISTS`t_teacher`;
create table t_teacher
(
	tid char(20) primary key,
    tname char(20),
-- 设置联合主键
    primary key(tid, tname)
);

对表追加字段:

-- alter table 表名 add (字段名 字段类型 默认值 是否为空);
alter table t2a_cust_c add (LEGALREPNAME varchar2(120));
comment  on  column  t2a_cust_c.LEGALREPNAME   is  '代理人姓名';

修改字段

alter table 表名 modify (字段名 字段类型 默认值 是否为空);

删除字段

alter table tablename drop (column);

字段的重命名

alter table 表名 rename  column  列名 to 新列名   (其中:column是关键字)

表的重命名

alter table 表名 rename to  新表名

创建视图

DROP view IF EXISTS t_student;
create or replace  view v_studentas
select * from v2a_trans ;

添加索引

ALTER TABLE t_studentADD INDEX (`stuname `);

参考

数据库常用SQL

-- 插入
insert into t_student(stuid, stuname, stuage, stuphone, stusex, tid) values (1, '张三', 22, '15645663891', 'man', 10086)
 insert into t_student(stuid, stuname, stuage, stuphone, stusex, tid) values (3, 'ww', 22, '15689563891', 'man', 10086)
-- 删除
 delete from t_student where stuid = 3;
-- 查询
 select stuid ,stuname,stuage from t_student;
-- 修改
update t_student set stuname = '李奏' ,stuage = 16, stusex = '女' where stuid = 1;
-- 查询员工工资高于5000的员工姓名
select ename from emp where sal > 5000;

--  多个条件查询
-- 查询员工工资高于5000,同时奖金少于2W的员工
select ename from emp where sal > 5000 and comm < 20000

-- 查询员工工资高于5000,或者奖金少于2W的员工
select ename from emp where sal > 5000 or comm < 20000
-- 查询员工工资高于5000低于20000
select ename from emp where sal between 5000 and 20000

--  模糊查询
-- 找出姓张的员工,%代表后面字符数>=0
select ename from emp where ename like '张%'

-- 找出姓张的员工,但名字总共只有两个字符,_代表后面字符数 ==1
select ename from emp where ename like '张_';

--  查询出员工的年薪 (表达式查询)
select sal * 12 + comm as totalSalary from emp;

-- 查询出员工名及其工资,格式如下:“张三:$1000”
select CONCAT(ename,':$1000', sal) from emp;

-- .函数查询 :统计函数 avg(), min(), count(), max() 
-- 计算公司员工的平均工资
select avg(sal) avgSalary from emp;
select sum(sal) sumSalary from emp;
select min(sal) minSalary from emp;
select max(sal) maxSalary from emp;
select count(*) avgSalary from emp;

-- 5. 分组查询 group by 分组查询select后面只能写group by 后面的字段
-- 求出每个部门的平均工资, 总工资
select deptno, avg(sal),sum(sal) from emp group by deptno;

-- where只能接字段名(列名)
-- 而having接统计函数
-- 6. 求出每个部门的平均工资,总工资,但只显示工资超过1W的部门
select deptno, avg(sal), sum(sal) from emp group by deptno having sum(sal) > 10000;

-- 7.限制记录的条数limit
-- 查询第五条到第七条数
-- 后面两个数字代表从哪个索引开始选,选择几条
select * from emp limit 4,3; 

-- 8.排序order by asc/desc 升序,降序
select *from emp order by sal asc;
select *from emp order by sal desc;

-- 找出工资最高的前三名
select *from emp order by sal desc limit 0,3

键的六大约束

类型用法说明
主键primary key唯一的,不重复的,不能为空
非空not null不为空必须填写
唯一unique不能重复,可为空
默认default不插入,则默认为默认约束
检查check检查数据合法性
外键references将不想相关的表关联起来

思维导图

在这里插入图片描述

  • 5
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值