數(shù)據(jù)庫基礎(chǔ)

1. 為什么軟件測試工程師還需要學(xué)習(xí)數(shù)據(jù)庫以及開發(fā)方面的知識?

測試工程師的目的是找出軟件的不足,并告訴開發(fā)工程師,出現(xiàn)問題的環(huán)境,操作步驟和輸入輸出數(shù)據(jù);優(yōu)秀的測試工程師,需要告訴開發(fā)團隊,軟件的不足,這類不足會導(dǎo)致什么情況,如何避免,以及如何去修改(這是為什么高級軟件測試工程師比開發(fā)工程師工資高的原因)。

測試工程師在測試軟件過程中,不僅僅需要在界面進行操作,還需要檢查數(shù)據(jù)庫中的數(shù)據(jù)是否正確,從而在軟件出現(xiàn)問題時候,能夠定位到問題原因

2. ? 什么是數(shù)據(jù)庫

Excle的數(shù)據(jù)的確很方便,但是對于企業(yè)來說就不一樣了。一個公司里面可能有成千上萬的Excel表格,還在不同的電腦上,而他們的員工和客戶需要實時看到企業(yè)給他們提供的所有數(shù)據(jù),這種文件管理的方法就很麻煩,總不能每分鐘都把一個新的巨大無比的Excel文件發(fā)給所有客戶呀。


數(shù)據(jù)庫呢,就是存儲數(shù)據(jù)的地方,就像冰箱是存儲食物的地方一樣。正是因為有了數(shù)據(jù)庫后,所有人可以直接在這個系統(tǒng)上查找數(shù)據(jù)和修改數(shù)據(jù)。例如你每天使用余額寶查看自己的賬戶收益,就是從后臺數(shù)據(jù)庫讀取數(shù)據(jù)后給你的。


數(shù)據(jù)庫的英文名稱叫DB(Database),那么數(shù)據(jù)庫里面有什么東東呢?


其實,數(shù)據(jù)庫通常包含一個或多個表組成。如果你用過Excel,就會知道Excel是一張一張的二維表。每個表都是由列和行組成的,其中每一列都用名字來標(biāo)識出來。同樣的,數(shù)據(jù)庫里存放的也是一張一張的表,只不過各個表之間是有聯(lián)系的。所以,簡單來說:


數(shù)據(jù)庫=多張表+各表之間的關(guān)系


其實數(shù)據(jù)庫是邏輯上的概念,它是一堆互相關(guān)聯(lián)的數(shù)據(jù),放在物理實體上,是一堆寫在磁盤上的文件,文件中有數(shù)據(jù)。這些最基礎(chǔ)的數(shù)據(jù)組成了表(table)。


常用的數(shù)據(jù)庫軟件,例如MySQL、Oracle、SQL Server等都是實現(xiàn)上面理論的數(shù)據(jù)庫。各個數(shù)據(jù)庫軟件的使用差別大同小異,但是因為MySQL是開源的,成為各大公司使用的主流,面試也主要以MySQL為主。


數(shù)據(jù)庫是按照數(shù)據(jù)的結(jié)構(gòu)來組織,存儲,和管理數(shù)據(jù)的倉庫,簡而言之,就是存放數(shù)據(jù)的倉庫

3. ?? 數(shù)據(jù)庫和SQL是什么關(guān)系?

結(jié)構(gòu)化查詢語言(Structured Query Language)簡稱SQL(發(fā)音:/?es kju? ?el/ "S-Q-L"),是一種特殊目的的編程語言,是一種數(shù)據(jù)庫查詢和程序設(shè)計語言,用于存取數(shù)據(jù)以及查詢、更新和管理關(guān)系數(shù)據(jù)庫系統(tǒng);同時也是數(shù)據(jù)庫腳本文件的擴展名。

數(shù)據(jù)庫里面放著數(shù)據(jù),而SQL是用來操作數(shù)據(jù)庫里數(shù)據(jù)的語言(工具)。

例如有一碗米飯(碗就是mysql,里面放的米是數(shù)據(jù)),你要吃碗里的米飯,拿什么吃?拿筷子(sql)。用筷子(sql)操作碗里(mysql)的米飯(數(shù)據(jù))

4.????數(shù)據(jù)庫有哪些

