Database System

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

F.Y.

CS
Database System Practical

Practical No:01

For given scenario


• Draw E-R diagram and convert entities and relationships to table.

NOTE: Read this Entity Relationship Model.pdf

Entity list- Student, Department, Faculty, Hostel, Course

Entities and relationships to table:


Department table:
DNumber DepartmntName
101 Computer Science
102 Information Technology
103 Biotech

Course table:
CourseNo CourseName NumberOfCredit
201 FOSS 2
201 Database Systems 2
203 Green Technology 2
Same as above for remaining entities student Hostel and faculty

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:02

Write relational algebra queries on the tables created in Practical-1.


Relational Algebra:
Relational algebra is a procedural query language, which takes instances of relations as input and yields
instances of relations as output. It uses operators to perform queries. An operator can be either unary or
binary.
The fundamental operations of relational algebra are as follows –
• Selection
• Projection
• Set Operations
1. Union
2. Intersection
3. Set different
4. Cartesian product
• Join
• Division

Selection: (σ)
It selects tuples that satisfy the given predicate/condition from a relation.
Notation – σp (r)
Where σ stands for selection predicate, r stands for relation/table and p stands for predicate/condition.
For example −

σsubject = "database"(Books)
Output − Selects tuples from books table where subject is 'database'.

Projection: (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2, An are attribute names of relation/table r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −

∏subject,author (Books)
Output - Selects and projects columns named as subject and author from the relation Books.

Set Operations:
1. Union: (∪)
It performs binary union between two given relations/tables and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Where r and s are database relations/tables.
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

For example-

∏author (Books) ⋃ ∏author (Articles)


Output − Projects the names of the authors who have either written a book or an article or both.

2. Intersection: (⋂)
It performs binary intersection between two given relations/tables and is defined as −
r ⋂ s = { t | t ∈ r and t ∈ s}
Notation – r ⋂ s
Where r and s are database relations/tables.
For a Intersection operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
For example-

∏author (Books) ⋂ ∏author (Articles)


Output − Projects the names of the authors who have written both a book and an article.

3. Set Difference: (−)


The result of set difference operation is tuples, which are present in one relation but are not in the second
relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
For example-

∏author (Books) − ∏author (Articles)


Output − Provides the name of authors who have written books but not articles.

4. Cross Product/Cartesian Product: (Χ)


Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
For example-

σauthor=’abc’(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by abc.

Join:
combinations of the Cartesian product which satisfy certain situations and so you can normally use a Join
operation instead of the Cartesian product operation.
There are various types of Join operation, each with subtle differences some more useful than others:

1. Conditional join

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Conditional join works similar to natural join. In natural join, by default condition is equal between
common attribute while in conditional join we can specify the any condition such as greater than, less
than, not equal etc.

For example:
Table-R Table-S

ID Sex Marks ID Sex Marks


1 F 45 10 M 20
2 F 55 11 M 22
3 F 60 12 M 59

Join between R And S with condition

R ⋈R.marks >= S.marks S


Output:
R.ID R.Sex R.Marks S.ID S.Sex S.Marks
1 F 45 10 M 20
1 F 45 11 M 22
2 F 55 10 M 20
2 F 55 11 M 22
3 F 60 10 M 20
3 F 60 11 M 22
3 F 60 12 M 59

2. Equijoin
Equijoin combines tuples from different relations provided they satisfy the theta condition. The join
condition is denoted by the symbol c.
Notation - R1 ⋈c R2, R1 and R2 are relations having attributes (A1, A2,.., An) and (B1, B2,.. ,Bn) such
that the attributes don’t have anything in common, that is R1 ∩ R2 = Φ(NULL/EMPTY).

For example:
Student Subjects

SID Name Std Class Subject

101 Alex 10 10 Math

102 Maria 11 10 English

11 Music

11 Sports

STUDENT ⋈student.std=subject.class SUBJECT

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Output:

Student_detail

SID Name Std Class Subject

101 Alex 10 10 Math

101 Alex 10 10 English

102 Maria 11 11 Music

102 Maria 11 11 Sports

3. Natural join:
Natural join does not use any comparison operator. It does not concatenate the way a Cartesian product
does. We can perform a Natural Join only if there is at least one common attribute that exists between
two relations. In addition, the attributes must have the same name and domain.

Natural join acts on those matching attributes where the values of attributes in both the relations are
same.
Courses HoD

CID Course Dept


Dept Head

CS01 Database CS
CS Alex

ME01 Mechanics ME
ME Maya

EE01 Electronics EE
EE Mira

Courses ⋈ HoD
Output:

Dept CID Course Head

CS CS01 Database Alex

ME ME01 Mechanics Maya

EE EE01 Electronics Mira

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Division:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:03

Perform the following:

1. Viewing all databases:

2. Creating a Database:

3. Creating Tables:

4. Viewing all Tables in a Database:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

5. Inserting values in table:

6. Updating table value:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

7. Deleting Records in a Table:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:04

Perform the following:

Altering a Table:

Truncating table:

Renaming Tables:

Dropping table:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Backing up / Restoring a Database :

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:05

Perform the following:

1. Simple Queries with Aggregate functions:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

2. Queries with Aggregate functions (group by and having clause):

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:06

Queries involving:

1. Date Functions:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

2. String Functions:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

3. Math Functions:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:07

Join Queries:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Inner Join:

Outer Join:
1. Left Join

2. Right Join:

3. Full Join:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:08

Subqueries:

With IN clause:

With EXISTS clause:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:09

Views

Creating Views:

Selecting from a view:

Dropping views:

Ms. R. B. Gaikwad
F.Y.CS
Database System Practical

Practical No:10

DCL statements

Granting and revoking permissions:


Create user
Grant and revoke prvileges

Show Current user and drop/delete user:

Ms. R. B. Gaikwad

You might also like