You are on page 1of 70

DATABASE MANAGEMENT

SYSTEMS (DBMS)

MODULE -II
21AIM45A
MODULE –II : SYLLABUS

Introduction to Logical Design and Relational


Model: Domains, Attributes, Tuples, and Relations;
Relational Model Constraints; Relational Database
Schemas; SQL-1: Overview of SQL language; SQL
Data Definition and Data Types; Schema change
statements in SQL; Enforcing basic constraints in
SQL; Basic structure of SQL queries Joins; Logical
connectives - AND, OR and NOT; Addition basic
operations ; Set operations; Aggregate function;
SQL modification language; Select, Delete, Update
clause
RELATIONAL MODEL
Relational Model Concepts:
The relational model represents the database as a collection of
relations(Tables). Informally, each relation resembles a table
of values or, to some extent, a flat file of records. It is called a
flat file because each record has a simple linear or flat
structure.
Domains, Attributes, Tuples, and Relations

Some examples of domains follow:


•Usa_phone_numbers. The set of ten-digit phone numbers valid in the United
States.
Names: The set of character strings that represent names of persons
Relational Model Constraints

 Relational database, there will typically be


many relations, and the tuples in those
relations are usually related in various
ways.
 The state of the whole database will
correspond to the states of all its relations
at a particular point in time.
 There are generally many restrictions or
constraints on the actual values in a
database state.
 These constraints are to achieve the
integrity of data n database.
 These constraints are derived from the
rules in the mini world that the database
represents.
Relational Integrity Constraints
Relational Integrity constraints in DBMS are
referred to conditions which must be present for
a valid relation. These Relational constraints in
DBMS are derived from the rules in the mini-
world that the database represents.

There are many types of Integrity Constraints in


DBMS. Constraints on the Relational database
management system is mostly divided into three
main categories are:

1.Domain Constraints
2.Key Constraints
3.Referential Integrity Constraints
4.The Entity integrity constraint
Domain Constraints
Domain constraints can be violated if an attribute value is
not appearing in the corresponding domain or it is not of the
appropriate data type.
Domain constraints specify that within each tuple, and
the value of each attribute must be unique. This is
specified as data types which include standard data types
integers, real numbers, characters, Booleans, variable
length strings, etc.
 Constraints in DBMS are the restrictions that are applied to
data or operations on the data. This means that constraints
allow only a particular kind of data to be inserted in the
database or only some particular kind of operations to be
performed on the data in the database.
 Thus, constraints ensure the correctness of data in a
Database Management System (DBMS).
Consider the following example relations.
Student Student Marks
ID Name (in %) So, we can say that this is a valid table.
This is because the student ID attribute
1 Guneet 90 can have only integers as ID and it does
have only integers as ID. Also, the names
2 Ahan 92 can be strings only and the marks can be
integers or floating values only. So, every
3 Yash 87 attribute for every tuple in this table has
its values within its domain.
4 Lavish 90
5 Ashish 79

Now, in the table above, the tuple with


Student ID = 4 and name = “Lavish” has
marks = A. This is not an integer or float
value. So, the domain constraint is
violated here.
Key Constraints
An attribute that can uniquely identify a tuple in a
relation is called the key of the table. The value of the
attribute for different tuples in the relation has to be
unique.

Example:
In the given table, CustomerID is a key attribute of
Customer Table. It is most likely to have a single key for one
customer, CustomerID =1 is only for the
CustomerName =” Google”.
Keys
•Keys play an important role in the relational database.
•It is used to uniquely identify any record or row of data from the
table. It is also used to establish and identify relationships between
tables.
Types of keys:
1. Primary key
•It is the first key used to identify one and only one instance of an entity
uniquely. An entity can contain multiple keys, as we saw in the PERSON
table. The key which is most suitable from those lists becomes a primary key.
•In the EMPLOYEE table, ID can be the primary key since it is unique for
each employee. In the EMPLOYEE table, we can even select
License_Number and Passport_Number as primary keys since they are also
unique.
•For each entity, the primary key selection is based on requirements and
developers.
2. Candidate key
•A candidate key is an attribute or set of attributes that
can uniquely identify a tuple.
•Except for the primary key, the remaining attributes are
considered a candidate key. The candidate keys are as
strong as the primary key.
For example: In the EMPLOYEE table, id is best
suited for the primary key. The rest of the attributes, like
SSN, Passport_Number, License_Number, etc., are
considered a candidate key.
3. Foreign key
•Foreign keys are the column of the table used to point to the
primary key of another table.
•Every employee works in a specific department in a company,
and employee and department are two different entities. So we
can't store the department's information in the employee table.
That's why we link these two tables through the primary key of
one table.
•We add the primary key of the DEPARTMENT table,
Department_Id, as a new attribute in the EMPLOYEE table.
•In the EMPLOYEE table, Department_Id is the foreign key,
and both the tables are related.
Department
In the given
example, we have 2
relations, Customer
and Billing.
Tuple for
CustomerID =1 is
referenced twice in
the relation Billing.
So we know
CustomerName=Go
ogle has billing
amount $300
EXAMPLE 2:
The Entity integrity constraint