Oracle是最掙錢的數(shù)據(jù)庫,出自甲骨文公司,市場占有率非常高,功能非常強大,被一些大型企業(yè),電信,銀行,證券公司所使用


MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB 公司開發(fā),目前屬于 Oracle 旗下產(chǎn)品。MySQL 是最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)之一,在 WEB 應(yīng)用方面,MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應(yīng)用軟件。MySQL目前最流行的開源數(shù)據(jù)庫,被甲骨文公司所占用,威脅著老大Oracle的地位,開源,免費,支持多平臺,易用


SQLServer,微軟開發(fā)的數(shù)據(jù)庫,僅支持Windows操作系統(tǒng),號稱是windows上最好用的數(shù)據(jù)庫。數(shù)據(jù)庫的天下不僅僅是MySQL和Oracle的,SQLServer也正在慢慢崛起,微軟網(wǎng)羅了不少數(shù)據(jù)庫的專家,推出了不少重磅功能。


MongoDB,最好用的文檔型數(shù)據(jù)庫,是NOSQL類型數(shù)據(jù)庫的領(lǐng)導(dǎo)者之一,也是當(dāng)前最成功的NoSQL類型數(shù)據(jù)庫,數(shù)據(jù)存儲格式采用JSON形式,非常靈活。


Redis,最好的內(nèi)存級數(shù)據(jù)庫,查詢效率極高,并且在Redis 3.0之后,支持多種數(shù)據(jù)類型,String,Set,List,Hash等類型,開始支持集群,彌補了自身短板。是目前做緩存最流行的數(shù)據(jù)庫


Neo4J,最好的圖形化數(shù)據(jù)庫,流行話較低,但是圖形化數(shù)據(jù)庫的絕對領(lǐng)導(dǎo)者。


SQLite,最流行的嵌入式數(shù)據(jù)庫,占領(lǐng)手機行業(yè)的絕對領(lǐng)導(dǎo)者地位,Android和IOS兩大手機系統(tǒng),都內(nèi)嵌了SQLite數(shù)據(jù)庫,SQLite是一個完整的關(guān)系型數(shù)據(jù)庫,支持標(biāo)準(zhǔn)SQL,支持事務(wù)操作,程序包非常小,是嵌入式設(shè)備的最佳選擇

5.????MySQL數(shù)據(jù)庫的安裝配置

5.1 ?? 安裝配置

參考MySQL安裝文檔

5.2????安裝問題說明

卸載mysql:

1. 在控制面版中找到mysql的服務(wù) ?刪除

2. Mysql安裝所在的路徑進行刪除文件

3. 在運行輸入regedit進行對注冊表的刪除

4.重啟電腦


多次安裝不無法正常安裝的原因是:

1,my.ini文件沒有刪除

2,注冊表有對應(yīng)的mysql服務(wù)沒有刪除

3,之前的mysqld的3306的被占用


安裝步驟:


服務(wù)端:看表情包

客戶端:一路傻瓜安裝



有些同學(xué)的電腦,安裝時,有可能卡死在最后一步上,建議多試幾次,或者安裝的時候,先選擇標(biāo)準(zhǔn)安裝方式,安裝之后,再到my.ini文件下,搜索latin修改為utf8

數(shù)據(jù)庫語言的分類:

DDL數(shù)據(jù)庫定義語言 ???對庫的增刪改 ??

DML數(shù)據(jù)操作語言 ??????對表的增刪改

DQL數(shù)據(jù)庫查詢語言 ????對庫和表的查詢(模糊查詢,分頁查詢,子查詢,多表查詢 ?聚合函數(shù) 分類 分組 ?排序等)

DCl數(shù)據(jù)控制語言



因為在昨天的禪道中修改過數(shù)據(jù)庫的密碼今天使用數(shù)據(jù)庫的密碼是昨天改過的密碼

6. ? DDL-數(shù)據(jù)庫定義語言

6.1????概念及作用:

database defination language數(shù)據(jù)庫定義語言

用于定義數(shù)據(jù)庫,用于定義表結(jié)構(gòu)

6.2 ? 表中字段基本數(shù)據(jù)類型

MySQL支持多種類型,大致可以分為三類:數(shù)值、日期/時間和字符串(字符)類型

