DBMS Assignment
DBMS Assignment
1. Database Overview:
o It provides an abstract view of data storage and maintenance, allowing users to interact
without worrying about the underlying complexity.
o Purpose: Efficient data retrieval and modification while maintaining simplicity for the user.
2. Data Abstraction:
o Data Abstraction: Simplifies user interactions by retrieving only the required information
and hiding the technical details of storage and maintenance.
1. Physical Level:
2. Logical Level:
▪ Describes what data is stored and the relationships among the data.
3. View Level:
▪ Customers can view employee names or IDs but not sensitive details
like salaries.
o Instance:
▪ The database's state at a particular moment, reflecting the data inserted, updated, or
deleted.
o Types of Schema:
2. Logical Schema: Design at the logical level, defining the database's structure.
3. Subschema: User-specific views or parts of the database designed at the view level.
4. Database Languages:
▪ Common commands:
▪ Common operations:
▪ Retrieval of information.
▪ Deletion of records.
▪ Procedural DML: Specifies both what data is needed and how to get
it.
▪ Declarative DML: Specifies what data is needed without specifying
how to get it.
5. Key Examples:
o Physical Level: Employee, department, and customer records stored as blocks of consecutive
storage locations.
o View Level: A customer service agent sees employee names and IDs but not sensitive
information like salaries.
2. Data Sharing:
o Allows multiple users to access and share data simultaneously while maintaining integrity.
o Provides automatic backup and recovery mechanisms to safeguard data from loss or
corruption.
o Enforces integrity constraints (e.g., primary keys, foreign keys) and restricts unauthorized
access to sensitive data.
o Setting up database systems requires significant costs for software, hardware, and skilled
personnel.
2. Complexity:
o Managing and maintaining a database system is complex and requires specialized expertise.
3. Performance Overhead:
o Handling large volumes of data or complex queries may lead to slower performance if not
optimized properly.
4. Vulnerability to Attacks:
o Centralized data storage makes databases a target for cyberattacks if proper security
measures are not in place.
1. What is SQL?
• Definition: SQL (Structured Query Language) is used for managing and manipulating relational
databases.
• Key Components of SQL:
o DDL (Data Definition Language): Defines database structure (CREATE, ALTER, DROP).
o DML (Data Manipulation Language): Modifies data (INSERT, UPDATE, DELETE).
o DCL (Data Control Language): Manages user permissions (GRANT, REVOKE).
o TCL (Transaction Control Language): Manages transactions (COMMIT, ROLLBACK).
o View Definition: Creates virtual tables using views.
o Integrity Constraints: Ensures data validity.
o Embedded SQL: SQL can be integrated into programming languages like C, Java.
1. Creating a Database
• Syntax:
• Example:
• Syntax:
• Example:
3. Inserting Data
• Syntax:
• Example:
4. Selecting Data
• Syntax:
• Example:
• Example:
6. Updating Data
• Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
• Example:
UPDATE person_details
SET City = 'Chennai'
WHERE AdharNo = 111;
7. Deleting Data
• Syntax:
• Example:
4. Logical Operators
• Syntax:
6. Altering a Table
• Add a column:
• Example:
• Delete a column:
• Example:
Conclusion
A super key is a set of one or more attributes that can uniquely identify each tuple (row) in a table. A super
key may contain extra attributes that are not necessary for unique identification.
Example
• {RegNo}
• {RollNo, Phone}
• {RollNo, Name, Phone}
• {RegNo, Name, Phone}
Note: {Name, Marks} is NOT a super key because two students can have the same name and marks,
meaning it does not guarantee uniqueness.
A candidate key is a minimal subset of a super key that uniquely identifies each tuple. A candidate key
cannot have redundant attributes.
Example
From the super keys, the minimal sets that uniquely identify each record are:
• {RegNo}
• {RollNo, Phone}
Every candidate key is a super key, but every super key is not necessarily a candidate key.
Example
An alternate key is a candidate key that is not chosen as the primary key.
Example
If we select RegNo as the primary key, then {RollNo, Phone} becomes the alternate key.
Implementation in SQL
ALTER TABLE Student
ADD CONSTRAINT unique_roll_phone UNIQUE (RollNo, Phone);
A foreign key is an attribute (or a group of attributes) in one table that references the primary key in
another table.
Example
Here, StudentRegNo in the Course table is a foreign key referencing RegNo in the Student table.
If a foreign key is used, we cannot insert a StudentRegNo in the Course table unless it already
exists in the Student table.
Data Control Language (DCL) is used to manage user access and permissions in a database. It is
primarily used for granting and revoking privileges on database objects such as tables, views, and
procedures.
DCL Commands
1. GRANT Command
The GRANT statement allows the database administrator to give permissions to users or roles.
Syntax
GRANT privilege(s) ON table_name TO user_name;
Example
Let's say we have a "Students" table:
Now, if we want to allow user 'bhavesh' to SELECT and INSERT data into the "Students" table, we use:
This means:
• Bhavesh can read (SELECT) the data from the "Students" table.
• Bhavesh can insert (INSERT) new records into the "Students" table.
2. REVOKE Command
The REVOKE statement is used to take back privileges from a user or role.
Syntax
REVOKE privilege(s) ON table_name FROM user_name;
Example
Now, Bhavesh:
Seminar Document
1. Introduction
Relational algebra is a procedural query language used in relational databases. It provides a set of
operations to retrieve and manipulate data efficiently.
• The selection operation retrieves rows (tuples) that satisfy a given condition.
• Example:
Given LOAN table:
Input:
arduino
CopyEdit
σ BRANCH_NAME="Perryride" (LOAN)
Output:
• Example:
objectivec
CopyEdit
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
• Notation: R ∪ S
• Example:
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Query: Find all unique customers who have either a loan or an account.
Input:
scss
CopyEdit
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Mayes
Jones
• Notation: R ∩ S
• Example:
Input:
scss
CopyEdit
Output:
CUSTOMER_NAME
Smith
• Notation: R - S
• Example:
Query: Find customers who have loans but do not have accounts.
Input:
scss
CopyEdit
CUSTOMER_NAME
Jones
• Notation: R X S
• Example:
EMP_ID EMP_NAME
1 Smith
2 Harry
DEPT_NO DEPT_NAME
A Marketing
B Sales
Input:
CopyEdit
EMPLOYEE X DEPARTMENT
Output:
1 Smith A Marketing
1 Smith B Sales
2 Harry A Marketing
2 Harry B Sales
• Example:
Input:
scss
CopyEdit
ρ (STUDENT1, STUDENT)
• Example:
EMP_CODE EMP_NAME
101 Stephan
102 Jack
EMP_CODE SALARY
101 50000
102 30000
Input:
CopyEdit
EMPLOYEE ⋈ SALARY
Output:
Outer joins help retain unmatched records from one or both tables.
• Includes all records from the left table and matching records from the right.
Example:
EMP_NAME CITY
Ram Mumbai
Shyam Kolkata
Ravi Delhi
Input:
CopyEdit
EMPLOYEE ⟕ FACTORY_WORKERS
Output:
Note: "Ravi" does not exist in FACTORY_WORKERS, so NULL is placed in the missing columns.
Example:
Input:
CopyEdit
EMPLOYEE ⟖ FACTORY_WORKERS
Output:
Note: "Hari" exists in FACTORY_WORKERS but not in EMPLOYEE, so NULL is placed in the missing
column.
Example:
Input:
CopyEdit
EMPLOYEE ⟗ FACTORY_WORKERS
Output:
Note: Both "Ravi" and "Hari" had missing values, so NULL is placed accordingly.
4. Conclusion
Functional dependency is a key concept in relational database management systems (RDBMS) that defines
a relationship between two attributes in a table. It indicates that the value of one attribute uniquely
determines the value of another attribute.
Notation:
Functional dependencies are crucial for designing efficient database schemas, ensuring data integrity, and
eliminating redundancy.
42 abc CO A4
43 pqr IT A3
44 xyz CO A4
45 xyz IT A3
46 mno EC B2
47 jkl ME B2
From this table, we can determine the following valid functional dependencies:
o Each student has a unique roll_no that determines their name, dept_name, and
dept_building.
2. roll_no → dept_name
o Since roll_no determines the entire set {name, dept_name, dept_building}, it also
determines its subset, dept_name.
3. dept_name → dept_building
• name → dept_name
o Students with the same name can be in different departments, so this dependency is not
valid.
• dept_building → dept_name
o Multiple departments can be in the same building (ME and EC are in B2), making this
dependency invalid.
Example:
• ABC → AB
• roll_no, name → name (Since name is already part of {roll_no, name}, it's trivial)
Example:
Example:
Example:
Example:
42 abc CO 4
43 pqr EC 2
44 xyz IT 1
• enrol_no → dept
• dept → building_no
Example:
101 P1 Alpha
102 P2 Beta
Example:
1 CS101 Prof. A
2 CS102 Prof. B
Conclusion
Functional dependencies play a crucial role in database normalization and ensure data consistency and
accuracy. They help identify redundancies and anomalies, leading to better database design.
This topic is fundamental for database normalization (1NF, 2NF, 3NF, BCNF, etc.), ensuring minimal
redundancy and efficient querying.
ER Diagrams in DBMS
An Entity-Relationship (ER) Diagram represents the logical structure of a database graphically. It is used
to model real-world objects (entities) and their relationships in a structured manner.
Features of ER Model
1. Graphical Representation – ER diagrams visually represent database relationships.
2. Real-World Modeling – They model objects like persons, companies, projects, etc..
3. No Technical Knowledge Required – Even a non-technical user can understand ER diagrams.
4. Ease of Conversion – ER diagrams can be easily converted into relational tables.
5. Standard Notation – ER diagrams provide a structured way to represent logical relationships.
Components of an ER Diagram
1. Entity
Example of Entities
+-------------+
| Student |
+-------------+
| Student_ID |
| Name |
| Age |
+-------------+
+------------+
| Course |
+------------+
| Course_ID |
| Course_Name|
+------------+
2. Relationship
Example of Relationship
1. One-to-One (1:1)
ER Diagram:
+-----------------+ +-------------+
| Project_Manager | | Project |
+-----------------+ +-------------+
| 1 | 1
---------------------
2. One-to-Many (1:M)
ER Diagram:
+-----------+ +--------+
| Customer | | Order |
+-----------+ +--------+
| 1 | M
-------------------
3. Many-to-One (M:1)
• Definition: Multiple entities in set A can be related to one entity in set B.
• Example: Many students enroll in one Computer Science course.
ER Diagram:
+----------+ +--------------------+
| Student | | ComputerSciCourse |
+----------+ +--------------------+
| M | 1
------------------------
4. Many-to-Many (M:N)
ER Diagram:
+----------+ +--------+
| Teacher | | Student|
+----------+ +--------+
| M | N
-------------------
Example:
A Player needs a Team to be identified uniquely.
+--------+ +-------+
| Player | | Team |
+--------+ +-------+
| Name | | Team_ID |
| Number | | Name |
+--------+ +-------+
| | 1
-------------------
| M (Weak Entity)
1. A weak entity set has one or more many-one relationships to a supporting entity set.
2. A weak entity’s key is formed using its own attributes + supporting entity’s primary key.
o Example: The key for Player = (Player-Number, Team-Name).
Conclusion
• ER diagrams visually represent the logical structure of a database.
• Different types of relationships (1:1, 1:M, M:1, M:N, ternary) define how entities interact.
• Weak entities depend on strong entities for unique identification.
Normalization
Normalization is the process of organizing data in a database to reduce redundancy and ensure data
integrity. It involves dividing large tables into smaller ones and defining relationships between them.
Issues:
Rule: No repeating or multi-valued attributes (each field must contain atomic values).
Fixed multi-valued attribute issue by creating separate rows for each phone number.
Rule: Must be in 1NF, and no partial dependencies (all non-key attributes must be fully dependent on the
entire primary key).
Decomposed Tables:
Student Table
StudentID Name
101 Alex
102 Ben
103 Alex
Course Table
Course Instructor
Student_Course Table
StudentID Course
101 Math
102 Science
103 Physics
Rule: Must be in 2NF, and no transitive dependencies (non-key attributes should not depend on another
non-key attribute).
Decomposed Tables:
StudentID Name
101 Alex
102 Ben
103 Alex
Course Instructor
101 Math
102 Science
103 Physics
Phone Table
101 9876543210
101 9123456789
102 9765432101
103 9876543210
103 9123456789
Rule: Must be in 3NF, and for every functional dependency (X → Y), X should be a superkey.
Issue: A student can enroll in multiple courses and also have multiple phone numbers.
Decomposed Tables:
StudentID Name
101 Alex
102 Ben
103 Alex
101 Math
102 Science
103 Physics
101 9876543210
101 9123456789
102 9765432101
103 9876543210
103 9123456789
• table is in 5NF (Projection-Join Normal Form) if it is already in 4NF and cannot be further
decomposed without losing data.
• It resolves join dependencies, meaning that if a table can be split into smaller tables and rejoined
without data loss, it should be decomposed.
In 4NF, we have:
If students are also assigned projects based on their courses, we may store:
StudentID Course Project
101 Math P1
101 Math P2
102 Science P3
103 Physics P4
Here, a student can have multiple courses and multiple projects, creating a multi-value dependency
between StudentID, Course, and Project.
Now, instead of storing Student-Course-Project in one table, we store them separately and join them when
needed.
1. Student Table
StudentID Name
101 Alex
102 Ben
103 Alex
2. Course Table
Course Instructor
3. Student_Course Table
StudentID Course
101 Math
102 Science
103 Physics
4. Student_Phone Table
101 9876543210
101 9123456789
102 9765432101
103 9876543210
103 9123456789
5. Course_Project Table
Course Project
Math P1
Math P2
Science P3
Physics P4
6. Student_Project Table
StudentID Project
101 P1
101 P2
102 P3
103 P4
• Now, StudentID, Course, and Project are managed in separate tables, eliminating redundancy.
• The original Student-Course-Project table is now broken down into three smaller tables without
losing data.
An aggregate function in SQL performs a calculation on a set of values and returns a single scalar value.
These functions are commonly used with the GROUP BY clause to summarize data in a database.
Built-in Aggregate Functions in SQL
5. COUNT (Count) – Returns the total number of values in a column, including or excluding NULL
values.
1 Alice 50000 HR
2 Bob 60000 IT
3 Charlie 55000 IT
4 David 70000 HR
5 Eve 65000 IT
sql
CopyEdit
Output: 60000
2. Minimum Salary:
sql
CopyEdit
Output: 50000
3. Maximum Salary:
sql
CopyEdit
sql
CopyEdit
Output: 300000
sql
CopyEdit
Output: 5