0% found this document useful (0 votes)
6 views38 pages

ET Unit 03

The document provides an overview of Relational Query Language, detailing its types: Procedural and Non-Procedural languages, along with SQL as a structured query language for relational databases. It explains the creation and manipulation of databases and tables, including querying multiple tables, establishing relationships, and managing data through commands like INSERT, DELETE, and ALTER TABLE. Additionally, it covers integrity constraints essential for maintaining data quality within relational databases.

Uploaded by

Aasha Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views38 pages

ET Unit 03

The document provides an overview of Relational Query Language, detailing its types: Procedural and Non-Procedural languages, along with SQL as a structured query language for relational databases. It explains the creation and manipulation of databases and tables, including querying multiple tables, establishing relationships, and managing data through commands like INSERT, DELETE, and ALTER TABLE. Additionally, it covers integrity constraints essential for maintaining data quality within relational databases.

Uploaded by

Aasha Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

UNIT 2

Relational Query Language


Relational Database systems are expected to be equipped with a query
language that assists users to query the database. Relational Query
Language is used by the user to communicate with the database user
requests for the information from the database. Relational algebra
breaks the user requests and instructs the DBMS to execute the requests.
It is the language by which the user communicates with the database.

Types of Relational Query Language

There are two types of relational query language:

 Procedural Query Language


 Non-Procedural Language

Procedural Query Language


In Procedural Language, the user instructs the system to perform a
series of operations on the database to produce the desired results. Users
tell what data to be retrieved from the database and how to retrieve it.
Procedural Query Language performs a set of queries instructing the
DBMS to perform various transactions in sequence to meet user requests
Non-Procedural Language
In Non Procedural Language user outlines the desired information
without giving a specific procedure or without telling the steps by step
process for attaining the information. It only gives a single Query on one
or more tables to get .The user tells what is to be retrieved from the
database but does not tell how to accomplish it.
For Example: get the name and the contact number of the student with a
Particular ID will have a single query on STUDENT table.
SQL Query Language
Structured query language (SQL) is a programming language for
storing and processing information in a relational database. A relational
database stores information in tabular form, with rows and columns
representing different data attributes and the various relationships
between the data values.
SQL is case insensitive. But it is a recommended practice to use
keywords (like SELECT, UPDATE, CREATE, etc.) in capital letters

1
UNIT 2
and use user-defined things (like table name, column name, etc.) in
small letters.
Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:

 An RDBMS database program (i.e. MS Access, SQL Server,


MySQL)
 To use a server-side scripting language, like PHP or ASP
 To use SQL to get the data you want
 To use HTML / CSS to style the page

RDBMS
RDBMS stands for Relational Database Management System.

RDBMS is the basis for SQL, and for all modern database systems
such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access.

The data in RDBMS is stored in database objects called tables. A table


is a collection of related data entries and it consists of columns and rows.

Look at the "Customers" table:

Example

SELECT * FROM Customers;

Every table is broken up into smaller entities called fields. The fields in
the Customers table consist of CustomerID, CustomerName,
ContactName, Address, City, PostalCode and Country. A field is a
column in a table that is designed to maintain specific information about
every record in the table.

A record, also called a row, is each individual entry that exists in a


table. For example, there are 91 records in the above Customers table. A
record is a horizontal entity in a table.

A column is a vertical entity in a table that contains all information


associated with a specific field in a table

2
UNIT 2

Querying Multiple Relations in SQL


Here, we are going to see how to query multiple tables in SQL. For
example, here, we will first create a database named “geeks” then we will
create 2 tables “department” and “employee” in that database. After, that
we will execute our query on the tables.

Creating a Database :

Use the below SQL statement to create a database called geeks:


CREATE DATABASE geeks;

Using Database :

USE geeks;

The department Table Definition:

We have the following department table in our geeks database :


Create Table department(
ID int,
SALARY int,
NAME Varchar(20),
DEPT_ID Varchar(255));
Output:

3
UNIT 2

You can use the below statement to query the description of the created table:
EXEC SP_COLUMNS department;

Adding Data to department Table:

