0% found this document useful (0 votes)
16 views

Ch 8 DBMS

The document discusses the differences between file-based systems and relational databases, highlighting the limitations of file-based systems such as data redundancy and lack of integrity. It outlines the benefits and features of relational databases, including reduced redundancy, improved data integrity, and the ability to perform complex queries. Additionally, it covers key terms related to databases, SQL commands for data manipulation, and the importance of normalization and referential integrity in maintaining data accuracy and consistency.

Uploaded by

mhd.ali2122
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Ch 8 DBMS

The document discusses the differences between file-based systems and relational databases, highlighting the limitations of file-based systems such as data redundancy and lack of integrity. It outlines the benefits and features of relational databases, including reduced redundancy, improved data integrity, and the ability to perform complex queries. Additionally, it covers key terms related to databases, SQL commands for data manipulation, and the importance of normalization and referential integrity in maintaining data accuracy and consistency.

Uploaded by

mhd.ali2122
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

DATABASES

File Based Systems

• File-based Approach vs. Relational Database:


⚬ A file-based approach stores data in one or more separate computer files.
⚬ A relational database structures information in tables, rows, and columns, facilitating more
efficient data management.
• Limitations of Using a File-based System:
⚬ Data redundancy: The same data is repeated in more than one file, leading to unnecessary
duplication.
⚬ Data dependency: Changes to data format require changes to every program that
accesses the data.
⚬ Lack of data integrity: There may be inconsistencies, such as the same entity having
different attributes in different files.
⚬ Lack of data privacy: If a single file is accessible, it can potentially expose all data to all
users.
Benefits of Relational DB
• Reduced data redundancy: Eliminates unnecessary duplicates by storing data once and
referencing it via keys.
• Reduced data dependency: Changes in database structure do not directly affect the
applications using the data.
• Improved data integrity: Ensures that data remains accurate and consistent across the
database.
• Improved data privacy: Provides mechanisms to restrict access to data based on user roles.
• Program-data independence: The structure of a database can be modified without affecting
the program's ability to access data.
• Ability to create ad hoc queries: Users can compose queries for specific needs without
requiring changes to the database structure.
Features of Relational DB
• Linked Tables:
⚬ Multiple tables are linked together to reduce data redundancy and increase data integrity.
⚬ Referential integrity is enforced, ensuring that relationships between tables remain
consistent (e.g., a foreign key must correspond to a valid primary key).
• Program-Data Independence:
⚬ Allows changes to the data's structure without affecting the application's code, and vice
versa.
⚬ Enables adaptability and scalability in database and application development.
• Complex Queries:
⚬ Facilitates the writing of complex queries to retrieve specific data, improving the ease of
data manipulation and retrieval.
Features of Relational DB
• Access Rights:
⚬ Different users can be assigned varying access rights, enhancing security by controlling
who can view or manipulate data.
⚬ Ensures that users only have access to the data necessary for their role (for example, a
salesperson may only access customer contact information, not financial data).
• Data Views:
⚬ Different users can be provided with tailored views of the data, so confidential information
remains protected.
⚬ Supports data privacy by allowing users to see only the data that they are authorized to
access, thus maintaining confidentiality.
Key Terms

• DBMS (Database Management System):


