Manual Software Testing - Database
Testing
Topics Cover
1. What is database testing
2. Experience sharing
3. What to check
4. Testing activities
5. Testing scenarios
6. How to do DB query
7. Mindmap of sql
8. Cheatsheet
9. Triggers
10. SQL commands
11. Join
12. Union Clause
13. Constraints
14. Truncate and Drop
15. Group By Clause
16. Views
17. Interview / Written test expectations and sample question
1
Type of Testing
2
DB Testing
3
Mind map of sql
4
Testing Activities
Testing Scenarios
Test Site For learning DB query
Creating own database
-------------------------
Install JSON-Server
npm install -g json-server
Create a file named filename.json with sample data(JSON format)
Run command
json-server filename.json
========Where to test query======
offline hoila laragon but installation and setup needed
online hoile https://sqliteonline.com
table create and table data insert code needed to be save some where else so that we can copy and run once before demonstration.
Learning site
============
w3school
What to check
During testing following can also be checked
1. no of table according
2. number of column
3. data type of column
4. size of the column
5. null field
6. column keys
7. "stored procedure" exist in the database
8. valid and invalid data stored procedure execution and exception handling
9. Trigger
Example of a test case
TC Title Steps Expected Result Actual Result Status
1 No. of table 10 10 pass
Basic table related Query need to check according to database design specifications
SELECT
name
FROM
sqlite_schema
WHERE
type ='table' AND
name NOT LIKE 'sqlite_%';
SELECT Count(*)
FROM information_schema.tables
WHERE table_schema = 'dbo'
AND table_type = 'BASE TABLE'
SELECT count(*) as NumberOfColumns
FROM information_schema.columns
WHERE table_name = 'customers'
SELECT column_name
FROM
information_schema.columns
WHERE table_name = 'customers'
SELECT column_name, data_type
FROM
information_schema.columns
WHERE table_name = 'customers'
SELECT column_name, column_type
FROM information_schema.columns
WHERE table_name = 'customers'
SELECT column_name, is_nullable
SELECT column_name,
FROM
collumn_key
information_schema.columns
FROM
WHERE table_name = 'customers'
information_schema.columns
WHERE table_name = 'custome
Cheat Sheet
Cheat Sheet Continued
SQL commands
Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL
SQL commands
Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL
DDL(Data Definition Language)
To create & modify database objects
Example: CREATE,DROP,ALTER,TRUNCATE
DQL(Data Query Language)
To fetch the data from the database
Example: SELECT
DML(Data Manipulation Language)-
To modify the database objects
Example: INSERT,UPDATE,DELETE
Interview question
Difference between where clause and having clause?
Difference between delete and truncate?
Join
The SQL Joins clause is used to combine records from two or more tables in a database.
A JOIN is a means for combining fields from two tables
by using values common to each.
There are different types of joins available in SQL −
1. INNER JOIN − returns rows when there is a match in both tables.
2. LEFT JOIN − returns all rows from the left table, even if there are no matches in the right table.
3. RIGHT JOIN − returns all rows from the right table, even if there are no matches in the left table.
4. FULL JOIN − returns rows when there is a match in one of the tables
5. SELF JOIN − is used to join a table to itself as if the table were two tables, temporarily renaming at
least one table in the SQL.
6. CARTESIAN JOIN − returns the Cartesian product of the sets of records from the two or more joined
tables
Join (example)
Basic syntax is given below, eg for Inner Join which is frequently used. Rest of the join is similar. User
decides
Which one to use depending on the requirements as given above.
Interview Question: Difference between inner and outer join
Union Clause
UNIONS CLAUSE
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without
returning any duplicate rows.
To use this UNION clause, each SELECT statement must have
• The same number of columns selected
• The same number of column expressions
• The same data type and
• Have them in the same order
But they need not have to be in the same length.
Syntax
The basic syntax of a UNION clause is as follows −
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
Here, the given condition could be any given expression based on your requirement.
Union Clause (example)
Interview Question: What is the difference between union and union all
Constraints
Constraints are the rules enforced on the data columns of a table.
These are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the database.
Constraints could be either on a column level or a table level.
The column level constraints are applied only to one column,
whereas the table level constraints are applied to the whole table.
Following are some of the most commonly used constraints available in SQL.
Interview Question:
Constraints contd.
• NOT NULL Constraint − Ensures that a column cannot have NULL value.
• DEFAULT Constraint − Provides a default value for a column when none is specified
• UNIQUE Constraint − Ensures that all values in a column are different.
• PRIMARY Key − Uniquely identifies each row/record in a database table.
• FOREIGN Key − Uniquely identifies a row/record in any of the given database table.
• CHECK Constraint − The CHECK constraint ensures that all the values in a column satisfies certain conditions.
• INDEX − Used to create and retrieve data from the database very quickly.
Constraints can be specified when a table is created with the CREATE TABLE statement or you can use the
ALTER TABLE
statement to create constraints even after the table is created.
Interview Question:
Constraints contd.
• NOT NULL Constraint − Ensures that a column cannot have NULL value.
Below are an example for creating “not null” constraints.
But all the other constraints can be creating and altered in similar fashion.
Interview Question:
Truncate and Drop
The SQL TRUNCATE TABLE command is used to delete complete data from an existing table.
You can also use DROP TABLE command to delete complete table but it would remove complete table structure
form the database and
you would need to re-create this table once again if you wish you store some data.
Syntax
TRUNCATE TABLE table_name
Interview Question: Difference between truncate and drop
Group By Clause
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into
groups.
This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY
clause.
Syntax
The basic syntax of a GROUP BY clause is shown in the following code block. The GROUP BY clause must
follow the conditions
in the WHERE clause and must precede the ORDER BY clause if one is used.
Syntax
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2
Interview Question:
Group By Clause cont.
if you want to know the total amount of salary on each customer, then the GROUP BY query would be as follows
Additional Question: Imagine and practice a query with group by and having clause.
Views
A view is nothing more than a SQL statement that is stored in the database with an associated name. A view is
actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or select rows from a table.
A view can be created from one or many tables which depends on the written SQL query to create a view.
Views, which are a type of virtual tables allow users to do the following −
• Structure data in a way that users or classes of users find natural or intuitive.
• Restrict access to the data in such a way that a user can see and (sometimes) modify exactly what they need
and no more.
• Summarize data from various tables which can be used to generate reports.
Views contd
Creating Views
Database views are created using the CREATE VIEW statement.
Views can be created from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the specific implementation.
The basic CREATE VIEW syntax is as follows −
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
You can include multiple tables in your SELECT statement in a similar way as you use them in a normal SQL
SELECT query.
Views example
example
Example contd