1、一個漢字占多少長度與編碼有關(guān):


UTF-8:一個漢字=3個字節(jié)


GBK:一個漢字=2個字節(jié)


[if !supportLists]2、[endif]varchar(n)表示 n 個字符,無論漢字和英文,Mysql 都能存入 n 個字符,僅是實際字節(jié)長度有所區(qū)別

取值范圍如果加了unsigned,則最大值翻倍,如 tinyint unsigned 的取值范圍為(0~256)。

int(m) 里的 m 是表示 SELECT 查詢結(jié)果集中的顯示寬度,并不影響實際的取值范圍,沒有影響到顯示的寬度,不知道這個 m 有什么用。


設(shè)一個字段定義為float(5,3),如果插入一個數(shù) 123.45678,實際數(shù)據(jù)庫里存的是 123.457,但總個數(shù)還以實際為準(zhǔn),即 6 位。

3、定點數(shù)

浮點型在數(shù)據(jù)庫中存放的是近似值,而定點類型在數(shù)據(jù)庫中存放的是精確值。

decimal(m,d) 參數(shù) m<65 是總個數(shù),d<30 且 d<m 是小數(shù)位。

4、字符串(char,varchar,_text)


char 和 varchar:

[if !supportLists]·?[endif]?1.char(n) 若存入字符數(shù)小于n,則以空格補于其后,查詢之時再將空格去掉。所以 char 類型存儲的字符串末尾不能有空格,varchar 不限于此。

[if !supportLists]·?[endif]?2.char(n) 固定長度,char(4) 不管是存入幾個字符,都將占用 4 個字節(jié),varchar 是存入的實際字符數(shù) +1 個字節(jié)(n<=255)或2個字節(jié)(n>255),所以 varchar(4),存入 3 個字符將占用 4 個字節(jié)。

[if !supportLists]·?[endif]?3.char 類型的字符串檢索速度要比 varchar 類型的快。

varchar 和 text:

[if !supportLists]·?[endif]?1.varchar 可指定 n,text 不能指定,內(nèi)部存儲 varchar 是存入的實際字符數(shù) +1 個字節(jié)(n<=255)或 2 個字節(jié)(n>255),text 是實際字符數(shù) +2 個字節(jié)。

[if !supportLists]·?[endif]?2.text 類型不能有默認值。

[if !supportLists]·?[endif]?3.varchar 可直接創(chuàng)建索引,text 創(chuàng)建索引要指定前多少個字符。varchar 查詢速度快于 text, 在都創(chuàng)建索引的情況下,text 的索引似乎不起作用。

5.二進制數(shù)據(jù)(_Blob)

[if !supportLists]·?[endif]?1._BLOB和_text存儲方式不同,_TEXT以文本方式存儲,英文存儲區(qū)分大小寫,而_Blob是以二進制方式存儲,不分大小寫。

[if !supportLists]·?[endif]?2._BLOB存儲的數(shù)據(jù)只能整體讀出。

[if !supportLists]·?[endif]?3._TEXT可以指定字符集,_BLO不用指定字符集。

6.日期時間類型


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

數(shù)據(jù)類型的屬性



6.3 ?? 數(shù)據(jù)庫語句操作

我們可以在登陸MySQL服務(wù)后,使用?create?命令創(chuàng)建數(shù)據(jù)庫,語法如下:


CREATE DATABASE數(shù)據(jù)庫名;


創(chuàng)建第一個數(shù)據(jù)庫mydb1

create database mydb1


在刪除數(shù)據(jù)庫過程中,務(wù)必要十分謹慎,因為在執(zhí)行刪除命令后,所有數(shù)據(jù)將會消失

drop命令刪除數(shù)據(jù)庫


刪除創(chuàng)建的數(shù)據(jù)庫

drop database mydb1;


在你連接到MySQL數(shù)據(jù)庫后,可能有多個可以操作的數(shù)據(jù)庫,所以你需要選擇你要操作的數(shù)據(jù)庫,


選擇數(shù)據(jù)庫

user mydb1


注意:所有的數(shù)據(jù)庫名,表名,表字段都是區(qū)分大小寫的。所以你在使用SQL命令時需要輸入正確的名稱。


查看數(shù)據(jù)庫創(chuàng)建細節(jié)

show create database mydb1