4
UNIT 2
The date data type uses the format ‘YYYY-MM-DD‘. Use the
below statement to add data to the department table:
INSERT INTO department VALUES (1,'Neha','F','1994-06-03');
INSERT INTO department VALUES (2,'Harsh','M','1996-03-12');
INSERT INTO department VALUES (3,'Harsh','M','1995-05-01');
INSERT INTO department VALUES (4,'Rupali','F',1996-11-11');
INSERT INTO department VALUES (5,'Rohan','M','1992-03-08');
To verify the contents of the table use the below statement:
SELECT * FROM department;

The employee Table Definition:

Now create another table called employee:


CREATE TABLE employee(
ID int,
Email Varchar(255),
City Varchar(20) );

Adding Data to employee Table:

Add values into the table “employee“:


INSERT INTO employee VALUES (1, "[email protected]",
"Noida");
INSERT INTO employee VALUES (2, "[email protected]", "Jaipur");
INSERT INTO employee VALUES (3, "[email protected]", "Noida");
INSERT INTO employee VALUES (4, "[email protected]", "Jaipur");
INSERT INTO employee VALUES (5, "[email protected]", "Noida");
To verify the contents of the table use the below statement:

5
UNIT 2
SELECT * FROM employee;

Querying Multiple Tables in SQL:

Method 1:
The most common way to query multiple tables is with a simple
SELECT expression. To integrate results from different tables, use the
FROM clause to name more than one table. Here’s how it works in
practice:
Syntax:
SELECT table1name.column1name, table2name.column2name FROM
table1name, table2name
WHERE table1name.column1name = table2name.column1name;
Example:
SELECT department.ID, department.NAME, employee.Email,
employee.City FROM department, employee
WHERE department.ID = employee.ID;

6
UNIT 2

Output:

Method 2: Using JOINS


SQL Joins can also be used for the same purpose using the below syntax:
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
JOIN table2
ON table1.matching_column = table2.matching_column;
Example:
SELECT department.ID, department.NAME, employee.Email,
employee.City
FROM department JOIN employee ON department.ID = employee.ID;
Output:

7
UNIT 2

Create a Relationship in SQL


In SQL, you create a relationship by creating a foreign key constraint.

More specifically, you have a parent table and a child table. The parent
contains the primary key, and the child table contains a foreign key that
references the primary key of the parent table.

When you use SQL to create a relationship, you can create the
relationship at the time you create the table, or you can create it later (by
altering the table). This article covers both scenarios.

Create a Relationship When Creating the Table


Here’s an example of creating a relationship within your CREATE
TABLE statement at the time you create the table.

CREATE TABLE Parent (


ParentId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ParentName nvarchar(255) NOT NULL
)
CREATE TABLE Child (
ChildId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ChildName nvarchar(255) NOT NULL,
ParentId int NOT NULL
CONSTRAINT FK_Child_Parent FOREIGN KEY (ParentId)
REFERENCES Parent (ParentId)
);
Here I created two tables; one called Parent and the other called Child.

8
UNIT 2
I created the relationship within the table definition for child. The
relationship is created with the CONSTRAINT argument. Note that this is still
inside the CREATE TABLE statement.

The relationship needs a name. In this case I called it FK_Child_Parent.


The FOREIGN KEY part is followed by the name of the column (in the child
table) that will be the foreign key.

The REFERENCES part specifies the column that the foreign key will
reference. In this case it references the ParentId column of the Parent table.
This is done using REFERENCES Parent (ParentId).

That’s all that’s required to create the relationship.

Note that the examples on this page were done using SQL Server.
Depending on your DBMS, you may need to change some details of the
column definitions.

For example IDENTITY is SQL Server’s version of what is sometimes


called AUTO_INCREMENT in other DBMSs (such as MySQL). If you
use SQLite, then here’s how to create an auto-incrementing column in
SQLite.

Add a Relationship to an Existing Table


You can also add a relationship to an existing table, simply by using
the ALTER TABLE statement.

Let’s pretend that we didn’t create the relationship when creating the two
tables from the previous example. So let’s pretend that we’d done this
instead:

CREATE TABLE Parent (


ParentId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ParentName nvarchar(255) NOT NULL
)
CREATE TABLE Child (
ChildId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
ChildName nvarchar(255) NOT NULL,
ParentId int NOT NULL
);
So in this scenario, all we did was create two tables. No relationship was
created between them.

9
UNIT 2
Now, after creating the tables, we suddenly remember “oh dang, I forgot
to create a relationship!”.

No problem, we can now do this:

ALTER TABLE Child


ADD CONSTRAINT FK_Child_Parent FOREIGN KEY (ParentId)
REFERENCES Parent (ParentId);
Done. We’ve just added the relationship using the same details as per the
previous example.

Note that SQLite doesn’t support adding foreign keys with the ALTER
TABLE statement. See how to add a foreign key to an existing table in
SQLite for more about that.

On Update/Delete
By default, SQL Server relationships are created using ON DELETE NO
ACTION and ON UPDATE NO ACTION. Therefore, the previous examples were
created using this setting.

However, different DBMSs may use other default settings.

Either way, you can explicitly specify this in your code. So we can
modify the previous example to look like this:

ALTER TABLE Child


ADD CONSTRAINT FK_Child_Parent FOREIGN KEY (ParentId)
REFERENCES Parent (ParentId)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
What this actually means is that, if someone was to try to delete or update
a record in the primary key, an error would occur and the change would
be rolled back. This is SQL Server’s way of preventing any changes that
could break the referential integrity of your system.

Basically, the reason you create a relationship in the first place is to


enforce referential integrity.

However, you do have some options with how you want SQL Server to
deal with these situations.

Specifically, you can use any of the following values:

10
UNIT 2
 NO ACTION: An error is raised and the delete/update action on the
row in the parent table is rolled back.
 CASCADE: Corresponding rows are deleted from/updated in the
referencing table if that row is deleted from/updated in the parent
table.
 SET NULL: All the values that make up the foreign key are set
to NULL if the corresponding row in the parent table is deleted or
updated. This requires that the foreign key columns are nullable.
 SET DEFAULT: All the values that make up the foreign key are set to
their default values if the corresponding row in the parent table is
deleted or updated. For this constraint to execute, all foreign key
columns must have default definitions. If a column is nullable, and
there is no explicit default value set, NULL becomes the implicit
default value of the column.

DELETING DATA FROM TABLES

To delete some data from tables, DELETE command is used. The


DELETE command removes rows from a
table. The syntax of DELETE command is :
DELETE FROM <tablename>
WHERE <condition> ;
For example, to remove the details of those employee from EMPLOYEE
table whose grade is A1.
DELETE FROM EMPLOYEE
WHERE GRADE =’A1’ ;

TO DELETE ALL THE CONTENTS FROM A TABLE

DELETE FROM EMPLOYEE ;


So if we do not specify any condition with WHERE clause, then all the
rows of the table will be deleted. Thus
above line will delete all rows from employee table.
DROPPING TABLES
The DROP TABLE command lets you drop a table from the database.
The syntax of DROP TABLE command is :
DROP TABLE <tablename> ;
e.g. to drop a table employee, we need to write :
DROP TABLE employee ;
Once this command is given, the table name is no longer recognized and
no more commands can be given on that table.

11
UNIT 2
After this command is executed, all the data in the table along with table
structure will be deleted.
S.NO.
DELETE COMMAND
DROP TABLE COMMAND

Syntax of DELETE command is:


Syntax of DROP command is :
DELETE FROM <tablename>
DROP TABLE <tablename> ;
WHERE <condition> ;
ALTER TABLE COMMAND
The ALTER TABLE command is used to change definitions of existing
tables.(adding columns,deleting columns
etc.). The ALTER TABLE command is used for :
1. adding columns to a table2. Modifying column-definitions of a table.
3. Deleting columns of a table.
4. Adding constraints to table.
5. Enabling/Disabling constraints.
ADDING COLUMNS TO TABLE
To add a column to a table, ALTER TABLE co

SQL: Insertion And Deletion


Inserting tuples into a database

The INSERT command can be used to insert one or more tuples into a
relation.

There are 2 forms of the INSERT command:

 Insert tuple(s) using literal values


 Insert tuple(s) using result from a SELECT command

Inserting a tuple using literal (constant) values

Syntax:

INSERT INTO relationName VALUES ( .... )

or
INSERT INTO relationName(attributeList) VALUES ( .... )

Format 1: Inserting ONE COMPLETE tuple:

12
UNIT 2
You must specify all attribute values in the exact order at
the relation schema.

You can omit the list of attributes.

Example:

INSERT INTO employee VALUES


('Richard', 'K', 'Marini', '222669999', '30-Dec-52',
'98 Oak Street, Katy, TX', 'M', 37000, '987654321', 4);

Format 2: Inserting a PARTIAL tuple:


You specify a subset of the attribute values in the any order

You must provide a list of attributes.

INSERT INTO employee(fname, lname, ssn) VALUES


('Richard', 'Marini', '222669999');

Deleting tuples from a database


 You can delete one or more tuples from a relation using the
DELETE command.

 Syntax:

DELETE FROM relationName


[WHERE tuple-boolean-condition]

Example 1:

Delete the employee 'John Smith'

DELETE FROM employee


WHERE fname = 'John'
AND lname = 'Smith';

Example 2:

Delete all employees from the 'Research' department

13
UNIT 2

DELETE FROM employee


WHERE dno IN (SELECT dnumber
FROM department
WHERE dname = 'Research')

Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the
quality of information.
o Integrity constraints ensure that the data insertion, updating, and
other processes have to be performed in such a way that data
integrity is not affected.
o Thus, integrity constraint is used to guard against accidental
damage to the database.

Types of Integrity Constraint

1. Domain constraints

o Domain constraints can be defined as the definition of a valid set of


values for an attribute.

14
UNIT 2
O The data type of domain includes string, character, integer, time,
date, currency, etc. The value of the attribute must be available in
the corresponding domain.

Example:

2. Entity integrity constraints

o The entity integrity constraint states that primary key value can't be
null.
o This is because the primary key value is used to identify individual
rows in relation and if the primary key has a null value, then we
can't identify those rows.
o A table can contain a null value other than the primary key field.

Example:

3. Referential Integrity Constraints

o A referential integrity constraint is specified between two tables.

15
UNIT 2
o In the Referential integrity constraints, if a foreign key in Table 1
refers to the Primary Key of Table 2, then every value of the
Foreign Key in Table 1 must be null or be available in Table 2.

Example:

4. Key constraints

o Keys are the entity set that is used to identify an entity within its
entity set uniquely.
o An entity set can have multiple keys, but out of which one key will
be the primary key. A primary key can contain a unique and null
value in the relational table.

Example:

16
UNIT 2

Primary Key
A Primary Key is the minimal set of attributes of a table that has the task
to uniquely identify the rows, or we can say the tuples of the given
particular table.

A primary key of a relation is one of the possible candidate keys which


the database designer thinks it's primary. It may be selected for
convenience, performance and many other reasons. The choice of the
possible primary key from the candidate keys depend upon the following
conditions.

o Minimal: The primary key should be composed of the minimum


number of attributes that satisfy unique occurrences of the tuples.
So if one candidate key is formed using two attributes and another
using a single attribute then the one with the single attribute key
should be chosen as the primary key.
o Accessible: The primary key used should be accessible by anyone
who wants to use it. The user must easily insert, access or delete a
tuple using it.
o NON NULL Value: The primary key must have a non-null value
for each tuple of the relation, which is required for the
identification of the tuple.
o Time Variant: The values of the primary key must not change or
become null during the time of a relation.
o Unique: The value of the primary key must not be duplicated in
any of the tuples of a relation.

17
UNIT 2
Syntax for creating primary key constraint:
The primary key constraint can be defined at the column level or
table level.

At column level:

<column_name><datatype> Primary key;

At table level:
Primary key(<column_name1>[,column_name>]....);

Properties of a Primary Key:

o A relation can contain only one primary key.


o A primary key may be composed of a single attribute known as
single primary key or more than one attribute known as composite
key.
o A primary key is the minimum super key.
o The data values for the primary key attribute should not be null.
o Attributes which are part of a primary key are known as Prime
attributes.
o Primary key is always chosen from the possible candidate keys

Candidate Key
A candidate key is a part of a key known as Super Key (discussed in
the previous section), where the super key is the super set of all those
attributes that can uniquely identify a table.

A candidate key is a subset of a super key set where the key which
contains no redundant attribute is none other than a Candidate Key. In
order to select the candidate keys from the set of super key, we need to
look at the super key set.

Role of a Candidate Key


The role of a candidate key is to identify a table row or column uniquely.
Also, the value of a candidate key cannot be Null. The description of a
candidate key is "no redundant attributes" and being a "minimal
representation of a tuple," according to the Experts.

18
UNIT 2

Example of Candidate Key


Let's look at the same example took while discussing Super Key to
understand the working of a candidate key.

We have an EMPLOYEE_DETAIL table where we have the following


attributes:

Emp_SSN: The SSN number is stored in this field.

Emp_Id: An attribute that stores the value of the employee identification


number.

Emp_name: An attribute that stores the name of the employee holding


the specified employee id.

Emp_email: An attribute that stores the email id of the specified


employees.

The EMPLOYEE_DETAIL table is given below that will help you


understand better:

So, from the above table, we obtained the below given super keys
(discussed in the previous section):

SQL FOREIGN KEY

19
UNIT 2
The FOREIGN KEY constraint is used to prevent actions that would
destroy links between tables.

A FOREIGN KEY is a field (or collection of fields) in one table, that


refers to the PRIMARY KEY in another table.

The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.

Look at the following two tables:

Persons Table
PersonID LastName FirstName
1 Hansen Ola
2 Svendson Tove
3 Pettersen Kari

Orders Table
OrderID OrderNumber PersonID
1 77895 3
2 44678 3

3 22456 2
4 24562 1

Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.

The "PersonID" column in the "Persons" table is the PRIMARY KEY in


the "Persons" table.

The "PersonID" column in the "Orders" table is a FOREIGN KEY in the


"Orders" table.

The FOREIGN KEY constraint prevents invalid data from being inserted
into the foreign key column, because it has to be one of the values
contained in the parent table

20
UNIT 2
Referential Integrity
A referential integrity is also known as foreign key constraint. A
foreign key is a key whose values are derived from the Primary key of
another table.

The table from which the values are derived is known as Master or
Referenced Table and the Table in which values are inserted accordingly
is known as Child or Referencing Table, In other words, we can say that
the table containing the foreign key is called the child table, and the
table containing the Primary key/candidate key is called the referenced
or parent table. When we talk about the database relational model, the
candidate key can be defined as a set of attribute which can have zero or
more attributes.

Enforcing Referential Integrity


A referential integrity asserts a relationship between two tables such
that the values in a column of one table must match the values in a
column of the second table. Traditionally, the two tables have a parent-
child relationship:
•The parent table has a column, called the primary key, containing values
against which other values are compared. The primary key is normally
unique.
•The child table has a column, called the foreign key, whose values must
match those of the primary key in the parent table.
A primary key does not have to be referenced by a foreign key (that is,
there can be a parent without a child). However, every foreign key must
match a primary key. There cannot be a child without a parent (that is,
an orphan)—this constitutes a referential integrity violation.

Categories of SQL command

SQL commands or SQL sublanguage commands like DDL, DQL, DML,


DCL

These SQL commands are mainly categorized into five categories:


1.DDL (Data Definition Language)
2.DQL (Data Query Language)
3.DML(Data Manipulation Language)

21
UNIT 2
4.DCL (Data Control Language)
5.TCL (Transaction Control Language)

DDL (Data Definition Language)


DDL or Data Definition Language actually consists of the SQL
commands that can be used to define the database schema. It simply
deals with descriptions of the database schema and is used to create and
modify the structure of database objects in the database. DDL is a set of
SQL commands used to create, modify, and delete database structures
but not data. These commands are normally not used by a general user,
who should be accessing the database via an application.
List of DDL commands:
CREATE: This command is used to create a new database object. For
example
CREATE TABLE Student(
RollNo int PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Gender TEXT NOT NULL;
Subject VARCHAR(30),
MARKS INT (3)
INSERT INTO Student VALUES (1, Vaibhav, M, Mathematic, 100);
INSERT INTO Student VALUES (2, Vishal, M, Physics, 79);

Roll no Name Gender Subject Mark

1 Vaibhav M Mathematic 100

2 Vishal M Physics 79

1. ALTER: This command is used to modify an existing database


object, such as adding, deleting, or modifying columns in an
existing table.
Syntax for adding a column in a table: ALTER TABLE
table_name ADD column_name datatype;
Syntax for modifying a column in a table: ALTER TABLE
table_name MODIFY COLUMN column_name datatype;
Example:
ALTER TABLE Student
ADD State TEXT;

22
UNIT 2

SELECT * FROM Student;

Roll no Name Gender Subject Mark State

1 Vaibhav M Mathematic 100 NULL

2 Vishal M Physics 79 NULL

2. DROP: This command is used to delete an existing database object


like a table, a view, or other objects.
Syntax for dropping a table: DROP TABLE table_name;
EXAMPLE:
ALTER TABLE Student
DROP COLUMN Marks;

SELECT * FROM Student;

Roll no Name Gender Subject

1 Vaibhav M Mathematic

2 Vishal M Physics

3. TRUNCATE: This command is used to delete all data from a table,


but the structure of the table remains. It’s a fast way to clear large
data from a table.
Syntax: TRUNCATE TABLE table_name;
4. COMMENT: Used to add comments to the data dictionary.
Syntax: COMMENT ON TABLE table_name IS 'This is a
comment.';
5. RENAME: Used to rename an existing database object.
Syntax: RENAME TABLE old_table_name TO new_table_name;
EXAMPLE:
ALTER TABLE
RENAME COLUMN RollNo TO EnrollementNo;

SELECT * FROM Student;

23
UNIT 2
Enrollment Name Gender Subject Mark State
no
1 Vaibhav M Mathematic 100 NULL

2 Vishal M Physics 79 NULL

DQL (Data Query Language)


DQL statements are used for performing queries on the data within
schema objects. The purpose of the DQL Command is to get some
schema relation based on the query passed to it. We can define DQL as
follows it is a component of SQL statement that allows getting data
from the database and imposing order upon it. It includes the SELECT
statement. This command allows getting the data out of the database to
perform operations with it. When a SELECT is fired against a table or
tables the result is compiled into a further temporary table, which is
displayed or perhaps received by the program i.e. a front-end.
List of DQL:
1. SELECT: The main command used in DQL, SELECT retrieves
data from one or more tables.
Basic Syntax: SELECT column1, column2, ... FROM table_name;
To select all columns from a table, you use SELECT * FROM
table_name;
2. WHERE Clause: Used with SELECT to filter records based on
specific conditions.
Syntax: SELECT column1, column2, ... FROM table_name
WHERE condition;
Example: SELECT * FROM employees WHERE department =
'Sales';
3. JOIN Clauses: Used to combine rows from two or more tables
based on a related column between them.
Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL
JOIN.
Syntax: SELECT columns FROM table1 [JOIN TYPE] JOIN
table2 ON table1.column_name = table2.column_name;
4. GROUP BY Clause: Used with aggregate functions (like COUNT,
MAX, MIN, SUM, AVG) to group the result set by one or more
columns.
Syntax: SELECT column1, aggregate_function(column2) FROM
table_name GROUP BY column1;

24
UNIT 2
5. ORDER BY Clause: Used to sort the result set in ascending or
descending order.
Syntax: SELECT column1, column2 FROM table_name ORDER
BY column1 [ASC|DESC], column2 [ASC|DESC];

DML(Data Manipulation Language)


The SQL commands that deal with the manipulation of data present in
the database belong to DML or Data Manipulation Language and this
includes most of the SQL statements. It is the component of the SQL
statement that controls access to data and to the database. Basically,
DCL statements are grouped with DML statements.
List of DML commands:
INSERT: This command is used to add new rows (records) to a
1.
table.
Syntax: INSERT INTO table_name (column1, column2,
column3, ...) VALUES (value1, value2, value3, ...);
EXAMPLE:
INSERT INTO Student VALUES (1, Vaibhav, M, Mathematic, 100);
INSERT INTO Student VALUES (2, Vishal, M, Physics, 79);

Roll no Name Gender Subject Mark

1 Vaibhav M Mathematic 100

2 Vishal M Physics 79

2. UPDATE: This command is used to modify the existing records in


a table.
Syntax: UPDATE table_name SET column1 = value1, column2 =
value2, ... WHERE condition;
The WHERE clause specifies which records should be updated.
Without it, all records in the table will be updated.

3. DELETE: This command is used to remove one or more rows


from a table.
Syntax: DELETE FROM table_name WHERE condition;
Like with UPDATE, the WHERE clause specifies which rows
should be deleted. Omitting the WHERE clause will result in all
rows being deleted.

25
UNIT 2
4. SELECT: Although often categorized separately,
the SELECT command is sometimes considered part of DML as it
is used to retrieve data from the database.
Syntax: SELECT column1, column2, ... FROM table_name
WHERE condition;
The SELECT statement is used to query and extract data from a
table, which can then be used for various purposes.
DCL (Data Control Language)
DCL includes commands such as GRANT and REVOKE which
mainly deal with the rights, permissions, and other controls of the
database system.
List of DCL commands:
1. GRANT: This command is used to give users access privileges to
the database. These privileges can include the ability to select, insert,
update, delete, and so on, over database objects like tables and
views.
 Syntax: GRANT privilege_name ON object_name TO
user_name;
 For example, GRANT SELECT ON employees TO
user123; gives user123 the permission to read data from
the employees table.
2. REVOKE: This command is used to remove previously granted
access privileges from a user.
 Syntax: REVOKE privilege_name ON object_name FROM
user_name;
 For example, REVOKE SELECT ON employees FROM
user123; would remove user123‘s permission to read data from
the employees table.
Syntax:
REVOKE SELECT, UPDATE ON MY_TABLE FROM U
SER1, USER2;
TCL (Transaction Control Language)
Transactions group a set of tasks into a single execution unit. Each
transaction begins with a specific task and ends when all the tasks in the
group are successfully completed. If any of the tasks fail, the transaction
fails. Therefore, a transaction has only two results: success or failure.
You can explore more about transactions here. Hence, the following
TCL commands are used to control the execution of a transaction:

26
UNIT 2
BEGIN: Opens a Transaction.
COMMIT: Commits a Transaction.
Syntax:
COMMIT;
ROLLBACK: Rollbacks a transaction in case of any error occurs.
Syntax:
ROLLBACK;
SAVEPOINT: Sets a save point within a transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;

Normalization
Normalization is the process of organizing data in a database. It
includes creating tables and establishing relationships between those
tables according to rules designed both to protect the data and to make the
database more flexible by eliminating redundancy and inconsistent
dependency.

1. Insertion anomaly: It occurs when we cannot insert data to the


table without the presence of another attribute
2. Update anomaly: It is a data inconsistency that results from data
redundancy and a partial update of data.
3. Deletion Anomaly: It occurs when certain attributes are lost
because of the deletion of other attributes.

Normalization process is further categorized into the following types:

1) First Normal Form (1NF)


2) Second Normal Form (2NF)
3) Third Normal Form (3NF)
4) Boyce-Codd Normal Form (BCNF)
5) Fourth Normal Form (4NF)
6) Fifth Normal Form (5NF)
7)
First Normal Form with Example

