系統(tǒng)學(xué)測試 - Mysql數(shù)據(jù)庫的認(rèn)識和使用(上)

1.?前后臺數(shù)據(jù)交換過程

前后臺數(shù)據(jù)交換圖

2.?數(shù)據(jù)庫基本知識

2.1.關(guān)系型數(shù)據(jù)庫概念

關(guān)系型數(shù)據(jù)庫,是指采用了關(guān)系模型來組織數(shù)據(jù)的數(shù)據(jù)庫,其以行和列的形式存儲數(shù)據(jù),以便于用戶理解,關(guān)系型數(shù)據(jù)庫這一系列的行和列被稱為表,一組表組成了數(shù)據(jù)庫。用戶通過查詢來檢索數(shù)據(jù)庫中的數(shù)據(jù),而查詢是一個(gè)用于限定數(shù)據(jù)庫中某些區(qū)域的執(zhí)行代碼。關(guān)系模型可以簡單理解為二維表格模型,而一個(gè)關(guān)系型數(shù)據(jù)庫就是由二維表及其之間的關(guān)系組成的一個(gè)數(shù)據(jù)組織。?

2.2.?常見的數(shù)據(jù)庫系統(tǒng)

常見的數(shù)據(jù)庫管理系統(tǒng)有:

????Oracle?甲骨文

????DB2 IBM出品的一些列關(guān)系型數(shù)據(jù)庫

????Mysql?開源數(shù)據(jù)庫軟件

????SQL Server?微軟

????Redis?最好用的緩存數(shù)據(jù)庫



2.3.?MySQL數(shù)據(jù)庫服務(wù)器、數(shù)據(jù)庫和表的關(guān)系

所謂安裝數(shù)據(jù)庫服務(wù)器,只是在機(jī)器上裝了一個(gè)數(shù)據(jù)庫管理程序,這個(gè)管理程序可以管理多個(gè)數(shù)據(jù)庫,一般開發(fā)人員會針對每一個(gè)應(yīng)用創(chuàng)建一個(gè)數(shù)據(jù)庫。

為保存應(yīng)用中實(shí)體的數(shù)據(jù),一般會在數(shù)據(jù)庫創(chuàng)建多個(gè)表,以保存程序中實(shí)體的數(shù)據(jù)。數(shù)據(jù)庫服務(wù)器、數(shù)據(jù)庫和表的關(guān)系如圖所示:


2.4.?數(shù)據(jù)在數(shù)據(jù)庫中的存儲方式

表的一行稱之為一條記錄


2.5.?SQL語言

Structured Query Language, 結(jié)構(gòu)化查詢語言

SQL是用來存取關(guān)系數(shù)據(jù)庫的語言,具有查詢、操縱、定義和控制關(guān)系型數(shù)據(jù)庫的四方面功能


3.?數(shù)據(jù)庫的安裝

安裝5.7.3版本

參考MySQL安裝文件

{詳細(xì)視頻和資料,請前往騰訊課堂搜索育華志遠(yuǎn)查收}


4.?SQL分類

