System Design (DBMS)
System Design (DBMS)
Ashish Rastogi
Introduction
SQL
SQLConstraint
ConstraintMeaning
Meaning
Write
WriteConstraint
Constraint
Constraint
ConstraintType
Type
Cons tr a i nt Me a ni ng :-
Not Null .1
Unique Key .2
Primary Key .3
Foreign Key .4
Check Key .5
Default Key .6
Co ns tr a i nt Ty pe : -
Not Null
Unique Key .2
Primary Key .3
Unique Key
Unique Key
Primary Key
Co ns tr a i nt Ty pe : -
Unique
Unique Key Key
Primary
Unique Key
Key
Primary key does not accept null value
Foreign Key .4
Unique Key
A FOREIGN KEY in one table points to a
PRIMARY KEY in another table
Unique Key
The "Persons" table:
Unique Key
The “Order" table:
e
"P_Id" column in the "Persons" table.
No t
The "P_Id" column in the "Persons" table is the PRIMARY
KEY in the "Persons" table.
Check Key .5
);
Co ns tr a i nt Ty pe : -
Check
DefaultKey
Key.5.6
Check
DefaultKey
Key.5Example .6
CREATE TABLE Persons
(
P_Id Number(15),
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
);
o s i t e K e y E x a mpl e
Comp
The Oracle PL/SQL GROUPING function is used to identify the final aggregated rows in grouped rows using ROLLUP and
CUBE extension clauses. It returns numeric values to identify the column value in an aggregated row
SELECT COUNT (country), region FROM Countries GROUP BY region HAVING COUNT (country) > 1
SELECT fact_1_id,
fact_2_id,
SUM(sales_value) AS sales_value
FROM dimension_tab
GROUP BY ROLLUP (fact_1_id, fact_2_id)
ORDER BY fact_1_id, fact_2_id;
In addition to the subtotals generated by the ROLLUP extension, the CUBE extension will generate subtotals for all combinations
of the dimensions specified. If "n" is the number of columns listed in the CUBE, there will be 2n subtotal combinations.
SELECT fact_1_id,
fact_2_id,
SUM(sales_value) AS sales_value
FROM dimension_tab
GROUP BY CUBE (fact_1_id, fact_2_id)
ORDER BY fact_1_id, fact_2_id;
Ashish Rastogi
Understanding Data Redundancy
What is data
redundancy?
Understanding Data Redundancy (Contd.)
Redundancy:
Increases the time involved in updating, adding, and deleting
data.
Increases the utilization of disk space and hence, disk I/O
increases.
Redundancy can, therefore, lead to:
Insertion, modification, and deletion of data, which may cause
inconsistencies.
Errors, which are more likely to occur when facts are repeated.
Unnecessary utilization of extra disk space.
Understanding Data Redundancy (Contd.)
STUDENTNAME
STUDENTBIRTHDATE
STUDENTADDRESS
STUDENTCITY
STUDENTZIP
STUDENTCLASS
STUDENTSEMESTER
STUDENTTEST1
STUDENTTEST2
Understanding Data Redundancy (Contd.)
How can I
remove data
redundancy?
Definition of Normalization (Contd.)
Normalization:
Is a method of breaking down complex table structures into
simple table structures by using certain rules.
Has the following benefits:
It helps in maintaining data integrity.
It helps in simplifying the structure of tables, therefore, making a
database more compact.
It helps in reducing the null values, which reduces the complexity
of data operations.
Definition of Normalization (Contd.)
By applying the 1NF definition to the PROJECT table, you arrive at the table, as
shown in the following diagram.
Definition of Normalization (Contd.)
By applying the 1NF definition to the STUDENT table, you can arrive at the
tables, as shown in the following diagram.
Definition of Normalization (Contd.)
Functional dependency:
Attribute A is functionally dependent on B if and only if, for each
value of B, there is exactly one value of A.
Attribute B is called the determinant.
Definition of Normalization (Contd.)
Primary key
Definition of Normalization (Contd.)
ROLL_NUMBER
MARKS
COURSE_CODE
Definition of Normalization (Contd.)
COURSE_NAME
ROLL_NUMBER
T_NAME
COURSE_CODE
ROOM_NUMBER
Definition of Normalization (Contd.)
To convert the EMPLOYEE table into 3NF, you must remove the
DEPTHEAD column and place it in another table, as shown in
the following diagram.
Definition of Normalization (Contd.)
In above table, Rose takes both Mathematics and Physics class for Semester 1,
but she does not take Physics class for Semester 2. In this case, combination of
all these 3 fields is required to identify a valid data. Imagine we want to add a
new class - Semester3 but do not know which Subject and who will be taking
that subject. We would be simply inserting a new entry with Class as Semester3
and leaving Lecturer and subject as NULL. As we discussed above, it's not a
good to have such entries. Moreover, all the three columns together act as a
primary key, we cannot leave other two columns blank!
Fifth Normal Form (5NF)
Hence we have to decompose the table in such a way that it satisfies all the rules till 4NF
and when join them by using keys, it should yield correct record. Here, we can represent
each lecturer's Subject area and their classes in a better way. We can divide above table
into three - (SUBJECT, LECTURER), (LECTURER, CLASS), (SUBJECT, CLASS)
SQL Vs NoSQL
Video 2
99
Identifying the Types of Indexes
Index:
Enables fast searching of data.
Accelerates queries that join tables, and performs sorting and
grouping.
Enforces uniqueness of rows, (if configured for that)
Contains a collection of keys and pointers.
Keys are the values built from one or more columns in the
table.
Identifying the Types of Indexes (Contd.)
Clustered index:
It sorts and stores the data rows in the table based on their key values.
The following figure displays a clustered index on the Employee table.
Identifying the Types of Indexes (Contd.)
Nonclustered index:
The physical order of the rows is not the same as the index
order.
The following figure represents the working of a nonclustered
index.
Creating Indexes
Index:
Is created on the most frequently queried column in tables or
views.
Based on two or more columns is called a composite index.
Can be created by using the CREATE INDEX statement.
Creating Indexes (Contd.)
You can create a clustered index on the EmployeeID attribute of the Employee table by
using the following statement:
CREATE CLUSTERED INDEX IX_EmployeeID
ON Employee (EmployeeID)
WITH FILLFACTOR = 10
The following statement creates a nonclustered index on the ManagerID attribute of the
Employee table:
CREATE NONCLUSTERED INDEX IDX_Employee_ManagerID
ON Employee (ManagerID)
Creating Indexes (Contd.)
10
How to create a table with the same structure with data
as in the Student table?
10
Write q query to find all the employees whose salary is between
5000 to 50000.
11
Write a SQL query to fetch all employee records from EmployeeDetails
table who have a salary record inEmployeeSalary table.
11
Write a SQL query to fetch duplicate records from a table.
11
Write a SQL query to fetch project-wise count of employees
sorted by project's count in descending order.
11
Write a SQL query to fetch all the Employees who are also
managers from EmployeeDetails table.
Or
SELECT DISTINCT E.FullName FROM EmpDetails E JOIN EmpDetails M ON E.EmpID
=M.ManagerID;
11
Joins
Querying Data by Using Joins
What are
Joins?
Querying Data by Using Joins (Contd.)
Joins:
Allow to retrieve data from multiple tables.
Can be of the following types:
Inner join
Outer join
Cross join
Equi join
Self join
Using an Inner Join
Inner join:
Retrieves records from multiple tables after comparing
values present in a common column.
Retrieves only those rows that satisfy the join condition.
Syntax:
SELECT column_name, column_name [,column_name]
FROM table1_name JOIN table2_name
ON table1_name.ref_column_name join_operator
table2_name.ref_column_name
Using an Inner Join (Contd.)
COLUMNS COLUMNS
ABC BDE
Table X Table Y
INNER JOIN
ABCDE
COMMON ROWS
OUTPUT
Using an Inner Join (Contd.)
For example:
SELECT e.EmployeeID,e.Title,
eph.Rate,eph.PayFrequency
FROM Employee e JOIN EmployeePayHistory eph
ON e.EmployeeID = eph.EmployeeID
Output
Outer join:
Displays the result set containing all the rows from one table
and the matching rows from another table.
Displays NULL for the columns of the related table where it
does not find matching records.
Is of the following types:
Left outer join
Right outer join
Full outer join
Using an Outer Join (Contd.)
COLUMNS COLUMNS
ABC BDE
Table X Table Y
ABCDE
OUTPUT
Using an Outer Join (Contd.)
COLUMNS COLUMNS
ABC BDE
Table X Table Y
ABCDE
OUTPUT
Using an Outer Join (Contd.)
COLUMNS COLUMNS
ABC BDE
Table X Table Y
ABCDE
OUTPUT
Using an Outer Join (Contd.)
Syntax:
SELECT column_name, column_name
[,column_name]
FROM table1_name [LEFT | RIGHT| FULL] OUTER
JOIN table2_name ON
table1_name.ref_column_name join_operator
table2_name.ref_column_name
Using an Outer Join (Contd.)
Displays the
JobCandidateID column
from the JobCandidate
table and the Title
column from the
matching rows of the
Employee table
Using an Outer Join (Contd.)
Cross join:
Is also known as the Cartesian Product.
Joins each row from one table with each row of the other
table.
Let us now understand the
working of cross join.
Using a Cross Join (Contd.)
COLUMNS COLUMNS
ABC BDE
Table X Table Y
n ROWS
CROSS JOIN m ROWS
ABCDE
ALL ROWS (n X m)
EACH ROW OF
TABLE X JOINED
WITH EACH ROW OF
OUTPUT TABLE Y
Using a Cross Join (Contd.)
Output
Combines the records of both the tables to display the total price
of a computer with all the possible combinations