The entity integrity constraint states that no primary key value can be NULL. This is because
the primary key value is used to identify individual tuples in a relation. Having NULL values for the primary
key implies that we cannot identify some tuples. For example, if two or more tuples had NULL for their
primary keys, we may not be able to distinguish them if we try to reference them from other relations.

Key constraints and entity integrity constraints are specified on individual relations.
The referential integrity constraint:
The referential integrity constraint is specified between two relations and is used to
maintain the consistency among tuples in the two relations.

Informally, the referential integrity constraint states that a tuple in one relation that
refers to another relation must refer to an existing tuple in that relation.
Relational Database Schemas
A relational database usually contains many relations,
with tuples in relations that are related in various ways. In this
section, we define a relational database and a relational
database schema.

A relational database schema S is a set of relation


schemas S = {R1, R2, … , Rm} and a set of integrity
constraints IC. A relational database state10 DB of S is a
set of relation states DB = {r1, r2, … , rm} such that each ri
is a state of Ri and such that the ri relation states satisfy the
integrity constraints specified in IC. Figure 2.5 shows a
relational database schema that we call COMPANY =

{EMPLOYEE, DEPARTMENT, DEPT_LOCATIONS,


PROJECT, WORKS_ON, DEPENDENT}. In each relation
schema, the underlined attribute represents the primary key.
Figure shows a relational database state corresponding to the
COMPANY schema.
Schema diagram for the COMPANY relational database schema.
One possible database state for the COMPANY relational database
schema.
SQL-1: Overview of SQL language

SQL is a standard language for accessing and


manipulating databases.
What is SQL?
SQL stands for Structured Query
Language
SQL lets you access and manipulate
databases
SQL became a standard of the American
National Standards Institute (ANSI) in 1986,
and of the International Organization for
Standardization (ISO) in 1987
The standard SQL commands to interact with relational databases
are CREATE, SELECT, INSERT, UPDATE, DELETE and
DROP. These commands can be classified into the following
groups based on their nature:
DDL - Data Definition Language
Data Definition Language
(DDL)
•DDL changes the structure
of the table like creating a
table, deleting a table,
altering a table, etc.
•All the command of DDL
are auto-committed that
means it permanently save
all the changes in the
database.
DML - Data Manipulation Language

Data Manipulation
Language
•DML commands are
used to modify the
database. It is
responsible for all form
of changes in the
database.
•The command of DML
is not auto-committed
that means it can't
permanently save all the
changes in the database.
They can be rollback.
DCL - Data Control Language

DCL commands are used to grant and take back


authority from any database user.
SQL data types
 The data type of a column can be defined as basically what type of
data format in which each cell will store the data it can be any type of
integer, character, money, date and time, binary, etc.

 An SQL developer must aware of what type of data will be stored


inside each column while creating a table. The data type guideline for
SQL is to understand what type of data is expected inside each
column and it also identifies how SQL will interact with the stored
data.

 SQL (Structured Query Language) is a language used to interact with


relational databases. SQL data types define the type of data that can
be stored in a database column or variable. Here are the most
common SQL data types:
Data Type Properties
These are used to store numeric values.
Numeric data types Examples include INT, BIGINT, DECIMAL,
and FLOAT.

These are used to store character strings.


Character data types Examples include CHAR, VARCHAR, and
TEXT.

These are used to store date and time values.


Date and time data types Examples include DATE, TIME, and
TIMESTAMP.

This data type is used to store logical values.


Boolean data type
The only possible values are TRUE and FALSE.
These are used to store intervals of time. Examples
Interval data types include INTERVAL YEAR, INTERVAL MONTH,
and INTERVAL DAY.

These are used to store arrays of values. Examples


Array data types
include ARRAY and JSON.

