1. 程式人生 > >MYSQL資料庫知識詳解(1)

MYSQL資料庫知識詳解(1)

目錄

  1. 基本DDL語句—建表語句CREATE TABLE
  2. 基本DML語句-INSERT/UPDATE/DELECT
  3. 基本DQL語句-SELECT

DDL語句—建表語句CREATE TABLE

1、資料庫
檢視所有資料庫
show databases;

2、使用資料庫
use 資料庫名;

3、檢視當前使用的資料庫
select database();

4、建立資料庫
create database 資料庫名 charset=utf-8;
例:
create database python charset=utf-8;

5、刪除資料庫
drop database 資料庫名;
例:
drop database python;

6、資料表
1)檢視當前資料庫中的所有表
show tables;

2)查看錶結構
desc 表名;

3)建立表
auto_increment 表示自動增長

CREATE TABLE table_name(
column1 datatype contrai,
column2 datatype,
column3 datatype,

columnN datatype,
PRIMARY KEY(one or more columns));

基本DML語句-INSERT/UPDATE/DELECT

練習:建立班級表
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(10));

建立學生表
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default 0,
height decimal(5,2),
gender enum(‘男’,‘女’’,‘保密’),
cls_id int unsigned default 0)

修改表—新增欄位
alter table 表名 add 列名 型別;
例:
alter table students change birthday birth datetime not null;

修改表—修改欄位:不重名版


alter table 表名 modify 列名 型別及約束;
例:
alter table students modify birth date not null;

修改表—刪除欄位
alter table 表名 drop 列名;
例:
alter table students drop birthday;

刪除表
drop table 表名;
例:
drop table students;

查看錶的建立語句
show create table 表名;
例:
show create table classes;

基本DQL語句-SELECT

1、where子句對行記錄進行過濾
1.查詢學生表中性別為女,體重超高60公斤的學生的所有資訊
select * from stu where sex=‘女’ and weight>60;

2.查詢學生表中1班或者2班中,身高超過190的學生
select * from stu where (cno=1 or cno=2) and height>190;
等效寫法
select * from stu where cno in(1,2) and height>190;

3.查詢學生表中3班學生裡面考分在400和505.5之間的女生
select * from stu where cno=3 and sex=‘女’ and score between 400 and 505.5;
等效寫法
select * from stu where cno=3 and sex=‘女’ and (score>=400and score<=505.5);

4.查詢學生表中沒有分配班級而且是女生的學生
select * from stu where cno is null and sex=‘女’;

5.在學生表體重低於40公斤且身高低於1.65米的學生,列出其姓名,身高,體重,總分以及總分佔750分滿分的百分比
select sname,height,weight,score,score/750*100 from stu where height/100<1.65;

6.在學生表中查詢學生姓名,第二個字是‘候’,或者第一個字是‘張’且名字只有兩個字的學生
select * from stu where sname like’候%’ or sname like '張’;

2、mysql 函式
1.char_length(str) 計算字串長度
學生表中姓名為三個字的學生有哪些?
select sname,char_length(sname) from stu where char_length(sname)=3;

2.concat(str1,str2) 拼接字串
學生表列印已分班的學生姓名和班級,以xxx是x班的形式列印結果
select concat(sname,‘是’,cno,‘班’) from stu where cno is not null;

3.substring 擷取字串
二班的同學都有什麼姓氏?
select sname,substring(sname,1,1) from stu where cno=2;

4.round(num,n) 保留小數點後幾位
計算肥胖學生許褚的BMI值,四捨五入保留2位小數,體重/身高^2
select round(weight/(height/100*height/100),2) from stu where sname=‘許褚’;

5.year(date1) 獲取日期date1的年份
學生表中哪些同學是1990年出生的?
select sname,birth,year(birth) from stu where year(birth)=1990;
學生表中哪些同學是8月份出生的?
select sname,birth,year(birth),month(birth) from stu where month(birth)=8;

6.curdate() 獲取當前日期
curtime() 獲取當前時間
now() 獲取當前的日期和時間
datediff(date1,date2) 返回date1和date2期間隔的天數

計算2018年6月1號號2018年情人節之間間隔的天數
sele datediff(‘2018-6-1’,‘2018-2-14’);

7.if(expr,v1,v2) 如果表示式expr成立,返回v1值,否則,返回v2值
如果學生高考分大於等於520,其為統招生,否則其為委培生,從學生表中查詢,顯示姓名,考分,型別(統招或委培)
select sname,score,if(score>=520,‘統招’,‘委培’)型別 from stu;

