Unit 1 Notes RDBMS
Unit 1 Notes RDBMS
A Relational Database Management System (RDBMS) is a program that allows you to create,
update, and administer a relational database. Most modern databases (SQL Server, Oracle,
MySQL) are based on the relational model.
The Table Structure: In an RDBMS, data is organized into Tables (Relations). Each
table is divided into Rows (Tuples) and Columns (Attributes).
Data Integrity: RDBMS uses constraints (like Primary Keys, Foreign Keys, and Unique
constraints) to ensure data remains accurate and consistent across the system.
Term Meaning
Relation Table
Tuple Row
Attribute Column
Domain Set of values
Degree No. of columns
Cardinality No. of rows
Primary Key Unique identifier
Foreign Key Reference to another table
All information in the database is represented in exactly one way: by values in column positions
within rows of tables. There should be no "hidden" pointers or links; everything must be visible
in a table format.
Every single data element (value) in a relational database is guaranteed to be logically accessible
by providing a combination of the Table Name, Primary Key, and Column Name.
The description of the database (the metadata) must be stored in the same relational format as the
actual data. This "Data Dictionary" or "Catalog" should be queryable by authorized users using
the same SQL language used for data.
A system may support various languages, but it must support at least one well-defined language
(like SQL) that allows for data definition, view definition, data manipulation, integrity
constraints, and transaction boundaries.
All views that are theoretically updatable must also be updatable by the system. If the underlying
base tables can be changed, the "view" of that data should allow for those changes as well.
The system must support the ability to handle an entire "set" of data at once. It shouldn't just be
able to delete one row; it should be able to delete all rows that meet a certain criteria in a single
command.
Applications and ad hoc queries should remain logically unaffected when changes are made to
the physical storage (how the data is stored on the disk) or the access methods.
The application should not be affected by changes to the logical structure of the tables (like
splitting a table or joining two) as long as the information content remains the same. This is
usually achieved through views.
Integrity constraints (like "Age must be > 18") must be stored in the Data Dictionary, not in the
application programs. This ensures the database itself enforces the rules.
If the system has a low-level (record-at-a-time) interface, that interface cannot be used to bypass
the security or integrity constraints defined in the high-level relational language (SQL).
3. Normalization
Normalization is an important process in database design that helps improve the database's
efficiency, consistency, and accuracy. It makes it easier to manage and maintain the data and
ensures that the database is adaptable to changing business needs.
Database normalization is the process of organizing the attributes of the database to reduce or
eliminate data redundancy (having the same data but at different places).
Data redundancy unnecessarily increases the size of the database as the same data is repeated in
many places. Inconsistency problems also arise during insert, delete, and update operations.
In the relational model, there exist standard methods to quantify how efficient a database is. These
methods are called normal forms, and there are algorithms to convert a given database into normal
forms.
Normalization generally involves splitting a table into multiple ones, which must be linked each
time a query is made requiring data from the split tables.
Before Normalization: The table is prone to redundancy and anomalies (insertion, update, and
deletion).
After Normalization: The data is divided into logical tables to ensure consistency, avoid
redundancy and remove anomalies making the database efficient and reliable.
Need of Normalization
The primary objective for normalizing the relations is to eliminate the below anomalies. Failure to
reduce anomalies results in data redundancy, which may threaten data integrity and cause
additional issues as the database increases. Normalization consists of a set of procedures that assist
you in developing an effective database structure.
Insertion Anomalies: Insertion anomalies occur when it is not possible to insert data into a
database because the required fields are missing or because the data is incomplete. For
example, if a database requires that every record has a primary key, but no value is provided
for a particular record, it cannot be inserted into the database.
Deletion anomalies: Deletion anomalies occur when deleting a record from a database and
can result in the unintentional loss of data. For example, if a database contains information
about customers and orders, deleting a customer record may also delete all the orders
associated with that customer.
Updation anomalies: Updation anomalies occur when modifying data in a database and can
result in inconsistencies or errors. For example, if a database contains information about
employees and their salaries, updating an employee’s salary in one record but not in all related
records could lead to incorrect calculations and reporting.
Features of Database Normalization
Improved Database Design: Normalization helps in improving the overall design of the
database. By organizing the data in a structured and systematic way, normalization makes it
easier to design and maintain the database. It also makes the database more flexible and
adaptable to changing business needs.
Avoiding Update Anomalies: Normalization helps in avoiding update anomalies, which can
occur when updating a single record in a table affects multiple records in other tables.
Normalization ensures that each table contains only one type of data and that the relationships
between the tables are clearly defined, which helps in avoiding such anomalies.
First Normal A relation is in first normal form if every attribute in that relation is single-
Form (1NF) valued attribute.
Second A relation that is in First Normal Form and every non-primary-key attribute is
Normal Form fully functionally dependent on the primary key, then the relation is in Second
(2NF) Normal Form (2NF).
Fourth A relation R is in 4NF if and only if the following conditions are satisfied:
Normal Form It should be in the Boyce-Codd Normal Form (BCNF).
(4NF) The table should not have any Multi-valued Dependency.
SQL commands are fundamental building blocks used to perform given operations on database.
The operations include queries of data. creating a table, adding data to tables, dropping the table,
modifying the table and set permission for users.
SQL Commands are mainly categorized into five categories: SQL Commands
1. DDL - Data Definition Language
DDL (Data Definition Language) consists of SQL commands that can be used for defining,
altering and deleting database structures such as tables, indexes and schemas. It simply deals
with descriptions of the database schema and is used to create and modify the structure of
database objects in the database
RENAME TABLE
Rename an object existing in the
RENAME old_table_name TO
database
new_table_name;
Example:
SELECT column1
Filters rows before any grouping FROM table_name
WHERE
or aggregation WHERE condition;
Command Description Syntax
SELECT column1,
GROUP Groups rows that have the same AVG_FUNCTION(column2)
BY values in specified columns. FROM table_name
GROUP BY column1;
SELECT column1,
AVG_FUNCTION(column2)
HAVING Filters the results of GROUP BY FROM table_name
GROUP BY column1
HAVING condition;
SELECT column1
ORDER Sorts the result set by one or more
FROM table_name
BY columns
ORDER BY column1 [ASC | DESC];
Note: DQL has only one command, SELECT. Other terms like FROM, WHERE, GROUP BY,
HAVING, ORDER BY, DISTINCT and LIMIT are clauses of SELECT, not separate commands.
Example:
Example:
Example:
BEGIN TRANSACTION;
UPDATE employees SET department = 'Marketing' WHERE department = 'Sales';
SAVEPOINT before_update;
UPDATE employees SET department = 'IT' WHERE department = 'HR';
ROLLBACK TO SAVEPOINT before_update;
COMMIT;
In this example, a transaction is started, changes are made and a savepoint is set. If needed, the
transaction can be rolled back to the savepoint before being committed.
SQL Data Types
In SQL, each column must be assigned a data type that defines the kind of data it can store, such
as integers, dates, text, or binary values. Choosing the correct data type is crucial for data
integrity, query performance and efficient indexing.
Exact numeric types are used when precise numeric values are needed, such as for financial data,
quantities, and counts. Some common exact numeric types include:
-9,223,372,036,854,775,808 to
BIGINT Large integer numbers
9,223,372,036,854,775,807
Data Type Description Range
Example:
CREATE TABLE Product_Sales (
ProductID INT PRIMARY KEY,
Quantity SMALLINT,
UnitPrice DECIMAL(10,2),
TotalAmount DECIMAL(10,2)
);
These types are used to store approximate values, such as scientific measurements or large ranges
of data that don't need exact precision.
Example:
CREATE TABLE Employee_Info (
EmpID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName CHAR(30),
Bio TEXT
);
Example:
CREATE TABLE International_Users (
UserID INT PRIMARY KEY,
FullName NVARCHAR(100),
Country NCHAR(50)
);
store both the data and time (year, month, day, hour, minute,
DATETIME 8 Bytes
second)
Example:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE,
OrderTime TIME,
ShippedAt DATETIME
);
Example:
CREATE TABLE Product_Images (
ImageID INT PRIMARY KEY,
ImageName VARCHAR(100),
ImageData VARBINARY(MAX)
);
Formatting Commands:
These commands improve the readability of your reports without affecting the actual data:
Commands are stored in the Commands are not stored; they exist for
Persistence
database. the session.