1. 表欄位的操作
1. 語法: alter table 表名 執行動作;
2. 添加欄位(add)
alter table 表名 add 欄位名 資料型別;
alter table 表名 add 欄位名 資料型別 first;
alter table 表名 add 欄位名 資料型別 after 欄位名;
3. 洗掉欄位(drop)
alter table 表名 drop 欄位名;
4. 修改資料型別(modify)
alter table 表名 modify 欄位名 新資料型別;
5. 表重命名
alter table 表名 rename 新表名;
6. 練習
1. 創建庫 studb2
create database studb2;
2. 在庫中創建表 t1, 欄位有3個:name, age, phnumber
use studb2;
create table t1(
name char(15),
age tinyint unsigned,
phnumber int);
3. 查看表結構
desc t1;
4. 在表中第一列添加一個id 欄位
alter table t1 add id int first;
5. 把 phnumber 的資料型別改為 bigint
alter table t1 modify phnumber bigint;
6. 在表中最后一列添加一個欄位 address
alter table t1 add address char(100);
7. 洗掉表中的 age 欄位
alter table t1 drop age;
8. 查看表結構
desc t1;
2. 表記錄管理
1. 洗掉表記錄
1. delete from 表名 where 條件;
2. 注意
delete 陳述句后如果不加where條件,所有記錄全部清空
2. 更新表記錄
1. update 表名 set 欄位1=值1,欄位2=值2,... where 條件;
2.注意
必須加where條件
3. 練習
1. 查找所有蜀國人的資訊
select * from hero where country='蜀國';
2. 查找所有女英雄的姓名、性別和國家
select name,sex,country from hero where sex='女';
3. 把id位2的記錄改為典韋,性別男,國家魏國
update hero set name='典韋', sex='男', country='魏國' where id=2;
4. 洗掉所有蜀國英雄
delete from hero where country = '蜀國';
5. 把貂蟬的國籍改為魏國
update hero set country='魏國' where name='貂蟬';
6. 洗掉所有表記錄
delete from hero;
3. 運算子操作
1. 數值比較/字符比較
1. 數值比較 : = != > >= < <=
2. 字符比較 : = !=
3 練習
1. 查找攻擊力高于150的英雄的名字和攻擊值
select name,gongji from sanguo where gongji > 150;
2. 將趙云的攻擊力設定位360,防御力設定位68
update sanguo set gongji=360, fangyu=68 where name='趙云';
2. 邏輯比較
1. and(兩個或多個條件同時成立)
2. or(任意一個條件成立即可)
3. 練習
1. 找出攻擊值高于200的蜀國英雄的名字、攻擊力
select name,gongji from sanguo where gongji > 200 and country='蜀國';
select name as n, gongji as g from sanguo where gongji > 200 and country='蜀國';
2. 將吳國英雄中攻擊值位110的英雄的攻擊值改為100,防御力改為60
update sanguo set gongji=100, fangyu=60 where country='吳國' and gongji=110;
3. 查找蜀國和魏國的英雄資訊
select * from sanguo where country='蜀國' or country='魏國';
3. 范圍內比較
1. between 值1 and 值2
2. where 欄位名 in(值1, 值2, ...)
3. where 欄位名 not in(值1, 值2, ...)
4. 練習
1. 查找攻擊值100-200的蜀國英雄資訊
select * from sanguo where gongji between 100 and 200 and country='蜀國';
2. 找到蜀國和吳國以外的國家的女英雄資訊
select * from sanguo where country not in('蜀國', '吳國') and sex='女';
3. 找到id 位1、3或5的蜀國英雄和貂蟬的資訊
select * from sanguo where (id in(1,3,5) and country='蜀國') or name='貂蟬';
4. 匹配空、非空
1. 空:where name is null
2. 非空:where name is not null
3. 示例:
1. 姓名位NULL值的蜀國女英雄
select * from sanguo
where name is null and country='蜀國' and sex = '女';
2. 姓名位“”的英雄資訊
select * from sanguo where name="";
4. 注意
1. NULL:空值,只能用is 或者 is not 去匹配
2. "" :空字串,用 = 或者 != 去匹配
5. 模糊比較
1. where 欄位名 like 運算式
2. 運算式
1. _: 匹配單個字符
2. %:匹配0到多個字符
3. 示例
select name from sanguo where name like "_%_";
select name from sanguo where name like "%";
# NULL 不會被統計,只能用is、is not 去匹配
select name from sanguo where name like "___";
select name from sanguo where name like "趙%";
4. SQL查詢
1. 總結
3.select ... 聚合函式 from 表名
1. where ...
2. group by ...
4. having ...
5. order by ...
6. limit ...;
2. order by
1. 給查詢結果進行排序
2. ... order by 欄位名 升序/降序
3. 升序:ASC
降序:DESC
4. 示例
1. 將英雄按防御值從高到低排序
select * from sanguo order by fangyu desc;
2. 將蜀國英雄按攻擊值從高到低排序
select * from sanguo order by gongji desc;
3. 將魏蜀兩國英雄中名字為三個字的按防御值升序排列
select * from sanguo where country in('魏國', '蜀國') and name like '___' order by fangyu;
3. limit(永遠放在SQL陳述句的最后寫)
1. 作用:限制顯示查詢記錄的個數
2. 用法
1. limit n -> 顯示n條記錄
2. limit m, n
m 表示 從第m+1條記錄開始顯示,顯示 n 條
limit 2, 3 : 第 3、4、5 三條記錄
3. 示例
1. 在蜀國英雄中,查找防御值倒數第二名至倒數第四名的英雄的記錄
select * from sanguo
where country='蜀國'
order by fangyu asc
limit 1, 3;
2. 在蜀國英雄中,查找攻擊值前3名且名字不為 NULL的英雄的姓名、攻擊值和國家
select name,gongji,country from sanguo
where country="蜀國" and name is not null
order by gongji desc
limit 3;
4. 分頁
每頁顯示5條記錄,顯示第4頁的內容
第1頁: limit 0,5 # 1 2 3 4 5
第2頁: limit (2-1)*5,5 # 6 7 8 9 10
第3頁: limit (3-1)*5,5 # 11 12 13 14 15
第4頁; limit (4-1)*5,5 # 16 17 18 19 20
.....
每頁顯示n條記錄,顯示第m頁:limit (m-1)*n, n
4. 聚合函式
1. 分類
avg(欄位名) : 求該欄位平均值
sum(欄位名): 求和
max(欄位名):求最大值
min(欄位名): 求最小值
count(欄位名): 統計該欄位記錄的個數
2. 示例
1. 攻擊最強值是多少
select avg(gongji) from MOSHOU.sanguo;
select avg(gongji) as best from MOSHOU.sanguo;
select max(gongji) from MOSHOU.sanguo;
2. 統計id、name兩個欄位分別有幾條記錄
select count(id), count(name) from sanguo;
## 空值 NULL 不會被統計
3. 計算蜀國英雄的總攻擊力
select sum(gongji) from MOSHOU.sanguo where country='蜀國';
4. 統計蜀國英雄中攻擊值大于200的英雄的數量
select count(*) from MOSHOU.sanguo where country='蜀國' and gongji > 200;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/117975.html
標籤:MySQL
上一篇:MySQL修煉之路一
