You are on page 1of 56

Database Management System

(DBMS)
CAP200
By Priyanka Athia

seema kumari,Astt Proff,SCA 1


DATABASE MANAGEMENT SYSTEM
CAP200
UNIT-2

seema kumari,Astt Proff,SCA 2


What is RDBMS?
RDBMS stands
for Relational Database Management System. RDBMS is
the basis for SQL, and for all modern database systems like
MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access. It uses a structure that allows us to identify and
access data in relation to another piece of data in the
database. Often, data in a relational database is organized
into tables. It is a program that allows you to create,
update, and administer a relational database. Most
relational database management systems use the SQL
language to access the database.
A Relational database management system (RDBMS) is a
database management system (DBMS) that is based on the
relational model as introduced by E. F. Codd.
seema kumari,Astt Proff,SCA 3
What is a table?
The data in a RDBMS is stored in database objects
which are called as tables. This table is basically a
collection of related data entries and it consists of
numerous columns and rows.
Remember, a table is the most common and
simplest form of data storage in a relational
database.

seema kumari,Astt Proff,SCA 4


Columns/fields

Rows/Records/tuples

seema kumari,Astt Proff,SCA 5


What is a field?
• Every table is broken up into smaller entities called fields. The fields in the
CUSTOMERS table consist of ID, NAME, AGE, COUNTRY.
A field is a column in a table that is designed to maintain specific information
about every record in the table.

What is a Record or a Row?


• A record is also called as a row of data is each individual entry that exists
in a table. For example, there are 4 records in the above CUSTOMERS
table. Following is a single row of data or record in the CUSTOMERS table −

seema kumari,Astt Proff,SCA 6


• What is a NULL value?
A NULL value in a table is a value in a field that
appears to be blank, which means a field with a
NULL value is a field with no value.
It is very important to understand that a NULL
value is different than a zero value or a field that
contains spaces. A field with a NULL value is the
one that has been left blank during a record
creation.

seema kumari,Astt Proff,SCA 7


Relational Algebra
Relational algebra is a procedural query language.
It uses operators to perform queries.
It uses various operations to perform this action.
The output of these operations is a new relation,
which might be formed from one or more input
relations.

seema kumari,Astt Proff,SCA 8


Types of Relational operation

seema kumari,Astt Proff,SCA 9


1.Select Operation:
• The select operation selects tuples that satisfy a given predicate.
• It is denoted by sigma (σ).
• Notation: σ p(r)
• Where:
• σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like:
AND OR and NOT. These relational can use as relational operators like =, ≠,
≥, <, >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.

σsubject = "database" and price = "450"(Books)


Output − Selects tuples from books where subject is 'database' and 'price' is 450.

seema kumari,Astt Proff,SCA 10


For example: LOAN Relation
Input:
σ BRANCH_NAME="perryride" (LOAN)

BRANCH_NAME LOAN_NO AMOUNT


Downtown L-17 1000
Redwood L-23 2000
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300

seema kumari,Astt Proff,SCA 11


OUTPUT :

BRANCH_NAME LOAN_NO AMOUNT


Perryride L-15 1500
Perryride L-16 1300

seema kumari,Astt Proff,SCA 12


Project Operation:
•Projection is used project
required column data
from a relation.
•It is denoted by ∏(pi)
Notation: ∏ A1, A2, An (r)
Where
• A1, A2, A3 is used as an attribute name of relation r.
For example −
∏subject, author (Books)
Output-
Selects and projects columns named as subject and
author from the relation Books.
seema kumari,Astt Proff,SCA 13
seema kumari,Astt Proff,SCA 14
Union Operation:
• Suppose there are two tuples R and S. The
union operation contains all the tuples that are
either in R or S or both in R & S.
• It eliminates the duplicate tuples. It is denoted
by 𝖴.
Notation: R 𝖴 S
• A union operation must hold the following
condition:
• R and S must have the attribute of the same
number.
• Duplicate tuples are eliminated automatically.
seema kumari,Astt Proff,SCA 15
seema kumari,Astt Proff,SCA 16
Set Intersection:
• Suppose there are two tuples R and S. The set
intersection operation contains all tuples that
are in both R & S.
• It is denoted by intersection ∩.
Notation: R ∩ S
For example:

seema kumari,Astt Proff,SCA 17


seema kumari,Astt Proff,SCA 18
Set Difference:
• Suppose there are two tuples R and S. The set
intersection operation contains all tuples that
are in R but not in S.
• It is denoted by intersection minus (-).
Notation: R - S

seema kumari,Astt Proff,SCA 19