27
UNIT 2
If a relation contains a composite or multi-valued attribute, it violates
the first normal form, or the relation is in the first normal form if it does
not contain any composite or multi-valued attribute. A relation is in
first normal form if every attribute in that relation is single-valued
attribute.
A table is in 1 NF if:
 There are only Single Valued Attributes.
 Attribute Domain does not change.
 There is a unique name for every Attribute/Column.
 The order in which data is stored does not matter.

Consider the examples given below.

Example 1:

Relation STUDENT in table 1 is not in 1NF because of multi-valued


attribute STUD_PHONE. Its decomposition into 1NF has been shown in
table 2.

Second Normal Form


The second Normal Form (2NF) is based on the concept of fully
functional dependency. The second Normal Form applies to relations
with composite keys, that is, relations with a primary key composed of
two or more attributes. A relation with a single-attribute primary key is
automatically in at least 2NF. A relation that is not in 2NF may suffer
from the update anomalies. To be in the second normal form, a relation
must be in the first normal form and the relation must not contain any
partial dependency. A relation is in 2NF if it has No Partial Dependency,
i.e., no non-prime attribute (attributes that are not part of any candidate

28
UNIT 2
key) is dependent on any proper subset of any candidate key of the table.
In other words,
A relation that is in First Normal Form and every non-
primary-key attribute is fully functionally dependent on the
primary key, then the relation is in Second Normal Form
(2NF).
Example: Consider following functional dependencies in relation R
(A, B, C, D )
AB -> C [A and B together determine C]
BC -> D [B and C together determine D]
Answer:
First, we can check if there are any partial dependencies. A partial
dependency occurs when a non-prime attribute (not part of any
candidate key) depends on only part of a candidate key.
The candidate keys for relation R can be determined by finding the
closure of each attribute:
AB determines every keys.
Now, let’s check for partial dependencies:
There are no partial dependencies in this relation because each non-
prime attribute (C and D) depends on the whole candidate key(s) it is
part of (AB and BC, respectively).
Therefore, the relation R is already in 3rd Normal Form (3NF) because
it satisfies the conditions of 1st Normal Form (1NF) and 2nd Normal
Form (2NF) and does not have any transitive dependencies.
Third Normal Form (3NF)
A relation is in the third normal form, if there is no transitive
dependency for non-prime attributes as well as it is in the second normal
form. A relation is in 3NF if at least one of the following conditions
holds in every non-trivial function dependency X –> Y.
 X is a super key.
 Y is a prime attribute (each element of Y is part of some
candidate key).
In other words,
A relation that is in First and Second Normal Form and in
which no non-primary-key attribute is transitively
dependent on the primary key, then it is in Third Normal
Form (3NF).