XML data type This data type is used to store XML data.

These are used to store geometric or geographic


Spatial data types data. Examples include POINT, LINE, and
POLYGON.
CREATE
CREATE TABLE
does just what it Syntax:
sounds like: it
creates a table in the CREATE TABLE table_name
database. You can (column_1 datatype, column_2
specify the name of datatype, column_3 datatype );
the table and the
columns that should
be in the table.
Example:
CREATE TABLE EMPLOYEE
(Name VARCHAR(20), Email VARCHAR(1
00), DOB DATE);
ALTER

ALTER TABLE
changes the
structure of a table.
Here is how you
would add a 2. To modify existing column in the
column to a table:
database:
ALTER TABLE table_name MO
DIFY
(column_definitions....);

3. To rename existing column


alter table orders2 rename column
personid to personi ;
DROP:
It is used to delete
both the structure and
record stored in the
table.
Syntax

DROP TABLE table_name;

Example

DROP TABLE EMPLOYEE;


INSERT: The
Syntax:
INSERT statement
is a SQL query. It
INSERT INTO TABLE_NAME
is used to insert
data into the row
(col1, col2, col3,.... col N)
of a table.
VALUES (value1, value2, value3, .
... valueN);

INSERT INTO TABLE_NAME VA


LUES
(value1, value2, value3, .... valueN);
For example:
INSERT INTO Textbook (Author, Subject) VALUES ("Sonoo", "D
BMS");
UPDATE: This
Syntax:
command is used to
UPDATE table_name SET [colum
update or modify the
n_name1= value1,...column_name
value of a column in
N = valueN] [WHERE CONDITIO
the table.
N];
DELETE: It is
used to remove one
or more row from a
table.
SELECT: This is Syntax:
the same as the SELECT expressions
projection operation FROM TABLES
of relational WHERE conditions;
algebra. It is used to
select the attribute
based on the
condition described For example:
by WHERE clause. 1. SELECT emp_name
FROM employee
WHERE age > 20;

2. Select * from employee;


SQL Constraints

SQL constraints are used to specify rules for


data in a table.
SQL Create Constraints
Constraints can be specified when the table is created with the CREATE
TABLE statement, or after the table is created with the ALTER
TABLE statement.

Syntax
CREATE TABLE table_name
(
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints

SQL constraints are used to specify


rules for the data in a table.
Constraints are used to limit the
type of data that can go into a table.
This ensures the accuracy and
reliability of the data in the table. If
there is any violation between the
constraint and the data action, the
action is aborted.
Constraints can be column level or
table level. Column level constraints
apply to a column, and table level
constraints apply to the whole table.
The following constraints are commonly used in SQL:

•NOT NULL - Ensures that a column cannot have a NULL


value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
•FOREIGN KEY - Prevents actions that would destroy links
between tables
•CHECK - Ensures that the values in a column satisfies a
specific condition
•DEFAULT - Sets a default value for a column if no value is
specified
•CREATE INDEX - Used to create and retrieve data from the
database very quickly
SQL Joins

Custo Custom Contact Country


merID erName Name
1 Alfreds Maria German
Futterki Anders y
ste
2 Ana Ana Mexico
OrderID CustomerID OrderDate
Trujillo Trujillo
Empare
10308 2 1996-09-18 dados y
helados

10309 3 1996-09-19 3 Antonio Antonio Mexico


Moreno Moreno
Taquerí
10310 77 1996-09-20 a

Notice that the "CustomerID" column in the "Orders" table refers


to the "CustomerID" in the "Customers" table. The relationship
between the two tables above is the "CustomerID" column.
and it will produce
something like this:
Then, we can create the
following SQL statement
(that contains an INNER OrderI CustomerName OrderD
JOIN), that selects D ate
records that have 10308 Ana Trujillo Emparedados y helados 9/18/19
matching values in both 96
tables:
10365 Antonio Moreno Taquería 11/27/1
996
10383 Around the Horn 12/16/1
996
Example
10355 Around the Horn 11/15/1
996
10278 Berglunds snabbköp 8/12/19
96
SELECT Orders.OrderID, Customers.CustomerName,
Orders.OrderDate
FROM Orders
JOIN Customers ON Orders.CustomerID=Customers.Customer
ID;
Different Types of SQL JOINs:
Here are the different types of the
JOINs in SQL:
•(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
•FULL (OUTER) JOIN: Returns all records
when there is a match in either left or
right table
PRACTICE QUESTION:
1.Considering the Shema
Sailors(sid, sname,rating,age)
Boats(bid,bname,color)
Reserves(sid,bid,day)

Write relational algebraic queries for the following


a)Write names of sailors who have reserved boat
#103
b) Write names of sailors who have reserved a Red
boat
c) Write names of sailors who have reserved a Red or
Green boat
1. SELECT Sailors.sname FROM Sailors, Reserves
WHERE Sailors.sid = Reserves.sid AND Reserves.bid
= 103;

