1. 程式人生 > 實用技巧 >軟體測試的資料庫技術02

軟體測試的資料庫技術02

SQL語言
資料表操作:建立、刪除資料操作:增加、刪除、修改、簡單查詢資料操作:查詢此部分中查詢為重點,需要熟練掌握SQL語言編寫和執行滑鼠左鍵點選某個資料庫下面的查詢按鈕,然後點選新建查詢在開啟的查詢編輯器中,編寫SQL語言,再點選執行

資料表操作

建立表
create table 表名(
欄位名 型別 約束,
欄位名 型別 約束
...
)
例:建立學生表,欄位要求如下:姓名(長度為10)
create table students(
name varchar(10)
)
例:建立學生表,欄位要求如下:姓名(長度為10), 年齡
create table students(
name varchar(10),
age int unsigned
)
例:建立學生表,欄位要求如下:姓名(長度為10), 年齡,身高(保留小數點2位)
create table students(
id int unsigned primary key auto_increment,
name varchar(20),
age int unsigned,
height decimal(5,2)
)
刪除表格式一:drop table 表名格式二:drop table if exists 表名例:刪除學生表
drop table students
或
drop table if exists students

資料操作-增刪改查

簡單查詢
select * from 表名
例:查詢所有學生資料
select * from students
新增資料新增一行資料格式一:所有欄位設定值,值的順序與表中欄位的順序對應說明:主鍵列是自動增長,插入時需要佔位,通常使用0或者 default 或者 null 來佔位,插入成功後以實際資料為準
insert into 表名 values(...)
例:插入一個學生,設定所有欄位的資訊
insert into students values(0,'亞瑟',22,177.56)
格式二:部分欄位設定值,值的順序與給出的欄位順序對應
insert into 表名(欄位1,...) values(值1,...)
例:插入一個學生,只設置姓名
insert into students(name) values('老夫子')
修改
格式:update 表名 set 列1=值1,列2=值2... where 條件
例:修改id為5的學生資料,姓名改為 狄仁傑,年齡改為 20
update students set name='狄仁傑',age=20 where id=5
刪除格式:delete from 表名 where 條件例:刪除id為6的學生資料
delete from students where id=6
邏輯刪除:對於重要的資料,不能輕易執行delete語句進行刪除,一旦刪除,資料無法恢復,這時可以進行邏輯刪除。1、給表新增欄位,代表資料是否刪除,一般起名isdelete,0代表未刪除,1代表刪除,預設值為02、當要刪除某條資料時,只需要設定這條資料的isdelete欄位為13、以後在查詢資料時,只查詢出isdelete為0的資料
例:
1、給學生表新增欄位(isdelete),預設值為0,如果表中已經有資料,需要把所有資料的isdelete欄位更
新為0
update students set isdelete=0
2、刪除id為1的學生
update students set isdelete=1 where id=1
3、查詢未刪除的資料
select * from students where isdelete=0

資料操作-查詢

