將大量數(shù)據(jù)保存起來,通過計算機(jī)加工而成的可以進(jìn)行高效訪問的數(shù)據(jù)集合稱為數(shù)據(jù)庫(Database,DB)。將姓名、住址、電話號碼、郵箱地址、愛好和家庭構(gòu)成等數(shù)據(jù)保存到數(shù)據(jù)庫中,就可以隨時迅速獲取想要的信息了。用來管理數(shù)據(jù)庫的計算機(jī)系統(tǒng)稱為數(shù)據(jù)庫管理系統(tǒng)(Database Management System,DBMS)。DBMS有過數(shù)據(jù)的保存格式(數(shù)據(jù)庫的種類)來進(jìn)行分類,現(xiàn)階段主要有五種類型:層次數(shù)據(jù)庫(Hierarchical Database,HDB),關(guān)系數(shù)據(jù)庫(Relational Database,RDB),面向?qū)ο髷?shù)據(jù)庫(Object Oriented Database,OODB),XML 數(shù)據(jù)庫(XML Database,XMLDB),鍵值存儲系統(tǒng)(Key-Value Store,KVS)。DBMS 稱為關(guān)系數(shù)據(jù)庫管理系統(tǒng)(Relational Database Management System,RDBMS)。比較具有代表性的 RDBMS 有 Oracle Database :甲骨文公司;SQL Server :微軟公司;DB2 :IBM 公司;PostgreSQL :開源;mysql :開源。MySQL作為很好的 RDBMS 應(yīng)用軟件之一,使用率也是upup的。因?yàn)閼校闹胁僮鲀H在MySQL5.7上加以驗(yàn)證。
-
零、準(zhǔn)備
-
1、安裝MySQL
-
2、服務(wù)端啟動
-
3、客戶端連接
-
4、SQL 語句分類
-
一、數(shù)據(jù)庫操作
-
1、顯示數(shù)據(jù)庫
-
2、創(chuàng)建數(shù)據(jù)庫
-
3、使用數(shù)據(jù)庫
-
4、用戶管理
-
5、授權(quán)管理
-
二、數(shù)據(jù)表操作
-
1、創(chuàng)建表
-
2、刪除表
-
3、清空表
-
4、修改表
-
三、表內(nèi)容操作
-
1、增
-
2、刪
-
3、改
-
4、查
-
5、條件查詢
零、準(zhǔn)備
1、安裝MySQL
2、服務(wù)端啟動
mysql.server start
?
3、客戶端連接
mysql -u username -p --退出 QUIT 或者 Control+D
?
?
4、SQL 語句分類
-
DDL(Data Definition Language,數(shù)據(jù)定義語言)用來創(chuàng)建或者刪除存儲數(shù)據(jù)用的數(shù)據(jù)庫以及數(shù)據(jù)庫中的表等對象。DDL 包含以下幾種指令。
DDL(數(shù)據(jù)定義語言)
CREATE:創(chuàng)建數(shù)據(jù)庫和表等對象
DROP:刪除數(shù)據(jù)庫和表等對象
ALTER:修改數(shù)據(jù)庫和表等對象的結(jié)構(gòu)
-
DML(Data Manipulation Language,數(shù)據(jù)操縱語言)用來查詢或者變更表中的記錄。DML 包含以下幾種指令。
DML(數(shù)據(jù)操縱語言)
SELECT:查詢表中的數(shù)據(jù)
INSERT:向表中插入新數(shù)據(jù)
UPDATE:更新表中的數(shù)據(jù)
DELETE:刪除表中的數(shù)據(jù)
-
DCL(Data Control Language,數(shù)據(jù)控制語言)用來確認(rèn)或者取消對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行的變更。除此之外,還可以對 RDBMS 的用戶是否有權(quán)限操作數(shù)據(jù)庫中的對象(數(shù)據(jù)庫表等)進(jìn)行設(shè)定。DCL 包含以下幾種指令。
DCL(數(shù)據(jù)控制語言)
COMMIT:確認(rèn)對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行的變更
ROLLBACK:取消對數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行的變更
GRANT:賦予用戶操作權(quán)限
REVOKE:取消用戶的操作權(quán)限
一、數(shù)據(jù)庫操作
1、顯示數(shù)據(jù)庫
SHOW DATABASES;
默認(rèn)數(shù)據(jù)庫有以下:
mysql – 用戶權(quán)限相關(guān)數(shù)據(jù)
test – 用于用戶測試數(shù)據(jù)
information_schema – MySQL本身架構(gòu)相關(guān)數(shù)據(jù)
2、創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE 數(shù)據(jù)庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
一般是utf8。后續(xù)檢索、各種小工具都能用起來。
3、使用數(shù)據(jù)庫
--使用數(shù)據(jù)庫 USE 數(shù)據(jù)庫名稱; --顯示當(dāng)前使用的數(shù)據(jù)庫中所有表 SHOW TABLES;
4、用戶管理
--創(chuàng)建用戶 create user '用戶名'@'IP地址' identified by '密碼'; --刪除用戶 drop user '用戶名'@'IP地址'; --修改用戶 rename user '用戶名'@'IP地址'; to '新用戶名'@'IP地址'; --修改密碼 set password for '用戶名'@'IP地址' = Password('新密碼');
用戶權(quán)限相關(guān)數(shù)據(jù)保存在mysql數(shù)據(jù)庫的user表中,但不建議直接對其進(jìn)行操作。
5、授權(quán)管理
-- 查看權(quán)限 show grants for '用戶'@'IP地址'; -- 授權(quán) grant 權(quán)限 on 數(shù)據(jù)庫.表 to '用戶'@'IP地址' ; -- 取消權(quán)限 revoke 權(quán)限 on 數(shù)據(jù)庫.表 from '用戶'@'IP地址';
經(jīng)常使用的權(quán)限:
all privileges -除grant外的所有權(quán)限
select -僅查權(quán)限
select,insert -查和插入權(quán)限
使用* 匹配數(shù)據(jù)庫名和表名:
test.* ? ? -test數(shù)據(jù)庫所有表
*.* ? ?-所有數(shù)據(jù)庫所有表
使用%匹配IP地址
--舉個例子 grant all privileges on *.*TO '用戶名'@'%';
二、數(shù)據(jù)表操作
1、創(chuàng)建表
create table 表名( 列名 類型 NULL, 列名 類型 NOT NULL )ENGINE=InnoDB DEFAULT CHARSET=utf8
基本數(shù)據(jù)類型
MySQL的數(shù)據(jù)類型大致分為:數(shù)字型、字符型、日期型。
-
INT型: 用來指定存儲整數(shù)的列的數(shù)據(jù)類型(數(shù)字型),不能存儲小數(shù)。
-
CHAR型: 用來指定存儲字符串的列的數(shù)據(jù)類型(字符型)。可以像 CHAR(200) 這樣,在括號中指定該列可以存儲的字符串的長度(最大長度)。字符串超出最大長度的部分是無法輸入到該列中的。當(dāng)列中存儲的字符串長度達(dá)不到最大長度的時候,使用半角空格進(jìn)行補(bǔ)足。
-
VARCHAR型: 也可以通過括號內(nèi)的數(shù)字來指定字符串的最大長度(字符型)。但該類型的列是以可變長字符串的形式來保存字符串。可變長字符串即使字符數(shù)未達(dá)到最大長度,也不會用半角空格補(bǔ)足。
-
DATE型: 用來指定存儲日期(年月日)的列的數(shù)據(jù)類型(日期型)。
更多數(shù)據(jù)類型
默認(rèn)值
創(chuàng)建列時可以指定默認(rèn)。
create table tb1( nid int not null default 2, num int not null )
自增
如果為某列設(shè)置自增列,插入數(shù)據(jù)時無需設(shè)置此列,默認(rèn)將自增(表中只能有一個自增列)。
create table tb1( nid int not null auto_increment primary key, num int null ) create table tb1( nid int not null auto_increment, num int null, index(nid) )
注意:
1、對于自增列,必須是索引(含主鍵)。
2、對于自增可以設(shè)置步長和起始值
show session variables like ‘auto_inc%’;
set session auto_increment_increment=2;
set session auto_increment_offset=10;
show global ?variables like ‘auto_inc%’;
set global auto_increment_increment=2;
set global auto_increment_offset=10;
主鍵
一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
create table tb1( nid int not null auto_increment primary key, num int null ) create table tb1( nid int not null, num int not null, primary key(nid,num) )
外鍵
一個特殊的索引,只能是指定內(nèi)容
create table color( nid int not null primary key, name char(16) not null ) create table fruit( nid int not null primary key, smt char(32) null , color_id int not null, constraint fk_cc foreign key (color_id) references color(nid) )
2、刪除表
drop table 表名
3、清空表
delete from 表名 truncate table 表名
4、修改表
--添加列 alter table 表名 add column 列名 類型 --刪除列 alter table 表名 drop column 列名 --修改列 -- 類型 alter table 表名 modify column 列名 類型; -- 列名,類型 alter table 表名 change 原列名 新列名 類型; --添加主鍵 alter table 表名 add primary key(列名); --刪除主鍵 alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; --添加外鍵 alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段); --刪除外鍵 alter table 表名 drop foreign key 外鍵名稱 --修改默認(rèn)值 ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; --刪除默認(rèn)值 ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
三、表內(nèi)容操作
1、增
insert into 表 (列名,列名...) values (值,DEFAULT,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表A (列名,列名...) select (列名,列名...) from 表B
2、刪
--保留數(shù)據(jù)表,刪除全部行 delete from 表 delete from 表 where id=1 and name='dyan'; truncate 表
3、改
update 表 set name = 'dyan' where id>1;
4、查
select * from 表 select * from 表 where id > 1 select nid,name,gender as 新表名 from 表 where id > 1 子查詢的運(yùn)算符 =,<>,>,>=,<,<= is null, not, and, or,
5、條件查詢
1)條件 select * from 表 where id > 1 and name != 'dyan' and num = 12; select * from 表 where id between 5 and 16; select * from 表 where id in (11,22,33); select * from 表 where id not in (11,22,33); select * from 表 where id in (select nid from 表); 2)聚合 COUNT:計算表中的記錄數(shù)(行數(shù)) SUM:計算表中數(shù)值列中數(shù)據(jù)的合計值 AVG:計算表中數(shù)值列中數(shù)據(jù)的平均值 MAX:求出表中任意列中數(shù)據(jù)的最大值 MIN:求出表中任意列中數(shù)據(jù)的最小值 --后者會得到NULL之外的數(shù)據(jù)行數(shù) select count(*),count(<列名>) from 表名; 3)通配符 select * from 表 where name like 'ale%' - ale開頭的所有(多個字符串) select * from 表 where name like 'ale_' - ale開頭的所有(一個字符) 4)限制 select * from 表 limit 5; - 前5行 select * from 表 limit 4,5; - 從第4行開始的5行 select * from 表 limit 5 offset 4; - 從第4行開始的5行 5)分組 --group by 必須在where之后,order by之前 select num from 表 group by num select num,nid from 表 group by num,nid select num,nid from 表 where nid > 10 group by num,nid order nid desc select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid --取出符合指定條件的組having --SELECT → FROM → WHERE → GROUP BY → HAVING select num from 表 group by num having max(id) > 10 6)排序 select * from 表 order by 列 - 根據(jù) “列” 從小到大排列,默認(rèn)asc升序 select * from 表 order by 列 desc - 根據(jù) “列” 從大到小排列 select * from 表 order by 列1 desc,列2 asc - 根據(jù) “列1” 從大到小排列,如果相同則按列2從小到大排序 select 列名1,count(*) from 表 group by 列名1 order by count(*) 7)連表 無對應(yīng)關(guān)系則不顯示 select A.num, A.name, B.name from A,B Where A.nid = B.nid 無對應(yīng)關(guān)系則不顯示 select A.num, A.name, B.name from A inner join B on A.nid = B.nid A表所有顯示,如果B中無對應(yīng)關(guān)系,則值為null select A.num, A.name, B.name from A left join B on A.nid = B.nid B表所有顯示,如果B中無對應(yīng)關(guān)系,則值為null select A.num, A.name, B.name from A right join B on A.nid = B.nid 8)組合 組合,自動處理重合 select nickname from A union select name from B 組合,不處理重合 select nickname from A union all select name from B