29
UNIT 2
Example 1:
In relation STUDENT given in Table 4,

FD set: {STUD_NO -> STUD_NAME, STUD_NO -> STUD_STATE,


STUD_STATE -> STUD_COUNTRY, STUD_NO -> STUD_AGE}
Candidate Key: {STUD_NO} For this relation in table 4, STUD_NO ->
STUD_STATE and STUD_STATE -> STUD_COUNTRY are true. So
STUD_COUNTRY is transitively dependent on STUD_NO. It violates
the third normal form. To convert it in third normal form, we will
decompose the relation STUDENT (STUD_NO, STUD_NAME,
STUD_PHONE, STUD_STATE, STUD_COUNTRY_STUD_AGE) as:
STUDENT (STUD_NO, STUD_NAME, STUD_PHONE,
STUD_STATE, STUD_AGE)
STATE_COUNTRY (STATE, COUNTRY)
Boyce-Codd Normal Form (BCNF)
Boyce–Codd Normal Form (BCNF) is based on functional
dependencies that take into account all candidate keys in a relation;
however, BCNF also has additional constraints compared with the
general definition of 3NF.
Rules for BCNF
Rule 1: The table should be in the 3rd Normal Form.
Rule 2: X should be a superkey for every functional
dependency (FD) X−>Y in a given relation.
Note: To test whether a relation is in BCNF, we identify all the
determinants and make sure that they are candidate keys.

