DBMS_Notes3
DBMS_Notes3
DBMS – MYSQL
Constraints
• MySQL CONSTRAINT is used to define rules to allow or restrict what values can be stored in
columns. The purpose of inducing constraints is to enforce the integrity of a database.
• MySQL CONSTRAINTS are used to limit the type of data that can be inserted into a table.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
NOT NULL - allows to specify that a column can not contain any NULL value.
DEFAULT - In a MySQL table, each column must contain a value ( including a NULL). While inserting data
into a table, if no value is supplied to a column, then the column gets the value set as DEFAULT.
It is important to make regular backups of all data in case of loss. Database recovery is the process of
restoring the database to original state as it was before database failure occurs.
mysqldump is an effective tool to backup MySQL database. It creates a *.sql file.
# mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql
When you are restoring the dumpfilename.sql on a remote database, make sure to create the database
before you can perform the restore.
# mysql -u root -p[root_password] [database_name] < dumpfilename.sql
SELECT statement
The SQL SELECT command is used to fetch data from the MySQL database. The SELECT statement
controls which columns and rows that you want to see. Syntax is
1 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND operator displays a record if all the conditions separated by AND are TRUE.
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a
value to this field. Then, the field will be saved with a NULL value. To test for NULL values we will have to
use the IS NULL and IS NOT NULL operators
The GROUP BY statement groups rows that have the same values into summary rows, like "find the
number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to
group the result
The HAVING clause is used in the SELECT statement to specify filter conditions for a group of rows or
aggregates. The HAVING clause is often used with the GROUP BY clause to filter groups based on a
2 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
specified condition. It is similar to the WHERE clause. The HAVING clause was added to SQL because the
WHERE keyword can not be used with aggregate functions.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
WHERE CustomerName LIKE 'a%' - Finds any values that start with "a"
WHERE CustomerName LIKE '%a' - Finds any values that end with "a"
WHERE CustomerName LIKE '_r%' - Finds any values that have "r" in the second position
Aggregate Functions
By definition, an aggregate function performs a calculation on a set of values and returns a single
value. MySQL provides many aggregate functions that include AVG , COUNT , SUM , MIN , MAX , etc.
An aggregate function ignores NULL values when it performs calculation except for the COUNT function.
• The COUNT() function returns the number of rows that matches a specified criteria.
SELECT COUNT(ProductID) FROM Products;
• The AVG() function returns the average value of a numeric column.
• The SUM() function returns the total sum of a numeric column.
• The MIN() function returns the smallest value of the selected column.
• The MAX() function returns the largest value of the selected column.
Date Functions
3 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
SELECT CURDATE();
CURTIME(): Returns the current time. Example:
SELECT CURTIME();
DATE_ADD() : Adds a specified time interval to a date
DATE_ADD(date, INTERVAL expr type);
SELECT Name, DATE_ADD(BirthTime, INTERVAL 1 YEAR) from test;
Join
A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.
4 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the
right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from
the left table
SubQueries
In MySQL, a subquery is a query within a query. You can create subqueries within your SQL statements.
A sub query is a select query that is contained inside another query. The inner select query is usually
used to determine the results of the outer select query.
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
Types of Subqueries
1) A scalar subquery is a subquery that returns exactly one column value from one row.
2) An independent subquery is the subquery that return at most one value. SQL then compares the
value that results from the subquery with the value on the other side of the comparison
operator.
SELECT employee_id,name,salary
FROM employees WHERE salary >
(SELECT AVG(SALARY) FROM employees);
3) Multiple row subquery is the one that returns more than one row
4) A correlated subquery is a subquery that contains a reference to a table (in the parent query) that
also appears in the inner query. A correlated subquery is evaluated once for each row in outer
query
5 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.
1. Privilege abuse: When database users are provided with privileges that exceeds their day-to-day job
requirement, these privileges may be misused intentionally or unintentionally.
2. Operating System vulnerabilities: Vulnerabilities in underlying operating systems like Windows, UNIX,
Linux, etc., and the services that are related to the databases could lead to unauthorized access.
3.Weak authentication: Weak authentication models allow attackers to employ strategies such as to
obtain database login credentials and assume the identity of legitimate database users.
5. Unavailability of data : authorized users must be able to access data at any time
Security Mechanisms
• A DBMS system always has a separate system for security which is responsible for protecting
database against accidental or intentional loss, destruction or misuse.
• Security Mechanisms:
6 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
o Authenticate the User:This may be done with help of concept of views in Relational
databases.
• Granting of privileges:
A Database Administrator is a person or a group of person who are responsible for managing all the
activities related to database system. This job requires a high level of expertise.
7 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
6. Database design
The logical design of the database is designed by the DBA. Also a DBA is responsible for physical design,
external model design, and integrity control.
8. Decides validation checks on data
9. Monitoring performance
10. Provides help and support to user
Views
By virtual, we mean, the tables do not store any data of their own but display data stored in other tables
referenced in the SELECT statement.
Views improve security of the database by showing only intended data to authorized users. They hide
sensitive data.
Views make life easy as you do not have write complex queries time and again.
It's possible to use INSERT, UPDATE and DELETE on a VIEW. These operations will change the underlying
tables of the VIEW.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more
real tables in the database.
DCL Commands
DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE which mainly deals
with the rights, permissions and other controls of the database system.
DCL is about security. DCL is used to control the database transaction.DCL statement allow you to
control who has access to specific object in your database.
Creating and Dropping Users
8 Bindy Wilson
DATABASE SYSTEMS FYCS Sem I Unit 3
The CREATE USER statement is used to create a database account that allows the user to log into the
MySQL database.
CREATE USER user_account IDENTIFIED BY password;
1. user_account: It is the name that the user wants to give to the database account.The
user_account should be in the format ‘username’@’hostname’
2. password:It is the password used to assign to the user_account.The password is specified in the
IDENTIFIED BY clause.
The “Show Grants” statement is used to view the permissions of a user account.
The DROP USER statement in MySQL can be used to remove a user account along with its privileges
from the MySQL completely.But before using the drop user statement, the privileges of the user should
be revoked.
Privileges
A system privilege is the right to perform a particular action, or to perform an action on any schema
objects of a particular type.
An authorized user may pass on this authorization to other users. This process is called as granting of
privileges
The Revoke statement is used to revoke some or all of the privileges which have been granted to a user
in the past.
9 Bindy Wilson