2024
Database Management System
Database Management System
Introduction to DBMS
A Database Management System (DBMS) is a software system designed to store, retrieve, and
manage data efficiently. The system provides users with tools to create, modify, and extract data
from a database in a systematic way. The main objective is to ensure data is stored in an
organized manner, allowing for easy access, updates, and deletion.
Key Concepts:
Data: Raw facts and figures about any entity like people, products, or activities.
Examples include names, numbers, and dates.
Information: Processed data that is meaningful and useful for decision-making. For
example, marks scored by students and their roll numbers are data, while the report
card is the information.
DBMS vs. Traditional File Systems:
In traditional systems, data was stored in multiple files with various formats. The lack of
structure led to issues such as data redundancy, inconsistency, and difficulty in data access.
DBMS solves these issues by integrating data into a central system that users and applications
can interact with more efficiently.
Advantages of DBMS:
1. Organized Storage: Data in DBMS is stored in a structured format, which ensures quick
and accurate retrieval.
2. Data Analysis: DBMS allows complex data analysis, such as calculating sums, averages,
or finding the maximum and minimum values.
3. Data Sharing: A database can be shared across different applications, meaning it’s
created once and reused multiple times.
4. Minimal Data Redundancy: DBMS reduces repetition of data across tables, maintaining
efficiency and reducing storage costs.
5. Data Consistency: When one data item is updated, the DBMS ensures that all related
tables reflect the change, maintaining consistency across the system.
6. Increased Efficiency and Accuracy: DBMS ensures faster data operations, as well as
more accurate data storage and retrieval.
7. Security: DBMS supports user authentication, passwords, and encryption to ensure that
only authorized users can access or manipulate data.
Data Models in DBMS:
Data models define how data is structured, stored, and accessed in a DBMS. There are several
models, but the most important ones include:
Hierarchical Data Model: Organizes data in a tree-like structure. Each record has a
parent-child relationship. For example, a company might store employee records in a
hierarchy.
Network Data Model: Allows more complex relationships by enabling multiple records
to be linked to the same parent record. This model is a more flexible version of the
hierarchical model.
Relational Data Model: The most widely used model in modern DBMS, it organizes
data in tables (also known as relations). Each table is a collection of related data items in
Database Management System
rows and columns, and relationships between tables are established using keys
(Primary and Foreign keys).
Summary of Relational Data Model:
1. Entity: A real-world object, such as a student or an employee.
2. Attributes: The properties of the entity, such as name, age, or salary.
3. Tables: A collection of entities represented in rows and columns.
4. Keys: Special attributes that uniquely identify rows in a table (Primary Key) or create
links between tables (Foreign Key).
Relational Database Concepts
A Relational Database Management System (RDBMS) organizes data into tables, with each table
consisting of rows and columns. The data is structured in a way that relationships between different
data entities are easily established using keys. This model is particularly useful for organizing large
amounts of data in a structured format that allows for easy access and manipulation.
Key Elements of RDBMS:
Entity: A real-world object about which data is stored in a table. For example, a "Student"
can be an entity in a school database.
Attributes: The characteristics or properties of an entity. For example, a "Student" entity may
have attributes like Name, Roll Number, and Date of Birth.
Table (Relation): A collection of related records represented in rows and columns. For
example, a "Student" table might contain data about different students, with each row
representing a different student and each column representing an attribute (Name, Roll
Number, etc.).
Record (Row): A single entry in a table that contains data for all attributes related to one
specific entity.
Field (Column): A vertical entity in a table that holds data about a particular attribute for all
records. For instance, in a "Student" table, the "Name" field will contain the names of all
students.
Primary Key: A field (or combination of fields) that uniquely identifies a record in a table.
The primary key ensures that no two records in the table have the same value for that field.
For example, the Roll Number in a student table could be a primary key, ensuring that no two
students share the same Roll Number.
Foreign Key: A field in one table that creates a link between two tables. The foreign key is a
field in the transaction table that refers to the primary key of another table (often called the
master table). For example, in a "Student Marks" table, the Roll Number might be the foreign
key, referring to the Roll Number in the "Student" table.
Relational Database Objects
Relational databases support several objects that help users interact with data in various ways. These
objects include tables, forms, queries, and reports.
1. Tables: The foundation of a relational database. They store data in rows and columns, with each
row representing a record and each column representing an attribute. Tables are the primary means of
storing data in a DBMS.
2. Forms: A form is an interface that allows users to interact with the data in a user-friendly manner.
Instead of directly entering data into a table, users can use forms, which provide a more structured and
Database Management System
easy-to-navigate input screen. Forms contain various fields like text boxes, radio buttons, checkboxes,
and dropdown lists to make data entry more intuitive.
3. Queries: Queries are used to retrieve specific data from a database based on certain criteria. For
example, you might want to find all students who scored more than 50 marks in a subject. Queries can
be simple (retrieving data from a single table) or complex (involving multiple tables and conditions).
Example Query: A query to fetch all students who scored more than 50 marks might look
like this:
SELECT StudentName, Marks FROM StudentMarks WHERE Marks > 50;
4. Reports: Reports are used to format and display the output of queries in a more structured,
printable manner. While queries return raw data, reports can present this data in a more visually
appealing way, often with tables, charts, or graphs. This feature is particularly useful for generating
summaries, invoices, or official records.
Example of a Relational Database Setup:
Let's consider a school database with two tables: "Students" and "Marks."
Students Table:
o Roll Number (Primary Key)
o Name
o Class
o Date of Birth
Marks Table:
o Roll Number (Foreign Key)
o Subject
o Marks
In this example, the "Roll Number" field in the "Marks" table is a foreign key, establishing a
relationship between the "Marks" table and the "Students" table. This relationship ensures that each
student’s marks can be accurately linked to the correct student in the "Students" table.
Setting Relations between Tables
In relational databases, relations between tables are set using primary and foreign keys. Once a
relationship is established, data integrity is maintained across multiple tables, ensuring that records are
correctly linked.
One-to-One Relationship: A record in one table is linked to only one record in another table.
For example, each student in a "StudentDetails" table has only one corresponding record in a
"StudentContact" table.
One-to-Many Relationship: One record in the master table is linked to multiple records in a
transaction table. For instance, a teacher in a "Teachers" table might be linked to multiple
students in a "Students" table.
Creating and Managing Databases in LibreOffice Base
LibreOffice Base is a free, open-source database management system that supports creating,
managing, and interacting with databases. It allows users to create databases, enter data, retrieve data
through queries, and present data in forms and reports. This section will guide you through the steps
of setting up a database, managing tables, and using the interface efficiently.
Database Management System
Creating a New Database in LibreOffice Base
Follow these steps to create a new database:
1. Starting LibreOffice Base:
o Open the LibreOffice application.
o Select "Base" from the available options.
o Alternatively, type "Base" in the search box of your operating system to launch
LibreOffice Base.
2. Database Wizard:
o The first screen of the Database Wizard allows you to either create a new database or
open an existing one. Select "Create a new database."
o Click "Next."
3. Registering the Database:
o The wizard asks if you want to register the database with LibreOffice. This step is
optional, and it makes the database accessible to other LibreOffice applications.
Choose "No, do not register the database."
o Click "Finish."
4. Saving the Database:
o After setting up the database, you need to save it. A dialog box will appear where you
can choose a location and name for your database file (e.g., "[Link]").
o Click "Save."
5. User Interface Overview:
o Title Bar: Displays the name of the database and the LibreOffice Base application.
o Menu Bar: Contains menus like File, Edit, View, Insert, and Tools that help in
performing various database operations.
o Database Pane: Located on the left side, this pane shows different database objects
like Tables, Forms, Queries, and Reports.
o Object Area: Displays the created objects (tables, queries, etc.) when a specific
database object is selected.
o Tasks Pane: Provides options to create new objects like tables or forms.
Working with Data Types
When creating a table in LibreOffice Base, it’s essential to define the types of data that will be stored
in each field. Data types ensure that the data is stored correctly and helps in maintaining data integrity.
Common Data Types:
1. Text Data Type: Used for alphanumeric characters.
o CHAR: Stores fixed-length strings.
o VARCHAR: Stores variable-length strings.
o LONGVARCHAR: Stores longer text entries, useful for descriptive fields.
o Example: A field for "Student Name" would use the VARCHAR data type, which
allows varying lengths of names.
2. Numeric Data Types: Used for storing numbers, including integers and decimals.
o INTEGER: Stores whole numbers.
o DECIMAL: Stores fixed-point numbers.
o FLOAT and REAL: Used for floating-point numbers that represent fractions.
o Example: "Marks" or "Age" fields in a student table would use INTEGER or
DECIMAL, depending on the nature of the data.
3. Date/Time Data Types: Used for storing date and time values.
o DATE: Stores the date (e.g., 2024-10-10).
Database Management System
o TIME: Stores the time (e.g., 12:30 PM).
o TIMESTAMP: Stores both date and time.
o Example: "Date of Birth" would use the DATE data type.
4. Boolean Data Type: Used for storing true/false or yes/no values.
o Example: A field like "Active Student" could use the BOOLEAN data type to
indicate whether a student is currently enrolled.
5. Binary Data Type: Used to store binary data such as images, audio, or files.
o Example: You could store scanned documents or photos as binary data.
Creating a Table in LibreOffice Base
Once you’ve created a database, the next step is to create tables to store data. You can create tables
either using a wizard or the design view.
Creating a Table Using the Wizard:
1. From the main database screen, select "Use Wizard to Create Table" from the Task Pane.
2. Table Wizard opens, providing options for sample tables. You can choose a pre-built table
structure or create your own custom table.
3. Available Fields: Select fields from the left column and move them to the right column using
the arrow buttons.
4. Define Primary Key: Choose the field that will serve as the unique identifier (primary key)
for the table. This ensures no duplicate records for that field.
5. Finish: Once you have configured the fields and set the primary key, click "Finish" to create
the table.
Creating a Table in Design View:
The Design View offers more flexibility, allowing you to define custom fields, data types, and
properties without relying on the wizard.
1. From the main screen, select "Create Table in Design View" from the Task Pane.
2. Field Name: In the first column, enter the field names (e.g., Roll Number, Name, and Class).
3. Field Type: Choose the appropriate data type for each field (e.g., INTEGER for Roll
Number, VARCHAR for Name).
4. Field Properties: You can also set specific properties for each field, such as whether the field
is required or its default value.
5. Primary Key: Right-click on the field you want to make the primary key (e.g., Roll Number),
and select "Primary Key."
6. Saving the Table: Once the design is complete, click the save icon, name the table (e.g.,
"Students"), and click "OK."
Entering Data into Tables
After creating tables, you can enter data into them either directly in the table view or through forms.
1. Opening the Table: In the object area, double-click the table you wish to enter data into.
2. Entering Data: Enter the data row by row. Use the Tab key to move to the next field. Once
you finish a row, the cursor will move to the next row, allowing you to continue entering
records.
3. Saving Data: Data is automatically saved once entered, but you can manually save the table
by pressing Ctrl+S or using the save icon.
Database Management System
Working with Multiple Tables and Relationships in DBMS
When managing complex data in a relational database, we often need to break it down into multiple
related tables. These relationships allow data to be organized efficiently and ensure that duplication of
data (redundancy) is minimized. LibreOffice Base, like any RDBMS, supports relationships between
tables, enabling you to maintain a structured and integrated database.
Setting Relationships Between Tables
A relationship in a database is a connection between two or more tables based on a common field.
The field that establishes this connection is usually the foreign key in one table, referencing the
primary key in another table.
Example:
Consider a school database with two tables:
1. Students Table:
o Roll Number (Primary Key)
o Name
o Class
2. Marks Table:
o Roll Number (Foreign Key)
o Subject
o Marks
Here, the "Roll Number" field in the Marks Table is a foreign key that references the "Roll Number"
field in the Students Table. This relationship ensures that every mark entry corresponds to a valid
student in the database.
Types of Relationships
There are three main types of relationships in relational databases:
1. One-to-One (1:1) Relationship:
In a one-to-one relationship, each record in one table corresponds to only one record in
another table. This type of relationship is less common in practice but is used when there’s a
strict one-to-one mapping between two entities.
Example: A person’s passport and their personal details. Each person has one unique passport, and
each passport belongs to only one person.
2. One-to-Many (1
) Relationship:
This is the most common type of relationship. In a one-to-many relationship, one record in the
master table corresponds to multiple records in the transaction table.
Database Management System
Example: A teacher and their students. One teacher can teach multiple students, but each student can
have only one teacher (in this specific context). Similarly, one student can appear in multiple courses,
creating a one-to-many link between the student and their subjects.
Visual Example:
Table 1: Students
Student_ID Name
1 Rahul
2 Maya
Table 2: Courses
Course_ID Course_Name
C101 Mathematics
C102 English
Table 3: Student_Course (Junction Table)
Student_ID Course_ID
1 C101
1 C102
2 C102
This setup allows a student to enroll in multiple courses and a course to have multiple students.
Creating Relationships in LibreOffice Base
You can establish relationships between tables in LibreOffice Base by using a graphical interface that
allows you to visually link tables through their common fields. Here’s how to do it:
1. Open the Relationships Window:
o From the main LibreOffice Base menu, click on Tools and select Relationships.
2. Add Tables:
o In the Relationships window, click Add Tables. Select the tables you want to relate
and add them to the workspace.
3. Create the Relationship:
o To create a relationship, drag the primary key from one table and drop it onto the
corresponding foreign key in the related table. For example, you can drag
"Student_ID" from the "Students" table to the "Student_ID" field in the "Marks"
table.
4. Set Relationship Options:
o A dialog box will appear allowing you to set the properties of the relationship (e.g.,
"Enforce Referential Integrity"). Confirm the relationship, and it will be displayed as
a line connecting the two tables, labeled "1" and "∞" (for one-to-many relationships).
Referential Integrity
Database Management System
Referential integrity is a fundamental principle that ensures the consistency of data between related
tables. When referential integrity is enforced, the DBMS ensures that foreign key values in a
transaction table must correspond to valid primary key values in the master table. If a related record is
deleted in the master table, the DBMS prevents orphaned records in the transaction table unless
specific actions are defined.
Options for Maintaining Referential Integrity:
1. No Action: Prevents deletion or update of a record in the master table if related records exist
in the transaction table.
2. Cascade Update/Delete: Automatically updates or deletes related records in the transaction
table when changes are made to the master table. For example, if a student’s record is deleted,
all their related marks entries are also deleted.
3. Set Null: If the corresponding master record is deleted, the foreign key values in the
transaction table are set to NULL.
4. Set Default: When a master record is deleted or updated, the foreign key values in the
transaction table are set to a predefined default value.
Example of Referential Integrity in Action:
In a database managing student records:
If a student (record) is deleted from the Students Table, referential integrity ensures that any
corresponding records in the Marks Table are handled appropriately (e.g., deleted or set to
null), ensuring that there are no orphaned entries.
Advantages of Setting Relationships in a Database:
1. Prevents Data Redundancy: By storing data in separate, related tables, the same data does
not need to be repeated.
2. Maintains Data Integrity: Relationships ensure that data remains consistent across tables.
Any update in the master table is reflected in the transaction table.
3. Efficient Data Retrieval: Queries across multiple tables become simpler and more efficient
when relationships are properly set up.
4. Enhanced Security: Relationships can be used to enforce access rules, ensuring that only
authorized users can modify data in linked tables.
Key Visuals and Tables from the DBMS Chapter
1. Relational Database Example:
Table 1: Students
Roll Number Name Class Date of Birth
101 Rahul 10 12/04/2004
102 Maya 9 23/06/2005
Table 2: Marks
Roll Number Subject Marks
101 Math 78
Database Management System
Roll Number Subject Marks
101 English 85
102 Math 65
Relationship:
o The Roll Number field in the "Marks" table is a foreign key linking back to the Roll
Number in the "Students" table. This establishes a one-to-many relationship where
one student can have many subjects and marks associated with them.
Visual Representation:
mathematica
Copy code
Students Table Marks Table
------------- ------------
Roll Number 101 ----> Roll Number 101, Subject: Math
Name: Rahul Marks: 78
Class: 10 Subject: English
Marks: 85
This kind of visual helps illustrate the concept of one-to-many relationships, and it’s crucial for exam
purposes to understand how to connect tables using primary and foreign keys.
2. Example of a Primary and Foreign Key:
Primary Key: A unique identifier for records in a table.
o Example: In the "Students" table, the "Roll Number" is the primary key.
Foreign Key: A field in one table that refers to the primary key in another table.
o Example: In the "Marks" table, the "Roll Number" is a foreign key that links back to
the "Roll Number" in the "Students" table, establishing a relationship between the
two tables.
3. Data Models:
Hierarchical Data Model:
o Organizes data in a tree-like structure. Each record is linked to a parent record,
forming a hierarchy.
Example Visual:
Copy code
Company
├── Department
│ └── Employees
│ └── Projects
This model works well for scenarios where there is a clear parent-child relationship.
Database Management System
Network Data Model:
o Similar to the hierarchical model but allows multiple parent-child links.
Example Visual:
css
Copy code
Projects
├── Employee 1
└── Employee 2
├── Project A
└── Project B
It’s more flexible, allowing for more complex relationships.
Relational Data Model:
o Data is organized in tables (relations) and connected through keys.
Visual:
css
Copy code
Students Table <---------> Marks Table
This is the most commonly used model today due to its simplicity and efficiency in data
retrieval.
4. Referential Integrity and Relationships Visual:
One-to-One:
One entity in Table A corresponds to exactly one entity in Table B.
Example:
sql
Copy code
Employee Table Employee_Passport Table
Employee_ID 1 -------> Employee_ID 1 (unique)
One-to-Many:
One entity in Table A corresponds to multiple entities in Table B.
Example:
sql
Copy code
Teacher Table Students Table
Teacher_ID 101 ------> Student_ID 1, 2, 3 (many)
Many-to-Many:
Many entities in Table A correspond to many entities in Table B.
Database Management System
Example:
sql
Copy code
Students Table Courses Table
Student_ID 1 -------> Course_ID 101, 102
Extra
1. Data Types (With Examples)
In LibreOffice Base, there are various data types, each with specific use cases. Below are detailed
examples:
TINYINT: Used to store small integers. It occupies 1 byte, ranging from 0 to 255. Example:
A field storing age where values are small integers.
SMALLINT: Stores medium-sized integers within the range -32,768 to 32,767. Example: A
field for storing the number of items in stock.
INTEGER: Stores larger integer values ranging from -2.14e9 to 2.14e9. Example: Used in
ID fields like Student ID or Product ID.
BIGINT: Stores very large integer values, useful for scenarios requiring large numeric
ranges. Example: Financial transactions involving large sums, ranging from -2.3e18 to
2.3e18.
DECIMAL/NUMERIC: Both can store precise decimal values without rounding errors.
Example: Used for currency data, like product prices, to ensure accuracy.
VARCHAR: Variable-length text storage. Example: A field for storing customer names.
DATE/TIME: Used to store date or time values, respectively. Example: "Date of Birth"
fields, meeting time schedules.
TIMESTAMP: Stores both date and time together. Example: Used for logging events with
exact time data.
2. LibreOffice Base Features
Creating a Table Using Design View: Design View allows you to manually define fields,
data types, and constraints. This is useful when you need more flexibility in your table
creation. Steps:
o Open LibreOffice Base.
o In the task pane, click Create Table in Design View.
o Define each field's name, type, and constraints (e.g., primary keys or default values).
o After defining the fields, save the table with an appropriate name.
Opening a Database:
o Use File > Open or Ctrl + O to browse and select an existing database. The database
opens, showing the tables, forms, and queries you previously created.
Sorting and Editing Data:
o Sorting: In a table's datasheet view, click on the field header to sort records. Use Sort
Ascending or Sort Descending for specific ordering.
o Editing: Double-click on any record to edit, or right-click to delete a record.
3. Querying in LibreOffice Base
Querying is the process of retrieving data from a database according to specific criteria.
Database Management System
Writing Queries:
o Use the query wizard or SQL editor to write your queries.
o Example: SELECT * FROM Customers WHERE Age > 30 retrieves all
customers older than 30.
o JOIN Queries: Use joins to fetch data from multiple related tables. For example, an
inner join can combine customer and order data.
4. Forms and Reports
Forms: Forms simplify data entry. You can design custom forms with fields from one or
more tables. Forms can include text boxes, combo boxes, and buttons for ease of use.
o Use the Form Wizard to create forms or design them manually. For example, a
"Customer Data Entry" form could pull data from the "Customers" table.
Reports: Reports are for presenting data in a structured way, often for printing. Use the
Report Wizard to create summary reports or detailed records, with customizable layouts and
formatting.
5. Relationships in Depth
Relationships ensure data consistency between related tables.
Creating a Relationship:
o Go to Tools > Relationships.
o Add tables, drag the foreign key field from one table to the primary key in another to
establish the relationship.
Referential Integrity:
o This ensures that foreign keys in one table correspond to valid primary keys in the
related table. For example, deleting a student in the "Students" table should cascade
changes to all related tables (like exam records).
6. Practical Exercises
Table Exercise: Create a table named Participants for storing sports event data. Fields may
include Event_Id, Participant_Name, Score, etc. Enter data for multiple
participants and sort the table by score.
Query Exercise: Write queries like:
o SELECT * FROM Participants WHERE Score > 50 to retrieve
participants who scored above 50.