30
UNIT 2

BCNF
You came across a similar hierarchy known as the Chomsky Normal
Form in the Theory of Computation. Now, carefully study the hierarchy
above. It can be inferred that every relation in BCNF is also in 3NF.
To put it another way, a relation in 3NF need not be in BCNF. Ponder
over this statement for a while.
To determine the highest normal form of a given relation R with
functional dependencies, the first step is to check whether the BCNF
condition holds. If R is found to be in BCNF, it can be safely deduced
that the relation is also in 3NF, 2NF, and 1NF as the hierarchy shows.
The 1NF has the least restrictive constraint – it only requires a relation
R to have atomic values in each tuple. The 2NF has a slightly more
restrictive constraint.
The 3NF has a more restrictive constraint than the first two normal
forms but is less restrictive than the BCNF. In this manner, the
restriction increases as we traverse down the hierarchy.

Example

Let us consider the student database, in which data of the student are
mentioned.

Stu_I Stu_Branch Stu_Course Branch_Num Stu_Course_


D ber No

31
UNIT 2

Stu_I Stu_Branch Stu_Course Branch_Num Stu_Course_


D ber No

Computer
101 Science & DBMS B_001 201
Engineering

Computer
Computer
101 Science & B_001 202
Networks
Engineering

Electronics
&
VLSI
102 Communicati B_003 401
Technology
on
Engineering