創(chuàng)建一個使用gbk字符集的數(shù)據(jù)庫

create database mydb2 character set gbk

6.4????表結(jié)構(gòu)語句操作

選擇數(shù)據(jù)庫

user mydb1


創(chuàng)建MySQL數(shù)據(jù)表需要以下信息:


表名

表字段名

定義每個表字段


創(chuàng)建表

create table student(id int,name varchar(20),sex varchar(20),age int,salery float(6,2),birthday date)


刪除表

drop table student;


查看所有表

show tables


查看表的創(chuàng)建細節(jié)

show create table student;


展示表結(jié)構(gòu)

desc student

在原有的學(xué)生基礎(chǔ)上添加address列

alter table student add address varchar(20)

在原有的學(xué)生基礎(chǔ)上刪除address列

alter table student drop address

6.5 定義表的約束

create table student(id int primary key auto_increment,name varchar(20) unique not null,sex varchar(20),age int,salery float(6,2),birthday date)

[if !supportLists]l?[endif]如果你不想字段為NULL可以設(shè)置字段的屬性為 NOT NULL, 在操作數(shù)據(jù)庫時如果輸入該字段的數(shù)據(jù)為NULL ,就會報錯。

[if !supportLists]l?[endif]AUTO_INCREMENT定義列為自增的屬性,一般用于主鍵,數(shù)值會自動加1。

[if !supportLists]l?[endif]PRIMARY KEY關(guān)鍵字用于定義列為主鍵。為了標(biāo)識數(shù)據(jù)庫記錄唯一性,不允許記錄重復(fù),且鍵值不能為空,主鍵也是一個特殊索引。您可以使用多列來定義主鍵,列間以逗號分隔。

[if !supportLists]l?[endif]UNIQUE KEY的用途:主要是用來防止數(shù)據(jù)插入的時候重復(fù)的

[if !supportLists]l?[endif]ENGINE設(shè)置存儲引擎,CHARSET 設(shè)置編碼

7. ? ? ?? DML-數(shù)據(jù)庫操作語言

7.1 ?? 概念及作用

DML data manipulation language數(shù)據(jù)庫操作語言,用以操作數(shù)據(jù)庫

7.2 ?? 插入數(shù)據(jù)

insert into student values(1,’zhangsan’,’nan’,19,389.10,’1999-10-10’);


查詢

select * from student


insert into student values(2,’李四’,’男’,19,389.10,’1999-10-10’);



插入中文會報錯


通知服務(wù)器客戶端使用的編碼是gbk

set character_set_client=gbk;


通知服務(wù)器客戶端查看結(jié)果集使用的編碼是gbk

set character_set_results=gbk;


insert into student(id,name,sex,age) values(3,’王五’,’男’,19);

7.3 ?? 刪除數(shù)據(jù)

刪除單條數(shù)據(jù)

delete from student where?id=1;


刪除所有數(shù)據(jù)

delete from student;


摧毀表

truncate table student;

7.4修改數(shù)據(jù)

設(shè)置所有人的年齡加10歲

update student set age=age+10


修改zhangsan為張三

update?student set name=’張三’?where name=’zhangsan’

修改王五的salery和出生日期

update student set salery=100.01,birthday=’1999-10-10’?where id=3;

8. ? DQL-數(shù)據(jù)庫查詢語言

8.1 ?? 概念及作用

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

8.2 ?? 數(shù)據(jù)查詢

刪除student

drop table student

創(chuàng)建數(shù)據(jù)庫表-學(xué)生成績表

create table student(id int primary key auto_increment,name varchar(20) unique not null,chinese float,english float,math float);


添加幾條數(shù)據(jù)

insert into student values(1,’張三’,90,80,80);

insert into student values(2,’李四’,90,87,60);

insert into student values(3,’王五’,70,60,69);

insert into student values(4,’趙六’,99,90,87);

8.2.1 ?? 普通條件查詢

查詢所有學(xué)生信息

select * from student;


查詢id為1的學(xué)生信息

select * from student where id=1;


查詢id為1的學(xué)生姓名

select name from student where id=1;


查詢數(shù)學(xué)成績大于80的同學(xué)成績

select * from student where math>80



查詢所有學(xué)生成績,并輸出效果為姓名語文英語數(shù)學(xué)效果

