database testing Syntaxes
database testing Syntaxes
rows : it’s the record ( the information for a specific student : for example Ahmed
is a student that is enrolled in course of database and paying 200 LE as a price
for this course )
SQL ?
it stands for “ structure query language “ and it’s the way you can communicate with
database for managing the information within
— through SQL you can :
Create database
Create stored procedures for retrieving, adding, updating or even deleting data
from database
DBMS ?
it stands for DataBase Management System, it’s the software you use to manage
database through using SQL queries
SQLite database
SQL server
all you need to know is that there is no major difference between all the DBMSs in
Market
the differences are minor : mainly there are some queries supportive for some
DBMS but not for the others
you need to know the difference between SQL and MySQL ( SQL is the
structure query language, But MySQL is the DBMS you use to manage the
Database through using SQL queries
MySQL Server
MySQL Editor
https://dev.mysql.com/downloads/mysql/
for MySQL workbench : https://dev.mysql.com/downloads/workbench/
.
links for version 5.6
: https://dev.mysql.com/doc/refman/5.6/en/macos-installation-pkg.html
Workbench is the place where you will write your queries and manage the
Database
steps :
press on this :
the interface :
SCHEMAS :
QUERIES Playground :
as defined before, you can navigate them through the interface as shown
2. Tables
3. Columns
then hit the execute button ( Note : when you select a specific query like
shading it, and hit the execute button, it will execute the selected query only, but
if you hit the execute button without selecting a specific query, it will execute all
queries in the workbench )
to make sure that the result is successful, look at the action result and its details
first of all, to start working with a specific database, you must mention that with
command :
use NameOfDatabase;
Data type :
in all tables , for any type of information, you need to have an ID for the table
records and it must be an integer data
Check : https://dev.mysql.com/doc/refman/8.0/en/numeric-type-
attributes.html
first of all, if anyone asked you to know about the structure of a table you have
created, the schema : ( which is the fields of the table, each field constraints :
the data type expected for each field, if the field accept null value or not and if
the field takes a default value if doesn’t specified while adding a new record to
the table ), and all those data are important for database testing, right now, we
will use ( describe command to reach the schema of the table )
as shown :
Using ( select ) keyword, and it’s commonly used by 99%, it’s also majorly used
in database testing ( the goal is retrieving data from a specific database )
like this :
describe : is used to get the schema ( as we said it’s the description of our
table, the value type for each column, the data constraint for each column for
our table in database )
select : is used to retrieve data from database ( for a specific column or for the
whole table using * )
we can edit on any table of a specific database by using the query ( command )
( alter table )
alter table command is used only of editing on the table structure ( the schema ),
not editing the data inside the table itself
type of edits :
adding a new column
modifying the column specs ( constraints )
deleting a specific column
to delete a specific column from a table using alter table command with drop
column :
if we want to give value 1 to the column named co1 for the row which col2 is
value2 ( the condition syntax for SQL )
that error means we are using SQL in mode where we can’t update on any data
inside any table, so to edit this, we go to edit window from preferences ,,
uncheck the box of safe updates from SQL Editor, then you must reconnect to
the server from the tab ( Query > reconnect to server ):
Note : you must use the command ( use ) to point to the specific database you
want to delete before deleting it ( IMPORTANT )
4. to get the schema ( constraints for each row, datatype, and so on - the
structure of the table ) : describe TableName;
8. to edit on the schema of the table ( adding a new column ) : alter table
TableName add ColumnName DataType(limit);
10. to edit on the schema of the table ( deleting a column existing ) : alter
table TableName drop ColumnName;
11. to update data in table ( adding a new data to a specific column for a
record or updating this data - edit on this specific column for this
specific record ) : update TableName set col1=value1 where col2=value2;
12. to update data in table ( delete record from the table ) : delete from
TableName where col1=value1;
1. distinct keyword :
we use distinct keyword in case we have a lot of values for a specific column in
database ,, for example : we have two persons named ahmed and two persons
named hossam and two persons named kareem,, now we want to get all names
from the table but for each two hossams we want to get one hossam as it’s a
distinct name
as we see :
so, in case we have more than one condition we use and, but in case we have
some groups of conditions that we want to retrieve data based on it
for example :
we want to get all the names in table in the database we using that :
WOHOO :
when using AND operator and OR operator, make sure of using brackets
like this :
AND operator is used to get data based on satisfying the condition before
and after AND
the operator IN :
we want to get all names for persons whose age is in range from 22 to 30 .
we have only one way to express the range to SQL, using BETWEEN ——
AND ——— :
NOT operator :
NOT operator is only used with BETWEEN ( NOT BETWEEN ) in case we have
a 1000 records in database specific table, and we want to get data for all
records ( EXCLUDING ) a range of records from them ,, so how we do that :
:: select ColumnName from TableName where Col2 not between Value1 and
Value2;
like this :
when we need to retrieve some data for a specific condition for example, a
value we know to filter with for a specific column ( name as ex )
but if that value is varchar , and we don’t know the full spelling for it , we are
using % and _ :
for example we need to get all data for a person whose name is beginning with
“ a “ character :
:: select * from TableName where name like ‘a%’; #that means we want to get
all data from table for persons that their names start with “ a “ whatever the
number of the rest characters
and so on
note that :
don’t forget to give a concentration on where the letter located in the word
for example :
hossam > ____a%
ahmed > a%
we can order the result of retrieved data from table by using the keyword ( ORDER
BY )
we want to get all data from table ordering the results by name :
( Note : it will re-arrange them in ascending order by default as you will see down
there ) :
note that : if we used it by ordering the results as for a value that is string ( varchar )
, it will re-arrange them in an alphabetical way
Note : we may need to order them in descending order ( from larger to smaller
or from last to first in strings )
we use GROUP BY key word and Aggregate functions in case we have a lot of data
( records ) in database specific table, and we need to know a specific value that
present some records with a common factor between them ,, in more details , lets
see the example :
first of all : we will use the keyword GROUP BY , to group the records based on
months and let’s see what happens.
using syntax :
will be simply solved by changing the sql mode in MySQL by this command,
This too works for me.. I used this, because in my project there are many
Queries like this so I just changed this sql mode to only_full_group_by
OR simply include all columns in the GROUP BY clause that was specified by
the SELECT statement. The sql_mode can be left enabled.
Thank You... :-)
but, we need to get the sum of amount for the whole month, not the fastest result of
the amount of a specific day , so we use Aggregate function with group by called
SUM(), we can use it in form of syntax like this :
in this case it will give us the sum of amount but will not specify which value belongs
to which month like this :
so we can give more specification for the retrieved data by adding more values to
get to the sum of amount ( the specific month for each ) like this :
we can get the Minimum value for a specific column based on another column value
( filtering ) by using the function Min(ColumnName) :
also we can get the Average value for a specific column based on another column
value ( filtering ) by using the function Avg(ColumnName) :
for example : we need to count all records filtered by month in case the records for
a month is less than 3 records
how we can do it
or
when we said that we use the keyword (having) with group by, we used it as a
condition that will be applied on the results column, not a column for the table
we are retrieving data from
so when we said :
we meant to have all the records number for each month where the records
doesn’t exceed a specific number
but by using where, we are butting a condition on the results for retrieving the
data based on the column data contained itself , not the results
1. we must have a common column between the two tables that makes us able to
get the data we need for the same common ones
SubQueries :
using a query to provide a value for a condition to retrieve data using a parent query
from a specific table in terms of the other table
in our example :
we have those :
:: select name,age from citizens where name=(select name from citizenloc where
location=’texas’)
in case you have more than one person located in texas, you must take in
consideration that your subquery will give you more than one record, so it couldn’t
be ( name=(subquery)), instead you will use ( name in(subquery));
:: select name,age from citizens where name in(select name from citizenloc where
location=’california’)
in the previous case, we have two tables, and a common column between them,
and we used subqueries to get data from a table in terms of the others using that
common column values,
easily we can use JOIN concept to generate a table which is containing the data of
two tables :
Note : the same as subqueries , to join two or more tables, you need to have
one common column between them at least, or one common column between
two of them ( if you have more than two tables ) and another common one
between the other two ( no need for the common column to be common
among the three tables, it’s just needed to have a common column between
the first pairs and it could be different from the other common one between
the other pair )
1. use the select syntax and refer to the tables you want to get data from to insert
into the join table for each column from each one as this :
2. you must refer to the basement of join so SQL will not be confused by matching
the data based on columns in case they are different in arrangement ( give each
name its value in each table of them )
WOHOO :
so, if you want to but a condition, so you will use the refer concept with the new
letters ( as refered to each table of the two tables ) :
ex : we want to get the name, location and age of people who are living in california
practical example :
we want to get the total number of employees for a company which established in
1990
here is the solution, we are using inner join easily and get what we want :
1. inner join :
the same requirement, we need all data for first entity students who passed the
exam,
using inner join :
inner join is the way we used as before but to be more specific in types of join we
use INNER JOIN
and as a result , we don’t need to use the keyword AS
2. left join :
we want to get the student details for all from the first entity who appeared in the
exam ( attended but not passed ) > it means we want to get a join table that
contains all the students from the first entity student details table :
we are going to use left join, in the same syntax with inner but using LEFT instead
of INNER
we want to get the student details for all from the second entity who passed in the
exam ( no matter they are belonging to the first entity or not ) > it means we want to
get a join table that contains data for all students passed in the second entity no
matter they are belonging or not to the first entity :
we are going to use right join, same syntax as inner and left but using RIGHT
instead :
views
views are child tables created from a parent table to have a new table grapping all
data for a specific condition :
for example :
we have a table in database that gives you information about a lot of companies,
and you want a new table generated from it to get the data from, only for one or two
companies, then you need to use views :
like this :
and it will not be added to the tables of database, instead it will be added to views
as like :
using subqueries will allow you to get a comparison between the sum of
employees for a specific company and the average number of employees of the
whole companies in the parent table
so :
string functions :
1. if we need to concatenate two strings into single column we use the string
function called CONCAT:
in our example we need to concatenate the name and branch in this table in a new
column :
2. substring :
we use it when we need to have a column that contain string info from another
column from the table but not the full string contained in that column from the table
for example , we want a column that contain abbreviation for the company name (
let’s say the first and second letter only )
in case we need to take part from letter to the other, we specify the starting
index as well as the ending index like this : substr(name,3,5)
in our example we need only the first two letters from all of the companies names :
when we are asked to replace a name or part of the name of a data inside a specific
column, we use the string function REPLACE :
select replace( ColName, ‘the part of the name we want to replace’,’the desired
replacement of that part) from TableName where ColName=’the name we want to
replace part of it’;
for example :
it didn’t changed because you didn’t give concern for the capital letter in the
QAClickAcademy ( Aca instead of aca ) . very important
4. length
for example :
as you see, it gives you the count of letters contained on each company name in the
name column in company table
6. limit :
we use it when we are asked to get only the first (##) of records in a table in
database
for example :
we have two tables : the first table contains the names and IDs of the 1st and 2nd
year students
the second table contains the names and IDs of the 2nd year and 3rd year students
and we want to get a merged table contains the names and IDs for the 1st,2nd and
3rd year students.
applying that :
Union all :
in case we want a merged table ( with all records : containing duplicated records )
we use union all
applying in SQL :
we use it to get the intersection between two tables, that means it gives you a table
that contains the records only found in both tables :
as we said , we can’t use intersect keyword , for info only , in oracle we use it the
same syntax as union and union all like this :
applying that :
we use it when we need to retrieve all data from a specific table in case specific
record belongs to that table
so , the policeman want to know if the student belongs to the college or not (
condition )
then, if so, he want to get all the students’ data from that college :
we want to know if id = 574 exists in table or not, if so , get all data for the college :
:: select * from college where exists ( select * from college where id = ‘574’ );
:: select * from college where exists ( select * from college where id= 333 );
4. Case :
if we got a table, and we want to modify on some records of the table for specific
records only , we use case statement :
SQL constraints :
table constraints :
it’s the way how we can limit the data type which may be added in the future to the table
different columns.
it’s too important concept in database testing to validate on the constraints for the
columns of a specific table or group of tables in database.
1. NOT NULL :
it’s a constraint which can be added to the table schema for a specific column while
creating the table, which doesn’t allow any record to be added to the table while not
giving a value for that column ( Note : any column by default, when adding a record to
the table while the value for that column is missing, it takes NULL value )
( Note : after adding this constraint, when you try to add a record to a table in database
without providing value for that specific column, the query will not be executed and it will
give you an error )
then Col2 will not accept null values, and you can’t add records without giving a value
for that column, like that :
1. trying to add a new record to the table without giving a value to flight id :
2. trying to add record with null value for the column flight id :
it’s added to a column as a constraint, if we want to make that column take a default
value in case there is no value provided to it while adding a new record to the table
for a company that have too many branches and one main branch, if we are creating a
table for employees details, and we want to add records for each employee, with option
that if we are not providing information about for which branch he(she) belongs, then it’s
by default belonging to the main branch,
Note that ( in MySQL 8.0 version and above, in insert statement, you must specify the
columns you provide data for with the same arrangement of columns, without
mentioning the default value constrained column ), like this :
:: insert into TableName(ColName) values(ValueforTheColumnMentioned);
3. Unique constraint :
trying to insert duplicated values for two records ( values for the unique constrained
column ):
for overview :
when we want to put a customized constraint on a column value, it’s called CHECK,
for example, while we create the table, we want the ID column not to accept any values
that is less than 10, so we can simply do it by check constraint,
5. Primary Key :
Note : one table can contain more than one primary key, in this case it’s named as
composite key
Note2: the UNIQUE constraint + NOT NULL constraint = PRIMARY KEY constraint
that means Col1 is the primary key for that table, it means it couldn’t accept null value (
not null ), and it must be unique as well.
applying that :
the foreign key is the way we have to integrate between two tables in database,
the foreign key is the first table’s primary key and it should be contained in the second
table which we want to integrate the first table to :
1. the primary key for the first table must be unique and not null for the first table
2. the foreign key ( which is the primary for the first table ) may have one value for
more than one record ( may not be unique ) but it doesn’t accept null values for the
second table
3. to insert a record into the second table ( including value for the foreign key ), this
value must be contained in the first table to create that record in the second table
4. to delete a record in the first table, it must be checked first for all the records related
to the record need to be deleted in the second table, that they must be deleted
FIRST from the second table before deleting the record from the first table
assuming that we have an online shopping website , in this website, you must be
registered as customer before placing any order, so :
1. we must have a table for the registered customer data ( info ) including a primary
key ( ID for example and the best practice ) that will be the foreign key for any other
integrated table
2. we must have a table for the placed orders that contain ( the customer ID as a
foreign key , the order ID as the primary key )
that means :
ahmed’s account can’t be deleted before they two records for the two orders he placed
are deleted first.
how we can apply that for two tables in database ( 1 parent table and 1 child table ) :
Trying to have this on our real time scenario example ( customer details and order
details tables ) :
4- adding records to order details ( trying to add using id that doesn’t found in the parent
table ) :
7- deleting records from the parent table after deleting its related records from the child
table :
how to provide delete option directly from parent table ( even there are records in the
child one related to that record ) :
simply we are using the concept of “ ON DELETE CASCADE “ , here when we directly
try to delete a record from the parent table, it will be deleted and all the records related
to it in the child table will also be deleted.
applying that :
Note : if you tried to drop the parent table, you must drop all child
tables first ( the same as records )