mysql 语言 总结


数据库

显示数据库

show databases;

创建数据库

create database database_name(数据库名称);

删除数据库

drop database database_name(数据库名);

数据表

创建数据表
use 的使用:在创建表之前声明在哪个数据库

use database_name;

创建表

create table t_name(
字段名1 数据类型 约束条件,
字段名2 数据类型 约束条件,
字段名1 数据类型 约束条件(最后一个不要,)
)default charset=utf8;

主键、外键的链接(ta_1、ta_2)
创建表ta_1:

create table ta_1(
ta_1_id int(11) not null primary ,
ta_1_name varchar(50) not null
) default charset=utf8;

创建表ta_2:

create table ta_2(
ta_2_id int(11) not null primary key,
ta_2_name varchar(50) not null,
ta_2_type int(11) not null,
constraint `fk_1` foreign key(ta_2_type) references ta_1(ta_1_id)
) default charset=utf8;

约束

create table t_name(
字段名1 数据类型 约束条件,default 111(默认约束,默认111)
字段名2 数据类型 约束条件,auto_increment(自动增长)
字段名1 数据类型 约束条件,unique(唯一)
)default charset=utf8;

查看数据表

1.show tables;(查看数据表)
2.describe table_name;(查看数据表结构)
3.show create table name\G;(查看详细数据表结构语句)
使用\G参数可使表结构清楚。

修改数据表
修改表名

alter table <旧表名> rename <新表名>;

修改字段的数据类型

alter table <表名> modiey <字段名> <数据类型>;

修改字段名

alter table <表名> change <旧字段名> <新字段名> <新数据类型>;

添加字段名

alteer table <表名> add <字段名> <数据类型>
[约束条件] [first|after 已存在字段名];(添加在第一个或者哪个之后)

删除字段

alter table <表> drop <字段名>;

修改字段位置

alter table <表名> modiey <字段1> <数据类型> [first|after] <字段2>;

删除外键关系

alter table <表名> drop foreifn key <外键约束名——fk_1>;

删除数据表

drop table if exists 表1,表2,表n····;(针对没有关联的表)

有关联的表
1、先子表再父表
2、可以先解除外键关系,再删除

插入数据

insert into table_name (column_list) values (values_list),(values_list),(values_list)....;

与select结合使用,将查询到的表插入进去

insert into table_1 (colum_list1)
select (colum_list2) from table_2 where....

更改数据

update table_name
set c_id=1,c_name='玩具'
where....

删除数据

delete from table_name
where....

查询语言

基本查询语句

select * from table_name;
select 字段1,字段2,字段3,字段n from table_name;

单引号修饰值, 反引号修饰字段名称或表名称(反引号只要不是关键字段时可以省略)

as关键字 给字段起个名字

select ct_name as '商品名称',ct_id as '商品ID'
from commoditytype;

运算:+ - * /
商品的单间利润是多少?

select c_name as '商品名称', c_outprice-c_inprice as '商品利润'
from commodity;
商品名称 商品利润
变形金刚-擎天柱 30
变形金刚-霸天虎 25
变形金刚-威震天 125
魔仙玩偶 6
超人玩偶 70
忍者龟套装 NULL

空值还是会输出null,当值为null的情况下 参与运算 结果也为null。

我们把所有商品都卖出去后 的总利润是多少

select c_name , c_outprice , c_inprice , c_num ,
(c_outprice-c_inprice)*c_num as '总利润'
from commodity;

where 关键字
查询进价大于100的商品

select c_name,c_inprice from commodity
where c_inprice>100;

OR:进价小于50 大于200

select c_name,c_inprice from commodity
where c_inprice<50 or c_inprice>200;

and

select c_name,c_inprice from commodity
where c_inprice>=50 and c_inprice<=200;

between…and

select c_name,c_inprice from commodity
where c_inprice between 50 and 200;(包括50、200)

distinct 查询不重复

select distinct c_id from commodity ;

查询为空 或 不为空的关键字是 is null / is not null

select c_name,c_outprice from commodity
where c_outprice is null;
c_name c_outprice
乐高玩具-快乐家庭 NULL
手机模型玩具 NULL
哈利波特1-3 NULL

