Ch 8 DBMS
Ch 8 DBMS
• 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
• 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
UPDATE Employee
SET DepartmentNumber = 'D07'
WHERE EmployeeID = 'E1007';
Example
SELECT *
FROM Employee
WHERE EmployeeID = 'E1007';
Example
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
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.