建立資料表
drop table if exists students;
create table students (
studentNo varchar(10) primary key,
name varchar(10),
sex varchar(1),
hometown varchar(20),
age tinyint(4),
class varchar(10),
card varchar(20)
)
準備資料
insert into students values
('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
('002', '諸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
('003', '張飛', '男', '南京', '24', '3班', '340322199003247654'),
('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
('005', '大喬', '女', '天津', '19', '3班', '340322199004247654'),
('006', '孫尚香', '女', '河北', '18', '1班', '340322199006247654'),
('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
('008', '小喬', '女', '河南', '15', '3班', null),
('009', '百里守約', '男', '湖南', '21', '1班', ''),
('010', '妲己', '女', '廣東', '26', '2班', '340322199607247654'),
('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
('012', '孫臏', '男', '新疆', '26', '3班', '340322199000297655')
查詢所有欄位
select * from 表名
例:
select * from students
查詢指定欄位在select後面的列名部分,可以使用as為列起別名,這個別名出現在結果集中
select 列1,列2,... from 表名
-- 表名.欄位名
select students.name,students.age from students
-- 可以通過 as 給表起別名
select s.name,s.age from students as s
-- 如果是單表查詢 可以省略表明
select name,age from students
- 使用as給欄位起別名
select studentNo as 學號,name as 名字,sex as 性別 from students
消除重複行在select後面列前使用distinct可以消除重複的行
select distinct 列1,... from 表名;
例:
select distinct sex from students;

條件

使用where子句對錶中的資料篩選,符號條件的資料會出現在結果集中語法如下:
select 欄位1,欄位2... from 表名 where 條件;
例:
select * from students where id=1;
where後面支援多種運算子,進行條件的處理
比較運算邏輯運算模糊查詢範圍查詢空判斷
比較運算子等於: =大於: >大於等於: >=小於: <小於等於: <=不等於: != 或 <>例1:查詢小喬的年齡
select age from students where name='小喬'
例2:查詢20歲以下的學生
select * from students where age<20
例3:查詢家鄉不在北京的學生
select * from students where hometown!='北京'
邏輯運算子andornot例1:查詢年齡小於20的女同學
select * from students where age<20 and sex='女'
例2:查詢女學生或'1班'的學生
select * from students where sex='女' or class='1班'
例3:查詢非天津的學生
select * from students where not hometown='天津'
模糊查詢like%表示任意多個任意字元_表示一個任意字元例1:查詢姓孫的學生
select * from students where name like '孫%'
例2:查詢姓孫且名字是一個字的學生
select * from students where name like '孫_'
例3:查詢叫喬的學生
select * from students where name like '%喬'
例4:查詢姓名含白的學生
select * from students where name like '%白%'
範圍查詢in表示在一個非連續的範圍內例1:查詢家鄉是北京或上海或廣東的學生
select * from students where hometown in('北京','上海','廣東')
between ... and ...表示在一個連續的範圍內例2:查詢年齡為18至20的學生
select * from students where age between 18 and 20
空判斷注意:null與''是不同的判空is null例1:查詢沒有填寫身份證的學生
select * from students where card is null
判非空is not null例2:查詢填寫了身份證的學生
select * from students where card is not null

排序

為了方便檢視資料,可以對資料進行排序語法:
select * from 表名
order by 列1 asc|desc,列2 asc|desc,...
將行資料按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推預設按照列值從小到大排列asc從小到大排列,即升序desc從大到小排序,即降序例1:查詢所有學生資訊,按年齡從小到大排序
select * from students order by age
例2:查詢所有學生資訊,按年齡從大到小排序,年齡相同時,再按學號從小到大排序
select * from students order by age desc,studentNo

聚合函式

為了快速得到統計資料,經常會用到如下5個聚合函式count(*)表示計算總行數,括號中寫星與列名,結果是相同的聚合函式不能在 where 中使用例1:查詢學生總數
select count(*) from students;
max(列)表示求此列的最大值例2:查詢女生的最大年齡
select max(age) from students where sex='女';
min(列)表示求此列的最小值例3:查詢1班的最小年齡
select min(age) from students;
sum(列)表示求此列的和例4:查詢北京學生的年齡總和
select sum(age) from students where hometown='北京';
avg(列)表示求此列的平均值例5:查詢女生的平均年齡
select avg(age) from students where sex='女'

分組

按照欄位分組,表示此欄位相同的資料會被放到一個組中分組後,分組的依據列會顯示在結果集中,其他列不會顯示在結果集中可以對分組後的資料進行統計,做聚合運算語法:
select 列1,列2,聚合... from 表名 group by 列1,列2...
例1:查詢各種性別的人數
select sex,count(*) from students group by sex
例2:查詢各種年齡的人數
select age,count(*) from students group by age

分組後的資料篩選語法:
select 列1,列2,聚合... from 表名
group by 列1,列2,列3...
having 列1,...聚合...
having後面的條件運算子與where的相同例1:查詢男生總人數
方案一
select count(*) from students where sex='男'
-----------------------------------
方案二:
select sex,count(*) from students group by sex having sex='男'
對比where與havingwhere是對from後面指定的表進行資料篩選,屬於對原始資料的篩選having是對group by的結果進行篩選

分頁

獲取部分行當資料量過大時,在一頁中檢視資料是一件非常麻煩的事情語法:
select * from 表名
limit start,count
從start開始,獲取count條資料start索引從0開始例1:查詢前3行學生資訊
select * from students limit 0,3
分頁已知:每頁顯示m條資料,求:顯示第n頁的資料
select * from students limit (n-1)*m,m
求總頁數
查詢總條數p1使用p1除以m得到p2如果整除則p2為總數頁如果不整除則p2+1為總頁數

連線查詢

當查詢結果的列來源於多張表時,需要將多張表連線成一個大的資料集,再選擇合適的列返回等值連線查詢:查詢的結果為兩個表匹配到的資料左連線查詢:查詢的結果為兩個表匹配到的資料加左表特有的資料,對於右表中不存在的資料使用null填充右連線查詢:查詢的結果為兩個表匹配到的資料加右表特有的資料,對於左表中不存在的資料使用null填充準備資料
drop table if exists courses;
create table courses (
courseNo int(10) unsigned primary key auto_increment,
name varchar(10)
);
insert into courses values
('1', '資料庫'),
('2', 'qtp'),
('3', 'linux'),
('4', '系統測試'),
('5', '單元測試'),
('6', '測試過程');
drop table if exists scores;
create table scores (
id int(10) unsigned primary key auto_increment,
courseNo int(10),
studentno varchar(10),
score tinyint(4)
);
insert into scores values
('1', '1', '001', '90'),
('2', '1', '002', '75'),
('3', '2', '002', '98'),
('4', '3', '001', '86'),
('5', '3', '003', '80'),
('6', '4', '004', '79'),
('7', '5', '005', '96'),
('8', '6', '006', '80');
等值連線方式一
select * from 表1,表2 where 表1.列=表2.列
方式二(又稱內連線)
select * from 表1
inner join 表2 on 表1.列=表2.列
例1:查詢學生資訊及學生的成績
select
*
from
students stu,
scores sc
where
stu.studentNo = sc.studentNo
---------------------------------------
select
*
from
students stu
inner join scores sc on stu.studentNo = sc.studentNo
例2:查詢課程資訊及課程的成績
select
*
from
courses cs,
scores sc
where
cs.courseNo = sc.courseNo
---------------------------------------
select
*
from
courses cs
inner join scores sc on cs.courseNo = sc.courseNo
例3:查詢學生資訊及學生的課程對應的成績
select
*
from
    students stu,
    courses cs,
    scores sc
where
    stu.studentNo = sc.studentno
    and cs.courseNo = sc.courseNo
---------------------------------------
select
*
from
students stu
inner join scores sc on stu.studentNo = sc.studentNo
inner join courses cs on cs.courseNo = sc.courseNo
例4:查詢王昭君的成績,要求顯示姓名、課程號、成績
select
    stu.name,
    sc.courseNo,
    sc.score
from
    students stu,
    scores sc
where
    stu.studentNo = sc.studentNo
    and stu.name = '王昭君'
例5:查詢王昭君的資料庫成績,要求顯示姓名、課程名、成績
select
    stu.name,
    cs.name,
    sc.score
from
    students stu,
    scores sc,
    courses cs
where
    stu.studentNo = sc.studentNo
    and sc.courseNo = cs.courseNo
    and stu.name = '王昭君'
    and cs.name = '資料庫'
例6:查詢所有學生的資料庫成績,要求顯示姓名、課程名、成績
select
    stu.name,
    cs.name,
    sc.score
from
    students stu,
    scores sc,
    courses cs
where
    stu.studentNo = sc.studentNo
    and sc.courseNo = cs.courseNo
    and cs.name = '資料庫'
左連線
select * from 表1
left join 表2 on 表1.列=表2.列
例1:查詢所有學生的成績,包括沒有成績的學生
select
*
from
    students stu
left join scores sc on stu.studentNo = sc.studentNo
例2:查詢所有學生的成績,包括沒有成績的學生,需要顯示課程名
select
*
from
students stu
left join scores sc on stu.studentNo = sc.studentNo
left join courses cs on cs.courseNo = sc.courseNo  (都是左連線的stu這張表)
右連線
select * from 表1
right join 表2 on 表1.列=表2.列
新增兩門課程
insert into courses values
(0, '語文'),
(0, '數學');
例1:查詢所有課程的成績,包括沒有成績的課程
select
*
from
scores sc
right join courses cs on cs.courseNo = sc.courseNo
例2:查詢所有課程的成績,包括沒有成績的課程,包括學生資訊
select
*
from
scores sc
right join courses cs on cs.courseNo = sc.courseNo
left join students stu on stu.studentNo = sc.studentNo
記住:無論左還是右連線幾張表,都看from後面第一張表做左右連線基礎