Database and SQL Complete
Database and SQL Complete
SQL stands for Structural Query Language, and SQL is used for storing, manipulation, and retrieving data
from the database.
A database is a well-ordered collection of data. A database is an electronic system that permits data to
be easily manipulated, accessed, and updated, or an organization uses a database as a method of
managing, storing, and retrieving information. Modern databases are handled using a database
management system (DBMS).
Relational Databases are used to store data in tables (rows and columns). Some common relational
database management systems that use SQL are Oracle, Sybase, Microsoft SQL Server, Access,
Ingres, etc.
Row
Column
A Relational Database contains tables that store the data that is related in some way. SQL is the query
language that allows retrieval and manipulation of table data in the relational database. The
database below has two tables: one with data on Users and another with data on Products
To understand SQL, it is important to understand its keywords, data types and list of operators
An SQL data type refers to the type of data which can be stored in a column of a database table. For
example integer data, character data, monetary data, date and time data, binary strings, and so
on.
While creating a database table in a database, we need to specify following two attributes to define a table
column:
A database table's column defines the data, whereas database table rows populate data into the table.
For example, if you want to store student name in a column then you should give column name something
like student_name and it's data type will be char(50) which means it can store a string of characters up to 50
characters.
The data type provide guidelines for SQL to understand what type of data is expected inside each column, and
hence, prevents the user from entering any unexpected or invalid data in a column. For example, if we
want a column to store only integer values, we can specify its data types as INT. SQL will show an error if any
other value apart from an integer is inserted into that particular column. Different RDBMS supports different
type of data types to define their tables.
There are three main types of SQL data types available in any RDBMS. They are listed below −
• String
• Numeric
• Date and Time
SQL Data types are defined during the creation of a table in a database. While creating a table, it is required
to specify its respective data type and size along with the name of the column.
When we assign the appropriate data type to a column, we can make efficient use of memory by
allocating only the amount of system memory required for the data in the relevant column.
There are three main data types available in MySQL Database: string, numeric, and date and time.
Following section lists down all the data types available in MySQL Version 8.0
An SQL operator is a reserved word or a character used primarily in an SQL statement's WHERE clause to
perform operation(s), such as comparisons and arithmetic operations. These Operators are used to
specify conditions in an SQL statement and to serve as conjunctions for multiple conditions in a statement.
An SQL operator can be either a unary or binary operator. A unary operator (example unary + or unary - )
uses only one operand to perform the unary operation, whereas the binary operator (example + or - etc) uses
two operands to perform the binary operation.
• Arithmetic operators
• Comparison operators
• Logical operators
SQL Arithmetic Operators are used to perform mathematical operations on the numerical values. SQL
provides following operators to perform mathematical operations.
+ Addition 10 + 20 = 30
- Subtraction 20 - 30 = -10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 5%2=1
SQL Comparison Operators
SQL Comparison Operators test whether two given expressions are the same or not. These operators are used
in SQL conditional statements while comparing one expression with another and they return a Boolean value
which can be either TRUE or FALSE. The result of an SQL comparison operation can be UNKNOWN when one
or another operand has it's value as NULL.
SQL Logical Operators are very similar to comparison operators and they test for the truth of some given
condition. These operators return a Boolean value which can be either a TRUE or FALSE. The result of an
SQL logical operation can be UNKNOWN when one or another operand has its value as NULL.
Operator Description
The operator precedence in SQL is the sequence in which the SQL evaluates the different operators in a
given expression. The operators with higher precedence get evaluated first.
Following table lists all SQL operators as per their precedence. The operators with the highest precedence are
at the top and the operators with the lowest precedence are at the bottom.
Operator Operation
+, - identity, negation
*, / multiplication, division
+, - addition, subtraction
=, !=, <, >, <=, >=, IS NULL, LIKE, BETWEEN, Comparison
IN
NOT logical negation
AND conjunction
OR inclusion
Example
SELECT 20 - 3 * 5;
This will result in the following. Here multiplication operator gets evaluated first and then subtraction happens.
SQL Keywords
SQL keywords are reserved words that have special meanings in the SQL language. These keywords are used
to perform various operations on a database, such as creating, modifying, and querying data
Keyword Description
ADD Adds a column in an existing table
ALL Returns true if all of the subquery values meet the condition
ALTER Adds, deletes, or modifies columns in a table, or changes the data type of
a column in a table
ANY Returns true if any of the subquery values meet the condition
CHECK A constraint that limits the value that can be placed in a column
CREATE UNIQUE INDEX Creates a unique index on a table (no duplicate values)
CREATE VIEW Creates a view based on the result set of a SELECT statement
DROP CONSTRAINT Deletes a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint
FOREIGN KEY A constraint that is a key used to link two tables together
GROUP BY Groups the result set (used with aggregate functions: COUNT, MAX, MIN,
SUM, AVG)
INNER JOIN Returns rows that have matching values in both tables
INSERT INTO SELECT Copies data from one table into another table
LEFT JOIN Returns all rows from the left table, and the matching rows from the right
table
NOT NULL A constraint that enforces a column to not accept NULL values
OUTER JOIN Returns all rows when there is a match in either left table or right table
PRIMARY KEY A constraint that uniquely identifies each record in a database table
PROCEDURE A stored procedure
RIGHT JOIN Returns all rows from the right table, and the matching rows from the left
table
SELECT INTO Copies data from one table into a new table
SELECT TOP Specifies the number of records to return in the result set
SET Specifies which columns and values that should be updated in a table
TRUNCATE TABLE Deletes the data inside a table, but not the table itself
UNION Combines the result set of two or more SELECT statements (only distinct
values)
UNION ALL Combines the result set of two or more SELECT statements (allows
duplicate values)
UNIQUE A constraint that ensures that all values in a column are unique
WHERE Filters a result set to include only records that fulfill a specified condition
Open Google and type Download MySQL and Click on MySQL Community Downloads
There are many in-build Databases in MySQL; we can type show database.
we can use any of the above databases by just typing use database_name
3. SQL QUERY
A database most often contains tables. Some name identifies each table. The table includes
records(rows) with Data. To access those records, we need SQL Syntax. Most of the action you need to
perform Database by using the SQL Statement.
Note: SQL keywords are not case-sensitive (e.g., select as SELECT) o The
syntax of the language describes the language element. o SQL syntax is
somewhat like simple English sentences. o Keywords include SELECT,
UPDATE, WHERE, ORDER BY ETC.
Four fundamental operations that can apply to any databases are:
The SELECT statement permits you to read data from one or more tables.
FROM customer;
SELECT *
FROM customer;
The WHERE clause allows the user to filter the data from the table. The WHERE clause allows the user to
extract only those records that satisfy a specified condition.
SQL requires single quotes around text values (many database systems will also use double quotes). And
numeric fields should not be enclosed in quotes.
FROM customer
Order by is used to print the values from the table in order(ascending or descending)
FROM customer
FROM customer
ORDER BY first_name ASC;
The SELECT TOP is used to specify the number of records from the to return. The SELECT TOP is useful on
large tables with millions of records.
MySQL Syntax:
SELECT first_name, last_name,email
The MIN() function in SQL returns the smallest value of the selected column from the table. The MAX()
function in SQL returns the largest value of the selected column from the table.
MIN() Syntax
SELECT MIN(address_id)
FROM customer;
MAX() Syntax
SELECT MAX(address_id)
FROM customer;
The COUNT() function gives the number of rows that matches specified conditions. And the AVG()
function in SQL returns the average value of a numeric column. The SUM() function in SQL returns the
total sum of a numeric column.
COUNT() Syntax
SELECT COUNT(email)
FROM customer;
AVG() Syntax
SELECT AVG(active)
FROM customer;
SUM() Syntax
SELECT SUM(active)
FROM customer
The LIKE operator is used with the WHERE clause to find for a specified pattern in an attribute. The two
wildcards are used in conjunction with the LIKE operator:
Note: MS Access uses an asterisk (*) in place of the percent sign (%) and a question mark (?) in place of the
underscore (_).
LIKE Syntax
Selects all columns of the customer with a first_name starting with "D".
Selects all columns of the customer with a first_name Ending with "E":
Selects all columns of the customer with a first_name that have "or" in any position.
SELECT * FROM customer
Selects all columns of the customer with a first_name that starts with "a" and ends with "o":
SELECT * FROM customer
The IN operator allows users to specify multiple values in a WHERE clause. The IN operator is a
shorthand for various OR conditions.
IN Syntax
SELECT column_name(s)
FROM table_name
FROM table_name
The BETWEEN operator retrieves values within the given range. The values can be texts, numbers, or
dates. The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
Select all the columns from the customer with customer_id between 1 to 20.
Select all the columns from the customer with customer_id, not between 1 to 570.
Aliases are used to give a nickname to a column in a table, a temporary name. Aliases are used to make
column names more readable to the user.
FROM customer AS c
The GROUP BY used to group rows from the table. And it has the same values as summary rows. For
example, find the number of customers in each country, The GROUP BY is often used with aggregate
functions like (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Count the number of active and non-active customers
The HAVING clause is added to SQL because the WHERE keyword can not be used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
The UNION operator allows the user to combine the result-set of two or more SELECT statements in SQL.
Each SELECT statement within UNION should have the same number of columns. The columns in each SELECT
statement should also be in the same order. The columns should also have similar data types.
UNION
The UNION operator selects only different values by default. To allow duplicate values, the user can use
UNION ALL operator.
UNION ALL
Note: The column names in the output are usually equal to the column names in the first SELECT
statement in the UNION.
4. SQL JOIN
The SQL Join help in retrieving data from two or more database tables. The tables are mutually related
using primary keys and foreign keys.
Type of Joins
INNER JOIN
The INNER JOIN is used to print rows from both tables that satisfy the given condition. For example,
the user wants to get a list of users who have rented movies together with titles of movies rented by
them. Users can use an INNER JOIN for that, which returns rows from both tables that satisfy with given
conditions.
The INNER JOIN keyword selects records that have matching values in both the tables.
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
LEFT JOIN
The LEFT JOIN returns all the records from the table1 (left table) and the matched records from the
table2 (right table). The output is NULL from the right side if there is no match.
LEFT JOIN
SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
The RIGHT JOIN is the opposite of LEFT JOIN. The RIGHT JOIN prints all the columns from the
table2(right table) even if there no matching rows have been found in the table1 (left table). If there no
matches have been found in the table (left table), NULL is returned.
RIGHT JOIN
SELECT column_name(s)
FROM table1
The FULL OUTER JOIN keyword returns all records when there are a match in left (table1) or right
(table2) table records.
Note: FULL OUTER JOIN can potentially return very large result-sets!
SELECT column_name(s)
FROM table1
Note: MySQL does not support the Full Join, so we can perform left join and right join separately then
take the union of them.
SELECT * FROM t1
SELECT * FROM t1
SELF-JOIN
SELECT column_name(s)
WHERE condition;
5. SQL DATABASE
The CREATE DATABASE statement in SQL is used to create a new SQL database.
Syntax
Let’s create a database and give name as testdb CREATE database testdb;
Now, let’s check the databases in MySQL by using show databases query.
Show databases;
The DROP DATABASE statement in SQL is used to drop an existing SQL database.
Syntax
Let’s drop the created database by using drop database testdb. DROP database testdb;
Now, let’s check the databases in MySQL by using show databases query after dropping the testdb.
SHOW databases;
The CREATE TABLE statement in SQL is used to create a new table in a database.
Syntax
....
);
The column1, column2, ….., specify the names of the columns of the table. The datatype parameter
specifies the type of data the column can hold (e.g., varchar, integer, date, etc.)
CREATE TABLE cutomer(id integer, first_name varchar(10), last_name varchar(10), city varchar(10),
country varchar(15), phone varchar(15));
DESC customer;
The DROP TABLE statement in SQL is used to drop an existing table in a database. DROP TABLE
customer;
The table has dropped after running the query drop table table_name. As we can see, the table does not
exist after dropped.
Now we are going to create the same table again to insert the values in that table.
The INSERT INTO statement in SQL is used to insert new records in a table.
We can write the INSERT INTO statement in two ways. The first way is to specify both the column names
and the values to be inserted:
INSERT INTO customer(id , first_name, last_name ,city ,country,phone)VALUES (2, ‘Ana’, ‘Trujillo’,
‘Mexico’, ‘Mexico’, (5) 555-4729);
If users are adding values for all the columns of the table, you don’t need to specify the particular column
names in the SQL query. However, ensure the order of the values is in the same order as the columns in
the table.
We have inserted two rows yet. Similarly, we can insert many rows in the table. Finally, we have added
ten rows as we can see in the picture below.
The field with a NULL value is a field with no value. If the field in a table is optional, to insert new data or
update data without adding a value to this field and Then, the field will be saved as a NULL value.
Note: A NULL value is not the same as a zero value, or we can say a field that holds spaces. The field
with a NULL value is one that has been left blank during record creation!
As we can able to see, the last row contains one NULL value.
To test for NULL values in the table has to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT *
It will return those countries which have some values(expect Null values).
The UPDATE statement in SQL is used to modify the existing records in a table.
UPDATE Syntax
UPDATE customer
DELETE Syntax
The ALTER TABLE statement in SQL is used to add, modify, or delete columns in an existing table. And it
also used to add and drop various constraints on a current table.
To change the data type of column values in a table, use the following syntax: ALTER TABLE customer
ADD COLUMN dob date;
We have assigned the dob with the datatype date. But now we want to change the datatype from date to
year.
ALTER TABLE customer MODIFY dob year;
To delete a specific column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
Syntax:
5. SQL DATABASE
The CREATE DATABASE statement in SQL is used to create a new SQL database.
Syntax
Let’s create a database and give name as testdb CREATE database testdb;
Now, let’s check the databases in MySQL by using show databases query.
Show databases;
The DROP DATABASE statement in SQL is used to drop an existing SQL database.
Syntax
Let’s drop the created database by using drop database testdb. DROP database testdb;
Now, let’s check the databases in MySQL by using show databases query after dropping the testdb.
SHOW databases;
The CREATE TABLE statement in SQL is used to create a new table in a database.
Syntax
....
);
The column1, column2, ….., specify the names of the columns of the table. The datatype parameter
specifies the type of data the column can hold (e.g., varchar, integer, date, etc.)
CREATE TABLE cutomer(id integer, first_name varchar(10), last_name varchar(10), city varchar(10),
country varchar(15), phone varchar(15));
DESC customer;
The table has dropped after running the query drop table table_name. As we can see, the table does not
exist after dropped.
Now we are going to create the same table again to insert the values in that table.
The INSERT INTO statement in SQL is used to insert new records in a table.
We can write the INSERT INTO statement in two ways. The first way is to specify both the column names
and the values to be inserted:
INSERT INTO customer(id , first_name, last_name ,city ,country,phone)VALUES (2, ‘Ana’, ‘Trujillo’,
‘Mexico’, ‘Mexico’, (5) 555-4729);
If users are adding values for all the columns of the table, you don’t need to specify the particular column
names in the SQL query. However, ensure the order of the values is in the same order as the columns in
the table.
We have inserted two rows yet. Similarly, we can insert many rows in the table. Finally, we have added
ten rows as we can see in the picture below.
The field with a NULL value is a field with no value. If the field in a table is optional, to insert new data or
update data without adding a value to this field and Then, the field will be saved as a NULL value.
Note: A NULL value is not the same as a zero value, or we can say a field that holds spaces. The field
with a NULL value is one that has been left blank during record creation!
INSERT INTO customer VALUES(11, ‘Victoria’, ‘Ashworth’, ‘London’, NULL, ‘(171) 555-1212’)
As we can able to see, the last row contains one NULL value.
To test for NULL values in the table has to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT *
It will return those countries which have some values(expect Null values).
The UPDATE statement in SQL is used to modify the existing records in a table.
UPDATE Syntax
UPDATE customer
DELETE Syntax
DELETE FROM customer WHERE id = 11;
The ALTER TABLE statement in SQL is used to add, modify, or delete columns in an existing table. And it
also used to add and drop various constraints on a current table.
To change the data type of column values in a table, use the following syntax: ALTER TABLE customer
ADD COLUMN dob date;
We have assigned the dob with the datatype date. But now we want to change the datatype from date to
year.
ALTER TABLE customer MODIFY dob year;
To delete a specific column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
Syntax:
The Constraints in SQL can be specified when the table is created with the CREATE TABLE statement, or
after the table is altered with the ALTER TABLE statement.
Syntax:
);
SQL Constraints
SQL constraints are used to specify any rules for the records in a table. Constraints can be used to limit
the type of data that can go into a table. It ensures the accuracy and reliability of the records in the
table, and if there is any violation between the constraint and the record action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and tablelevel
constraints apply to the whole table.
CONSTRAINTS DESCRIPTION
Index It is Used to create and retrieve data from the database quickly.
The NOT NULL constraint enforces a column NOT to accept NULL values. This imposes a field always to
contain a value, which means that the user cannot insert a new record in a table or update a record
without adding a value to this field.
The following SQL ensures that the "id", "First_name" and "Last_name" columns will NOT accept NULL
values when the "student" table is created:
Example
CREATE TABLE student( id int NOT NULL, first_name varchar(25) NOT NULL, last_name
varchar(25) NOT NULL, age int
);
In the above table, it has specified the id, first_name, and last_name as not null and age as null.
To make a NOT NULL constraint on the "age" column when the "student" table is already created, use
the following SQL:
Example:
The UNIQUE constraint in SQL ensures that all values in a column are distinct. UNIQUE and PRIMARY
KEY constraints both provides a guarantee for uniqueness for a column or group of columns. A
PRIMARY KEY constraint, by default, has a UNIQUE constraint. However, the user can have many
UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
Creates UNIQUE constraint on the "id" column when the "person" table is created
CREATE TABLE person ( id int NOT NULL, last_name varchar(255) NOT NULL, first_name
varchar(255), age int,
UNIQUE (ID)
);
We have applied unique constraints on id, and as we can see, it is showing as the primary key.
Create a UNIQUE constraint on the "first_name" column when the "persons" table already
exists.
To name the UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the
following SQL syntax:
As we can see in the person table The unique constraint(UC_Persons) has been dropped.
SQL PRIMARY KEY CONSTRAINTS
The PRIMARY KEY constraint uniquely identifies each of the records in a table. Only ONE primary key can
have in a table. And also, in the table, this primary key can consist of single or multiple columns (fields).
Primary keys should contain UNIQUE values, and cannot contain NULL values.
CREATE TABLE person(ID int NOT NULL, last_name varchar(255) NOT NULL, first_name varchar(255),
age int, PRIMARY KEY(ID));
To allow the naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on
multiple columns, use the SQL syntax.
CREATE TABLE person ( id int NOT NULL, last_name varchar(255) NOT NULL, first_name
varchar(255), age int,
);
Note: In this example, there is only ONE PRIMARY KEY as PK_Person. And the VALUE of the primary key
is made up of two columns (id+ last_name).
Here we have assigned the primary key as “id” on the student table.
Allow the naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple
columns, use the SQL query:
To drop the PRIMARY KEY constraint from the table, use the SQL Query:
As we can see from the student table, the primary key has been dropped from the table.
A FOREIGN KEY is used to link two tables together. It is sometimes also called a referencing key. Foreign
Key is a combination of columns (can be single column) whose value matches a Primary Key in the
different tables. The relationship between two tables matches the Primary Key in one of the tables with a
Foreign Key in the second table. If the table contains a primary key defined on any field, then the user
should not have two records having the equal value of that field.
Let’s create two tables using the foreign key.
CUSTOMER table
Address varchar(25) ,
);
CREATE TABLE Orders (OrderID int NOT NULL, OrderNumber int NOT NULL, Id int,
Here the Id is the primary key for the customer table and foreign key for orders table.
To drop a FOREIGN KEY constraint from the table, use the SQL query:
The CHECK CONSTRAINTS is used to limit the range of value that can be placed in a column if the user
defines a CHECK constraint on a single column, it allows only specific values for the column. If the user
defines a CHECK constraint on a table, it can limit the values in particular columns based on values in
another column in the row.
SQL Query to creates a CHECK constraint on the column "Age" when the table "Persons" is created. The
CHECK constraint makes sure that the user can not have any person below 18 years: CREATE TABLE
Persons (
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
Here we have created the Persons table and given a check constraint on the Age column. If the Age<18,
then it will throw an error, as shown below.
For creating a CHECK constraint on multiple columns in the table, use the SQL syntax:
Create a CHECK constraint on the column "Age" when the table is already created, use the following SQL:
Defining CHECK constraint on multiple columns of a table, use the SQL query:
To drop a CHECK constraint from the table, use the following SQL:
Here we have dropped the CHK_PersonAge constraints by using the drop statement.
The DEFAULT constraint in SQL is used to provide a default value for a column of the table. The default
value will be added to every new record if no other value is mentioned.
The SQL query to sets a DEFAULT value for the "City" column when the "Persons" table is created
CREATE TABLE Persons (
ID int NOT NULL,
FirstName varchar(255),
Age int,
);
As we can see in the Persons table, the city name is written as Sandnes by Default.
To create a DEFAULT constraint on the column "City" when the table is already created, use the following
SQL:
To drop a DEFAULT constraint from the table, use the SQL query:
As we can see in the Persons table, the default value of the city has been removed.
CREATE INDEX statement in SQL is used to create indexes in tables. The indexes are used to retrieve
data from the database more quickly than others. The user can not see the indexes, and they are just
used to speed up queries /searches.
Note: Updating the table with indexes takes a lot of time than updating a table without indexes. It is
because the indexes also need an update. So, only create indexes on those columns that will be
frequently searched against.
Example:
Creates an index named "idex_lastname" on the "LastName" column in the "Persons" table: CREATE
INDEX idex_lastname on Persons (LastName)
If a user wants to create an index on a combination of columns, you can list the column names within the
parentheses, separated by commas:
It creates a unique index on a table and Duplicate values are not allowed.
Syntax:
Note: The query for creating indexes varies among different databases. Therefore, Check the query for
creating indexes in your database.
In SQL, the view is a virtual table based on the result-set of an SQL statement. A view holds rows and
columns, similar to a real table. The fields in a view are fields from one or more real tables in the
database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if
the data were coming from one single table.
FROM table_name
WHERE condition;
Note: A view always shows up-to-date data! The database engine recreates the data, using the view's
SQL statement, every time a user queries a view.
Create a view on the table customers. Here, the view would be used to have a customer name and age
from the customers table.
FROM customers;
The WITH CHECK OPTION in SQL is a CREATE VIEW statement option. The objective of the WITH
CHECK OPTION is to make sure that all UPDATE and INSERTs satisfy the condition(s) in the view
definition.
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following code block has an example of creating the same view CUSTOMERS_VIEW with the WITH
CHECK OPTION.
FROM customers
Rows of data can be deleted from a view. The same rules that apply to the UPDATE and INSERT
commands apply to the DELETE command.
Here we have deleted the row, which contains the age = 25.
DROPPING VIEWS
Where the user has a view, you need a method to drop the view if it is no longer needed. The query is
straightforward and is given below:
It’s similar to the other dropping option, as we have done yet for tables. As we can see, the view is not
available in the database after dropping the view.
Advance MySQL provides better understanding for Stored Procedure, View, Triggers, Events and Indexes.
In this chapter, we are going to understand all of the above terminology one by one in details with the
help of MySQL workbench.
The stored procedure is a prepared SQL query that you can save so that the query can be reused over
and over again. So, if the user has an SQL query that you write over and over again, keep it as a stored
procedure and execute it. Users can also pass parameters to a stored procedure so that the stored
procedure can act based on the parameter value that is given.
DELIMITER $$
FROM Table_name
END$$
DELIMITER
Here, the DELIMITER is not the part of Query, the first Delimiter is change the default delimiter to // and
the second delimiter is change the delimiter to the default. The Stored procedure is saved automatically
in the database while creation.
To execute the query in MySQL, use the MySQL workbench for better user-interface, and use inbuilt
databases to perform the advance MySQL queries.
Here we have create a procedure called Customer, and we have mentioned few column names in it. And
in last we have closed the procdure. If we want to know the output of the above query, then need to run
the procedure by clicking on the execution button on workbench display.
Execution of the Stored Procedure is very simple by using the CALL procedure_name, Execute the
below query to get the result of the defined stored procedure.
After calling the procedure, we are able to see the selected columns which are mentioned in the
procedure. The output is as follow:
Stored Procedue can have parameters,so while execution we can pass the argument and get the result.
We can use control flow (like: IF, LOOP, CASE, etc.)in the stored procedure to make dynamic queries and
also we can pass one stored procedure inside the other which will help to modulize the queries.
9.1.3. DROP THE STORED PROCEDURE
Drop procedure use to delete the stored procedure from the databases. The following query used to
delete the stored procedure for the Database:
DROP Stored_procedure_name
The below syntax used for conditionally drop of stored_procedure and first it check the procedure_name
& if it exist then drop the stored procedure from the database.
If the stored procedure is not available then it throw an error like mentioned below.
We can create a stored procedure with parameters. In Stored procedure the parameters are like IN, OUT
and INPUT. The parameters make the Stored Procedure more flexible and useful.
DEFINING A PARAMETERS
To define the parameter inside the stored procedure, run the below query:
IN Parameter
It is the default parameter in Stored Procedure and the calling program should pass an argument to
stored Procedure. The value of IN is protected that means even the IN value is changed inside the
stored procedure the original value will retained after end of the Stored Procedure.
Example for IN: Create a Stored Procedure that find all the active customers by the input parameter
as Active.
Output:
Out Parameter
The value of the Output Parameter can be changed inside the Store Procedure and pass the new value
while calling the Stored Procedure.
Example for OUT: write a stored procedure to print the square root of a number.
Output:
input_number : it takes input from user in interger format. out_number : it store the
output of the function.
To print the output, we have call the my_sqrt() and pass the arguments, the first argument is user input
and second is output, and to show the output use select @out_number.
INOUT
Example for INOUT : Here we are just counting the numbers between a region using INOUT stored
procedure.
Output:
It will print the query in sequence so if we call the stored procedure for many time,so it will count all the
sum and in last it will print the counter values.
In this unit we will learn about the variables, and also how to declare variables? How to use the
variables?. Basically a variable is a called as data object whose value can be change while execution of
Stored Procedure.
To declare the variable inside a stored procedure, use the below query:
Here,
DECLARE – It is a keyword and it is use to declare the variable. First write DECLARE keyword and then
variable_name.
Datatype(size) – it us use to define the variable datatype (like: IN, Varchar, or char)and size use to define
the length of the variable.
Default – it assign variable with default value option. If we declare the variable without specifying any
default values, then it’s values will be NULL.
Using MySQL Stored Procedure, we can declare more than one variable.
ASSIGNING VARIABLE
Once we declare the variable, now it is ready to use. To assign a value to the variable use SET
statement:
10. TRIGGERS
Trigger is a stored program that invoked automatically in response to an event such as insert, delete or
update that occurs in the table. Suppose, you defined a trigger and you insert a row inside the table, then
it will automatically invoked before or after the insertion of row.
Advantage of Triggers
CREATE TRIGGER statement is used to create the triggers. The syntax is following:
trigger_body;
Here,
• It will make the condition that the trigger invokes before or after any modification in
row.
Let’s understand the triggers by using an example. Now, we are going to create a table names as
Create another table and insert some rows into that table, now we are going to create one more as
“employee”. On the “employee” table, we are going to perform all the trigger operation on it and the
operations log will be stored in “EmployeeDetail”.
Let’s create a trigger using the before update operation on employee table. As we can see, the trigger is
created as name “before_on_employee_update”.
Now, let’s use the trigger and update the row values of employee,
Let’s check the table EmployeeDetail and check the action on it.
As we can see, the trigger is automatically invoked and inserted a new row inside the EmployeeDetail
table and the row is updated.
To delete the TRIGGER we use the DROP TRIGGER statement, and it will delete the trigger from the
database. The syntax is as follow:
Here,
• Firstly, it will check the triggers_name and if it exist then delete that particular trigger.
• To delete any trigger, the trigger_name should be written after the DROP TRIGGER.
OR
It will delete the trigger without checking their existence in the database.
The trigger before_on_employee_update has been deleted from the database.
The before insert trigger are automatically fired before an insert occurs on the table. The syntax for
before insert trigger as follow:
BEFORE INSERT
trigger_body;
Now, let’s create another table as totalamountstatus to store the summary of the triggers.
Lets create a before insert trigger to get the totalamount in the totalamountstatus table before a new
work center is inserted into the totalamount table.
The trigger is created successfully for updating before insert into the totalamount table. Let’s test the
trigger by inserting the value in it.
We have successfully insderted the value in the totalamount table. But the value is invoked in the
totalamountstatus table. Let’s call the totalamountstatus table to check the total amount.
The trigger is invoked and inserted a new row into the totalamountstatus. If we insert another value that
will automatically added into the present amount and return the totalamount.
The after insert trigger are automatically fired after an insert occurs on the table. The syntax for after
insert trigger as follow:
AFTER INSERT
Let’s understand the after insert trigger using an example; Create a table named as members.
Create another table as remembers.
Now, create a after insert trigger as after_members_insert and that trigger insert into reminders table
if the birth_date of any person is null.
We have inserted the two rows inside the members table and the members table is shown below;
As we can see here, the two rows are inserted but the birthdate of Hemant is null and as we mentioned
the condition in trigger, it will invoke a message if birth date is as null. Let’s check the reminders table.
As we have made the condition inside the trigger, it has invoked automatically when the birth day found
as null. And the message showing as Hello Hemant, update your date_of_birth.
The BEFORE UPDATE TRIGGER is invoked automatically before an update event occurs on the table
which associated with the trigger.
BEFORE UPDATE
trigger_body
Creating the BEFORE UPDATE TRIGGER, and assigning the error message as the new quantity cannot be
greater than 3-times of previous.
The trigger will automatically invoke and fire before updating any values in any row.
We have updated a value of quantity where the id = 1 but it will not satisfied the condition so it will not
give the error message, see the table;
Let’s update the quantity as some other value which are 3-times greater than the quantity150.
As we have increased the quantity as 3-times higher than previous, it’s showing message as “the new
quantity cannot be 3times greater than the current quantity”.
The ALTER UPDATE TRIGGER invoke automatically after updating the events in the associated table. The
syntax for AFTER update triggers as follow:
CREATE TRIGGER trigger_name
AFTER UPDATE
trigger_body
Let’s understand the after update trigger with an example; we are going to use the first table as sales
table and the second table as sales_changes. So let’s create the second table sales_changes.
The after_update_sales trigger automatically invoked after updating any row of the sales table. Updating
the quantity column in sales table, where id = 1
The BEFORE DELETE TRIGGER are fired automatically before a delete event occurs in table. The syntax
for before delete trigger as follow: TRIGGER trigger_name
BEFORE DELETE
trigger_body
Let’s create a table as salary;
Now let’s create a stored procedure, which contains the before delete triggers. Before delete trigger store
the deleted value into the deleted_salary table.
Now, check the deleted_Salary table to check whether the data is stored or not.
As we can see here, the BEFORE DELETE TRIGGER is automatically invoked the row before event occurs
on the salary table.
AFTER DELETE
trigger_body;
Create a table salary and insert few rows into the table;
Create another table to store the deleted row into that,we are creating another table as deleted_salary;
Now,let’s store the value of total into the deleted_salary table by using the below command. Here,we are
using the SUM() function to add the salaries from the salary table and store it into the deleted_salary as
total.
We are creating a trigger which update the total salary into the deleted_Salary table after deleting from
the salary table.
Let’s delete a row where the employee_no = 1022 inside the salary table;
Check the deleted_Salary;
As we can the value of total is decresed by 50000, because it is substracted from the total amount.
Week 2: Relational Database Conception Principles
a. Functional Dependence
i. Definition of Functional Dependence
In the context of database theory, specifically in the normalization process, functional dependence is a
R and attributes A
constraint between two sets of attributes in a relation from a database. Given a relation
and B, we say that A functionally determines B, represented as A→B, if for each pair of tuples in R that
have the same value for A, they also have the same value for B.
In simpler terms, functional dependence means that the value of one attribute (or set of attributes) in a
relation uniquely determines the value of another attribute (or set of attributes).
Examples:
StudentID → FirstName because a given student ID uniquely determines the first name.
In a product inventory database with attributes ProductID, ProductName, Price:
Here's an example of how you could define a table in MySQL where StudentID functionally determines
FirstName, LastName, and GPA.
FirstName VARCHAR(50),
LastName VARCHAR(50),
GPA FLOAT
);
In this example, StudentID is the primary key, which means it uniquely identifies each record in the table.
Therefore, it functionally determines all other attributes (FirstName, LastName, GPA) in the table.
Functional dependencies can be categorized into different types based on their characteristics. To enforce
functional dependencies in MySQL, you can use constraints like PRIMARY KEY, UNIQUE, and FOREIGN
KEY. The primary types of functional dependencies are:
A functional dependency A→B is a full functional dependency if removal of any attribute A′ from A means that
the dependency does not hold anymore. In simpler terms, B is fully functionally dependent on A and not on
any subset of A.
CourseID INT,
Grade CHAR(1),
);
Here, Grade is fully functionally dependent on the composite key {StudentID, CourseID}.
A functional dependency A→B is a partial functional dependency if some attribute B can be functionally
determined by a proper subset of A.
To avoid partial functional dependencies, you can decompose the table into two separate tables like below:
-- Student Information
StudentName VARCHAR(50)
);
-- Course Grades
StudentID INT,
CourseID INT,
Grade CHAR(1),
);
A functional dependency A→B is a transitive functional dependency if there exists an attribute set C where
A→C and C→B, but C is not a subset of A and B is not functionally dependent on A directly.
Example:In a table with attributes {EmployeeID,Department,DepartmentHead}, if EmployeeID→Department
and Department→DepartmentHead, then EmployeeID→DepartmentHead is a transitive dependency.
To resolve transitive dependencies, you can also break the table into smaller tables.
-- Department Information
CREATE TABLE Departments (
DepartmentHead VARCHAR(50)
);
-- Employee Information
DepartmentID INT,
);
iii. Closure of Attributes
In the context of database theory and functional dependency, the closure of a set of attributes A, denoted as
A+, is the set of attributes that can be functionally determined by A using a given set of functional
dependencies.
The closure helps to infer all the attributes that can be determined by a given set of attributes, and it is crucial
for various processes like database normalization, finding candidate keys, and more.
Let's say you have a table with attributes StudentID, CourseID, and Grade. To see if StudentID and
CourseID together can functionally determine Grade, you could use:
FROM Grades
If the query returns no rows, it indicates that the set {StudentID, CourseID} functionally determines
Grade, confirming that the closure {StudentID,CourseID}+ includes Grade
iv. Functional Dependency Diagrams
Consider a university database schema that has a table named Enrollments containing the following
attributes:
{CourseID}→ProfessorID
{StudentID,Semester}→CourseID
A student's ID and the semester uniquely determine the courses they are taking.
i. {StudentID,CourseID,Semester}→Grade:
ii. {CourseID}→ProfessorID:
a. CourseID alone determines the ProfessorID. So, there's a straight arrow from
CourseID to ProfessorID.
iii. {StudentID,Semester}→CourseID:
a. StudentID and Semester together determine the CourseID, which is why there's
an arrow from these attributes to CourseID.
By understanding the functional dependencies and visualizing them through the diagram, you can get insights
into:
Database Normalization is a technique used to design a relational database schema in such a way that it
reduces redundancy and improves data integrity. The process involves dividing larger tables into smaller, more
manageable tables and linking them with relationships. Each step of normalization is called a "Normal Form,"
and there are several Normal Forms, each with a specific set of rules.
ii. Purpose of Normalization
Elimination of Redundant Data: In an unnormalized database, the same information could be duplicated in
multiple places. Normalization helps in reducing this redundancy by ensuring that each piece of information is
stored only once. This not only saves storage space but also makes the database more efficient.
Example
Here, the CustomerName and Address for Alice is repeated. If Alice were to change her address, we would
have to update multiple rows, which is inefficient and error-prone.
Data Integrity: Normalization constraints, like primary and foreign keys, help in maintaining data integrity.
These constraints ensure that there can be no duplicate records, and that all references from one table to
another are valid.
Simplify Queries: Normalized databases are generally easier to query. Since each piece of information is
stored only once, there is less confusion and fewer errors when querying data.
Facilitate Updates: In a normalized database, updating a piece of information doesn't require changes to be
made in multiple places. This makes updates easier to implement and less error-prone.
Increase Scalability: Normalized databases are more scalable. As the amount of data increases, a well-
normalized database can adapt more easily without requiring major changes in the logical structure.
Optimize Indexes: Normalization often results in smaller table sizes, which allows for more efficient use of
indexes and faster query performance.
Improved Security: In a well-normalized database, it may be easier to implement security features. For
example, you could grant permissions on a table-by-table basis, thus providing more granular control over who
can access or modify what data.
In the context of database normalization, a functional dependency is a constraint between two sets of
attributes in a relation (table) from a database. Given a relation R, a set of attributes X in R is said to
functionally determine another set of attributes Y, written as X→Y, if and only if each X value is associated
with precisely one Y value.
For example, in a table of students with columns StudentID and StudentName, the StudentID functionally
determines the StudentName because each StudentID will have exactly one StudentName associated
with it. In mathematical terms, this is expressed as StudentID→StudentName.
Functional dependency plays a critical role in the process of normalization. The different normal forms (1NF,
2NF, 3NF, etc.) are defined based on satisfying certain functional dependency conditions.
Let's consider a simple example to illustrate these concepts. Suppose we have a table named
StudentCourses that contains the following columns:
StudentID INT,
StudentName VARCHAR(50),
CourseID INT,
CourseName VARCHAR(50),
);
Issues:
• Redundancy: The StudentName and CourseName are repeated for each combination of
StudentID and CourseID.
• Update Anomalies: If Alice changes her name, we have to update multiple rows.
Normalization:
1NF: The table is already in 1NF as it has a primary key (StudentID, CourseID) and atomic attributes.
StudentName VARCHAR(50)
);
CourseName VARCHAR(50)
);
3NF: Now, let's create a table for the relationship between students and courses.
StudentID INT,
CourseID INT,
);
The database is now in 3NF. We've removed redundancy and minimized update anomalies, all thanks to
functional dependencies guiding our normalization process.
iv. Anomalies in Unnormalized Data
Anomalies in an unnormalized MySQL database occur due to poor schema design. These anomalies can result
in incorrect data, inconsistencies, and inefficiencies. Here are the main types of anomalies:
Insertion Anomaly
Problems occur when you try to insert data into the database.
Example
1 Alice C1 Math
1 Alice C2 Science
2 Bob C1 Math
Inserting a new course for a new student would require knowledge of both the student and the course. If the
course is not yet known, the record can't be inserted.
Update Anomaly
Example
Continuing with the StudentsCourses table, suppose you need to update Alice's name to "Alicia". You'll have
to update multiple rows, which is inefficient and error-prone.
1 Alicia C1 Math
1 Alicia C2 Science
2 Bob C1 Math
Deletion Anomaly
Example
If Alice drops all her courses and you delete all rows concerning Alice, you lose all information about her, even
the fact that she was a student.
2 Bob C1 Math
Redundancy
Example
The StudentName and CourseName columns are redundant because the same names are repeated for
each new course or student.
Inconsistency
Example
If you need to rename a course from "Math" to "Mathematics", you'll have to update multiple rows. If you miss
any, the database will be inconsistent.
c. Normal Forms
Database normalization is a process used to organize a database into tables and columns. The main idea is
that a table should be about a specific topic and only supporting topics included. Let's explore each normal
form along with its requirements and examples:
Requirements:
MySQL Example:
Student Courses
Alice Math, English
Bob Science, Math
This table is not in 1NF because the Courses column contains multiple values. To bring this to 1NF:
Student Course
Alice Math
Alice English
Bob Science
Bob Math
Requirements:
i. Table is in 1NF.
ii. No partial dependencies of any column on the primary key.
MySQL Example:
1. Student table:
StudentID StudentName
1 Alice
2 Bob
2. Scores table:
StudentID Course Score
1 Math 90
1 English 85
2 Science 80
Requirements:
i. Table is in 2NF.
ii. No transitive dependencies of any column on the primary key.
MySQL Example:
Consider a table:
1. Courses table:
StudentID Course
1 Math
2 Eng
Requirements:
1. Table is in 3NF.
MySQL Example:
Consider a table:
1. Instructors table:
CourseID Instructor
101 Mr. Smith
102 Mr. Smith
2. StudentCourses table:
StudentID CourseID
1 101
2 102
Requirements:
i. Table is in BCNF.
ii. No multi-valued dependencies.
MySQL Example:
Consider a table:
1. StudentCourses:
Student Course
Alice Math
Bob Eng
2. StudentHobbies:
Student Hobby
Alice Painting
Alice Reading
Bob Swimming
Deals with cases where information can be reconstructed from smaller pieces of information that can be
maintained with less redundancy. A table is said to be in 5NF if and only if every join dependency in it is
implied by the candidate keys.
Example:
For a practical example, consider a scenario where we have a table containing information about courses,
instructors, and books. An instructor can teach multiple courses, and for each course, they can choose
different books.
Decomposed Tables:
1. Instructor_Course:
Instructor Course
John Math
John CS
Jane Math
Jane CS
2. Course_Book:
Course Book
Math Mathematics
Math Algebra
CS Programming
CS Algorithms
Now, these two tables are in 5NF. The original table can be reconstructed by performing a natural join on
these two tables without loss of information.
d. Integrity Constraints
Integrity constraints are rules applied to database tables to ensure the reliability and accuracy of the data
stored within them. They enforce the correctness of the data and maintain the consistency of the database.
Static integrity constraints are constraints that must be satisfied at all times, and they are checked whenever
any changes are made to the database.
• Definition: A primary key uniquely identifies each record in a table and must contain unique values. A
table can have only one primary key.
• Definition: A unique constraint ensures that all values in a column are distinct.
• Example: In a users table, email could have a unique constraint.
CREATE TABLE users (
user_id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE,
password VARCHAR(255)
);
c. Check Constraints
• Definition: A check constraint verifies that the values in a column satisfy a specific condition.
• Example: In an employees table, salary could have a check constraint to ensure it is always above
0.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2) CHECK (salary > 0)
);
II. Dynamic Integrity Constraints
Dynamic integrity constraints are constraints that are checked at the point of executing a statement, such as a
modification or a deletion.
• Definition: A foreign key is a column that creates a relationship between two tables. It refers to the
primary key in another table and ensures referential integrity.
• Example: In an orders table, customer_id could be a foreign key referring to customers table.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
b. Triggers for Data Validation
• Definition: Triggers are stored procedures that are automatically executed or fired when a specific
event occurs.
• Example: A trigger can be created to validate data before inserting it into a products table.
CREATE TRIGGER validate_product_price
BEFORE INSERT ON products
FOR EACH ROW
BEGIN
IF NEW.price < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Price cannot be negative';
END IF;
END;
c. Assertions for Data Validation
• Definition: Assertions are conditions or Boolean expressions which the database always evaluates as
true for the transactions to be committed.
• Example: An assertion can be created to ensure that the total salary of employees does not exceed a
certain limit.
CREATE ASSERTION salary_check
• Definition: Enforcing integrity constraints is the process of ensuring that all the defined constraints
are adhered to.
• Example: A database management system (DBMS) automatically enforces integrity constraints like
primary key, unique, check, and foreign key constraints when data is inserted, updated, or deleted
from the database.
Week 3: Database administration
a. Physical implementation of the data
The physical implementation of data refers to how data is stored, managed, and retrieved from disk by the
MySQL Server. This involves understanding the storage engines, table structures, indexing, and partitioning.
Storage Engines
Storage engines are the underlying software components that MySQL uses to create, read, update, and delete
data. Different storage engines offer different features, performance characteristics, and capabilities.
• InnoDB is a transaction-safe storage engine and supports foreign keys, row-level locking, and is
generally used for complex, high-volume transactional applications.
• MyISAM is non-transactional and is generally used for read-heavy, simple applications due to its
speed.
• Table Structures: The storage engine chosen affects how table structures are stored and managed
on the disk.
• Indexing: Different storage engines implement indexing in various ways, affecting query
performance.
• Partitioning: Some storage engines support table partitioning, allowing for more efficient data
management and retrieval.
data VARCHAR(255)
) ENGINE=InnoDB;
Table Structures
Table structures define how data is organized within tables. They are defined by the columns, data types, and
constraints applied to the tables.
• Indexing: The table structure determines which columns can be indexed and how effectively indexes
can be used.
• Partitioning: The design of table structures can impact the efficiency of partitioning strategies and
data retrieval.
Indexing
Indexes are data structures that improve the speed of data retrieval operations on a database table. They
work similarly to an index in a book.
• Secondary Index: Non-unique index that improves the performance of certain queries.
Partitioning
Partitioning is the database process where very large tables are divided into smaller, more manageable pieces,
yet still being treated as a single table. It can improve performance and assist in managing large tables.
sale_date DATE,
amount DECIMAL(10, 2)
);
Consider the large table storing sales data, structured with columns for date, sales_id, and amount. If you
choose InnoDB as the storage engine, you can leverage its support for partitioning to divide the table by date
ranges, enhancing data retrieval performance for range-based queries. Creating appropriate indexes on
sales_id and date will further optimize query performance, allowing rapid lookups within specific partitions.
MySQL organizes its data into different files and directories, each serving a specific purpose. Here’s a typical
organization of MySQL’s database files:
Data Directory
• Table Files: Every table is represented by one or more files within its database directory.
• .ibd files store table data and indexes for InnoDB tables.
• .MYD and .MYI files are for MyISAM tables: .MYD stores data and .MYI stores indexes.
Log Files
• Error Log: Contains information about errors that occur while the server is running.
• General Query Log: Provides a general record of what MySQL is doing, logging established client
connections and statements received from clients.
• Binary Log: Contains information about data modifications made by each transaction.
Temporary Files
MySQL may create temporary files when performing tasks like sorting large result sets
Socket File
MySQL Indexes
Indexes are used to quickly locate a row in a table without scanning the entire table. The two main types of
indexes in MySQL are:
Primary Index
Secondary Index
Full-text Index
• Used for full-text searches in textual column types like CHAR, VARCHAR, or TEXT.
name VARCHAR(50)
);
Creating a Database
CREATE DATABASE company;
Creating a Table
USE company;
name VARCHAR(50)
);
Adding an Index
-- Adding a Secondary Index to an Existing Table
Dropping an Index
-- Dropping an Index
Optimizing Indexes
Use the OPTIMIZE TABLE command to reclaim unused space and to defragment the data file:
Controlling concurrent access is crucial in MySQL database administration to ensure the integrity and
consistency of data. This involves managing how multiple transactions are processed simultaneously to avoid
conflicts and data corruption.
Transactions
Transactions are sequences of one or more SQL operations executed as a single unit. They follow the ACID
properties:
• Atomicity: The transaction is treated as a single unit, either fully completed or not executed at all.
• Consistency: The database remains in a consistent state before and after the transaction.
Locks
Locks are mechanisms that prevent multiple transactions from accessing the same data simultaneously,
avoiding conflicts and ensuring data integrity
• Shared Locks (READ LOCKS): Allow multiple transactions to read a resource but prevent any from
writing to it.
• Exclusive Locks (WRITE LOCKS): Prevent any other transaction from accessing the resource, either
for reading or writing.
Deadlocks
A deadlock occurs when two or more transactions are waiting for each other to release a lock, causing the
system to stall.
Handling Deadlocks: MySQL automatically detects and resolves deadlocks by choosing one transaction as
the 'victim' and rolling back its operations.
Isolation levels define the degree to which the operations in one transaction are isolated from those in other
concurrent transactions.
• READ UNCOMMITTED: Allows reading uncommitted changes made by other transactions. It has the
lowest level of isolation.
• READ COMMITTED: Only allows reading committed changes, but the data can change during the
transaction.
• REPEATABLE READ: Ensures that if a value is read, the same value will be seen throughout the
transaction.
• Optimistic Concurrency Control: Assumes conflicts are rare. Transactions are allowed to proceed
without locking, but conflicts are checked at commit time.
• Pessimistic Concurrency Control: Assumes conflicts are likely. Resources are locked as soon as
they are accessed.
Let's consider a scenario where we have a table named accounts with columns account_id and
balance. Two users, A and B, try to update the balance of the same account concurrently.
Here, User A will lock the row for update, preventing User B from modifying it until User A commits or rolls
back the transaction.
-- User A
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1 FOR UPDATE;
COMMIT;
Here, both users can read the value and attempt to update it, but only one will succeed based on a
versioning mechanism.
-- User A
START TRANSACTION;
UPDATE accounts SET balance = 400 WHERE account_id = 1 AND balance = 500;
-- If User B has changed the balance in the meantime, the update will fail for User A.
COMMIT;
d. Breakdown resistance
Database administration is crucial for ensuring the availability, performance, and security of MySQL databases.
One essential aspect of database administration is developing strategies to make the database resistant to
breakdowns, which typically involve backup and recovery, optimizing performance, and managing user
privileges.
MySQL Dump
MySQL Dump is a utility that performs logical backups by producing a set of SQL statements that can
recreate the original database.
Example:
Binary Backups
Binary backups consist of copying the actual database files. This method is usually faster than logical
backups.
Example :
cp /var/lib/mysql/[database_name] /path/to/backup/
Performance Optimization
Indexing
Indexes are used to quickly locate a row in a table without scanning the entire table.
Example:
Query Optimization
Optimizing queries is crucial to improving database performance. Use the EXPLAIN statement to
understand how MySQL processes a query.
Example:
Creating Users
Creating users with the least required privileges is crucial for securing the database.
Example:
Granting Privileges
After creating a user, assign the necessary privileges using the GRANT statement.
Example:
Error Log
MySQL error log contains information about errors that occur while the server is running. Regularly
monitoring the error log can help in identifying and resolving issues promptly.
Example: To view the error log, locate the log file and use a text viewer:
cat /var/log/mysql/error.log
The general query log provides information on client connections and SQL queries.
Example: To enable the general query log, add the following lines to the MySQL configuration file (my.cnf
or my.ini):
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/mysql.log
Master-Slave Replication
Master-slave replication allows the slave to replicate data changes made by the master, providing data
redundancy and read scalability.
[mysqld]
server-id=1
log-bin=mysql-bin
[mysqld]
server-id=2
MASTER_HOST='master_host',
MASTER_USER='slave_user',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=107;
START SLAVE;
Securing and protecting data is crucial. We will discuss various topics, including user privilege management,
secure connections, and data encryption.
User privilege management is essential for securing MySQL databases. MySQL employs a robust privilege
system that authenticates a user connecting from a host and associates the user with access privileges on a
database.
Replace username with the desired username and password with a secure password.
Secure Connections
To secure connections to the MySQL server, use Secure Sockets Layer/Transport Layer Security (SSL/TLS)
encryption. MySQL supports encrypted connections based on the OpenSSL library.
To enable SSL on MySQL Server running on Windows, you need to specify the server certificates in the my.ini
configuration file:
[mysqld]
ssl-ca=ca-cert.pem
ssl-cert=server-cert.pem
ssl-key=server-key.pem
Data Encryption
Data encryption is a crucial part of data protection. MySQL provides multiple ways to encrypt data, such as
Transparent Data Encryption (TDE) and column-level encryption.
TDE encrypts MySQL tablespace data files. To enable TDE, use the ALTER TABLE statement:
To add a rule in Windows Firewall allowing MySQL connections, use the following PowerShell command:
New-NetFirewallRule -DisplayName "MySQL" -Direction Inbound -Protocol TCP -LocalPort 3306 -Action
Allow
Regular backups are essential for data protection. Use mysqldump to create backups of your databases.
To create a backup of your MySQL database on Windows, run the following command in Command Prompt:
To restore a backup:
Administering MySQL databases also involves managing database parameters, starting and stopping the
server, and performing database save and restoration operations.
Parameter Setting
MySQL parameters are typically set in the my.ini configuration file, located in the MySQL installation directory.
This file allows you to configure various settings related to the MySQL server, such as buffer sizes, log files,
and port numbers.
To change a parameter, open my.ini with a text editor and modify the desired setting. For example, to
change the maximum allowed packet size:
[mysqld]
max_allowed_packet=64M
After making changes, save the file and restart the MySQL server for the changes to take effect.
To start the MySQL server on Windows, open Command Prompt and navigate to the MySQL bin directory, then
run:
mysqld
To stop the MySQL server, open another Command Prompt window and run:
Replace username with your MySQL username and database_name with the name of the database you
want to back up.
Monitoring MySQL performance and logging are crucial for maintaining database health and troubleshooting
issues.
[mysqld]
general_log=1
general_log_file="C:/path/to/logfile.log"
MySQL’s Performance Schema provides detailed information about server execution. Enable it by adding the
following line to my.ini:
performance_schema=ON
In Windows, MySQL can run as a service, which can start automatically with Windows startup.
To install MySQL as a Windows service, open Command Prompt as an Administrator, navigate to the MySQL
bin directory, and run:
mysqld –install
Distributed databases and distributed processing in MySQL involve splitting data across multiple servers to
improve performance, availability, and fault tolerance.
MySQL NDB Cluster is a high-availability, high-redundancy version of MySQL, designed for distributed
computing environments. It combines the MySQL Server with the NDB storage engine and enables data to be
distributed across multiple nodes for higher availability.
Download the MySQL NDB Cluster package from the official MySQL website and follow the installation
instructions for Windows.
NoOfReplicas=2
[ndb_mgmd]
hostname=management_node_hostname
datadir=C:\path\to\management\node\data\directory
[ndbd]
hostname=data_node1_hostname
datadir=C:\path\to\data\node1\data\directory
[ndbd]
hostname=data_node2_hostname
datadir=C:\path\to\data\node2\data\directory
[mysqld]
hostname=sql_node_hostname
ndbd –initial
Mysqld
MySQL Fabric
MySQL Fabric is a framework for managing a collection of MySQL servers. It provides mechanisms to execute
tasks on MySQL servers and organize them into groups, facilitating the distribution of databases.
To install MySQL Fabric, use the MySQL Installer for Windows and select the MySQL Utilities package during
installation.
uuid = fabric_uuid
address = fabric_address:port
[storage]
user = fabric_user
database = fabric_database
Distributed Processing
Distributed processing involves splitting tasks across multiple servers to improve performance and reliability.
MySQL NDB Cluster and MySQL Fabric enable distributed processing by allowing read and write operations to
be distributed across multiple nodes.
When querying distributed databases in MySQL NDB Cluster or MySQL Fabric, the syntax is the same as
querying a regular MySQL database:
The underlying distributed processing mechanisms ensure that the query is executed across all relevant nodes
and the results are aggregated and returned to the client.
h. Auditing, optimization
Auditing and optimization are integral parts of MySQL Database administration. Auditing helps in maintaining
the security and integrity of the database by tracking user activities and changes made to the database.
Optimization ensures that the database performs efficiently, minimizing the resource usage and response
times.
Auditing in MySQL
Auditing in MySQL can be achieved by using the MySQL Enterprise Audit plugin, which logs the activities
performed by users and the server.
To enable auditing, add the following lines to the my.ini file located in the MySQL installation directory:
[mysqld]
audit_log_file = C:/path/to/audit.log
audit_log_policy = ALL
The audit log file specified in the my.ini file will contain all the audit entries. Open the audit log file with any
text editor to view the entries.
Optimization in MySQL
Optimization in MySQL involves optimizing the database schema, queries, and the MySQL server configuration.
Design the database schema efficiently, use appropriate data types, and index the columns that are frequently
searched or sorted. To add an index to a column:
Write efficient SQL queries and use the EXPLAIN statement to understand how MySQL executes a query:
[mysqld]
innodb_buffer_pool_size = 1G
Use performance monitoring tools and analyze the performance data to identify bottlenecks and optimize the
database accordingly.
Enable the Performance Schema to monitor the performance of MySQL server by adding the following line to
the my.ini file:
performance_schema = ON
[mysqld]
slow_query_log = 1
slow_query_log_file = C:/path/to/slow-query.log
long_query_time = 2
MySQL Workbench provides various tools for monitoring and optimizing MySQL server performance. Connect
to the server using MySQL Workbench and use the ‘Performance Reports’ and ‘Performance Schema Reports’
options to analyze the performance.
Regularly maintain and repair the databases to ensure optimal performance and reliability.