1.SQL介紹
2.SQL分類
3.數據庫字符集介紹
4.數據庫列屬性類型
- 數字類型
- 字符串類型
- 時間類型
5.列約束
- 主鍵
- 非空
- 無符號
6.數據庫管理
- 數據庫定義規范
- 創建庫
- 修改庫
- 查看庫
- 刪除庫
7.表管理
- 表定義規范
- 創建表
- 查看表
- 查看表結構
- 刪除表
8.SQL語句-插入數據
9.SQL語句-修改數據
10.SQL語句-刪除數據
11.SQL語句-查詢
12.GROUP BY 聚合函數
13.HAVING 聚合判斷
14.ORDER BY 聚合排序
16.LIMIT 分頁查詢
SQL語句:
增
刪
改
查
第1章 MySQL名詞解釋
1.名詞解釋
數據庫
表
列/字段
行
SQL語句
第2章 SQL查詢環境準備
1.導入練習數據
mysql -uroot -p123< world.sql
2.查看數據庫
mysql> show databases;
3.查看表
mysql> use world;
mysql> show tables;
4.如何了解一個表有哪些內容
第一步: 先查看這個表有哪些列
mysql> desc city;
第二步: 查看少量數據
mysql> select * from city limit 10;
第三步: 理解每一列的意義
ID? ? ? ?
Name? ? ? 城市名稱
CountryCode? ? 國家代碼
District? ? ? ? 省會
Population? ? ? 人口
第3章 SQL簡單查詢練習
0.創建遠程連接賬號
GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.0.%'? IDENTIFIED BY '123';
select user,host from mysql.user;
1.查詢表里所有的數據
select * from city;
查詢? 所有字段? 從? 表;
2.查詢部分字段的數據
select name from city;
select name,CountryCode from city;
select CountryCode,name from city;
select CountryCode,name,Population from city;
select countrycode,name,population from city;
3.查詢city表中,所有中國的城市信息 CHN
select * from city where countrycode='chn';
查詢? 所有字段? 從? ? 表? 條件? 字段='值';
4.查詢city表中,城市名稱叫shenzhen的信息
select * from city where name='shenzhen';
5.查詢人口數小于100人城市信息
select * from city where Population < 100;
6.查詢中國,人口數超過500w的所有城市信息
select * from city where CountryCode='chn' and Population > 5000000;
7.查詢中國或美國的城市信息
select * from city where CountryCode='chn' or CountryCode='usa';
select * from city where CountryCode in ('usa','chn');
8.查詢人口數為100w-200w(包括兩頭)城市信息
select * from city where Population >=1000000 and Population <=2000000;
9.查詢中國或美國,人口數大于500w的城市
select * from city where Population > 5000000 and countrycode in ('chn','usa');
10.查詢CHN城市名為qing開頭的城市信息并且人口大于100w
select * from city where name like 'qing%' and population > 1000000;
11.查詢CHN城市名城市名為jing結尾的城市信息,只顯示name和Population列
select name,population from city where name like '%qing';
12.查詢表的字段都有哪些
desc city;
第4章 GROUP BY 和 聚合函數
1.GROUP BY 和 聚合函數解釋
GROUP BY:
根據條件進行分組
聚合函數:
聚合函數是在GROUP BY分組之后再進行數據運算
count()? ? ? ? ? ? 統計數量
sum()? ? ? ? ? ? ? 求和
avg()? ? ? ? ? ? ? 平均數
max()? ? ? ? ? ? ? 最大值
min()? ? ? ? ? ? ? 最小值
group_concat()? ? 列轉行
第5章 GROUP BY和聚合函數練習題
1.統計city表的行數
SELECT COUNT(*) FROM city;
SELECT COUNT(*)'總數' FROM city;
2.統計中國城市的個數
select count(*)'中國城市總數' from city where CountryCode='chn';
3.統計中國的總人口數
select sum(Population)'中國總人口' from city where CountryCode='chn';
4.統計中國和美國的城市個數 group by
國家? 城市個數
CHN? 200
USA? 20
select count(name)'城市個數',CountryCode'國家' from city where CountryCode='chn' or CountryCode='usa' group by countrycode;
統計每個國家的城市個數 group by
select count(name)'城市個數',CountryCode'國家' from city group by countrycode;
5.統計每個國家的總人口數
select countrycode'國家',sum(Population)'人口總數' from city group by countrycode;
6.統計中國每個省的城市個數及城市名列表
省名 城市個數 城市名
select count(name)'城市個數',District'省份' from city where countrycode='chn' group by District limit 10;
7.計算人口數超過1億的國家
國家代碼? 人口數
mysql> select countrycode,sum(population) from city group by countrycode
? ? -> having sum(population) >100000000;
8.查詢所有國家人口數,顯示出大于5000萬,并排序輸出
#升序:
select countrycode'國家',sum(population)'人口數量' from city group by countrycode having sum(population) >50000000 order by sum(population);
#降序:
select countrycode'國家',sum(population)'人口數量' from city group by countrycode having sum(population) >50000000 order by sum(population) desc;
9.統計每個國家人口超過5000萬的名稱,并且按照從大到小排序,取前5
select countrycode'國家',sum(population)'人口數量' from city group by countrycode having sum(population) >50000000 order by sum(population) desc limit 5;
統計中國每個省人口超過500萬的名稱,并且按照從大到小排序,取前5
select District,sum(population) from city where countrycode='chn' group by District having sum(population) >5000000 order by sum(population) desc limit 5;
10.統計中國每個省人口超過500萬的名稱,并且按照從大到小排序,跳過前6行,顯示后面10行
select District,sum(population) from city where countrycode='chn' group by District having sum(population) >5000000 order by sum(population) desc limit 6,10;
11.執行順序
select 聚合運算 from? where group_by having order by? limit
? ? ? ? 4? ? ? ? 1? ? 2? ? ? 3? ? ? 5? ? ? ? 6? ? ? 7