当前位置:网站首页>What are the commonly used SQL statements in software testing?
What are the commonly used SQL statements in software testing?
2022-07-06 15:12:00 【Test Xiaowan】
Catalog
1、 Data definition language
DDL:Data Definition Language
One 、 Operation Library :
-- Create a library
create database db1;
-- Create whether the library exists , Create if it does not exist
create database if not exists db1;
-- View all databases
show databases;
-- View the definition information of a database
show create database db1;
-- Modify database character information
alter database db1 character set utf8;
-- Delete database
drop database db1;
Two 、 Operation table :
-- Create table
create table student(
id int,
name varchar(32),
age int,
score double(4,1),
birthday date,
insert_time timestamp
);
-- View table structure
desc Table name ;
-- View the SQL sentence
show create table Table name ;
-- Modify the name of the table
alter table Table name rename to New table name ;
-- Add a column
alter table Table name add Name data type ;
-- Delete column
alter table Table name drop Name ;
-- Delete table
drop table Table name ;
drop table if exists Table name ;
2、 Data operation language
DML:Data Manipulation Language
One 、 increase insert into:
-- Write all the names
insert into Table name ( Name 1, Name 2,... Name n) values( value 1, value 2,... value n);
-- No listing ( Add all columns )
insert into Table name values( value 1, value 2,... value n);
-- Insert partial data
insert into Table name ( Name 1, Name 2) values( value 1, value 2);
Two 、 Delete delete:
-- Delete data in table
delete from Table name where Name = value ;
-- Delete all data in the table
delete from Table name ;
-- Delete all data in the table ( Efficient Delete the table first , Then create the same table .)
truncate table Table name ;
3、 ... and 、 modify update:
-- Modification without conditions ( All lines will be modified )
update Table name set Name = value ;
-- Conditional modification
updata Table name set Name = value where Name = value ;
3、 Data query language
DQL:Data Query Language
There's a lot about query statements , Here we will not introduce the basic . This paper mainly introduces Sort query 、 Aggregate functions 、 Fuzzy query 、 Group query 、 Paging query 、 Internal connection 、 External connection 、 Subquery
One 、 Basic keywords :
- BETWEEN...AND( Between what ) and IN( aggregate )
-- Query age is greater than or equal to 20 Less than or equal to 30 SELECT * FROM student WHERE age >= 20 && age <=30; SELECT * FROM student WHERE age >= 20 AND age <=30; SELECT * FROM student WHERE age BETWEEN 20 AND30; -- Check age 22 year ,18 year ,25 Age information SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25 SELECT * FROM student WHERE age IN (22,18,25);
is null( Not for null value ) And like( Fuzzy query )、distinct( Remove duplicate values )
-- The English score is not null
SELECT* FROM student WHERE english IS NOT NULL;
_: Any single character
%: More than one arbitrary character
-- Find out what's the name of Ma ?like
SELECT * FROM student WHERE NAME LIKE ' Horse %';
-- The second word for a name is the person who changed
SELECT * FROM student WHERE NAME LIKE "_ turn %";
-- The search name is 3 One word man
SELECT * FROM student WHERE NAME LIKE '___';
-- Search for the person whose name contains De
SELECT * FROM student WHERE NAME LIKE '% Virtue %';
-- key word DISTINCT Used to return a unique different value .
-- grammar :SELECT DISTINCT Column name FROM The name of the table
SELECT DISTINCT NAME FROM student ;
Two 、 Sort query order by
grammar :order by Clause
order by Sort field 1 sort order 1 , Sort field 2 sort order 2...
Be careful : If there are multiple sorting conditions , When the condition value of the current edge is the same , To judge the second condition .
-- Example
SELECT *FROM person ORDER BY math; -- Default ascending order
SELECT * FROM person ORDER BY math desc; -- Descending
3、 ... and 、 Aggregate functions : Take a column of data as a whole , Do the longitudinal calculation .
- count: Calculate the number of
- max: Calculate the maximum
- min: Calculate the minimum
- sum: The calculation and
- avg: Calculate the average
Four 、 Group query grout by
grammar :group by Grouping field ;
Be careful : Fields to be queried after grouping : Grouping field 、 Aggregate functions
-- Group by sex . Check the men separately 、 The average score of female students
SELECT sex , AVG(math) FROM student GROUP BY sex;
-- Group by sex . Check the men separately 、 The average score of female students , The number of
SELECT sex , AVG(math),COUNT(id) FROM student GROUP BY sex;
-- Group by sex . Check the men separately 、 The average score of female students , The number of requirement : The score is below 70 Divided people , Don't participate in grouping
SELECT sex , AVG(math),COUNT(id) FROM student WHERE math > 70 GROUP BY sex;
-- Group by sex . Check the men separately 、 The average score of female students , The number of requirement : The score is below 70 Divided people , Don't participate in grouping , After grouping . There are more people than 2 personal
SELECT sex , AVG(math),COUNT(id) FROM student WHERE math > 70 GROUP BY sex HAVING COUNT(id) > 2;
SELECT sex , AVG(math),COUNT(id) The number of FROM student WHERE math > 70 GROUP BY sex HAVING The number of > 2;
5、 ... and 、 Paging query :
grammar :limit Index started , Number of queries per page ;
The formula : Index started = ( The current page number - 1) * Number of entries per page
limit It's a MySQL" dialect "
-- Each page shows 3 Bar record
SELECT * FROM student LIMIT 0,3; -- The first 1 page
SELECT * FROM student LIMIT 3,3; -- The first 2 page
SELECT * FROM student LIMIT 6,3; -- The first 3 page
6、 ... and 、 Internal connection query :
- From which tables to query data
- What are the conditions
- Which fields to query
1. Implicit inner join : Use where Conditions eliminate useless data
-- Query the name of the employee table , Gender . The name of the Department table
SELECT emp.name,emp.gender,dept.name FROM emp,dept WHERE emp.`dept_id` = dept.`id`;
SELECT
t1.name, -- The name of the employee form
t1.gender,-- The gender of the employee table
t2.name -- The name of the Department table
FROM
emp t1,
dept t2
WHERE
t1.`dept_id` = t2.`id`;
2. Explicit inner connection
-- grammar :
select Field list from Table name 1 [inner] join Table name 2 on Conditions
-- for example :
SELECT * FROM emp INNER JOIN dept ON emp.`dept_id` = dept.`id`;
SELECT * FROM emp JOIN dept ON emp.`dept_id` = dept.`id`;
7、 ... and 、 External connection query
1. The left outer join -- It queries all the data in the left table and its intersection part .
-- grammar :
select Field list from surface 1 left [outer] join surface 2 on Conditions ;
-- Example :
-- Query all employee information , If the employee has a department , Then query the Department name , There is no department , The Department name is not displayed
SELECT t1.*,t2.`name` FROM emp t1 LEFT JOIN dept t2 ON t1.`dept_id` = t2.`id`;
2. Right connection -- It queries all the data in the right table and its intersection part .
-- grammar :
select Field list from surface 1 right [outer] join surface 2 on Conditions ;
-- Example :
SELECT * FROM dept t2 RIGHT JOIN emp t1 ON t1.`dept_id` = t2.`id`;
8、 ... and 、 Subquery : Nested query in query
-- Query the highest paid employee information
-- 1 Find out what the maximum wage is 9000
SELECT MAX (salary) FROM emp;
-- 2 Query employee information , And the salary is equal to 9000 Of
SELECT * FROM emp WHERE emp.`salary` = 9000;
-- One sql Just do it . This is the subquery
SELECT * FROM emp WHERE emp.`salary` = (SELECT MAX(salary) FROM emp);
1. The result of subquery is single row and single column :
Subqueries can be used as conditions , Use operators to judge . Operator :> >= < <= =
-- Query people whose wages are less than the average
SELECT * FROM emp WHERE emp.salary < (SELECT AVG(salary) FROM emp);
2. The result of subquery is multi row and single column :
Subqueries can be used as conditions , Use the operator in To judge
-- Inquire about ' Finance Department ' and ' The Marketing Department ' All employee information
SELECT id FROM dept WHERE NAME = ' Finance Department ' OR NAME = ' The Marketing Department ';
SELECT * FROM emp WHERE dept_id = 3 OR dept_id = 2;
-- Subquery
SELECT * FROM emp WHERE dept_id IN (SELECT id FROM dept WHERE NAME = ' Finance Department ' OR NAME = ' The Marketing Department ');
3. The result of subquery is multi row and multi column :
Subquery can participate in query as a virtual table
-- Query the entry date of the employee 2011-11-11 Future employee information and department information
-- Subquery
SELECT * FROM dept t1 ,(SELECT * FROM emp WHERE emp.`join_date` > '2011-11-11') t2 WHERE t1.id = t2.dept_id;
-- Common internal connection
SELECT * FROM emp t1,dept t2 WHERE t1.`dept_id` = t2.`id` AND t1.`join_date` > '2011-11-11'
4、 Data control language
DCL:Data Control Language
One 、 Manage users
- Add users
grammar :CREATE USER ' user name '@' Host name ' IDENTIFIED BY ' password ';
Delete user
grammar :DROP USER ' user name '@' Host name ';
Two 、 Rights management
- Query authority
-- Query authority
SHOW GRANTS FOR ' user name '@' Host name ';
SHOW GRANTS FOR 'lisi'@'%';
Grant authority
-- Grant authority
grant Permission list on Database name . Table name to ' user name '@' Host name ';
-- Give all permissions to Zhang San , On any table in any database
GRANT ALL ON *.* TO 'zhangsan'@'localhost';
Revoke authority
-- Revoke authority :
revoke Permission list on Database name . Table name from ' user name '@' Host name ';
REVOKE UPDATE ON db3.`account` FROM 'lisi'@'%';.
边栏推荐
- 软件测试工作太忙没时间学习怎么办?
- Express
- Sleep quality today 81 points
- Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
- 软件测试有哪些常用的SQL语句?
- Pedestrian re identification (Reid) - data set description market-1501
- Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
- 自动化测试中敏捷测试怎么做?
- UCORE lab1 system software startup process experimental report
猜你喜欢
Nest and merge new videos, and preset new video titles
接口测试面试题及参考答案,轻松拿捏面试官
ucore lab2 物理内存管理 实验报告
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
UCORE lab8 file system experiment report
Fundamentals of digital circuits (III) encoder and decoder
Quaternion -- basic concepts (Reprint)
Database monitoring SQL execution
Investment operation steps
随机推荐
How to rename multiple folders and add unified new content to folder names
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
王爽汇编语言学习详细笔记一:基础知识
How to learn automated testing in 2022? This article tells you
Summary of thread implementation
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Daily code 300 lines learning notes day 9
Brief description of compiler optimization level
Pedestrian re identification (Reid) - Overview
Portapack application development tutorial (XVII) nRF24L01 launch B
Keil5 MDK's formatting code tool and adding shortcuts
基于485总线的评分系统双机实验报告
MySQL数据库(四)事务和函数
About the garbled code problem of superstar script
[200 opencv routines] 98 Statistical sorting filter
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
How to use Moment. JS to check whether the current time is between 2 times
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
软件测试面试回答技巧