3 rows in set

select c_name,c_outprice from commodity
where c_outprice is not null;
select c_name , c_outprice , c_inprice ,
c_outprice-c_inprice as '单件利润'
from commodity
where c_outprice is not null;

排除空值后计算

in关键字 在…里面10 20 30 40(or关系-或)

select c_name , c_inprice from commodity
where c_inprice in (10,20,30,40,50);

not in(且关系—和)

select c_name , c_inprice from commodity
where c_inprice not in (10,20,30,40,50);

in里面是或关系 not in是且关系

使用like关键字实现模糊查询

select c_name from commodity
where c_name like '%av%';
c_name
java入门到精通
疯狂java
java思考1

3 rows in set

select c_name from commodity where c_name like '%玩具';

±-------------+
| c_name |
±-------------+
| EVA模型玩具 |
| 手机模型玩具 |
| 手机模型玩具 |
| 手机模型玩具 |
±-------------+
4 rows in set

如果没有通配符那么like关键字的效果和=一致

select c_name from commodity
where c_name like '玩具';

Empty set
只有通配符,结果是全部

select c_name from commodity
where c_name like '%';

order by排序

 根据进价排序输出 升序select c_id,c_name,c_inprice from commodity
order by c_inprice ;
逆序 降序 order by . descselect c_name,c_outprice from commodity
where c_outprice is not null
order by c_outprice desc;

通过limit关键字来限制输出的记录数
排行榜
进价最贵的5件商品

select c_name,c_inprice from commodity
order by c_inprice desc (需要先进行排序)
limit 5;

±----------------------±----------+

c_name c_inprice
X-BOX游戏机 1200
衣柜 600
任天堂游戏机 300
乐高玩具-蝙蝠侠纪念版 290
牛津英语 217

±----------------------±----------+
5 rows in set

售价排行榜的 6-10

select c_name,c_outprice from commodity
where c_outprice is not null
order by c_outprice desc
limit 5,5;

第一行为0,所以想要得到第六行的数,应该是5,然后往下数5名
±------------------±-----------+

c_name c_outprice
EVA模型玩具 450
上下五千年 400
牛津英语 300
熊大图案拉杆箱-小 260
变形金刚-威震天 245

±------------------±-----------+
5 rows in set

count计数
使用count进行获取数据集的个数
输出结果的记录数 而并不是数据表中的记录数

select * from commodity where c_outprice is null;

sum

select sum(c_inprice) from commodity;

±---------------+
| sum(c_inprice) |
±---------------+
| 4723 |
±---------------+
1 row in set
max

mysql> select max(c_inprice) from commodity;

±---------------+
| max(c_inprice) |
±---------------+
| 1200 |
±---------------+
1 row in set

count 和 其它聚合函数的区别

  1. 当查询结果为空的时候 count返回0 其它函数返回null

avg自动过滤null

select avg(c_inprice) from commodity;

±---------------+
| avg(c_inprice) |
±---------------+
| 76.1774 |
±---------------+
1 row in set

group by通过分组来查询 商品种类(多数下与聚合函数一起用)

select * from commodity group by c_type;

分组查询一般和聚合函数一起使用
每种商品类型总进价最贵的

select c_type, max(c_inprice) from commodity group by c_type;

±-------±---------------+

c_type max(c_inprice)
1 1200
2 160
3 217
4 20
5 2
6 600

±-------±---------------+
6 rows in set

having
1、核心注意!having的优先级比where低
2、where 在select之前 having在select之后

select c_type , avg(c_inprice) from commodity
group by c_type;
select c_type , avg(c_inprice) from commodity
where avg(c_inprice)>100
group by c_type;

where 在select 之前,所以之前没有avg,就会出现报错,而having就不会了,他是在select之后的。

select c_type,avg(c_inprice) from commodity
group by c_type
having avg(c_inprice) > 100;

±-------±---------------+
| c_type | avg(c_inprice) |
±-------±---------------+
| 1 | 116.5714 |
| 6 | 600.0000 |
±-------±---------------+
2 rows in set