select name as姓名,chinese as 語文,english as 英語,math as 數(shù)學(xué) from student



查詢所有成績及數(shù)學(xué)分+10分

select *,(math+10)from student


統(tǒng)計每個學(xué)生的總分

select name,(math+english+chinese) as總分 from student


查詢總分大于230分的同學(xué)

select * from student where (math+english+chinese)>230


查詢數(shù)學(xué)成績在80-90之間的同學(xué)

select * from student where math between 80 and 90


查詢數(shù)學(xué)語文英語都大于80的同學(xué)成績

select * from student where math>80 and english>80 and chinese >80;


查詢數(shù)學(xué)成績在80 60 90內(nèi)的同學(xué)

select * from student where math in(80,60,90);

8.2.2 ?? 模糊查詢

?

查詢所有姓名中包含張的同學(xué)

select * from student where name like ‘%張%’

8.2.3 ?? 排序查詢

按照數(shù)學(xué)成績從小到大查詢

select * from student order by math;

按照數(shù)學(xué)成績從大到小查詢

select * from student order by math desc;

8.2.4 ?? 分頁查詢

limit是mysql的語法

select * from table limit m,n


其中m是指記錄從m+1開始

,N代表取n條記錄。

select * from student limit 2,4

即取出第3條至第6條,4條記錄


查詢出數(shù)學(xué)成績由高到低前兩名

select * from student order by math desc limit 0,2;

8.2.5 ?? 分組查詢

創(chuàng)建一個訂單表

create table employee(id int,name varchar(20),sex varchar(20),age int);

insert into employee?values(1,'sunsan','男',18);

insert into employee?values(2,'lisi','男',18);

insert into employee values(3,'wangwu','女',19);

insert into employee??values(4,'zhaoliu','男',15);


分組查詢

?select * from employee group by sex;

分組查詢加條件

?select * from employee group by sex having age>18;


(1) having條件表達式:用來分組查詢后指定一些條件來輸出查詢結(jié)果(2) having作用和where一樣,但having只能用于group by

8.3 ?? 報表查詢

count個數(shù)

sum 總數(shù)

avg 平均數(shù)

max 最大值

min 最小值


統(tǒng)計班級里邊有多少學(xué)生

select count(*)from student;


統(tǒng)計總成績大于250分的人數(shù)

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


統(tǒng)計班級里邊各科總成績

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


統(tǒng)計所有科目的總成績

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


統(tǒng)計一下語文平均成績

select sum(chinese)/count(*) from student;

select avg(chinese) from student;


統(tǒng)計一下班級語文最高分和最低分

select max(chinese) from student;

select min(chinese) from student;


報表查詢訂單根據(jù)名稱合并后,總價格>10000的商品

select * from orders group by product having sum(price) >7000

9. ? ? ?? 多表設(shè)計

9.1 ?? 一對一

一張表的一條記錄一定只能與另外一張表的一條記錄進行對應(yīng),反之亦然。


有時候,為了業(yè)務(wù),或者避免一張表中數(shù)據(jù)量過大,過復(fù)雜,在開發(fā)中會進行一對一方式來設(shè)計表。

7.2????一對多(1方建主表(id為主鍵字段), 多方建外鍵字段)

指的是一個實體的某個數(shù)據(jù)與另外一個實體的多個數(shù)據(jù)有關(guān)聯(lián)關(guān)系,一對多的關(guān)系在設(shè)計的時候,需要設(shè)計表的外鍵

9.1.1 ?? 班級表和學(xué)生表設(shè)計


部門表和員工表設(shè)計


9.1.2 ?? 創(chuàng)建數(shù)據(jù)庫表

constraint 約束?

foreign key就是表與表之間的某種約定的關(guān)系,由于這種關(guān)系的存在,能夠讓表與表之間的數(shù)據(jù),更加的完整,關(guān)連性更強。

foreign key語句的式例:FOREIGN KEY(Sno) REFERENCES Student(Sno)

附:表的外鍵必須是另一張表的主鍵

create table class(id int primary key auto_increment,name varchar(20));

create table student(id int primary key auto_increment,name varchar(20),sex varchar(20),class_id int,constraint ?foreign key(class_id) references class(id));



?insert into class values(1,'ceshiban');

