mysql 语言 总结
mysql 語言 總結(jié)
數(shù)據(jù)庫
顯示數(shù)據(jù)庫
show databases;創(chuàng)建數(shù)據(jù)庫
create database database_name(數(shù)據(jù)庫名稱);刪除數(shù)據(jù)庫
drop database database_name(數(shù)據(jù)庫名);數(shù)據(jù)表
創(chuàng)建數(shù)據(jù)表
use 的使用:在創(chuàng)建表之前聲明在哪個數(shù)據(jù)庫
創(chuàng)建表
create table t_name( 字段名1 數(shù)據(jù)類型 約束條件, 字段名2 數(shù)據(jù)類型 約束條件, 字段名1 數(shù)據(jù)類型 約束條件(最后一個不要,) )default charset=utf8;主鍵、外鍵的鏈接(ta_1、ta_2)
創(chuàng)建表ta_1:
創(chuàng)建表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 數(shù)據(jù)類型 約束條件,default 111(默認約束,默認111) 字段名2 數(shù)據(jù)類型 約束條件,auto_increment(自動增長) 字段名1 數(shù)據(jù)類型 約束條件,unique(唯一) )default charset=utf8;查看數(shù)據(jù)表
1.show tables;(查看數(shù)據(jù)表) 2.describe table_name;(查看數(shù)據(jù)表結(jié)構) 3.show create table name\G;(查看詳細數(shù)據(jù)表結(jié)構語句) 使用\G參數(shù)可使表結(jié)構清楚。修改數(shù)據(jù)表
修改表名
修改字段的數(shù)據(jù)類型
alter table <表名> modiey <字段名> <數(shù)據(jù)類型>;修改字段名
alter table <表名> change <舊字段名> <新字段名> <新數(shù)據(jù)類型>;添加字段名
alteer table <表名> add <字段名> <數(shù)據(jù)類型> [約束條件] [first|after 已存在字段名];(添加在第一個或者哪個之后)刪除字段
alter table <表> drop <字段名>;修改字段位置
alter table <表名> modiey <字段1> <數(shù)據(jù)類型> [first|after] <字段2>;刪除外鍵關系
alter table <表名> drop foreifn key <外鍵約束名——fk_1>;刪除數(shù)據(jù)表
drop table if exists 表1,表2,表n····;(針對沒有關聯(lián)的表)有關聯(lián)的表
1、先子表再父表
2、可以先解除外鍵關系,再刪除
插入數(shù)據(jù)
insert into table_name (column_list) values (values_list),(values_list),(values_list)....;與select結(jié)合使用,將查詢到的表插入進去
insert into table_1 (colum_list1) select (colum_list2) from table_2 where....更改數(shù)據(jù)
update table_name set c_id=1,c_name='玩具' where....刪除數(shù)據(jù)
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;運算:+ - * /
商品的單間利潤是多少?
| 變形金剛-擎天柱 | 30 |
| 變形金剛-霸天虎 | 25 |
| 變形金剛-威震天 | 125 |
| 魔仙玩偶 | 6 |
| 超人玩偶 | 70 |
| 忍者龜套裝 | NULL |
空值還是會輸出null,當值為null的情況下 參與運算 結(jié)果也為null。
我們把所有商品都賣出去后 的總利潤是多少
select c_name , c_outprice , c_inprice , c_num , (c_outprice-c_inprice)*c_num as '總利潤' from commodity;where 關鍵字
查詢進價大于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 ;查詢?yōu)榭?或 不為空的關鍵字是 is null / is not null
select c_name,c_outprice from commodity where c_outprice is null;| 樂高玩具-快樂家庭 | 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關鍵字實現(xiàn)模糊查詢
select c_name from commodity where c_name like '%av%';| 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
只有通配符,結(jié)果是全部
order by排序
根據(jù)進價排序輸出 升序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關鍵字來限制輸出的記錄數(shù)
排行榜
進價最貴的5件商品
±----------------------±----------+
| 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,所以想要得到第六行的數(shù),應該是5,然后往下數(shù)5名
±------------------±-----------+
| EVA模型玩具 | 450 |
| 上下五千年 | 400 |
| 牛津英語 | 300 |
| 熊大圖案拉桿箱-小 | 260 |
| 變形金剛-威震天 | 245 |
±------------------±-----------+
5 rows in set
count計數(shù)
使用count進行獲取數(shù)據(jù)集的個數(shù)
輸出結(jié)果的記錄數(shù) 而并不是數(shù)據(jù)表中的記錄數(shù)
sum
select sum(c_inprice) from commodity;±---------------+
| sum(c_inprice) |
±---------------+
| 4723 |
±---------------+
1 row in set
max
±---------------+
| max(c_inprice) |
±---------------+
| 1200 |
±---------------+
1 row in set
count 和 其它聚合函數(shù)的區(qū)別
avg自動過濾null
select avg(c_inprice) from commodity;±---------------+
| avg(c_inprice) |
±---------------+
| 76.1774 |
±---------------+
1 row in set
group by通過分組來查詢 商品種類(多數(shù)下與聚合函數(shù)一起用)
select * from commodity group by c_type;分組查詢一般和聚合函數(shù)一起使用
每種商品類型總進價最貴的
±-------±---------------+
| 1 | 1200 |
| 2 | 160 |
| 3 | 217 |
| 4 | 20 |
| 5 | 2 |
| 6 | 600 |
±-------±---------------+
6 rows in set
having
1、核心注意!having的優(yōu)先級比where低
2、where 在select之前 having在select之后
where 在select 之前,所以之前沒有avg,就會出現(xiàn)報錯,而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不同版本語法上有細節(jié)的區(qū)別
2、在不同版本的sql中
3、部分sql版本中having中的條件必須出現(xiàn)在select中
連接查詢
內(nèi)連接
當兩個字段重名時用tablename.+字段名來區(qū)分(c.c_type)
- 處可以只填所需的字段名
on 后面可以跟多個條件,用and或or連接或排序,或函數(shù)
外鏈接(left|right join)
select * from commodity as c left join commoditytype as ct on c.c_type=ct.ct_id ;1、外連接是有區(qū)分主表副表
2、主表在前
3、主表為準,主表所有都要顯示出來,副表沒有的就用null填充
4、主表沒有的,副表有的顯示不出來
子查詢(可以理解為嵌套關系)
想要查出進價大于c_id的商品,先查出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、使用連接查詢
2、使用子查詢
select * from commodity where c_type in(select ct_id from commoditytype where ct_name='玩具' or '書籍');對比以下例子單列多行需要用in連接不能用=連接
問’玩具’的商品有哪些
1、使用連接查詢
2、使用子查詢
select * from commodity where c_type=(select ct_id from commoditytype where ct_name='玩具');例題二
any 任一
所有大于文具類商品中進價最便宜的有哪些
大于任一個價格就可以滿足條件,那么只要大于最低價就行,
all 所有
所有大于文具類商品中進價最貴的有哪些
必須要大于所有價格,所以只要大于最高的就可以滿足所有條件
多行多列(一個表,從一個表中查出另一個表)
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,那么輸出表
not exists用法一樣
合并查詢
union all
多個條件用 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總結(jié)
以上是生活随笔為你收集整理的mysql 语言 总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何查看电脑上是否安装了MySQL
- 下一篇: 速卖通韩国下载量再次登顶,7500万投资