首页 / MYSQL / mysql 语言 总结
mysql 语言 总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql 语言 总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含10879字,纯文字阅读大概需要16分钟。
内容图文
![mysql 语言 总结](/upload/InfoBanner/zyjiaocheng/899/a8146d2e6ab7400c99320a20dcb2289f.jpg)
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 . desc
select 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 语言 总结全部内容,希望文章能够帮你解决mysql 语言 总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。