1、注意sql不同版本语法上有细节的区别
2、在不同版本的sql中
3、部分sql版本中having中的条件必须出现在select中

连接查询
内连接

select * from commodity as c  inner join commoditytype as ct
on c.c_type=ct.ct_id ;

当两个字段重名时用tablename.+字段名来区分(c.c_type)

  • 处可以只填所需的字段名
    on 后面可以跟多个条件,用and或or连接或排序,或函数
select c_name,c_inprice,ct_name from commodity as c
inner join commoditytype as ct
on c.c_type=ct.ct_id
where ct_name='文具' ;

外链接(left|right join)

select * from commodity as c left join commoditytype as ct
on c.c_type=ct.ct_id ;

1、外连接是有区分主表副表
2、主表在前
3、主表为准,主表所有都要显示出来,副表没有的就用null填充
4、主表没有的,副表有的显示不出来
子查询(可以理解为嵌套关系)
想要查出进价大于c_id的商品,先查出c_id=22的价格

select c_inprice from commodity where c_id=22;

查出c_id=22 的进价为2,再查询价格大于2

select c_name,c_inprice from commodity where c_inprice>2;

运用子查询可以把两部合一起,2用第一句直接替换

select c_name,c_inprice from commodity where c_inprice>(select c_inprice from commodity where c_id=22);

单行单列(只有一个值,像上面例子)

in单行多列 (使用场景比较少)

select * from commodity where (c_name,c_inprice) in (select c_name,c_inprice from commodity where c_id=2);

单列多行
例题一、问’玩具’,'书籍’的商品有哪些
1、使用连接查询

select * from commodity inner join commoditytype
on c_type=ct_id where ct_name='玩具' or '书籍';

2、使用子查询

select * from commodity  where c_type in(select ct_id from  commoditytype where ct_name='玩具' or '书籍');

对比以下例子单列多行需要用in连接不能用=连接
问’玩具’的商品有哪些
1、使用连接查询

select * from commodity inner join commoditytype
on c_type=ct_id where ct_name='玩具';

2、使用子查询

select * from commodity  where c_type=(select ct_id from  commoditytype where ct_name='玩具');

例题二

any 任一
所有大于文具类商品中进价最便宜的有哪些
大于任一个价格就可以满足条件,那么只要大于最低价就行,

select c_name,c_inorice from commodity where c_inorice>any(select c_inprice from commodity where c_type=(select ct_id from commoditytype where ct_name='文具')
);

all 所有
所有大于文具类商品中进价最贵的有哪些
必须要大于所有价格,所以只要大于最高的就可以满足所有条件

select c_name,c_inorice from commodity where c_inorice>any(select c_inprice from commodity where c_type=(select ct_id from commoditytype where ct_name='文具')
);

多行多列(一个表,从一个表中查出另一个表)

select c_name,c_inprice,ct_name from(select * from commodity as c  inner join commoditytype as     ct  on c.c_type=ct.ct_id )as a;

带exists的子查询语句
如果表中存在c_id=3,那么输出表

select * from 表1 where exists(select c_name from 表2 where c_id=3);

not exists用法一样
合并查询
union all

select c_id,c_name from commodity
where c_id in (1,2,3)
union all
select c_id,c_name from commodity
where c_id>4;

多个条件用 union all 连接
如果没有all 会去掉重复项。

三个表连接查询

 select deposite.c_id as '客户名称',name as '客户名称',bank_name as '银行',amount as '存款' from
( deposite left join customer on deposite.c_id=customer.c_id)left join bank on deposite.b_id=bank.b_id