Electronics
& Mobile
102 Communicati Communicati B_003 402
on on
Engineering
Functional Dependency of the above is as mentioned:
Stu_ID −> Stu_Branch
Stu_Course −> {Branch_Number, Stu_Course_No}
Candidate Keys of the above table are: {Stu_ID, Stu_Course}

4th and 5th Normal Form


Two of the highest levels of database normalization are the fourth normal
form (4NF) and the fifth normal form (5NF). Multivalued dependencies
are handled by 4NF, whereas join dependencies are handled by 5NF.
If two or more independent relations are kept in a single relation or we
can say multivalue dependency occurs when the presence of one or more
rows in a table implies the presence of one or more other rows in that
same table. Put another way, two attributes (or columns) in a table are
independent of one another, but both depend on a third attribute.
A multivalued dependency always requires at least three attributes
because it consists of at least two attributes that are dependent on a third.

32
UNIT 2
For a dependency A -> B, if for a single value of A, multiple values of B
exist, then the table may have a multi-valued dependency. The table
should have at least 3 attributes and B and C should be independent for A
->> B multivalued dependency.
Fourth Normal Form (4NF)

The Fourth Normal Form (4NF) is a level of database normalization


where there are no non-trivial multivalued dependencies other than a
candidate key. It builds on the first three normal forms (1NF, 2NF, and
3NF) and the Boyce-Codd Normal Form (BCNF). It states that, in
addition to a database meeting the requirements of BCNF, it must not
contain more than one multivalued dependency.

