MySQL_day2笔记
數(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)算符
| > | 大于 | AND(&&) | 邏輯與 |
| < | 小于 | OR(||) | 邏輯或 |
| = | 等于 | XOR | 邏輯異或 |
| !=(<>) | 不等于 | NOT(!) | 邏輯非 |
| <= | 小于等于 | ||
| >= | 大于等于 |
查
查詢表中所有數(shù)據(jù)
查詢刪除重復(fù)項(xiàng)
select distinct c_type from commodity;關(guān)系運(yùn)算與邏輯運(yùn)算與between and
篩選進(jìn)價(jià)在10~100的類型為玩具(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è)字符為華的商品名稱
?????匹配最后一個(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ù))
| 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ù)
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)題。
- 上一篇: 任志强:松房贷等政策出台后 房价走低趋势
- 下一篇: linux cmake编译源码,linu