4.1.?DDL(數(shù)據(jù)定義語句--了解

數(shù)據(jù)定義語言- Data Definition Language

用來定義數(shù)據(jù)庫對象:庫、表、列等;創(chuàng)建、刪除、修改:庫、表結(jié)構(gòu)

4.2.?DML(數(shù)據(jù)操縱語句--掌握

數(shù)據(jù)處理語言- Data Manipulation Language

在數(shù)據(jù)庫表中更新,增加和刪除記錄

如update, insert, delete --- 增刪改

4.3.?DQL(數(shù)據(jù)查詢語句--掌握

數(shù)據(jù)查詢語言– Data Query Language

select

4.4.?DCL(數(shù)據(jù)控制語句--了解

數(shù)據(jù)控制語言– Data Control Language

指用于設(shè)置用戶權(quán)限和控制事務(wù)語句

如grant,revoke,if…else,while,begin transaction


5.DDL(數(shù)據(jù)定義語句)

5.1.?數(shù)據(jù)庫創(chuàng)建和操作

create?database??數(shù)據(jù)庫名;??創(chuàng)建數(shù)據(jù)庫????

create?database?testDB;?????

show?databases;??展示數(shù)據(jù)庫

show create database; 展示數(shù)據(jù)庫的創(chuàng)建細(xì)節(jié)

use?testDB;????切換數(shù)據(jù)庫?????

select?database();?查看當(dāng)前數(shù)據(jù)庫??

alter database character set 字符集修改數(shù)據(jù)庫

drop?database?數(shù)據(jù)庫名;????刪除數(shù)據(jù)庫????

drop?database?testDB;?


5.2.?表的創(chuàng)建

1.?[endif]CREATE?TABLE?table_name? (??

field1??datatype,??

field2??datatype,??

?field3??datatype??

);?

field:指定列名 datatype:指定列類型??

注意:創(chuàng)建表時(shí),要根據(jù)需保存的數(shù)據(jù)創(chuàng)建相應(yīng)的列,并根據(jù)數(shù)據(jù)的類型定義相應(yīng)的列類型。


5.3.?數(shù)據(jù)類型

int:整型

float/double:浮點(diǎn)型,例如double(5,2)表示最多5位,其中必須有2位小數(shù),即最大值為999.99最小值是0.01;??

decimal:浮點(diǎn)型,在表單錢方面使用該類型,因?yàn)椴粫霈F(xiàn)精度缺失問題;對十進(jìn)制運(yùn)算比較精確的類型

char:固定長度字符串類型;char(255),數(shù)據(jù)的長度不足指定長度,補(bǔ)足到指定長度!

sex?Char(2)???????男???

varchar:可變長度字符串類型;varchar(65535),

name?varchar(10)? ?

text(clob):字符串類型;??

blob:字節(jié)類型;??

date:日期類型,格式為:yyyy-MM-dd;

time:時(shí)間類型,格式為:hh:mm:ss

timestamp:時(shí)間戳類型;若定義一個(gè)字段為timestamp,這個(gè)字段里的時(shí)間數(shù)據(jù)會隨其他字段修改的時(shí)候自動刷新,所以這個(gè)數(shù)據(jù)類型的字段可以存放這條記錄最后被修改的時(shí)間。

5.4. 表的刪除修改查看

alter?table?表名?add?列名?列的類型?列的約束??

alter?table?表名?modify?列名?列的類型?列的約束???

show?tables?;?查看當(dāng)前數(shù)據(jù)庫中所有的表名??

show?create?table?表名:?查看表的定義結(jié)構(gòu)/創(chuàng)建語句??

desc?表名?:?查看表的結(jié)構(gòu)

drop?table?表名??


5.5.創(chuàng)建表練習(xí)

創(chuàng)建一個(gè)員工表employee ----查看表結(jié)構(gòu): desc 表名;

CREATE TABLE employee (

id INT ,

NAME VARCHAR (20),

gender VARCHAR (20),

birthday date,

entry_date date,

job VARCHAR (20),

salary FLOAT (10, 2),

resume text

);


創(chuàng)建一個(gè)商品表product 有pid,price價(jià)格,shelves_date 上架日期 shelves_time上架時(shí)間,tamp時(shí)間戳

CREATE TABLE product (

pid INT,

price FLOAT (9,2),

shelves_date date,

shelves_time time,

tamp TIMESTAMP

);

{大家自己可以上手試一下,多多練習(xí)!}

5.6.?單表字段的約束

定義主鍵約束

????????primary key:不允許為空,不允許重復(fù)

????????刪除主鍵:alter table tablename drop primary key ;

? ? ? ? 主鍵自動增長:auto_increment


定義唯一約束? ?unique??

例如:name varchar(20) unique

定義非空約束? ?not null??

例如:salary double not null


6.?DML(數(shù)據(jù)操縱語句)

6.1.數(shù)據(jù)庫CRUD語句

Insert語句 ???(增加數(shù)據(jù))

Update語句 ?(更新數(shù)據(jù))

Delete語句 ???(刪除數(shù)據(jù))

Select語句 ??(查找數(shù)據(jù))


6.2.?Insert語句

1. 方式一:INSERT?INTO?表名(列名1,列名2,?...)?VALUES(列值1,?列值2,?...);??

2. 方式二:INSERT?INTO?表名(列名1,列名3)?VALUES(列值1,?列值3);??

3. 方式三:INTERTINTO?表名?VALUES(列值1,?列值2)



插入的數(shù)據(jù)應(yīng)與字段的數(shù)據(jù)類型相同。

數(shù)據(jù)的大小應(yīng)在列的規(guī)定范圍內(nèi),例如:不能將一個(gè)長度為80的字符串加入到長度為40的列中。

在values中列出的數(shù)據(jù)位置必須與被加入的列的排列位置相對應(yīng)。

字符和日期型數(shù)據(jù)應(yīng)包含在單引號中。

插入空值:不指定或insert into table value(null)


6.3.?使用insert語句向表中插入三個(gè)員工的信息。


方式一:

insert into employee (id,name,gender,birthday,entry_date,job,salary,resume) values(1,"張三","男","1999-10-10","2020-12-20","文員",4000.01,"來自山東");

方式二:

insert into employee ?values(2,"李四","男","1998-10-10","2020-12-30","會計(jì)",4500.01,"來自北京");

方式三:-多條插入

insert into employee ?values(3,"王五","男","1998-10-10","2020-12-30","會計(jì)",4500.01,"來自北京"),

(4,"趙六","男","1998-10-10","2020-12-30","會計(jì)",4500.01,"來自北京"),

(5,"小花","女","1998-10-10","2020-12-30","出納",4500.01,"來自上海");


6.4.?update語句

修改數(shù)據(jù):UPDATE?表名?SET?列名1=列值1,?列名2=列值2,?...?[WHERE?條件]?

UPDATE語法可以用新值更新原有表行中的各列。

SET子句指示要修改哪些列和要給予哪些值。

WHERE子句指定應(yīng)更新哪些行。如沒有WHERE子句,則更新所有的行。


練習(xí):

-- 將所有員工薪水修改為5000元。

update employee set salary=5000;

-- 將姓名為張三的員工薪水修改為3000元。

update employee set salary=3000 where name="張三";

-- 將姓名為李四的員工薪水修改為4000元,job改為ccc。

update employee set salary=4000,job="ccc" where name="李四";

-- 將王五的薪水在原有基礎(chǔ)上增加1000元。

update employee set salary=salary+1000 where name="王五";


6.5.?delete語句

1.?DELETE?FROM?表名?[WHERE?條件];??


如果不使用where子句,將刪除表中所有數(shù)據(jù)。

delete語句不能刪除某一列的值(可使用update)

使用delete語句僅刪除記錄,不刪除表本身。如要刪除表,使用drop table語句。

同insert和update一樣,從一個(gè)表中刪除記錄將引起其它表的參照完整性問題,在修改數(shù)據(jù)庫數(shù)據(jù)時(shí),頭腦中應(yīng)該始終不要忘記這個(gè)潛在的問題。

刪除表中數(shù)據(jù)也可使用TRUNCATE TABLE語句,它和delete有所不同,參看mysql文檔。

* delete可以加條件 truncate是刪除整張表

* delete支持回滾 truncate不支持

* delete清理速度慢 trucate快


練習(xí):

刪除表中名稱為張三的記錄。? ? ? delete from employee where name="張三";

刪除表中所有記錄。? ? ? ? ? ? ? ? ? ? ?delete from employee;

使用truncate刪除表中記錄。? ? ? ? ?truncate employee;


7.?DQL(數(shù)據(jù)查詢語句--掌握

7.1.?簡單查詢

1.SELECT?*?FROM?表名;????{其中“*”表示查詢所有列}??

7.2.?去重復(fù)查詢

關(guān)鍵字distinct

select distinct job from employee;

7.3.?查詢指定列

SELECT?列1?[,?列2,?...?列N]?FROM?表名;

select name,job,salary from employee;

7.4.?列運(yùn)算

數(shù)據(jù)類型的列可以做加、減、乘、除運(yùn)算

給員工漲工資【倍數(shù)1.5】:select salary*1.5 from employee;

工資加獎金:select?*,salary+bonus?from?employee;??

當(dāng)數(shù)字碰到null的時(shí)候整個(gè)就會變成null??

select *,salary*12+ifnull(bonus,0) from employee;

給列起別名? :查詢出的結(jié)果集中的列名稱不好看,可以給列名起個(gè)別名,

select *,salary*12+ifnull(bonus,0) 年薪?from employee;


7.5.?模糊查詢

關(guān)鍵字:like

_表示的單個(gè)字符 。%表示的是多個(gè)字符

select * from employee where name like "小_"; 查詢的結(jié)果是兩個(gè)字的名字 并以小字開頭

select * from employee where name like "小%"; 查詢的結(jié)果是未知個(gè)字的名字但是是以小字開頭

select * from employee where name like "%花%"; 查詢的結(jié)果最少也是一個(gè)字,有可能花開頭也有可能花結(jié)尾 也有可能花在中間的位置 【這種模糊查詢才是經(jīng)常使用的】


7.6.?關(guān)系運(yùn)算符

= 等于 , != 不等于, > ,? < , >= , <=

select * from employee where job >3000;

select * from employee where job != 3000;


7.7.?邏輯運(yùn)算符

and 并且的意思? ?多個(gè)條件的話需要都要滿足

or或者的意思? ? 多個(gè)條件的話有一個(gè)滿足即可


select * from employee where job="會計(jì)" and salary>3000;

select * from employee where job="會計(jì)" &&?salary>3000;

select * from employee where gender="女" or salary>9000;

select * from employee where gender="女" ||?salary>9000;


7.8.limit 分頁查詢

Limit 開始索引,長度

limit m,n ???第一頁:limit 0,10 ?(下標(biāo)從0開始,10表示加載10條數(shù)據(jù)) 第二頁:limit 10,10 第三頁: limit 20,10


7.9.between ... and ...

查詢薪資是6000到9000之間的員工? 【不包含開始和結(jié)束】

select * from employee where salary between 6000 and 9000;


7.10.?in在范圍中

查詢名字是小王/張/趙的員工

select * from employee where name in ("小張","小王","小趙");

not in? ? 是不在范圍內(nèi)的意思


7.11.?排序order by

1. ASC【升序排序】? ? 默認(rèn)就是升序排序

2. DESC【降序排序】??


select * from employee order by birthdayasc;按照出生時(shí)間排序(數(shù)字從小到大)

select * from employee order by birthday desc;按照出生時(shí)間倒序(數(shù)字從大到小)

select * from employeeorder by salary desc,birthday desc;(多條件排序)

select * from employee order?by salary desc limit 0,3;查詢salary 最大的三名


8.?聚合函數(shù)

8.1.?count() :統(tǒng)計(jì)數(shù)量

Count(列名)返回某一列,行的總數(shù)

1. Select?count(*)|count(列名)?from?表名[WHERE?條件]???

coun(*) 和count(1) 的結(jié)果都是一樣的最終都會得到總條數(shù)

練習(xí):

-- 統(tǒng)計(jì)一個(gè)班級共有多少學(xué)生?? ? ? ? ? ? ? ? ? select count(*) from student;

-- 統(tǒng)計(jì)數(shù)學(xué)成績大于90的學(xué)生有多少個(gè)?? ? select count(*) from student where math>90;

-- 統(tǒng)計(jì)總分大于250的人數(shù)有多少?

select count(*) from student where (math+english+chinese)>250;


8.2.?sum :求和

1.?Select?sum(列名){,sum(列名)…}?from?表名[WHERE?條件]?????


練習(xí):

統(tǒng)計(jì)一個(gè)班級數(shù)學(xué)總成績?? ?select sum(math) 數(shù)學(xué)總成績 from student;

統(tǒng)計(jì)一個(gè)班級語文、英語、數(shù)學(xué)各科的總成績

select sum(chinese),sum(english),sum(math) from student;

統(tǒng)計(jì)一個(gè)班級語文、英語、數(shù)學(xué)的成績總和

select sum(chinese+english+math) from student;


8.3.??avg() ?:平均值

AVG函數(shù)返回滿足where條件的一列的平均值

1.?Select?avg(列名){,avg(列名)…}?from?表名?[WHERE]? ?

練習(xí):

統(tǒng)計(jì)一個(gè)班級語文成績平均分????????????select avg(chinese) from student;

求一個(gè)班級數(shù)學(xué)平均分?? ? ? ? ? ? ? ? ? ?select avg(math) from student;

求一個(gè)班級總分平均分? ? ? ? ? ? ? ? ? ? ? ?select avg(chinese+math+english) from student;


8.4.?max() :最大值/min() :最小值

Max/min函數(shù)返回滿足where條件的一列的最大/最小值

1.?Select?max(列名) from?tablename?[WHERE?條件]???


練習(xí):

求班級數(shù)學(xué)最高分和最低分

select max(math),min(math) from student;

求班級總分最高分和最低分

select max(math+english+chinese) from student;

求數(shù)學(xué)最高分的同學(xué)

select * from student where math=(select max(math) from student);

select * from student order by math desc limit 0,1;(本條語句結(jié)果不準(zhǔn)確 最高分相同 只能查出一條數(shù)據(jù))

求班級總分最高的同學(xué)

select * from student where (chinese+english+math)=(select max(chinese+math+english) from student );

select * from student order by (chinese+english+math) desc limit 0,1;(本條語句結(jié)果不準(zhǔn)確 最高分相同 只能查出一條數(shù)據(jù))

9.?分組查詢

分組原理:按照分組的列名,將該列中數(shù)據(jù)相同的部分分到一組,然后對每一組中的數(shù)據(jù)進(jìn)行計(jì)算。【分組查詢不是用來查詢個(gè)人信息的】

1.?select??列名?from?表名?where??限定條件?group?by?分組的列?[having??分組之后的篩選條件]??

Having和where均可實(shí)現(xiàn)過濾,但在having可以使用聚集函數(shù),having通常跟在group by后,它作用于分組


練習(xí)表數(shù)據(jù)

create table orders(id int,product varchar(20),price float);


insert into orders(id,product,price) values(1,'電視',900);

insert into orders(id,product,price) values(2,'洗衣機(jī)',100);

insert into orders(id,product,price) values(3,'洗衣粉',90);

insert into orders(id,product,price) values(4,'桔子',9);

insert into orders(id,product,price) values(5,'洗衣粉',90);



練習(xí):

對訂單表中商品歸類后,顯示每一類商品的總價(jià)

select product, sum(price) from orders group by product;

查詢購買的商品類別,并且每類總價(jià)大于100的商品

select product, sum(price) from orders group by product having sum(price)>100;

查詢每一類商品買的個(gè)數(shù)

select product,count(*) from orders group by product;

查詢出購買個(gè)數(shù)大于1的商品

select product,count(*) from orders group by product having count(*)>1;


10.?Navicat工具使用

10.1.?安裝

10.2.?連接

{詳細(xì)安裝視頻和使用步驟,騰訊課堂請搜索育華志遠(yuǎn)查看}

今天先聊到這里,休息一下!后期努力更新中...........

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基礎(chǔ)查詢 select 查詢列表 from 表名; USE myemployees; //最好寫上 1.查詢表中的...
    曖莓悠閱讀 691評論 0 1
  • SQL語言基礎(chǔ) 本章,我們將會重點(diǎn)探討SQL語言基礎(chǔ),學(xué)習(xí)用SQL進(jìn)行數(shù)據(jù)庫的基本數(shù)據(jù)查詢操作。另外請注意本章的S...
    厲鉚兄閱讀 5,460評論 2 46
  • 7.1數(shù)據(jù)庫表的基本結(jié)構(gòu) 關(guān)系結(jié)構(gòu)數(shù)據(jù)庫是以表格(Table)進(jìn)行數(shù)據(jù)存儲,表格由“行和“列”組成。 執(zhí)查詢語句返...
    Ten_tones閱讀 385評論 0 0
  • 數(shù)據(jù)庫基礎(chǔ)知識 為什么要使用數(shù)據(jù)庫 數(shù)據(jù)保存在內(nèi)存優(yōu)點(diǎn): 存取速度快缺點(diǎn): 數(shù)據(jù)不能永久保存 數(shù)據(jù)保存在文件優(yōu)點(diǎn):...
    淺時(shí)咣閱讀 440評論 0 1
  • 一、數(shù)據(jù)庫概述 1.為什么軟件測試工程師還需要學(xué)習(xí)數(shù)據(jù)庫以及開發(fā)方面的知識? 測試工程師的目的是找出軟件的不足,并...
    故里里閱讀 615評論 0 1

友情鏈接更多精彩內(nèi)容