mysql 语言 总结相关推荐

  1. 【openai】请帮我设计一个通用的ERP管理系统,涉及到的表结构用mysql语言表达出来,全部写出来

    背景 这周末把openAi集成到自己的web系统里面了 尝试提问了几个技术和日常问题,感觉回答的还不错 问题1:[请帮我设计一个通用的ERP管理系统,涉及到的表结构用mysql语言表达出来,全部写出来 ...

  2. MySQL语言的算法_MySQL知识整理

    写在前面的话:秋招来临,博主对MySQL知识进行了整理,全文包括数据库优化和数据库基础两部分 数据库优化 1.优化的目的 [1]     避免页面访问的出错(5xx错误:慢查询无法加载:阻塞无法提交) ...

  3. 连接mysql语言_杂谈各个语言连接数据库如何实现的-第一讲

    我们都知道各个语言连接数据库都有封装好的API.比如操作MySQL,php有pdo,mysqli等,java有jdbc,c#有mysql-connector-net,nodejs也有mysql的驱动. ...

  4. MySQL语言解析----1

    SQL:Structure Query Language 结构查询语言,是所有数据库操作的基础语言,是和数据库软件交互的基础接口. 一.SQL语言分类 DDL语言:Data Definition La ...

  5. mysql语言中有什么运算_SQL知识点,新手感悟

    之所以写这篇文章呢,是因为看的过程就是记忆--遗忘--记忆--遗忘,所以把看书过程中我觉得我自己没有掌握的地方记下来,之后可以借助这篇文章再回忆一下.看完书之后打算去SQLZOO实战练习,会再写一篇练 ...

  6. 第九章 Mysql语言

    #[例9.1] 使用RAND()函数求3个随机值. #[例9.2] 求3和4的平方根. #[例9.3] 求7.2和-7.2的绝对值. #[例9.4] 求小于或等于-3.5或6.8的最大整数,大于或等于 ...

  7. mysql语言的学习

    语言的学习 DQL(Data Query Lanuage ):数据查询语言(查select) DML(Data Manipalution Lanuage):数据操作语言(增删改) DDL(Data D ...

  8. mysql语言定义_MySQL定义语言[DDL]

    创建表tablecreate table table_name ( 列名 属性, age int, ... name varchar(10) ); 整型列tinyint:1字节,取值范围[-128 ~ ...

  9. mysql语言的简介_谁能帮我介绍一下 MY SQL ?

    展开全部 行服务器类型的选择e68a843231313335323631343130323136353331333264626638,分别有三种服务器类型的选择,(Developer(开发机).Ser ...

最新文章

  1. 亮相百度WAVE SUMMIT+2021,Intel OpenVINO带来新气象
  2. 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)
  3. python-map函数
  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 80 Days(双向队列+尺取法)
  5. LiveVideoStack线上分享第四季(十二):实时音视频抗丢包的实践
  6. Workflow Core + asp.net core 5.0 实现简单审批工作流
  7. 绩效考核中什么是KPI和KRA
  8. C语言基本语法——结构体、联合和枚举
  9. leetcode解题报告:Interleaving String
  10. android 添加注释,向Android Saripaar添加自定义注释
  11. URL重写,asp.net URL重写,URLRewriter.dll下载
  12. Visio2019中插入数学公式
  13. 宾馆客房管理系统设计
  14. 设置共享文件夹以计算机用户名和密码访问,Win10正式版系统怎么设置共享文件夹密码访问...
  15. echarts去除x轴和y轴上的刻度值_Echarts实现隐藏x轴,y轴,刻度线,网格
  16. 忘记了已保存自动登录的密码,怎么办?
  17. 召唤神龙无敌版------小鱼吃大龙
  18. linux 应用编程(持续更新)
  19. 基于RBAC 的SAAS系统权限设计
  20. win10 系统网络驱动出现黄色感叹号

热门文章

  1. 数字IC笔试题,大疆校招16题(仅供参考)
  2. vatic标注工具安装步骤(非docker安装)以及错误解决办法
  3. 我的移动智能2-corner detection
  4. windows中docker 安装和使用
  5. 漫天杂想系列之五:2018年总结
  6. 亚洲一些物流软件航空争抢货机投放
  7. 内部版本号android,分享个老教程:修改手机型号、品牌、内部版本号、Android版本...
  8. SpringCloud熔断机制大概什么意思
  9. 谷歌浏览器安全证书不受信任_win7系统谷歌浏览器提示该网站的安全证书不受信任的解决方法...
  10. windows防火墙是干什么的_我可以用windows防火墙做什么