Properties

A relation R is in 4NF if and only if the following conditions are


satisfied:
1. It should be in the Boyce-Codd Normal Form (BCNF).
2. The table should not have any Multi-valued Dependency.
A table with a multivalued dependency violates the normalization
standard of the Fourth Normal Form (4NF) because it creates
unnecessary redundancies and can contribute to inconsistent data. To
bring this up to 4NF, it is necessary to break this information into two
tables.
Example: Consider the database table of a class that has two relations
R1 contains student ID(SID) and student name (SNAME) and R2
contains course id(CID) and course name (CNAME).
Table R1

SID SNAME

S1 A

S2 B
Table R2

CID CNAME

33
UNIT 2

CID CNAME

C1 C

C2 D
When their cross-product is done it resulted in multivalued
dependencies.
Table R1 X R2

SID SNAME CID CNAME

S1 A C1 C

S1 A C2 D

S2 B C1 C

S2 B C2 D
Multivalued dependencies (MVD) are:
SID->->CID; SID->->CNAME; SNAME->->CNAME

Fifth Normal Form/Projected Normal Form (5NF)

A relation R is in Fifth Normal Form if and only if everyone joins


dependency in R is implied by the candidate keys of R. A relation
decomposed into two relations must have lossless join Property, which
ensures that no spurious or extra tuples are generated when relations are
reunited through a natural join.