2. SELECT Sailors.sname, Sailors.age FROM Sailors ,


Reserves, Boats WHERE Sailors.sid = Reserves.sid AND
Reserves.bid = Boat.bid AND Boat.color = ‘red’ ;

3. SELECT sailors.sname FROM


Boats ,Reserves,sailors WHERE Sailors.sid =
Reserves.sid AND Reserves.bid = Boat.bid Boats.color
= ‘red’
UNION
SELECT sailors.sname FROM sailors,Boats Reserves
WHERE Sailors.sid = Reserves.sid AND Reserves.bid =
SQL Queries with AND, OR, and NOT Logical Operators

AND, OR, and NOT are commonly used T-SQL Logical


Operators that are used with a WHERE or HAVING clause
to filter on more than one condition.
 AND displays records where ALL the
conditions specified are true
 OR displays records where ANY of the
conditions specified are true
 NOT displays records where the condition(s)
specified are NOT TRUE
The WHERE clause can be combined with AND, OR, and NOT operators

The AND and OR operators are used to filter records based


on more than one condition:
•The AND operator displays a record if all the conditions
separated by AND are TRUE.
•The OR operator displays a record if any of the
conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is
NOT TRUE.
AND Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 .
..;

OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 .
..;

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Examples:

SELECT * FROM Customers


WHERE Country='Germany' AND City='Berlin';

SELECT * FROM Customers


WHERE City='Berlin' OR City='München';

SELECT * FROM Customers


WHERE NOT Country='Germany';
SET Operators in SQL
SET operators are special type of
operators which are used
to combine the result of two
queries.
Operators covered under SET
operators are:
1.UNION
2.UNION ALL
3.INTERSECT
4.MINUS
There are certain rules which must be followed to perform
operations using SET operators in SQL. Rules are as follows:
1.The number and order of columns must be the same.
2.Data types must be compatible.

Let us see each of the SET operators in more detail with the help of
examples. Consider we have the following tables with the given data.
Table 1: t_employees Table 2: t2_employees
I Name Department Salar Year_
ID Name Department Salary Year_ D y of_Ex
of_Ex perien
perien ce
ce 1 Prashant Wagh R&D 49000 1
1 Aakash Singh Development 72000 2 2 Abhishek Pawar Production 45000 1
2 Abhishek Pawar Production 45000 1
3 Gautam Jain Development 56000 4
3 Pranav Deshmukh HR 59900 3
4 Shubham Accounts 57000 2
4 Shubham Mahale Accounts 57000 2
Mahale
5 Sunil Kulkarni Development 87000 3 5 Rahul Thakur Production 76000 4
6 Bhushan Wagh R&D 75000 2 6 Bhushan Wagh R&D 75000 2
7 Paras Jaiswal Marketing 32000 1 7 Anand Singh Marketing 28000 1
1. UNION:
•UNION will be used to combine the result of two select
statements.
•Duplicate rows will be eliminated from the results obtained
after performing the UNION operation.
Example 1:
Write a query to perform union between the table t_employees
and the table t2_employees.

 Here, in a single query, we have written two SELECT


queries. The first SELECT query will fetch the records from
the t_employees table and perform a UNION operation with
the records fetched by the second SELECT query from the
t2_employees table.
 You will get the following output:
ID Name Department Salary Year_of_Expe
rience
1 Aakash Singh Development 72000 2
2 Abhishek Production 45000 1
Pawar
3 Pranav HR 59900 3
Deshmukh
4 Shubham Accounts 57000 2
Mahale
5 Sunil Kulkarni Development 87000 3
6 Bhushan R&D 75000 2
Wagh
7 Paras Jaiswal Marketing 32000 1
1 Prashant R&D 49000 1
Wagh
3 Gautam Jain Development 56000 4
5 Rahul Thakur Production 76000 4
7 Anand Singh Marketing 28000 1
Since we have performed union operation between both the tables, so only the records from the
first and second table are displayed except for the duplicate records.
2. UNION ALL
•This operator combines all the records from both the queries.
•Duplicate rows will be not be eliminated from the results
obtained after performing the UNION ALL operation.

