日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 语言 总结

發布時間:2024/1/8 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 语言 总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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_namec_outprice
樂高玩具-快樂家庭NULL
手機模型玩具NULL
哈利波特1-3NULL

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_namec_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_namec_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 和 其它聚合函數的區別

  • 當查詢結果為空的時候 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_typemax(c_inprice)
    11200
    2160
    3217
    420
    52
    6600

    ±-------±---------------+
    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 语言 总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。