Properties

A relation R is in 5NF if and only if it satisfies the following conditions:


1. R should be already in 4NF.
2. It cannot be further non loss decomposed (join dependency).

34
UNIT 2
Example – Consider the above schema, with a case as “if a company
makes a product and an agent is an agent for that company, then he
always sells that product for the company”. Under these circumstances,
the ACP table is shown as:
Table ACP
Agent Company Product

A1 PQR Nut

A1 PQR Bolt

A1 XYZ Nut

A1 XYZ Bolt

A2 PQR Nut

The relation ACP is again decomposed into 3 relations. Now, the natural
Join of all three relations will be shown as:

Table R1

Agent Company

A1 PQR

A1 XYZ

A2 PQR
Table R2

Agent Product

A1 Nut

35
UNIT 2

Agent Product

A1 Bolt

A2 Nut
Table R3

Company Product

PQR Nut

PQR Bolt

XYZ Nut

XYZ Bolt
The result of the Natural Join of R1 and R3 over ‘Company’ and then
the Natural Join of R13 and R2 over ‘Agent’and ‘Product’ will be Table
ACP.

Denormalization
Denormalization is a database optimization technique in which we add
redundant data to one or more tables. This can help us avoid costly joins
in a relational database. Note that denormalization does not mean
‘reversing normalization’ or ‘not to normalize’. It is an optimization
technique that is applied after normalization.
Basically, The process of taking a normalized schema and making it
non-normalized is called denormalization, and designers use it to tune the
performance of systems to support time-critical operations.
In a traditional normalized database, we store data in separate logical
tables and attempt to minimize redundant data. We may strive to have
only one copy of each piece of data in a database.
Denormalization, then, strikes a different compromise. Under
denormalization, we decide that we’re okay with some redundancy and
some extra effort to update the database in order to get the efficiency
advantages of fewer joins.
Pros of Denormalization:

36
UNIT 2
1. Retrieving data is faster since we do fewer joins
2. Queries to retrieve can be simpler(and therefore less likely to have
bugs),
since we need to look at fewer tables.
Cons of Denormalization:
1. Updates and inserts are more expensive.
2. Denormalization can make update and insert code harder to write.
3. Data may be inconsistent.
4. Data redundancy necessitates more storage.
In a system that demands scalability, like that of any major tech
company, we almost always use elements of both normalized and
denormalized databases.

Advantages of Denormalization:

Improved Query Performance: Denormalization can improve query


performance by reducing the number of joins required to retrieve data.
Reduced Complexity: By combining related data into fewer tables,
denormalization can simplify the database schema and make it easier to
manage.
Easier Maintenance and Updates: Denormalization can make it easier
to update and maintain the database by reducing the number of tables.
Improved Read Performance: Denormalization can improve read
performance by making it easier to access data.
Better Scalability: Denormalization can improve the scalability of a
database system by reducing the number of tables and improving the
overall performance.

Disadvantages of Denormalization:

Reduced Data Integrity: By adding redundant data, denormalization


can reduce data integrity and increase the risk of inconsistencies.
Increased Complexity: While denormalization can simplify the
database schema in some cases, it can also increase complexity by
introducing redundant data.
Increased Storage Requirements: By adding redundant data,
denormalization can increase storage requirements and increase the cost
of maintaining the database.

37
UNIT 2
Increased Update and Maintenance Complexity: Denormalization
can increase the complexity of updating and maintaining the database by
introducing redundant data.
Limited Flexibility: Denormalization can reduce the flexibility of a
database system by introducing redundant data and making it harder to
modify the schema.

38

You might also like