seema kumari,Astt Proff,SCA 20
Cartesian product
• The Cartesian product is used to combine
each row in one table with each row in the
other table. It is also known as a cross
product.
• It is denoted by X

Notation: E X D

seema kumari,Astt Proff,SCA 21


seema kumari,Astt Proff,SCA 22
Rename Operation:
• The rename operation is used to rename the
output relation. It is denoted by rho (ρ).
• Example: We can use the rename operator to
rename STUDENT relation to STUDENT1.
Syntax with ex: ρ(STUDENT1, STUDENT)

seema kumari,Astt Proff,SCA 23


Join Operations:
• A Join operation combines related tuples from
different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈ (Bowtie).

Salary

seema kumari,Astt Proff,SCA 24


Types of Join operations:

seema kumari,Astt Proff,SCA 25


Natural Join:
• A natural join is the set of tuples of all combinations in R and S
that are equal on their common attribute names.
• It is denoted by ⋈.

Salary

seema kumari,Astt Proff,SCA 26


Outer Join
The outer join operation is an extension of the join operation. It is used to deal with missing information.

seema kumari,Astt Proff,SCA 27


seema kumari,Astt Proff,SCA 28
An outer join is basically of
three types:
• Left outer join
• Right outer join
• Full outer join

seema kumari,Astt Proff,SCA 29


a. Left outer join:

• Left outer join contains the set of tuples of all combinations in R and S that are
equal on their common attribute names.
• In the left outer join, tuples in R have no matching tuples in S.
• It is denoted by ⟕.

seema kumari,Astt Proff,SCA 30


seema kumari,Astt Proff,SCA 31
b. Right outer join:

• Right outer join contains the set of tuples of all combinations in R and S that are
equal on their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• It is denoted by ⟖.
A𝖴B
(A ∩ B) 𝖴 B

seema kumari,Astt Proff,SCA 32


seema kumari,Astt Proff,SCA 33
c. Full outer join:
• Full outer join is like a left or right join except that it contains all rows from both tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples in S that have no matching
tuples in R in their common attribute name.
• It is denoted by ⟗.

seema kumari,Astt Proff,SCA 34


seema kumari,Astt Proff,SCA 35
3. Equi join:
• It is also known as an inner join. It is the most
common join. It is based on matched data as per
the equality condition. The equi join uses the
comparison operator(=).

seema kumari,Astt Proff,SCA 36


seema kumari,Astt Proff,SCA 37
What is SQL?
SQL is a database language designed for the
retrieval and management of data in a relational
database.
SQL is the standard language for database
management. All the RDBMS systems like MySQL,
MS Access, Oracle, Sybase, Postgres, and SQL
Server use SQL as their standard database
language. SQL programming language uses
various commands for different operations. We
will learn about the like DCL, TCL, DQL, DDL and
DML commands in SQL with examples.
seema kumari,Astt Proff,SCA 38
History of SQL
• Here, are important landmarks from the history of SQL:
• 1970 - Dr. Edgar F. "Ted" Codd described a relational
model for databases.
• 1974 - Structured Query Language appeared.
• 1978 - IBM released a product called System/R.
• 1986 - IBM developed the prototype of a relational
database, which is standardized by ANSI.
• 1989- First ever version launched of SQL
• 1999 - SQL 3 launched with features like triggers,
object-orientation, etc.
• SQL2003- window functions, XML-related features, etc.
• SQL2006- Support for XML Query Language
• SQL2011-improved support for temporal databases
seema kumari,Astt Proff,SCA 39
Types of SQL
• Here are five types of widely used SQL
queries.
• Data Definition Language (DDL)
• Data Manipulation Language (DML)
• Data Control Language(DCL)
• Transaction Control Language(TCL)
• Data Query Language (DQL)

seema kumari,Astt Proff,SCA 40


seema kumari,Astt Proff,SCA 41
DDL(Data definition language)
A schema is the collection of multiple database objects, which are known as schema objects and
these objects can be created by using DDL statements.

CREATE Table STATEMENT


Naming Conventions for creating table
The name you choose for a table must follow these standard
rules:
• The name must begin with a letter A-Z or a-z.
• Can contain numbers and underscores
• Can be in UPPER of lower case
• Can be up to 30 characters in length
• Cannot use the same name of another existing object in your
schema
• Must not be a SQL reserved word
seema kumari,Astt Proff,SCA 42
Syntax : Example:
CREATE TABLE table_name CREATE TABLE Persons
( column1 datatype(size), ( PersonID int,
column2 datatype(size), LastName varchar(255),
. FirstName varchar(255),
. Address varchar(255),
columnN datatype(size) City varchar(255)
); );