?insert into class values(2,'kaifa');

?insert into student values(1,'zhangsan','nan',1);

?insert into student values(2,'lisi','nan',2);

insert into student values(3,'jingjing','nan',2);


select * from student where class_id=(select id from class where id=2);

補一個外鍵的概念(默認是約束):刪除主鍵信息時,當(dāng)該主鍵字段值在外鍵表中存在時,該記錄是不能刪除的。---要把外鍵表是的相關(guān)信息刪除之后,才能刪除。

7.3????多對多(3個表= 2個實體表 + 1個關(guān)系表 )

一個實體的數(shù)據(jù)對應(yīng)另外一個實體的多個數(shù)據(jù),另外實體的數(shù)據(jù)也同樣對應(yīng)當(dāng)前實體的多個數(shù)據(jù)。

一個學(xué)生可以有多個老師,一個老師可以教多個學(xué)生

解決方案:創(chuàng)建一個中間表,專門用來維護多表之間的對應(yīng)關(guān)系,通常是能夠唯一標(biāo)識出數(shù)據(jù)的字段(主鍵)



create table teacher(id int primary key,name varchar(100));

create table student (id int primary key,name varchar(100));


create table teacher_student(teacher_id int,student_id int,constraint?foreign key(teacher_id) references teacher(id),constraint foreign key(student_id) references student(id));


insert into teacher values(1,'梁老師');

?insert into teacher values(2,'李老師');


insert into student values(1,”張三”);

insert into student values(2,”李四”);

insert into teacher_student values(1,1);

insert into teacher_student values(1,2);

insert into teacher_student values(2,1);

insert into teacher_student values(2,2);


查詢李老師所教的學(xué)生

select id from teacher where name=’李老師’

select student_id from teacher_student where teacher_id=id


select * from student where id in(select student_id from teacher_student where teacher_id =(select id from teacher where name='李老師'));


查詢張三的所有老師

select * from teacher where id in(select teacher_id from teacher_student where student_id=(select id from student where name='張三'));


10. ? ? ?? 連接查詢

10.1 ?? 初始定義表結(jié)構(gòu)

create table customer(id int primary key auto_increment,name varchar(20),city varchar(20));


create table orders(id int primary key auto_increment,good_name varchar(20),price float(8,2),customer_id int);

insert into customer (name,city) values('李老師','東北');

insert into customer (name,city) values('崔老師','山西');

insert into customer (name,city) values('張老師','內(nèi)蒙');

insert into customer (name,city) values('閆老師','天津');


insert into orders(good_name,price,customer_id) values('電腦',59,1);

insert into orders(good_name,price,customer_id) values('筆記本',88,2);

insert into orders(good_name,price,customer_id) values('吹風(fēng)機',99,1);

insert into orders(good_name,price,customer_id) values('香水',300,3);

insert into orders(good_name,price,customer_id) values('牛奶',100,6);

10.2 ?? 交叉查詢

交叉查詢,又叫笛卡爾積查詢,會將左表和右表的信息,做一個乘積將所有信息查詢出來,會產(chǎn)生臨時表,比較占用內(nèi)存,生成的記錄數(shù)=表1 X表2


select * from customer,orders;

select * from customer cross join orders;

10.3 ?? 內(nèi)連接查詢

內(nèi)連接,inner join on查詢兩張表,設(shè)定條件,將兩張表中對應(yīng)的數(shù)據(jù)查詢出來

不會產(chǎn)生笛卡爾積,不會產(chǎn)生臨時表,性能高

select * from customer c inner join orders o on c.id=o.customer_id;

select * from customer,orders where customer.id=orders.customer_id;

select * from customer c,orders o where c.id=o.customer_id;

10.4 ?? 左外連接

左外連接left join ??on設(shè)定條件,將兩張表對應(yīng)的數(shù)據(jù)查詢出來,同時將左表自己沒有關(guān)聯(lián)的數(shù)據(jù)也查詢出來

注意:join前面是左,后面是右


select * from customer c left join orders o on c.id=o.customer_id;

10.5 ?? 右外連接

右外連接right join ?on設(shè)定條件,將兩張表對應(yīng)的數(shù)據(jù)查詢出來,同時將右表自己沒有關(guān)聯(lián)的所有數(shù)據(jù)查詢出來