⚬ A software system that uses a standard method of cataloging, retrieving, and running
queries on data.
• Entity:
⚬ Represents a real-world object or concept within the system that is modeled and stored
(e.g., a 'Student' in a university database).
• Attributes:
⚬ Data items within a table that represent characteristics of an entity (e.g., 'Name', 'Roll No.',
and 'Course' are attributes of the 'Student' entity).
• Fields and Records/Tuples:
⚬ Fields: Individual columns in a table that store specific attributes.
⚬ Record/Tuple: A row in a table that collectively represents a single item or entity with values
for each field.
Key Terms
• Primary Key:
⚬ A field with a unique value for each record, used to uniquely identify each tuple (e.g., 'Roll
No.' in a student table).
• Foreign Key:
⚬ A field in one table that links to a primary key in another table, establishing a relationship
between the two tables.
• Candidate Key and Secondary Key:
⚬ Candidate Key: Any field or combination of fields that can uniquely identify a record in a
table (e.g., 'Roll No.', 'CNIC', 'Passport Number' could all be candidate keys if they are
unique).
⚬ Secondary Key: A candidate key that is not chosen as the primary key is considered a
secondary key, used for other indexing and lookup purposes.
• Note on Keys:
⚬ A set of keys that could uniquely identify a record is known as candidate keys. One is
chosen as the primary key, and the rest are secondary keys, which may serve alternative
purposes like sorting or searching within the database.
Key Terms
• Primary and Foreign Keys in Database Relationships:
⚬ Primary Key: Uniquely identifies each record (or tuple) in a table (e.g., 'Roll No.' in a student
table ensures that each student is uniquely identifiable).
⚬ Foreign Key: Used in a table to reference the Primary Key of another table, forming a
relationship between the two tables (e.g., 'Department ID' in a 'Professor' table that
references the 'ID' in a 'Department' table).
Structured Query Language

• Data Definition Language (DDL): Used to define or modify data structures in the database.
• Data Manipulation Language (DML): Utilized for performing operations such as insert, update,
delete, and query data within the database.
• Data Types in SQL:
⚬ Integer: A data type for whole numbers (e.g., '54').
⚬ Date: Represents calendar dates (e.g., '04/01/2020').
⚬ Time: Represents time of day (e.g., '18:00').
⚬ Varchar: A string data type with variable length (e.g., 'Varchar(50)' can store a string with a
maximum of 50 characters).
DDL
• DDL Statement to Create a Database: CREATE DATABASE <Name>;
⚬ (e.g., CREATE DATABASE Employees; to create a new database for employee records).
• DDL Statement to Create a Table: CREATE TABLE <Name> ( <attribute_name> <data_type>,
... , PRIMARY KEY (<name>));
⚬ (e.g., CREATE TABLE Student (RollNo INTEGER, Name VARCHAR(100), Course
VARCHAR(50), PRIMARY KEY
DDL

CREATE TABLE Employee_Data (


EmployeeID varchar(7),
FirstName varchar,
LastName varchar,
DateOfBirth Date,
Gender varchar(5),
DepartmentNumber varchar(2),
PRIMARY KEY (EmployeeID) NOT NULL
);
DDL

• DDL Statement to add new field


ALTER TABLE <table_name>
ADD <field name> <datatype>
Data Manipulation Language

• 1. Adding Data to a Table:


⚬ SQL Statement:
■ INSERT INTO <table_name>
■ VALUES (<data_in_column_1>, <data_in_column_2>, ...);
⚬ Explanation: This command is used to insert a new row into a table with specified values
for each column.
⚬ Example:
■ INSERT INTO Customers
■ VALUES (1, 'John Doe', '123 Elm St', '[email protected]');
Data Manipulation Language

• Updating Data in a Table:


■ UPDATE <table_name>
■ SET <column_1> = <value_1>, <column_2> = <value_2>
■ WHERE <condition>;
⚬ Explanation: This command updates existing records in a table. The WHERE clause
specifies which records should be updated.
⚬ Example:
■ UPDATE Orders
■ SET Status = 'Shipped'
■ WHERE OrderID = 1003;
Data Manipulation Language

• 3. Displaying/Returning Values:
• SQL Statement:
⚬ SELECT <column_1>, <column_2>, ...
⚬ FROM <table_name>
⚬ WHERE <condition>;
• Explanation: This command is used to select and display data from a database. The WHERE
clause filters the results to meet the specified conditions.
• Example:
⚬ SELECT FirstName, LastName
⚬ FROM Employees
⚬ WHERE Department = 'Sales';
Example

How would you add a new employee's information to the existing `Employee` table in the
database, update their department number after a department change, and then retrieve
their updated information?
Example

1. Adding a New Employee


To add a new employee named 'Alice Smith', born on '1985-02-28', with a gender of 'Female',
an initial department number 'D05', and an assigned EmployeeID of 'E1007':

INSERT INTO Employee (EmployeeID, FirstName, LastName, DateOfBirth, Gender,


DepartmentNumber)
VALUES ('E1007', 'Alice', 'Smith', '1985-02-28', 'Female', 'D05');
Example

2. Updating the Employee's Department:


- If Alice Smith changes her department from 'D05' to 'D07', the following SQL statement
would update her records:

UPDATE Employee
SET DepartmentNumber = 'D07'
WHERE EmployeeID = 'E1007';
Example

3. Retrieving Alice Smith's Updated Information:


- To retrieve the full details of Alice Smith after her department change:

SELECT *
FROM Employee
WHERE EmployeeID = 'E1007';
Example

3. Retrieving Alice Smith's Updated Information:


- To retrieve the full details of Alice Smith after her department change:

SELECT *
FROM Employee
WHERE EmployeeID = 'E1007';
Reffertential Integrity

• Referential integrity is about making sure tables do not reference data that does not exist.
It prevents orphaned records and maintains the link between related records across
different tables.
• A primary key cannot be deleted if there are dependent records in another table. This is to
ensure that no foreign key references to non-existent primary keys are left in the database
(e.g., a customer ID cannot be deleted from the 'Customers' table if there are orders linked
to it in the 'Orders' table).
• Cascading delete refers to the automatic deletion of dependent records when a primary
key is deleted (e.g., deleting a customer will also delete all their orders).
• A primary key cannot be updated if dependent records are not also updated. This ensures
that foreign key references remain valid after the change.
Reffertential Integrity

• Cascading update/edit means that when a primary key is updated, all associated foreign
key values are also updated to reflect the change.
• Every foreign key value must have a matching value in the corresponding primary key
column, ensuring referential integrity (e.g., an order's customer ID must match an existing
customer ID in the 'Customers' table).
• Foreign keys must be the same data type as the corresponding primary key to maintain
data consistency.
A relational database can significantly reduce data redundancy through several
mechanisms:
• Records are stored uniquely and referenced by a primary key, ensuring each data point is
stored only once (e.g., a customer's details are stored in a single 'Customers' table rather
than repeated in every 'Order' record).
• Data is organized into individual tables for different entities and linked through relationships,
which helps to avoid duplicate information (e.g., 'Orders' table linked to 'Customers' table
through a foreign key).
• The use of primary and foreign keys enables distinct data entities to be linked efficiently
(e.g., an 'EmployeeID' in the 'Employees' table as a primary key that is referenced as a
foreign key in the 'Salaries' table).
• Enforcing referential integrity ensures that references between tables are consistent and
that related data is not inadvertently deleted or made inconsistent (e.g., preventing the
deletion of a 'Product' record that is referenced in 'Orders').
• Normalization, which is the process of structuring a relational database in accordance with
a series of so-called normal forms to reduce redundancy and improve data integrity (e.g.,
dividing a 'Contacts' table into separate 'PhoneNumbers' and 'Addresses' tables).
Primary keys and foreign keys serve to create connections between tables:
• A primary key in one table (e.g., 'EmployeeID' in an 'Employees' table) is used to uniquely
identify each record.
• This primary key is linked to a foreign key in another table (e.g., 'ManagerID' in a
'Departments' table), establishing a relationship between the two entities.

Data integrity is the assurance of the consistency and accuracy of data over its lifecycle:
• It is maintained by the enforcement of referential integrity and by implementing validation
rules (e.g., constraints that ensure only valid dates are entered into a 'DateOfBirth' field).
• Data integrity ensures that the data remains an accurate reflection of the real-world
scenarios it models, which is crucial for maintaining the reliability of database operations
and analytics.
DBMS Tools and Functions
• Developer Interface:
⚬ Used to create user-friendly features like forms for entering new bookings.
⚬ Enables the creation of outputs such as reports on bookings for a specific date.
⚬ Allows the creation of interactive features, such as buttons or menus.
• Query Processor:
⚬ Allows the creation of SQL queries.
⚬ Searches for data that meet set criteria (e.g., retrieving all bookings for the next week).
⚬ Performs calculation of extracted data (e.g., finding the number of empty rooms).
⚬ Organizes the results to be displayed in a user-friendly format.
• Data Dictionary:
⚬ Stores all the information about the database, including fields, datatypes, and keys,
providing a reference for developers and ensuring consistency across the database.
DBMS Tools and Functions
• Tasks Performed by DBMS Developer Interface:
⚬ Create a table to structure data.
⚬ Set up relationships between tables to define how data in one table relates to data in
another.
⚬ Create a form to input data into the database through a graphical interface.
⚬ Generate reports to summarize or analyze data.
⚬ Craft queries to retrieve specific information from the database.
Security and DBMS
• Issue Usernames and Passwords:
⚬ Controls access to the database, preventing unauthorized data access.
⚬ Enforces the use of strong passwords to enhance security.
• Access Rights:
⚬ Ensures that only specific usernames have access to certain parts of the data, which
can be restricted to read-only or given full access (e.g., only the finance department can
edit/read financial data).
• Regular Backups:
⚬ Protects against data loss or damage by maintaining copies of the data (e.g., daily
backups).
• Encryption of Data:
⚬ Secures data so it remains unintelligible without proper authorization, adding an
additional layer of security against unauthorized access.
Normalisation

• First Normal Form (1NF)


⚬ No Repeated Groups of Attributes: A table is in 1NF when it has a flat structure with no
repeating groups. This means each column must be unique and not contain sets of
values (e.g., a column for phone numbers should not hold multiple numbers).
⚬ All Attributes Should Be Atomic: Attributes must be indivisible, ensuring that each field
contains the smallest possible data item, not composite data (e.g., a 'Name' field
should not contain both first and last names).
⚬ No Duplicate Rows: Each row in the table should be unique, identifiable by a primary
key, preventing the same record from appearing more than once.
Normalisation

• Second Normal Form (2NF)


⚬ Should be in 1NF: A table must first meet all criteria of the 1NF before it can be
considered to be in the 2NF.
⚬ No Partial Dependency: Partial dependency means that an attribute in a table depends
only on a part of a composite primary key. To be in 2NF, all non-key attributes must
depend on the whole primary key (e.g., in a table with a composite key [OrderID,
ProductID], the ProductName should depend on the ProductID, not just the OrderID).
Normalisation

• Third Normal Form (3NF)


⚬ Should be in 1NF and 2NF: To be in 3NF, a table must first satisfy the criteria for both 1NF
and 2NF.
⚬ No Transitive Dependency: A transitive dependency occurs when non-key attributes
depend on other non-key attributes rather than directly on the primary key. For a table
to be in 3NF, there should be no transitive dependency (e.g., a 'CustomerID' determines
'CustomerName', and 'CustomerName' determines 'CustomerCredit', which violates 3NF.
'CustomerCredit' should depend directly on 'CustomerID').
Normalisation

1. There are partial dependencies in the software purchase table e.g. SoftwareDescription only
depends on software name and does not depend on both."
2."There is a non-key dependency in the software purchase table: Licence cost depends on
Licence type and does not depend on primary keys."
Schema Levels

1. External Schema:
⚬ Reflects the individual's view of the database.
i. Tailored to the needs of particular user groups.
ii. Can present a subset of the entire database and abstract away details irrelevant to
the user (e.g., a user interface that shows a customer's profile information but not the
underlying storage details).
2.Conceptual Schema:
⚬ Describes the views which a user of the database might have.
i. Represents the global view of the entire database.
ii. Includes all entities, their attributes, and their relationships; abstracts away the
details of physical storage (e.g., a diagram showing how customers, orders, and
products are interrelated).
Schema Levels

1. Logical Schema:
⚬ Describes how the relationships will be implemented in the logical structure of the
database.
i. Defines the structure of the data elements and the relationships among them.
ii. Independent of the actual physical storage, focusing on how data is organized
logically (e.g., tables, columns, rows, indexes).
2.Physical/Internal Schema:
⚬ Describes how the data will be stored on the physical media.
i. Involves the specific techniques for storing and retrieving data.
ii. Details like data compression, data encryption, data partitioning, and storage spaces
are defined at this level (e.g., file paths, block sizes, ordering of records).
1. Inner Join:
⚬ The INNER JOIN keyword selects records with matching values in both tables.
⚬ It combines rows from two or more tables based on a related column between them.
⚬ Example: SELECT ActorID FROM FILM_ACTOR INNER JOIN FILM_FACT ON FILM_ACTOR.FilmID
= FILM_FACT.FilmID WHERE FILM_FACT.FilmTitle = 'Cinderella'; (Retrieves the IDs of all
actors in films with the title 'Cinderella'.)
2.Delete:
⚬ The DELETE statement is used to remove existing records from a table.
⚬ It is executed with a condition that specifies which records should be deleted.
⚬ Example: DELETE FROM Customers WHERE CustomerName = 'Alfreds Futterkiste'; (Deletes
the customer record where the customer name is 'Alfreds Futterkiste'.)
1. Count the Number of Products:
⚬ SELECT COUNT(ProductID) FROM Products;
⚬ This command calculates the total number of product entries in the 'Products' table by
counting the rows where 'ProductID' is present.
2.Find the Average Price of All Products:
⚬ SELECT AVG(Price) FROM Products;
⚬ It computes the average value of the 'Price' column for all rows in the 'Products' table.
3.Calculate the Sum of All the Prices:
⚬ SELECT SUM(Price) FROM Products;
⚬ This command sums up the values in the 'Price' column for all rows in the 'Products'
table, giving the total cost of all products listed.

You might also like