MySQL_day2笔记
數據表的基本操作
建表
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;- 根據以上的兩張表進行增刪改查
增
#為第一張表進行增加操作,多個操作用“,”分隔 insert into commoditytype (ct_id,ct_name) values (1,'玩具'),(2,'文具'),(3,'家具'); #為第二張表進行增加操作 insert into `commodity` VALUES ('1', '變形金剛-擎天柱', '中國', '1', '20', '50', '60');刪
delete from commoditytype where ct_id=2; #全表刪除 delete from commoditytype;改
update commmoditytype set ct_name='家具' where ct_id=2; #全表修改為相同數據 update commoditytype set ct_name='家具';關系運算符與邏輯運算符
| > | 大于 | AND(&&) | 邏輯與 |
| < | 小于 | OR(||) | 邏輯或 |
| = | 等于 | XOR | 邏輯異或 |
| !=(<>) | 不等于 | NOT(!) | 邏輯非 |
| <= | 小于等于 | ||
| >= | 大于等于 |
查
查詢表中所有數據
查詢刪除重復項
select distinct c_type from commodity;關系運算與邏輯運算與between and
篩選進價在10~100的類型為玩具(1)的商品
篩選進價不在10~100的類型為玩具(1)的商品
select c_name as 商品名稱,c_inprice as 商品進價 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;#不包括兩端的值關鍵字
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:里面的數字之間的關系是或關系
select c_name,c_inprice from commodity where c_inprice not in (10,20,30,40);not in:里面的數字之間的關系是且關系
select c_name,c_inprice from commodity where c_inprice not in (10,20,30,40);like:_ 匹配一個字符;% 匹配一個或多個字符
?????匹配第二個字符為華的商品名稱
?????匹配最后一個字符為典的商品名稱
select c_name from commodity where c_name like '%典';order by:排序(desc 逆序;默認為 asc:從大到大排序)
?????選出5個價格最高的玩具顯示出他的商品名稱和商品進價
?????使用limit實現翻頁效果
select c_name,c_inprice from commodity where c_type=1 order by c_inprice desc limit 5;group by:實現分組,一般都是和統計函數一起出現的,如下avg()舉例。
統計函數(聚合函數)
| COUNT()函數:統計記錄數 |
| AVG()函數:求平均值 |
| SUM()函數:求和 |
| MAX()函數:求最大數 |
| MIN()函數:求最小數 |
| 統計函數均忽略NULL值 |
- count() 函數
如果表中無數據,count()函數返回的是0,其它函數返回null,可以解決Java讀取數據時的空指針異常*
?????統計類型為玩具的總數量
select count(*) from commodity where c_type=1;- avg()函數
?????查找各商品類型的平均進價
select c_type,avg(c_inprice) from commodity group by c_type;?????查找各類型的平均進價,并篩選出平均進價大于100的商品類型
?????對分組查詢結果進行條件限制查詢,不能使用WHERE關鍵字,需要使用HAVING關鍵字,having優先級比where低,where不能在having后使用
select c_type,avg(c_inprice) from commodity group by c_type having avg(c_inprice)>100;- sum()函數;Max函數;Min函數
sql優化(1)
- 索引掃描
SQL CREATE INDEX語法
# 在表上創建一個簡單的索引,允許使用重復的值 create index index_name on table_name(column_name)SQL CREATE UNIQUE INDEX語法
# 在表上創建一個唯一的索引,唯一的索引意味著兩個行不能擁有相同的索引值 create unique index index_name on table_name(column_name)- 注意項:
1、盡量不要有空判斷的語句,空判斷將導致全表掃描;解決方案對有null值得列創建數據庫默認值
select * from commodity where c_outprice is null;# 空判斷將導致全表掃描 # where c_outprice = '';# 假設有默認值:空字符串2、盡量不要使用不等于條件,這會導致全表掃描對于不等于這種情況,考慮改為范圍查詢解決
select * from commodity where c_outprice <> 20;# 不等于條件,將導致全表掃描 # where c_outprice > 20;# 假設20是最大值3、盡量不要使用左右模糊查詢,這會導致全表掃描對于左右模糊查詢的情況,試著改為右側模糊查詢,這樣是可以索引查找的
select * from commodity where c_name like '%玩具%';# 將導致全表掃描 # where c_name like '樂高玩具%';# 右側模糊查詢總結
以上是生活随笔為你收集整理的MySQL_day2笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 任志强:松房贷等政策出台后 房价走低趋势
- 下一篇: mysql 创建学生表、课程表、学生选课