当前位置:网站首页>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

2、 Data operation language

3、 Data query language

4、 Data control language


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'@'%';.

 

原网站

版权声明
本文为[Test Xiaowan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919129650.html