願意學習的人都是孤獨的,是以,狼才獨行牛羊才成群,我們都是站在一條線上的對手。
因為内容較多是以回顧較多
1. MySQL資料庫的特點:
關系型資料庫,跨平台,支援多種開發語言
2. 啟動和連接配接資料庫
sudo /etc/init.d/mysql start | restart | stop | status
mysql -hlocalhost -uroot -p123456
3. SQL 語句
1. 庫的管理
show databases;
select database();
create database 庫名;
use 庫名;
show tables;
drop database 庫名;
2. 表的管理
use 庫名;
create table 表名(字段名 資料類型,字段名 資料類型);
insert into 表名 values (行記錄),();
insert into 表名(字段1,字段2) values(字段值1,字段值2);
select * from 表名;
select 字段名 from 表名;
select * from 表名 where 條件;
desc 表名;
drop table 表名;
4. 資料類型
1. 數值類型
整型 :
int tinyint bigint smallint
有無符号 :signed(預設有符号)unsigned(無符号)
浮點型 :
float double decimal (有符号)
使用 :
float(m,n) m表示總位數 n表示小數位位數
2. 字元型
char(m) varchar(m) text blob
定長 char(m) : 根據指定的最大字元數,固定配置設定存儲
空間,速度快,效率高,浪費空間
變長 varchar(m) : 在不超過最大字元數的情況下,動态
配置設定存儲空間,節省空間,效率低
整型的顯示寬度與字元的最大個數:
int(11) char(10)
11 : 表示整型的顯示寬度,跟存儲空間無關,跟
資料類型的取值範圍有關
10 :表示存儲空間,超出無法存入
3. 枚舉類型
單選 :使用枚舉定義字元串集合,作為選項,執行資料
存儲時必須從給定的集合中選取
多選 :使用集合定義字元串
course set('1','2','3')
values ('1,2,3')
day02
1. MySQL 資料類型
1. 數值類型
2. 字元型
3. 枚舉與集合
4. 日期與時間
1. date : 表示日期 "YYYY-MM-DD"
2. time : 表示時間 "hh:mm:ss"
3. datetime : 表示日期時間 "YYYY-MM-DD hh:mm:ss"
4. timestamp : 表示日期時間 "YYYY-MM-DD hh:mm:ss"
注意 :
日期時間的表示方法 :
使用字元串表示,格式可采用 "2011/11/11 11:11:11"
"2011-11-11 11:11:11"
"20181201103050"
日期時間函數 :
1. now() 傳回目前系統時間
2. curdate() 傳回目前日期
3. curtime() 傳回目前時間
4. year(date) 根據給定的日期擷取年份資訊
5. date('20111010121212') 擷取日期資訊
6. time('20111010121212') 擷取時間資訊
練習 :
在表中插入若幹條記錄
查詢2018年8月30日中有哪些使用者充值了
select id,name,czTime from info2
where date(czTime)='20180830';
查詢2018年8月份的充值資訊
8月份 20180801 - 20180831
where date(czTime)>="20180801" and
date(czTime)<="20180831"
查詢2018年8月30日10:00:00~15:00:00之間的充值記錄
select * from info2 where
date(czTime)="20180830"
and time(czTime)>="100000"
and time(czTime)<="150000";
方法2 :
2018-08-30 10:00:00 ~ 2018-08-30 15:00:00
select * from info2 where
czTime>="20180830100000"
and czTime<="20180830150000";
日期時間運算
1. 文法格式 :
select * from info2 where 字段名
運算符(時間-interval 時間間隔 機關)
時間間隔 機關 :
1 day | 1 month | 1 year
1 hour | 1 minute
正值表示過去的時間點 1 day 一天前
負值表示未來的時間點 -1 day 一天後
2. 練習 :
1. 查詢一天以内的充值記錄
select * from info2 where
czTime>(now()-interval 1 day);
類似于比較運算
czTime > "20181129111111"
2. 查詢一年前所有的充值記錄
where czTime<"20171129111111";
where czTime<(now()-interval 1 year);
3. 查詢1天前,3天内的充值記錄
where czTime < (now()-interval 1 day)
and czTime > (now()-interval 3 day);
4. 查詢20171010 一年前的所有充值記錄
where czTime <
("20171010200000"-interval 1 year);
類似于
czTime < "20161010200000"
2. 修改表字段(修改結構)
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 新表名
練習 :
修改age字段的資料類型為tinyint 無符号
重命名表
删除身高字段
3. 修改表記錄(修改表中資料)
1. 删除表記錄
delete from 表名 where 條件;
注意 :
where 條件可以省略,delete from 表名;
表示清空表記錄
2. 更新操作
update 表名 set 字段名=值,字段名=值 where 條件;
注意 :
更新操作中,where條件必須寫,如果省略,會
将表中所有記錄都進行修改
練習 :
基于hero表操作
1. 查詢所有蜀國人的資訊
select * from hero where country = '蜀國';
2. 查詢所有女英雄的姓名,國家
select name,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='貂蟬';
7. 删除表記錄
delete from hero;
4. 運算符
1. 比較運算符
> >= < <= = !=
練習(sanguo表)
1. 查詢攻擊力高于150的英雄姓名及攻擊值
select name,gongji from sanguo where gongji > 150;
2. 将表中趙雲的攻擊力設定為360,防禦值99
update sanguo set gongji=360,fangyu=99 where name="趙雲";
2. 邏輯運算符
1. and 與
連接配接兩個條件,要求同時成立
2. or 或
連接配接條件,表示任意一個條件成立都可以
練習 :
1. 找出攻擊值高于200的蜀國英雄的姓名
select name from sanguo where gongji>200
and country="蜀國";
2. 将吳國英雄中攻擊值為110的資料,修改為
100的攻擊力,50的防禦力
update sanguo set gongji=100,fangyu=50
where country="吳國" and gongji=110;
3. 查詢蜀國和魏國的英雄資訊
select * from sanguo where
country="蜀國" or country="魏國";
或 :
select * from sanguo where country != '吳國';
3. 範圍内查找
1. between 值1 and 值2
2. where 字段名 in (值1,值2)
查找字段值在給定集合範圍内的資料
3. where 字段名 not in (值1,值2)
查找字段值不在給定集合範圍内的資料
練習 :
1. 查找攻擊值在100到200之間的英雄資訊
select * from sanguo where gongji
between 100 and 200;
2. 查找攻擊值在100到200之間蜀國英雄資訊
select * from sanguo where gongji
between 100 and 200 and country="蜀國";
3. 查找蜀國和吳國以外,其他國家中女英雄的資訊
select * from sanguo where
country not in ('蜀國','吳國')
and sex='女';
4. 查找id=1,3,5的蜀國英雄資訊和貂蟬的資訊
select * from sanguo where
(id in (1,3,5) and country="蜀國")
or name="貂蟬";
4. 比對空與非空
注意 :
null 是特殊的值類型,表示空,
使用比較運算符 = 查詢時,傳回空的資料,查詢無果
區分 :
null : 關鍵字,值類型
'null' : 字元串,普通字元串
1. 比對 null
where 字段 is null
2. 比對非空
where 字段 is not null
練習 :
1. 查詢name為null的英雄資訊
select * from sanguo where name is null;
2. 查詢name為''的英雄資訊
select * from sanguo where name='';
3. 空字元串
空字元串,指沒有任何有效顯示字元的字元串
例 :
'' 等價于 ' '
都是空字元串,空格不計入有效字元
5. 模糊查找
1. 文法
where 字段名 like 表達式
2. 表達式文法 :
1. _ : 表示比對單個字元
2. % : 表示比對0個或多個字元
練習 :
1. 比對姓名為三個字的英雄資訊
select * from sanguo where name like '___';
2. 比對姓名至少是兩個字的英雄資訊
select * from sanguo where name like '__%';
注意 :
null 空類型不會被比對出來,隻能通過
is null / is not null 比對
空字元串可以通過 '%' 模糊比對
5. SQL查詢操作
1. 總結(書寫順序,從上至下;執行順序,看序号)
3. select 聚合函數 from 表名
1. where 條件
2. group by...
4. having ...
5. order by...
6. limit
2. order by..
對查詢結果進行排序
文法 :
where 條件 order by 字段名 ASC/DESC
asc : 升序排列(預設排序方式)
desc : 降序排列
練習 :
1. 将英雄按照防禦值從高到低排列
select * from sanguo order by fangyu desc;
2. 将蜀國英雄按攻擊值從高到低排列
select * from sanguo where country="蜀國"
order by gongji desc;
3. 将魏蜀兩國英雄中名字為三個字的,按照防禦值
升序排列
select * from sanguo where
country in ('魏國','蜀國') and
name like '___' order by fangyu;
3. limit
分頁查詢 :限制查詢結果的顯示數量和顯示位置
文法 :
1. limit 永遠放在SQL語句的最後書寫
2. limit n ; 顯示n條資料
3. limit m,n ;
表示從第m+1條資料開始顯示
顯示n條
例 :
limit 2,3 顯示第3,4 5條資料
練習 :
1. 在蜀國英雄中,查找防禦值倒數第二名
至第四名的英雄記錄
select * from sanguo where
country="蜀國"
order by fangyu
limit 1,3;
2. 在蜀國英雄中,查找攻擊值前3名且姓名
不為null的英雄的姓名,攻擊值
select name,gongji from sanguo where
country="蜀國" and
name is not null
order by gongji desc
limit 3;
3. 已知每頁顯示5條資料,顯示第四頁的資料
1-5 6-10 11-15 16-20
limit 15,5
4. 聚合函數
1. 對指定字段中的資料進行二次處理
2. 分類 :
avg(字段) :求平均值
max(字段) :求最大值
min(字段) :求最小值
sum(字段) :求和
count(字段) :統計目前字段中記錄的條數
練習資料需要自己建立:建表,添加人物以及資料!