第七章 數(shù)據(jù)庫操作
基本操作:
數(shù)據(jù)庫操作:
-- CREATE SCHEMA mj01 DEFAULT CHARACTER SET utf8 ;
--新增一個數(shù)據(jù)庫
-- DROP DATABASE aiaf13; --刪除一個數(shù)據(jù)庫
表操作:
-- show tables; --展示數(shù)據(jù)庫中的表
-- create table TableName(col1 type1 [not null] [primary key],col2 type2 [not null],..);--創(chuàng)建新表
-- drop table TableName;--刪除表
修改表中字段:
1,
alter table table_name modify column column_name 數(shù)據(jù)類型(修改后長度)?--?更改?column?類型
示例如下:
ALTER TABLE?shangpin?MODIFY COLUMN?warehouseId?VARCHAR(255);
2,
alter table table_name change old_column_name?? new_column_name?? int(11);?--?修改字段名稱?
3,
alter table table_name add colum?column_name int(11);?--?增加字段?
alter table?table_name?add colum?column_name 數(shù)據(jù)類型(長度)DEFAULT NULL;?--?增加默認(rèn)為空的字段
alter table?table_name?add colum?column_name?數(shù)據(jù)類型(長度)NOT NULL;?--?增加非空的字段
4,
alter table table_name drop?colum?column_name ;?--?刪除字段
增刪改查:
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 范圍
更新:update table1 set field1=value1 where 范圍
查找:select * from table1 where?范圍
模糊查詢:
select * from table1 where field1 like ’%value1%’?
---like?的語法很精妙,其應(yīng)用與is、=、>和<等符號用法類似。
Like主要支持兩種通配符,分別是"_"和"%"。
1、"_"代表匹配1個任意字符,常用于充當(dāng)占位符;
2、"%"代表匹配0個或多個任意字符。
匹配方式:
%xx? ?表示右匹配,右邊的xx字符需要完全相等,左邊可以是任意字符,也可以沒有字符
_xx? ? 表示右匹配,右邊的xx字符需要完全相等,左邊可以是任意一個字符,必須是一個不能沒有字符
xx%? ?表示左匹配,右邊的xx字符需要完全相等,右邊可以是任意字符,也可以沒有字符
xx_? ? 表示左匹配,左邊的xx字符需要完全相等,右邊可以是任意一個字符,必須是一個不能沒有字符
%xx% 表示中間匹配,中間必須完全相等,左右兩邊可以是任意字符,左右兩邊可以沒有其他字符
_xx_? ?表示中間匹配,中間必須完全相等,左右兩邊可以是任意一個字符,左右兩邊必須是一個不能沒有字符
排序:select * from table1 order by field1?[DESC]降序/[ASC]升序
order by之后不加關(guān)鍵詞時,默認(rèn)按照[ASC]升序排序
二級排序:select * from table1?order by?field1?DESC,field2?ASC
分組:select * from table1 group?by field1,field2
select * from table1?group?by field1? having 條件表達(dá)式 with rollup;
對數(shù)據(jù)再次進(jìn)行匯總,with rollup 就是作用在聚合函數(shù)上的。如果聚合函數(shù)是COUNT(*)則會在統(tǒng)計的記錄中再次求COUNT(*),如果是AVG(),則會在所有記錄后增加統(tǒng)計結(jié)果
GROUP BY語句后面可以使用HAVING條件語句查詢分組后性別為男的人數(shù)。
select sex,count(*)from student?group?by sex having sex='男'
總數(shù):select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
between 的用法,between 限制查詢數(shù)據(jù)范圍時包括了邊界值,not between 不包括
--select * from table1 where time between time1 and time2
--select a,b,c, from table1 where a not between 數(shù)值 1 and 數(shù)值 2
in 的使用方法
--select * from zibiao where Name in ('1','2','3');
--select * from zibiao where Name NOT in ('1','2','3');
分頁查詢
MySQL?中:
SELECT?*?FROM?metamj03 .menu?ORDER BY?updatedOn?DESC?limit?10;
SQLserver?中:
SELECT top 10?*?FROM?token?where?oid?IN?(SELECT?Token?FROM?tokendatasegment?where
createdBy!='saasuser')AND?token.Description?IS NOT NULL ORDER BY?oid?ASC?;
查,多表聯(lián)查,最多不超過4個表
四表聯(lián)查問題:
--select?*?from?a?left inner join?b?on?a.a=b.b?right inner join?c?on?a.a=c.c?inner join
d?on?a.a=d.d?where?.....
??JOIN:?如果表中有至少一個匹配,則返回行
??LEFT JOIN:?即使右表中沒有匹配,也從左表返回所有的行
??RIGHT JOIN:?即使左表中沒有匹配,也從右表返回所有的行
??FULL JOIN:?只要其中一個表中存在匹配,就返回行
備份數(shù)據(jù)庫:
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat';--創(chuàng)建備份數(shù)據(jù)的
device
BACKUP DATABASE pubs TO testBack;--開始備份
BACKUP DATABASE'被備份的數(shù)據(jù)庫名'TO DISK = '備份文件路徑';
RESTORE DATABASE '被恢復(fù)的數(shù)據(jù)庫名'FROM DISK = '還原文件路徑(源文件)';
查看某個數(shù)據(jù)庫正在執(zhí)行的 sql
返回的 id 是線程 id,發(fā)現(xiàn)有情況可以 kill 掉
select a.* from information_schema.processlist a join information_schema.INNODB_TRX b on
a.id=b.trx_mysql_thread_id where a.db='metadev';
常見問法:
1,先按成績排,成績一樣按學(xué)號排,前10
Select top 10 *? ?from 成績表 where 查詢條件 order by 成績 asc,學(xué)號 desc
2,結(jié)合項目,學(xué)生表,找出分?jǐn)?shù)最高的前三名
3,測試工作中的應(yīng)用
接口測試時,查庫確認(rèn)數(shù)據(jù)落地及正確性
功能測試-搜索功能,大量數(shù)據(jù)的正確性和完整性,查庫
功能測試-定位問題,查庫
測試中,造數(shù)據(jù),比如性能測試需要大量數(shù)據(jù),還有需要去年的數(shù)據(jù)測試過期