seema kumari,Astt Proff,SCA 43


ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table . The ALTER TABLE
statement is also used to add and drop various constraints on an existing table.

• Add columns
alter table emp2 add class varchar(20 );
• Remove columns
alter table emp2 drop column class;
• Modify datatype
alter table emp2 modify numberr int;
• Modify datatype length
alter table emp2 add newname varchar(20);
alter table emp2 modify newname varchar(40);
• Add constraints
alter table emp2 add primary key(id);
• Remove constraints
alter table emp2 drop primary key;
• Rename column table
alter table emp2 rename column numberr to roll_no;

seema kumari,Astt Proff,SCA 44


seema kumari,Astt Proff,SCA 45
Drop command
Drop table
• Drop table emp2 ;
Drop column
• alter table emp2 drop column class;
Drop constraint
alter table emp2 drop primary key;

seema kumari,Astt Proff,SCA 46


Select command
• Select * from tablename
To findselect the inserted data!
Select * from emp

***
•Use of both insert and select
This will copy the table1 to table2
Insert into table1(column1,column2,columnN)
Select column,coulmn2,columnN from table2
Insert into table1(id,firstname,lastname)
Select id2,firstname2,lastname from table2

seema kumari,Astt Proff,SCA 47


Condtion:
WHERE Clause
• The WHERE clause is used to filter records.
• The WHERE clause is used to extract only those records that fulfill a
specified condition.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

seema kumari,Astt Proff,SCA 48


Create and insert values !

seema kumari,Astt Proff,SCA 49


copy one table data to another table data!
Syntax:
INSERT INTO table2
SELECT * FROM table1
Eaxmple:
INSERT INTO Customer (CustomerName, City,
Country)
SELECT SupplierName, City,
Country FROM Suppliers;

seema kumari,Astt Proff,SCA 50


Where condition
Copying a particular value of table column by
selecting it from another table!
Syntax:
INSERT INTO table1 (column1, column2,
columnN)
SELECT table2, column1, column2 FROM table2
WHERE column=‘value';
Example:
INSERT INTO Customer (CustomerName, City,
Country)
SELECT SupplierName, City,
Country FROM Suppliers
WHERE Country='Germany';
seema kumari,Astt Proff,SCA 51
UPDATE Statement
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customer
SET ContactName = 'Alfred Schmidt',
City= 'Frankfurt'
WHERE CustomerID = 1;

seema kumari,Astt Proff,SCA 52


UPDATE Multiple Records
Example:
UPDATE Customer
SET ContactName='Jan'
WHERE Country='Mexico';
Update Warning!
Be careful when updating records. If you omit the WHERE clause, ALL
records will be updated!
Syntax:
UPDATE table
SET Column=‘value';
Example:
UPDATE Customer
SET ContactName='Jan';

seema kumari,Astt Proff,SCA 53


DELETE Statement
Syntax:
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Customer
WHERE CustomerName=‘Ana';
Delete All Records
DELETE FROM table_name;
Example:
DELETE FROM Customer;
seema kumari,Astt Proff,SCA 54
TRUNCATE TABLE
Syntax:
TRUNCATE TABLE tablename;

Example:
TRUNCATE TABLE Customer;

seema kumari,Astt Proff,SCA 55


Difference between DELETE, DROP and TRUNCATE
TRUNCATE :
DELETE : DROP :
It is also a Data Definition
Basically, it is a Data It is a Data Definition Language
Language Command
Manipulation Language Command (DDL). It is use to
(DDL). It is use to delete
Command (DML). It is use drop the whole table. With the
all the rows of a relation
to delete the one or more help of “DROP” command we
(table) in one go. With the
tuples of a table. With the can drop (delete) the whole
help of “TRUNCATE”
help of “DELETE” command structure in one go i.e. it
command we can’t delete
we can either delete all the removes the named elements
the single row as here
rows in one go or can of the schema. By using this
WHERE clause is not used.
delete row one by one. i.e., command the existence of the
By using this command
we can use it as per the whole table is finished or say
the existence of all the
requirement or the lost.
rows of the table is lost. It
condition using Where Note –
is comparatively faster
clause. It is comparatively Here we can’t restore the table
than delete command as
slower than TRUNCATE by using the “ROLLBACK”
it deletes all the rows
cmd. command.
fastly . Here we can’t
Note –
restore the tuples of the
Here we can use the
table by using the
“ROLLBACK” command to
“ROLLBACK” command.
restore the tuple. seema kumari,Astt Proff,SCA 56

You might also like