一颖变、數(shù)據(jù)庫概述
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)問題時候评抚,能夠定位到問題原因豹缀;
- 學(xué)習(xí)數(shù)據(jù)庫,掌握數(shù)據(jù)庫操作慨代,增加面試成功機會邢笙,可以提高工資。
2. 什么是數(shù)據(jù)庫
- Excle的數(shù)據(jù)的確很方便侍匙,但是對于企業(yè)來說就不一樣了氮惯。一個公司里面可能有成千上萬的Excel表格,還在不同的電腦上,而他們的員工和客戶需要實時看到企業(yè)給他們提供的所有數(shù)據(jù)筐骇,這種文件管理的方法就很麻煩债鸡,總不能每分鐘都把一個新的巨大無比的Excel文件發(fā)給所有客戶呀江滨。
- 數(shù)據(jù)庫是按照數(shù)據(jù)的結(jié)構(gòu)來組織铛纬、存儲、和管理數(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)垄懂。
3. 常見數(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的地位肺然,開源,免費腿准,支持多平臺际起,簡單易學(xué)易操作,市場占有率第二吐葱。
- SQLServer是微軟開發(fā)的數(shù)據(jù)庫街望,針對服務(wù)器,僅支持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è)備的最佳選擇鸽嫂。
各個數(shù)據(jù)庫軟件的使用差別大同小異纵装,但是因為MySQL是開源的,成為各大公司使用的主流据某,面試也主要以MySQL為主橡娄。
4.數(shù)據(jù)庫和SQL是什么關(guān)系?
結(jié)構(gòu)化查詢語言(Structured Query Language)簡稱SQL癣籽,是一種特殊目的的編程語言挽唉,是一種數(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ù))
二裤唠、MySQL數(shù)據(jù)庫安裝配置
三、SQL語句分類
1. DDL——數(shù)據(jù)庫定義語言
1. 概念:數(shù)據(jù)庫定義語言预鬓,用于定義數(shù)據(jù)庫巧骚,用于定義表結(jié)構(gòu)
2. 數(shù)據(jù)類型:
數(shù)值赊颠、日期/時間和字符串(字符)類型
- 一個漢字占多少長度與編碼有關(guān):
UTF-8:一個漢字=3個字節(jié)
GBK:一個漢字=2個字節(jié) - varchar(n) 表示 n 個字符,無論漢字和英文劈彪,Mysql 都能存入 n 個字符竣蹦,僅是實際字節(jié)長度有所區(qū)別
3. 語句操作
1.//創(chuàng)建數(shù)據(jù)庫
create database db_1;
2.//刪除創(chuàng)建的數(shù)據(jù)庫
drop database db_1;
3.//連接數(shù)據(jù)庫
use db_1;
4.//查看創(chuàng)建數(shù)據(jù)庫細節(jié)
show create database db_1;
5.//創(chuàng)建一個使用gbk字符集的數(shù)據(jù)庫
create database db_1 character set gbk
4. 表結(jié)構(gòu)語句操作
1.//選擇數(shù)據(jù)庫
use db_1;
2.//創(chuàng)建表
create table user(id int primary key auto_increment,name varchar(20),age int,sex varchar(10),date date);
3.//展示表結(jié)構(gòu)
desc user;
4.//在原有的學(xué)生基礎(chǔ)上添加address列
alter table user add address varchar(20);
5.//在原有的學(xué)生基礎(chǔ)上刪除address列
alter table user drop address;
2. DML——數(shù)據(jù)庫操作語言
1. 概念
數(shù)據(jù)庫操作語言,用于操作數(shù)據(jù)庫
2. 增刪改查
1. //插入數(shù)據(jù)
insert into user values(1,'張三',18,98,'2002-12-09'),(2,'李四',20,75.4,'2000-08-05'),(3,'劉翔',28,99.99,'1994-06-25'),(4,'穆豬',22,50,'1999-12-05');
2. //修改數(shù)據(jù)
update user set name='你個der' where id=4;
3. //刪除數(shù)據(jù)
delete from user where id=3;
4. //查詢數(shù)據(jù)
select * from user where age>18;
3. DQL——數(shù)據(jù)庫查詢語言
1. 概念
數(shù)據(jù)庫查詢語言沧奴,用來查詢數(shù)據(jù)庫信息
2. 數(shù)據(jù)查詢
1. //查詢所有學(xué)生信息
select * from user;
2. //查詢id為1的學(xué)生信息
select * from user where id=1;
3. //查詢成績大于80的學(xué)生信息
select * from user where score>80;
4. //查詢成績在60到80以內(nèi)學(xué)生信息
select * from user where score>60 and score<80;
5. //模糊查詢滔吠,查詢名字中包含李的學(xué)生信息
select * from user where name like '%李';
6. //排序查詢
select * from user order by score;(升序)
select * from user order by score desc;(降序)
7. //分頁查詢
select * from user order by score desc limit 2;
8. //分組查詢
select * from user group by score>80;
9. //報表查詢
//(1)年齡大于18的學(xué)生的個數(shù)
select count(*) as 個數(shù) from user where age>18;
//(2)學(xué)生表中平均的分數(shù)
select avg(score) as 平均分 from user;
//(3)學(xué)生表中總分
select sum(score) as 總分 from user;
//(4)學(xué)生表中分數(shù)最高學(xué)生的信息
select max(score) as 最高分 from user;
//(5)學(xué)生表中分數(shù)最低學(xué)生的信息
select min(score) as 最低分 from user;
4. DCL——數(shù)據(jù)庫控制語言
概念
數(shù)據(jù)控制語言:簡稱【DCL】(Data Control Language)疮绷,用來定義數(shù)據(jù)庫的訪問權(quán)限和安全級別,及創(chuàng)建用戶冬骚;關(guān)鍵字:grant等
四椅贱、多表設(shè)計
1. 一對一
一張表的一條記錄一定只能與另外一張表的一條記錄進行對應(yīng),反之亦然庇麦。
有時候舍悯,為了業(yè)務(wù)航棱,或者避免一張表中數(shù)據(jù)量過大,過復(fù)雜贱呐,在開發(fā)中會進行一對一方式來設(shè)計表丧诺。
2. 一對多
一個實體的某個數(shù)據(jù)與另外一個實體的多個數(shù)據(jù)有關(guān)聯(lián)關(guān)系, 一對多的關(guān)系在設(shè)計的時候奄薇,需要設(shè)計表的外鍵驳阎。
1. 創(chuàng)建數(shù)據(jù)庫表
//創(chuàng)建班級表
create table class(id int primary key auto_increment,name varchar(20));
//創(chuàng)建學(xué)生表
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));
//插入班級數(shù)據(jù)
insert into class values(1,'ceshiban');
insert into class values(2,'kaifa');
//插入學(xué)生數(shù)據(jù)
insert into student values(1,'zhangsan','nan',1);
insert into student values(2,'lisi','nan',2);
insert into student values(3,'jingjing','nan',2);
//聯(lián)查
select * from student where class_id=(select id from class where id=2);
- constraint 約束
- foreign key就是表與表之間的某種約定的關(guān)系,由于這種關(guān)系的存在馁蒂,能夠讓表與表之間的數(shù)據(jù)呵晚,更加的完整,關(guān)連性更強沫屡。
- foreign key語句的式例:FOREIGN KEY(Sno) REFERENCES Student(Sno)
注意:表的外鍵必須是另一張表的主鍵
3. 多對多
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='張三'));
五饵隙、連表查詢
分類:內(nèi)連接、外連接沮脖、交叉連接
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_namevarchar(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);
2. 交叉查詢
select * from customer,orders;
select * from customer cross join orders;
交叉查詢金矛,又叫笛卡爾積查詢芯急,會將左表和右表的信息,做一個乘積將所有信息查詢出來驶俊,會產(chǎn)生臨時表娶耍,比較占用內(nèi)存,生成的記錄數(shù)=表1 X表2
3. 內(nèi)連接查詢
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;
內(nèi)連接饼酿,inner join on 查詢兩張表榕酒,設(shè)定條件,將兩張表中對應(yīng)的數(shù)據(jù)查詢出來
不會產(chǎn)生笛卡爾積故俐,不會產(chǎn)生臨時表想鹰,性能高
4. 左外連接
select * from customer c left join orders o on c.id=o.customer_id;
左外連接 left join on 設(shè)定條件,將兩張表對應(yīng)的數(shù)據(jù)查詢出來药版,同時將左表自己沒有關(guān)聯(lián)的數(shù)據(jù)也查詢出來
注意:join前面是左辑舷,后面是右
5. 聯(lián)合查詢
select * from customer left join orders on customer.id=orders.customer_id
having price>20;