select * from customer c right join orders o on c.id=o.customer_id;

10.6 ?? 聯(lián)合查詢

select * from customer left join orders on customer.id=orders.customer_id

?having price>20;

11. ? ? ?? MySQL圖形化工具navicat

11.1.1 ?? 安裝介紹

先執(zhí)行安裝navicat111_mysql.exe文件

安裝成功之后,執(zhí)行patchNavicat.exe,

找到所安裝目錄里的navicat.exe文件,進行破解

11.2 ?? Navicat工具使用步驟

11.2.1 ?? 鏈接,mysql,輸入用戶名,密碼

11.2.2 ?? 新建庫,鼠標(biāo)點擊右鍵


11.2.3 ?? 新建表


12. ? ? ?? 數(shù)據(jù)庫備份與恢復(fù)

使用圖形界面工具:

使用doc命令:

> mysqldump–u用戶名 –p密碼 數(shù)據(jù)庫名>生成的腳本文件路徑

>注意,不要打分號,不要登錄mysql,直接在cmd下運行

>注意,生成的腳本文件中不包含create database語句

mysqldump -uroot -proot host>C:\Users\Administrator\Deskt

op\mysql\1.sql



導(dǎo)入SQL文件


12.1 ?? 恢復(fù)


13. ? ? ?? 數(shù)據(jù)庫常用性能優(yōu)化(了解)

數(shù)據(jù)庫性能優(yōu)化這塊,我們考慮比較多的還是查詢這塊,互聯(lián)網(wǎng)項目對數(shù)據(jù)查詢非常頻繁,對效率,性能要求比較高。


查詢這塊優(yōu)化的話,主要就需要使用索引這種方式,所謂索引就是建立一種快速查找的方式,比如我們查字典,有一個ABCD的索引.


舉個例子,如果我們創(chuàng)建一個表 create table user(id integer ,name varchar(20),job varchar(20)); 如果我們數(shù)據(jù)庫中有1000萬條數(shù)據(jù),當(dāng)我查詢 select * from user where name=’張三’的時候,這種查詢方式就類似于整個數(shù)據(jù)庫的掃描,效率非常低。

我們可以給這個name設(shè)置一個索引create?index n on user (name);這是設(shè)置一種普通(normal)索引,然后我們查詢的時候,有了這個索引,效率就會大大提升,當(dāng)然對于索引,它的方式有BTree類型和Hash類型,是兩種管理數(shù)據(jù)庫索引的方式,這個我沒有深入研究。這個我們可以自己設(shè)置。默認是btree。


索引類型的話,有normal(普通類型)類型,還有unique(唯一類型),fulltext全文索引

主鍵索引,還有非空索引,還有聚集索引。


主鍵索引,primary key在設(shè)置的時候,已經(jīng)指定了,其實也是非空索引。


非空索引是not null,設(shè)置這種方式的該字段下內(nèi)容不能為空,


聚集索引(聯(lián)合索引),是在設(shè)置多個查詢條件的時候使用。比如 創(chuàng)建一張表,有名字,有工作,我們想經(jīng)常頻繁的用到名字和工作它倆結(jié)合在一起來查詢數(shù)據(jù)庫中表的數(shù)據(jù)。這個時候,可以將名字和工作指定為聚集索引。create index m on user(name,job); 這樣當(dāng)我們指定select * from user where name=xxx and job=xxx的時候,就會按照索引方式來做。


這種優(yōu)化方式就是索引優(yōu)化,在使用索引優(yōu)化方案的時候,我們需要注意避免在索引字段上使用條件函數(shù)等操作。

了解:

Show index form orders;查看索引


4

14. ? ? ?? 數(shù)據(jù)庫性能檢測方式(了解)

在設(shè)計SQL的時候,我們一般會使用explain(分析)https://segmentfault.com/a/1190000008131735檢測sql,看是否使用到索引,避免出現(xiàn)整表搜索方式查詢[filesort(不是以索引方式的檢索,我們叫做filesort)](我在這張表中把gender設(shè)置成normal索引,name沒有任何設(shè)置)

第一步:set profiling=1;(開啟profiling)?

第二步運行:select title from tb_product ;

第三步:查看運行時間show profiles;


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

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