ID Name Department Salary Year_of_Experience

1 Aakash Singh Development 72000 2


2 Abhishek Pawar Production 45000 1

3 Pranav Deshmukh HR 59900 3

4 Shubham Mahale Accounts 57000 2

5 Sunil Kulkarni Development 87000 3

6 Bhushan Wagh R&D 75000 2


7 Paras Jaiswal Marketing 32000 1
1 Prashant Wagh R&D 49000 1

2 Abhishek Pawar Production 45000 1

3 Gautam Jain Development 56000 4


4 Shubham Mahale Accounts 57000 2

5 Rahul Thakur Production 76000 4


6 Bhushan Wagh R&D 75000 2
7 Anand Singh Marketing 28000 1
3. INTERSECT:
•It is used to combine two SELECT statements, but it only returns
the records which are common from both SELECT statements.
Example 1:
Write a query to perform intersect operation between the table
t_employees and the table t2_employees.

Here, in a single query, we have written two SELECT queries. The


first SELECT query will fetch the records from the t_employees
table and perform INTERSECT operation with the records fetched
by the second SELECT query from the t2_employees table.
You will get the following output:

ID Name Hometown Percentage Favourite_Subject


2 Abhishek Pawar Production 45000 1
4 Shubham Mahale Accounts 57000 2
6 Bhushan Wagh R&D 75000 2
4.MINUS
•It displays the rows which are present in the first query but
absent in the second query with no duplicates.

ID Name Depart Salary Year_o


Here, in a single query, we
ment f_Expe have written two SELECT
rience queries. The first SELECT
1 Aakash Develo 72000 2 query will fetch the records
Singh pment
from the t_employees table
3 Pranav HR 59900 3 and perform MINUS
Deshm
ukh operation with the records
5 Sunil Develo 87000 3 fetched by the second
Kulkar pment SELECT query from the
ni t2_employees table.You
7 Paras Marketi 32000 1 will get the following
Jaiswal ng
output as shown.
An SQL aggregate function

In database management an aggregate function is a


function where the values of multiple rows are grouped
together as input on certain criteria to form a single value
of more significant meaning.
It calculates on a set of values and returns a single value

Various Aggregate Functions


Count()
 Sum()
 Avg()
 Min()
Max()
Now let us understand each Aggregate function with a example:
Count():
Id Name Salary
1 A 80 Count(*): Returns total number of records .i.e 6.
Count(salary): Return number of Non Null values
2 B 40 over the column salary. i.e 5.
3 C 60 Count(Distinct Salary): Return number of distinct
4 D 70 Non Null values over the column salary .i.e 4
5 E 60
6 F Null
Sum():

sum(salary): Sum all Non Null


values of Column salary i.e., 310
sum(Distinct salary): Sum of all
distinct Non-Null values i.e., 250.
Avg():
Id Name Salary
1 A 80
Avg(salary) = Sum(salary) /
2 B 40
count(salary) = 310/5
3 C 60
Avg(Distinct salary) =
4 D 70
sum(Distinct salary) /
5 E 60
Count(Distinct Salary) = 250/4
6 F Null

Min():

Min(salary): Minimum value in Max(salary): Maximum value in


the salary column except NULL the salary i.e., 80.
i.e., 40.
1. Differentiate between weak entity and strong entity.
2. Give a short note on constraints in relational model
3. Distinguish DDL,DML with syntax and example for each
4. Explain SQL constraints
5. Write about SET operations and aggregate functions in SQL.
6. Discuss various types of joins with examples
7. Explain SQL Logical operators with example for each.
8. Write a SQL query to display three numbers in three columns.
9. Write an SQL query to display the result of an arithmetic expression.

9. Write relational algebra queries


a. Viewing all databases, Creating a Database,
b. Viewing all Tables in a Database,
c. Creating Tables (With and Without Constraints),
d.Inserting/Updating/Deleting
10. Considering the schema
Sailors(sid, sname,rating,age)
Boats(bid,bname,color)
Reserves(sid,bid,day)

Write relational algebraic queries for the following


a)Write names of sailors who have reserved boat #103
b) Write names of sailors who have reserved a Red boat
c) Write names of sailors who have reserved a Red or Green boat
d) Write names of sailors who have reserved all boats

You might also like