DBT
Assignment_01
1. What is a database ,tuple & Records ?
• Database → A database is an organized collection of related data that can be stored,
managed, and retrieved easily. You can think of it like a digital cupboard or filing
cabinet where information is neatly arranged.
Example: A library database may have tables like Books, Members, and
Borrow_Records.
• Tuple → A tuple is a single row in a table, representing one complete entry of data. In
relational databases, a tuple contains values for all the columns of the table.
Example: In a Books table, a row like (101, 'Database Systems', 1, 2010) is a tuple.
• Record → A record is just another term for a tuple. Both mean the same — one
complete row of information in a table with all its column values.
Example: The same row (101, 'Database Systems', 1, 2010) is also called a record.
“A database stores data in tables. Each row in a table is called a tuple or record, which
together represent one complete entry of information.”
2. What is the difference between a database and a DBMS?
Basis Database DBMS (Database Management System)
A database is an organized DBMS is a software system that helps in
Meaning collection of related data stored creating, managing, and interacting with
in a structured way. databases.
It stores, retrieves, updates, and
Function It only stores data.
manages data in the database.
Active – performs operations on the
Nature Passive – just holds data.
data.
Control Over Does not provide control or Provides security, access control, and
Data access features. concurrency management.
Data High – same data can be repeated Low – data is centralized and managed
Redundancy many times. properly to avoid duplication.
Basis Database DBMS (Database Management System)
Data
Not guaranteed. Ensures data consistency and integrity.
Consistency
Both end users and database
End users usually do not directly
Users administrators use DBMS to interact
access it.
with data.
Backup and DBMS provides automatic backup and
Manual process.
Recovery recovery features.
Software like MySQL, Oracle, SQL
Example A file containing student records.
Server, MS Access.
“A database is like a digital cupboard where data is stored. A DBMS is the software that helps
us open that cupboard, add new data, update it, or delete it safely. DBMS makes working
with the database easy, secure, and efficient.”
“So, in short — the database is where data lives, and the DBMS is how we manage it.”
3. What are the different types of database models?
A Database Model describes the structure of a database —
it tells how data is stored, organized, connected, and accessed.
There are several types of database models, but the main and most used ones are explained
below
1. Hierarchical Database Model
Definition:
In this model, data is organized in a tree-like structure, where each record has a parent-child
relationship.
Each child record can have only one parent, but a parent can have many children.
Example:
Imagine a company structure —
• CEO → Manager → Employee
Here, “CEO” is the root, “Manager” is a child of CEO, and “Employee” is a child of
Manager.
Advantages:
• Very fast data access for hierarchical relationships.
• Easy to understand when relationships are simple.
Disadvantages:
• Difficult to reorganize or modify structure.
• Cannot easily handle many-to-many relationships.
Used In:
Early systems like IBM’s Information Management System (IMS).
2. Network Database Model
Definition:
This model is an extension of the hierarchical model.
Here, a child can have multiple parents, so it supports many-to-many relationships.
Example:
In a university database:
• A student can enroll in many courses, and
• Each course can have many students.
Advantages:
• Handles complex relationships better.
• Faster access due to multiple relationships.
Disadvantages:
• Structure is complex to design and maintain.
Used In:
Systems like IDMS and Raima Database Manager.
3. Relational Database Model (Most Popular)
Definition:
In this model, data is stored in the form of tables (relations) —
each table has rows (tuples) and columns (attributes).
Tables are related using keys (Primary Key, Foreign Key).
Example:
Two tables —
• Students(student_id, name, class)
• Courses(course_id, name, student_id)
The student_id acts as a foreign key connecting both tables.
Advantages:
• Very easy to use and understand.
• Supports SQL for data manipulation.
• Data integrity and consistency are maintained.
Disadvantages:
• Performance can slow down with very large data.
Used In:
MySQL, Oracle, SQL Server, PostgreSQL etc.
4. Object-Oriented Database Model
Definition:
In this model, data is stored as objects, just like in Object-Oriented Programming (OOP).
Each object has data (attributes) and methods (functions).
Example:
A “Student” object might have:
• Attributes → name, roll_no, marks
• Methods → calculateGrade(), showDetails()
Advantages:
• Can store complex data like images, audio, video.
• Works well with object-oriented programming languages like Java or C++.
Disadvantages:
• More complex to design and query compared to relational.
Used In:
db4o, ObjectDB, Versant ODBMS
5. Entity-Relationship (E-R) Model
Definition:
This model represents data in terms of entities (objects) and relationships between them.
It is mostly used for designing a database before implementation.
Example:
• Entities: Student, Course, Teacher
• Relationships: Student enrolls in Course, Teacher teaches Course
Advantages:
• Easy to visualize and plan.
• Helps to design efficient databases.
Disadvantages:
• Not used for storing actual data, only for design.
Used In:
Database design phase using E-R Diagrams.
6. Document Model (NoSQL)
Definition:
This model stores data in documents like JSON or XML instead of tables.
Each document can have different structures.
Example:
A MongoDB document might look like:
"name": "Shruti",
"age": 22,
"skills": ["SQL", "Java"]
Advantages:
• Flexible — no need for fixed table structure.
• Very fast for large, unstructured data.
Disadvantages:
• No strict schema — can lead to inconsistency.
Used In:
MongoDB, CouchDB
7. Key-Value Database Model (NoSQL)
Definition:
Data is stored as key–value pairs, like a dictionary or map.
Each key is unique and maps to a value.
Example:
"student_id": 101
"name": "Shruti"
"course": "DBMS"
Advantages:
• Very simple and extremely fast.
• Scales easily for large applications.
Disadvantages:
• No relationships between data.
• Limited querying capabilities.
Used In:
Redis, DynamoDB, Riak
8. Graph Database Model
Definition:
Stores data in the form of nodes (entities) and edges (relationships).
Best for representing complex relationships like social networks.
Example:
In a social media app:
• Nodes = Users
• Edges = "Follows" or "Friends With"
Advantages:
• Great for relationship-heavy data (like social networks, routes, recommendations).
• Very fast for connected data queries.
Disadvantages:
• More complex than relational models.
Used In:
Neo4j, Amazon Neptune
“A database model defines how data is structured and related.
The main types are — Hierarchical, Network, Relational, Object-Oriented, ER Model, and
NoSQL models like Document, Key-Value, and Graph.
Among these, the Relational Model is the most widely used in modern systems like MySQL
and Oracle.”
4. What is normalization? Why is it important?
• Normalization means organizing the data in a database in a proper and clean way.
• The main goal is to remove duplicate data and make sure the information is stored
only once.
• It divides a large table into smaller connected tables using primary keys and foreign
keys.
• This helps keep the data accurate, simple, and easy to manage.
Example (Easy Example)
Suppose we have one table like this:
Student_ID Student_Name Course Teacher
1 Shruti DBMS Raj
2 Priya DBMS Raj
3 Rohan Java Meena
Here, the teacher Raj is repeated.
If Raj changes his name, we must update it everywhere — this causes repetition and errors.
After Normalization, we make two tables:
Student Table
Student_ID Student_Name Course
1 Shruti DBMS
2 Priya DBMS
3 Rohan Java
Teacher Table
Course Teacher
DBMS Raj
Java Meena
Now there’s no repetition, and the data is clear and easy to update.
Why Normalization is Important
• It removes duplicate data.
• It keeps the data accurate and consistent.
• It makes the database easy to update and manage.
• It saves storage space.
• It avoids errors and confusion in data.
Types (Normal Forms in Simple Words)
1. 1NF (First Normal Form) – Each column should have single (atomic) values.
2. 2NF (Second Normal Form) – Table should be in 1NF and every column must depend
on the whole primary key.
3. 3NF (Third Normal Form) – Table should be in 2NF and no column should depend on
another non-key column.
4. BCNF – A stronger version of 3NF to remove all dependency problems.
“Normalization is a process used to organize data in a database by removing duplicate and
unnecessary data.
It makes the database clean, easy to manage, and helps to keep the information accurate.”
5. Explain the different normal forms (1NF, 2NF, 3NF, BCNF).
Normalization means arranging data properly in tables so there are no duplicates,
and data becomes easy to store and update.
It is done step by step using normal forms.
Each normal form has some rules that make our database more organized.
1NF (First Normal Form)
Rule: Each column should have only one value, not a list or group.
Also, all rows should be unique.
Example (Before 1NF):
Student_ID Name Courses
1 Shruti DBMS, Java
Here, “Courses” has two values — not allowed.
After 1NF:
Student_ID Name Course
1 Shruti DBMS
1 Shruti Java
Now every column has single values → table is in 1NF.
2NF (Second Normal Form)
Rule:
• Table should be in 1NF.
• Every non-key column must depend on the whole primary key, not part of it.
• Removes partial dependency.
Example:
If primary key = (Student_ID, Course),
and column “Student_Name” depends only on Student_ID (not full key),
then we split the table:
Student Table
Student_ID Student_Name
1 Shruti
Course Table
Course Fee
DBMS 5000
Now, every non-key depends on full key → table is in 2NF.
3NF (Third Normal Form)
Rule:
• Table should be in 2NF.
• No column should depend on another non-key column (removes transitive
dependency).
Example:
Student_ID Name City Pincode
1 Shruti Pune 411001
Here, “Pincode” depends on “City”, not directly on “Student_ID”.
So we split it:
Student Table
Student_ID Name City
1 Shruti Pune
City Table
City Pincode
Pune 411001
Now, no non-key depends on another → table is in 3NF.
BCNF (Boyce–Codd Normal Form)
Rule:
• Table should be in 3NF.
• For every dependency (A → B), A must be a key.
• It’s just a stronger version of 3NF that removes the last few problems.
Example:
Teacher Subject Department
Raj DBMS Computer
Meena Java Computer
Here, “Teacher” decides “Subject”, but “Department” is not fully dependent on key.
So we further split it to remove such dependency.
Result: Table follows BCNF.
Easy Summary
Normal Main Rule
Removes
Form
Repeating or multiple Each cell has only one value
1NF
values
Every column depends on whole
2NF Partial dependency primary key
No column depends on another
3NF Transitive dependency non-key
BCNF Remaining anomalies Every determinant must be a key
“Normalization means organizing data into proper tables.
• 1NF removes repeating values,
• 2NF removes partial dependency,
• 3NF removes transitive dependency,
• and BCNF removes all remaining issues.
It helps keep data clean and reduces duplication.”
6. What is denormalization and when should we use it?
• Denormalization is the opposite of normalization.
• Instead of splitting data into many tables, we combine some tables or store
duplicate data on purpose.
• The goal is to make reading or fetching data faster, even if it means having a little
repeated data.
Easy Example
Imagine a library database:
Normalized Tables:
1. Books(Book_ID, Title, Author_ID)
2. Authors(Author_ID, Name)
If you want to see the book with author name, you need to join both tables.
Denormalized Table:
Book_ID Title Author_Name
101 Database Basics Raj
Book_ID Title Author_Name
102 Java Guide Meena
Now you can see book and author directly, no join needed. But “Author_Name”
repeats for multiple books by the same author.
When to Use Denormalization
• When speed of reading data is more important than storage.
• When there are too many joins, which make queries slow.
• In reporting or data warehouse systems, where data changes less often.
• When you want faster performance for complex queries.
“Denormalization means merging tables or allowing some duplicate data to make
data retrieval faster.
We use it when performance is more important than storage, like in reporting or
analytics systems.”
7. What is a primary key and a foreign key?
Primary Key (PK)
Definition:
• A primary key is a column (or a set of columns) in a table that uniquely identifies
each row.
• No two rows can have the same primary key value.
• It cannot be NULL.
Example:
Students Table:
Student_ID (PK) Name Class
1 Shruti 12
2 Priya 11
3 Rohan 12
• Here, Student_ID is the primary key because it uniquely identifies each student.
Key Points: Unique, Not NULL, identifies each row.
Foreign Key (FK)
Definition:
• A foreign key is a column in one table that refers to the primary key in another
table.
• It creates a link or relationship between two tables.
Example:
Courses Table:
Course_ID Course_Name Student_ID (FK)
101 DBMS 1
102 Java 2
103 Python 1
• Here, Student_ID is a foreign key because it refers to Student_ID in the Students
Table.
• It links each course to the student who enrolled in it.
Key Points: Links two tables, can have duplicate values, ensures referential
integrity.
Visual Relationship
Students Table
+------------+-------+
| Student_ID | Name |
+------------+-------+
|1 | Shruti|
|2 | Priya |
+------------+-------+
Courses Table
+-----------+-------------+------------+
| Course_ID | Course_Name | Student_ID |
+-----------+-------------+------------+
| 101 | DBMS |1 |
| 102 | Java |2 |
+-----------+-------------+------------+
• Student_ID in Students Table → Primary Key
• Student_ID in Courses Table → Foreign Key
“A primary key uniquely identifies a row in a table, and a foreign key is used to link
two tables by referring to the primary key of another table.”
8. What are constraints in SQL?
• Constraints are rules that control the kind of data that can go into a table.
• They help keep data correct, clean, and consistent.
• Think of them as rules you set on columns so wrong data cannot be entered.
Common Constraints (Simple Version)
1. PRIMARY KEY → Makes sure each row is unique and not empty.
o Example: Student_ID in a Students table.
2. FOREIGN KEY → Links two tables together, ensures related data exists.
o Example: Student_ID in Courses table refers to Student_ID in Students table.
3. NOT NULL → Column cannot be left empty.
o Example: Name should always have a value.
4. UNIQUE → All values in the column must be different.
o Example: Email column in Users table.
5. CHECK → Makes sure column values follow a condition.
o Example: Age >= 18
6. DEFAULT → Sets a default value if none is provided.
o Example: Status = 'Active'
Super Simple Table Example
Name Age Status
Student_ID (DEFAULT
(NOT Email (UNIQUE) (CHECK
(PK) 'Active')
NULL) >=18)
1 Shruti shruti@[Link] 22 Active
2 Priya priya@[Link] 20 Active
Easy Interview Line to Say
“Constraints are rules we put on table columns to make sure the data is correct,
complete, and consistent.
Examples include Primary Key, Foreign Key, NOT NULL, UNIQUE, CHECK, and
DEFAULT.”
9. What is an index? How does it improve performance?
• An index in SQL is like a book index at the end of a textbook.
• It helps the database find data faster without scanning the entire table.
• Basically, it’s a pointer to the data, making searches quicker.
How It Works
• Without an index: Database looks row by row to find what you want → slow for large
tables.
• With an index: Database uses the index to jump directly to the rows → much faster.
Example
Students Table (without index)
Student_ID Name Age
1 Shruti 22
2 Priya 20
3 Rohan 21
… … …
Query: SELECT * FROM Students WHERE Name='Shruti';
• Without index → Database checks every row.
• With index on Name → Database jumps directly to Shruti’s row.
SQL to create index:
CREATE INDEX idx_name ON Students(Name);
Benefits / How It Improves Performance
1. Faster Searches: Quickly finds rows in large tables.
2. Faster Sorting: Speeds up ORDER BY operations.
3. Faster Filtering: Speeds up WHERE conditions.
Things to Note
• Indexes take extra space in the database.
• Too many indexes can slow down INSERT, UPDATE, DELETE operations.
“An index is like a book’s index — it points to the data so the database can find it
quickly.
It improves performance by making searches, sorting, and filtering much faster,
especially in large tables.”
10. What is a view in SQL?
Simple Meaning
• A view is like a virtual table made from a SELECT query.
• It doesn’t store data — it just shows data that already exists in other tables.
• You can use it like a table to make your queries easier.
Think of it like a saved query that you can open anytime.
Example
Let’s say you have a Books table:
Book_ID Title Author Year
101 DBMS Basics Raj 2010
102 Java Guide Meena 2012
103 Python Notes Raj 2015
Now you only want to see books written by Raj again and again.
Instead of writing the same SELECT query every time, you can create a view.
CREATE VIEW RajBooks AS
SELECT * FROM Books WHERE Author = 'Raj';
Now you can use it like this:
SELECT * FROM RajBooks;
This will show only Raj’s books — like a small virtual table.
Why We Use Views
1. To simplify complex queries
→ You don’t have to rewrite the same joins or filters again.
2. To give limited data access
→ You can hide sensitive columns from users.
3. To make data reading easier
→ Acts like a simple version of a big table.
Important Point
• A view doesn’t store data, it just shows data from the original tables.
• If the base table changes, the view shows the updated data automatically.
“A view is a virtual table based on a SELECT query.
It doesn’t store data but shows data from other tables.
We use it to simplify queries, protect data, and make reports easier.”
11. What are DDL, DML, DCL, and TCL in SQL?
SQL (Structured Query Language) is used to store, manage, and manipulate data in a
database.
To make it easy to use, SQL commands are divided into different categories, based
on what they do.
The main categories are:
1. DDL – Data Definition Language
2. DML – Data Manipulation Language
3. DCL – Data Control Language
4. TCL – Transaction Control Language
1. DDL (Data Definition Language)
➤ Meaning:
DDL commands are used to define or change the structure of the database — like
creating, modifying, or deleting tables and other database objects.
It affects the schema or design of the database, not the actual data.
➤ Common DDL Commands:
Command Description Example
Used to create a new table, CREATE TABLE Students (id
CREATE INT, name VARCHAR(50));
database, view, or index
Used to modify the ALTER TABLE Students ADD
ALTER structure of an existing age INT;
table
Deletes an entire table or DROP TABLE Students;
DROP
database
Removes all records from a
TRUNCATE table but keeps the TRUNCATE TABLE Students;
structure
Changes the name of a RENAME TABLE Students TO
RENAME College_Students;
table
➤ Key Point:
DDL commands are automatically committed, meaning once executed, the changes
cannot be undone.
2. DML (Data Manipulation Language)
➤ Meaning:
DML commands are used to manipulate the data inside the database tables.
These commands deal with inserting, updating, deleting, and retrieving data.
➤ Common DML Commands:
Command Description Example
INSERT INTO Students VALUES
INSERT Adds new data into a table (1, 'Shruti', 20);
UPDATE Students SET age =
UPDATE Modifies existing data 21 WHERE id = 1;
DELETE FROM Students
DELETE Removes data from a table WHERE id = 1;
Retrieves data from one or SELECT * FROM Students;
SELECT
more tables
➤ Key Point:
DML commands can be rolled back (undone) until committed using TCL commands.
3. DCL (Data Control Language)
➤ Meaning:
DCL commands are used to control user access and permissions in a database.
These commands help manage who can view or modify data.
➤ Common DCL Commands:
Command Description Example
Gives a user specific rights to GRANT SELECT, INSERT ON
GRANT Students TO user1;
perform actions
Takes away permissions REVOKE INSERT ON
REVOKE Students FROM user1;
previously granted
➤ Key Point:
DCL ensures database security by controlling access to data and objects.
4. TCL (Transaction Control Language)
➤ Meaning:
TCL commands are used to manage transactions in a database.
A transaction is a group of SQL statements that are executed together as one unit.
Example: When transferring money between accounts, both the debit and credit
should happen — or none at all.
➤ Common TCL Commands:
Command Description Example
Saves all changes made during COMMIT;
COMMIT
the transaction permanently
Undoes changes made in the ROLLBACK;
ROLLBACK
transaction (before commit)
Sets a point in a transaction to SAVEPOINT save1;
SAVEPOINT
roll back to later
RELEASE RELEASE
Removes a savepoint SAVEPOINT save1;
SAVEPOINT
SET SET TRANSACTION;
Begins a new transaction
TRANSACTION
➤ Key Point:
TCL commands maintain data integrity and ensure all operations are completed
successfully.
Summary Table
Category Full Form Purpose Examples
Defines CREATE, ALTER,
Data Definition DROP,
DDL database
Language TRUNCATE
structure
Data INSERT, UPDATE,
Handles data
DML Manipulation DELETE, SELECT
inside tables
Language
Data Control Controls access GRANT, REVOKE
DCL
Language & permissions
COMMIT,
Transaction Manages ROLLBACK,
TCL
Control Language transactions SAVEPOINT
“SQL is divided into four main parts —
DDL defines the structure,
DML manages the data,
DCL controls access, and
TCL handles transactions.
Together, they help in organizing, maintaining, and securing the database efficiently.”
12. Write a SQL query to create a table.
The CREATE TABLE command is used in SQL to create a new table in the database.
You define:
• The table name
• The column names
• The data types for each column
Basic Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
Example:
Let’s create a table named Students.
CREATE TABLE Students (
student_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
course VARCHAR(50),
admission_date DATE
);
Explanation:
Column Name Data Type Meaning
student_id INT Stores unique student ID
Stores student name (up to 50
name VARCHAR(50) characters)
age INT Stores student’s age
course VARCHAR(50) Stores the course name
admission_date DATE Stores the admission date
“We use the CREATE TABLE command to make a new table in the database.
It defines the table name, column names, and the type of data each column will store.”
13. How do you insert data into a table?