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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

MySQL_day2笔记

發(fā)布時(shí)間:2024/3/12 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL_day2笔记 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

數(shù)據(jù)表的基本操作

建表

create table commoditytype( ct_id int(11) primary key, ct_name varchar(50) not null )default charset=utf8; create table commodity( c_id int(11) primary key, c_name varchar(50) not null, c_madein varchar(50) not null, c_type int(11) not null, c_inprice int(11) not null, c_outprice int(11) , c_num int(11) default '100', constraint fk_1 foreign key (c_type) references commoditytype (ct_id) #commodity與commoditytype的外鍵 )defalut charset=utf8;
  • 根據(jù)以上的兩張表進(jìn)行增刪改查

#為第一張表進(jìn)行增加操作,多個(gè)操作用“,”分隔 insert into commoditytype (ct_id,ct_name) values (1,'玩具'),(2,'文具'),(3,'家具'); #為第二張表進(jìn)行增加操作 insert into `commodity` VALUES ('1', '變形金剛-擎天柱', '中國(guó)', '1', '20', '50', '60');

delete from commoditytype where ct_id=2; #全表刪除 delete from commoditytype;

update commmoditytype set ct_name='家具' where ct_id=2; #全表修改為相同數(shù)據(jù) update commoditytype set ct_name='家具';

關(guān)系運(yùn)算符與邏輯運(yùn)算符

算數(shù)運(yùn)算符描述邏輯運(yùn)算符描述
>大于AND(&&)邏輯與
<小于OR(||)邏輯或
=等于XOR邏輯異或
!=(<>)不等于NOT(!)邏輯非
<=小于等于
>=大于等于


查詢表中所有數(shù)據(jù)

select * from commoditytype;

查詢刪除重復(fù)項(xiàng)

select distinct c_type from commodity;

關(guān)系運(yùn)算與邏輯運(yùn)算與between and
篩選進(jìn)價(jià)在10~100的類型為玩具(1)的商品

select c_name as 商品名稱,c_inprice as 商品進(jìn)價(jià) from commodity where (c_inprice >= 10 and c_inprice <= 100) and c_type = 1; #where (c_inprice between 10 and 100) and c_type = 1;#包括兩端的值

篩選進(jìn)價(jià)不在10~100的類型為玩具(1)的商品

select c_name as 商品名稱,c_inprice as 商品進(jìn)價(jià) from commodity where (c_inprice < 10 or c_inprice > 100) and c_type = 1; #where (c_inprice not between 10 and 100) and c_type = 1;#不包括兩端的值

關(guān)鍵字

is null:判斷是否為空

select c_name,c_outprice,c_outprice-c_inprice from commodity where c_outprice is null;

is not null:判斷非空

select c_name,c_outprice,c_outprice-c_inprice from commodity where c_outprice is not null;

in:里面的數(shù)字之間的關(guān)系是或關(guān)系

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

not in:里面的數(shù)字之間的關(guān)系是且關(guān)系

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

like:_ 匹配一個(gè)字符;% 匹配一個(gè)或多個(gè)字符
?????匹配第二個(gè)字符為華的商品名稱

select c_name from commodity where c_name like '_華%';

?????匹配最后一個(gè)字符為典的商品名稱

select c_name from commodity where c_name like '%典';

order by:排序(desc 逆序;默認(rèn)為 asc:從大到大排序)
?????選出5個(gè)價(jià)格最高的玩具顯示出他的商品名稱和商品進(jìn)價(jià)

?????使用limit實(shí)現(xiàn)翻頁(yè)效果

select c_name,c_inprice from commodity where c_type=1 order by c_inprice desc limit 5;

group by:實(shí)現(xiàn)分組,一般都是和統(tǒng)計(jì)函數(shù)一起出現(xiàn)的,如下avg()舉例。

統(tǒng)計(jì)函數(shù)(聚合函數(shù))

聚合函數(shù)
COUNT()函數(shù):統(tǒng)計(jì)記錄數(shù)
AVG()函數(shù):求平均值
SUM()函數(shù):求和
MAX()函數(shù):求最大數(shù)
MIN()函數(shù):求最小數(shù)
統(tǒng)計(jì)函數(shù)均忽略NULL值
  • count() 函數(shù)
    如果表中無(wú)數(shù)據(jù),count()函數(shù)返回的是0,其它函數(shù)返回null,可以解決Java讀取數(shù)據(jù)時(shí)的空指針異常*

?????統(tǒng)計(jì)類型為玩具的總數(shù)量

select count(*) from commodity where c_type=1;
  • avg()函數(shù)

?????查找各商品類型的平均進(jìn)價(jià)

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

?????查找各類型的平均進(jìn)價(jià),并篩選出平均進(jìn)價(jià)大于100的商品類型

?????對(duì)分組查詢結(jié)果進(jìn)行條件限制查詢,不能使用WHERE關(guān)鍵字,需要使用HAVING關(guān)鍵字,having優(yōu)先級(jí)比where低,where不能在having后使用

select c_type,avg(c_inprice) from commodity group by c_type having avg(c_inprice)>100;
  • sum()函數(shù);Max函數(shù);Min函數(shù)
select sum(c_inprice),max(c_inprice),min(c_inprice) from commodity;

sql優(yōu)化(1)

  • 索引掃描

SQL CREATE INDEX語(yǔ)法

# 在表上創(chuàng)建一個(gè)簡(jiǎn)單的索引,允許使用重復(fù)的值 create index index_name on table_name(column_name)

SQL CREATE UNIQUE INDEX語(yǔ)法

# 在表上創(chuàng)建一個(gè)唯一的索引,唯一的索引意味著兩個(gè)行不能擁有相同的索引值 create unique index index_name on table_name(column_name)
  • 注意項(xiàng):

1、盡量不要有空判斷的語(yǔ)句,空判斷將導(dǎo)致全表掃描;解決方案對(duì)有null值得列創(chuàng)建數(shù)據(jù)庫(kù)默認(rèn)值

select * from commodity where c_outprice is null;# 空判斷將導(dǎo)致全表掃描 # where c_outprice = '';# 假設(shè)有默認(rèn)值:空字符串

2、盡量不要使用不等于條件,這會(huì)導(dǎo)致全表掃描對(duì)于不等于這種情況,考慮改為范圍查詢解決

select * from commodity where c_outprice <> 20;# 不等于條件,將導(dǎo)致全表掃描 # where c_outprice > 20;# 假設(shè)20是最大值

3、盡量不要使用左右模糊查詢,這會(huì)導(dǎo)致全表掃描對(duì)于左右模糊查詢的情況,試著改為右側(cè)模糊查詢,這樣是可以索引查找的

select * from commodity where c_name like '%玩具%';# 將導(dǎo)致全表掃描 # where c_name like '樂(lè)高玩具%';# 右側(cè)模糊查詢

總結(jié)

以上是生活随笔為你收集整理的MySQL_day2笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。