8.case when expr then v1 [when expr2 v2 then]…else vn end
case運算子為SQL語句增加了多重條件判斷比if函式更加靈活
如果考分700以上,優秀,600以上,良好,520以上,中等,否則,較差,按照這個原則列出學生表中的學生,顯示姓名,考分,和評判等級
select sname,score(case when score>=700 then ‘優秀’
when score>=600 then ‘良好’
when score>=520 then ‘中等’
else '較差’end) 等級 from stu;

9.空值問題
使用in時,值列表中有null值,不會返回null相關結果
select * from stu where cno in (2,null)
使用not in 列表中有空值時,無查詢結果返回
select * from stu where cno not in (2,null);
有空值會造成意想不到的問題,需要用空值函式先對空值進行處理
常用空值處理函式
ifnull(v1,v2)–如果v1為空返回v2,否則返回v1
isnull(expr)- 如果表示式為null空值,返回1,表示式非空則返回0

3、聚合函式
max(列名)
統計出列中所有行中的最大值,列的資料型別可以是數值、字元、日期型
min(列名)
統計出列中所有行中的最小值,列的資料型別可以是數值、字元、日期型
sum(列名)
統計出列中所有行中數值的總和,列的資料型別不能是字元型
avg(列名)
統計出列中所有行中數值的平均值,列的資料型別不能是字元型
count(列名)
統計出列中所有行的數量,列中如果有空值不會被統計技術

1.找出學生中最高的身高、最輕的體重、平均高考分數、學生的總數
select max(height),min(weight),sum(score)/count(),
avg(score),count(
),count(sno),count(cno) from stu;

2.用一個查詢語句找出學生中最低的身高、再顯示所有人的名字
select min(height),sname from stu;

4、select語句 distinct關鍵字
disinct關鍵字查詢出指定的1個或多個欄位的不重複記錄
查詢出學生表中都有哪些班級號,要求取出重複班級號
select distinct cno from stu;
列出學生表中不重複的班號和性別
select distinct cno,sex from stu;

5、列的別名
給列起個額外的名稱用來代替列原來的名稱
select sname [as] 姓名 from stu; ‘as’ 可寫可不寫
列出2班的學生姓名,性別、生日。表頭用對應的中文顯示
select sname as 學生姓名,sex 性別,birth 生日 from stu where cno=2;

6.select-order by子句
隊查詢結果集按照order by 後面指定的1列或多列排序
分為增序和降序
增序ASC,預設可以不寫
降序DESC
對於數值,增序是從小到大,降序是從大到小
對於日期和時間,增序是從遠到近,降序是從近到遠
對於英文字元,增序是從a到z,降序是z到a
對於中文字元,增序是按照字符集編碼從小到大,降序相反,如果選擇gbk編碼可以按照拼音來排序
對於序列的名稱,可以用以下方式
列名、列別名、列的序號、函式、表示式等

按照體重增序列出學生資訊
select * from stu order by weight;

展示已分班學生資訊,先按照班號增序,再按照考分降序排序
select * from stu where cno id not null order by cno,score desc;

按照拼音降序排列所有學生
select * from stu order by convert(sname using gbk)desc;

顯示學生性別,姓名,生日3列資訊,先按第1列,再按3列降序排序
select sex,sname,birth from stu order by 1,3 desc;

7、select-limit子句
select語句的limit子句最後執行
作用:僅顯示查詢結果集中部分內容
顯示stu表中考分最高的3名學生
select * from stu order by score desc limit 3;

select * from stu order by score desc limit 0,3;

8、select-group by 子句
group by子句按照指定的列column_name對標資料進行分組
group by後面跟得列也叫分組特性列
使用group by後,能選擇的列通常只能包括分組特性列和聚合函式

按照班號分組,列出學生表中的班號
select cno from stu group by cno;

按照班號分組,列出學生表中的班號,還要列出學生姓名
select sname,cno from stu group by cno;
查詢報錯
注意:學生有20人,姓名一共20行記錄,班號分組去重後有4行記錄,20行無法與4行拼接在一起
使用group by後,能選擇的列通常只能包括分組特性列和聚合函式

按照班號分組,列出學生表中的班號,統計每一個班的平均身高,平均體重,人數,最高分,不包括沒分班的那些同學
select cno 班號,avg(height)平均身高,avg(weight)平均體重,count(*)人數,
max(score)最高分 from stu where cno is not null group by cno;

按照學生出生年份分組,統計出所有學生每個年份的人數,最高分,最低分,按照年份排序
select year(birth) 出生年份,count(sno) 人數,max(score) 最高分, min(score) 最低分 from stu group by year(birth) order by 1;

找出已分班的學生中,哪些班學生的平均身高超過175,列出其班號和人數
select cno 班號,count(*) 人數 from stu where cno is not null group by cno having avg(height)>175 order by 1;

找出已分班的學生中,哪些班的學生每個人的身高都超過165,列出其班號和人數
select cno 班號,count(*) 人數 from stu where cno is not null group by cno having min(height)>165 order by 1;

統計1班的學生人數,列出班號和人數
方法1:
select cno 班號,count() 人數 from stu group by cno having cno=1;
方法2:
select 1 班號,count(
) 人數 from stu where cno=1;