Database1 Final Revision ٠٤٥٢٢٤

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Database System

Content
1-Introduction
 data, information
 File System, DB System
 Formal, Informal Term
 DB Player
 DB States
 DB Lifecycle
 DB Model, Data Model
 3 Schema Architecture

2-Relational Model
 Schema, Instance
 Domain
 DB structure
 Key Constrains

3- SQL
 DDL, DML, DCL
 Type of Joins [Inner, Outer, Cross, Self join], [Equi, Natural]
 Set Operation [U, ∩, −]
 Devid Operation
 Sub query
 Type of Sub query [Non-Correlated, Correlated]
4- Relational Algebra
 Unary Operation [σ, π, ρ]
 Binary Operation [U, ∩, −, Χ]
 Join operator
 Set Operator
 Transform from SQL to RA
 Aggregate Function

5- DB Design
 DB Design Process
 Conceptual Design
 ERD
 Component of ERD [Entities, Attribute, Relationships]
 IE notation
 Type of Entities, Attribute & Relationship
 Structural Constrains [Cardinality, Participation]
 ERD- into- Relations

 EERD
 Generalization, Specialization (Supertype, Subtype)
 Constraints in Supertype, Subtype [Total, Partial, Disjoint, Overlap]
 EERD-into-Relations

6- Normalization
 Data Redundancy, Insertion, Deletion & Update Anomaly
 Functional Dependency
 Normalization Stage [1NF, 2NF, 3NF]
Introduction
Data Information File System DB System DB Model Data
- Small System - Large System - Collection of - Collection of
- Row fact that can be -Data with context
recorded & that have concepts that can be tools to describe
-Given meaning in a - Often single user - Multiple user used to define the DB data, data
implicit meaning. specific context structure relationships &
- No context. -Processed data data constraints
- Simple structure - Complex structure
- An important
organizational resource
Ex: - Useful for decision- - Isolated data - Shared data
- text making

- number - Redundant data - Reduced redundancy


DB Player
- Relatively cheap - Relatively expensive
- DBA : - gives access
authorization,
- Less secure - More secure coordination &
monitoring
database usage,
problem
determination,
performance
Formal Term Informal Term tuning
- Designer : - chooses the
- Relation - Table appropriate
structures to
- Attribute - Column Header represent & store
the
- Row - Tuple or record
data
- Domain - All possible column - System Analysts : - interact with
value stakeholders,
- Schema - Table definition including business
users & DBA
- State of Relation - Populated Table - User
- Relation Degree - Num of Column

- Relation Cardinality - Num of Row

DB States

DB Lifecycle - Empty State : - DB is empty when


we first define the DB
schema
- Initial State : - DB is first populated
or loaded with data

- Current State : - Snapshot in time


Relational Model
Simple Domain Composite Domain
Schema Instance
- is a set of values, from which - the current values contained in the
- the name of the relation and the set - the current values contained in the one or more attributes takes their relation
of attributes relation values. - Can be divided into parts
- Schema is same for whole database. - Data in instances can be changed - It can’t be divided into parts
using addition, deletion, updation.
Ex: Ex:
- CITY = {Cairo, London, Paris, -DATE = (DAY, MONTH, YEAR)
- Doesn’t change frequently - Change frequently Doha, Rome, Dobai, Madrid} Where:
DAY = {1..31}, MONTH = {1..12},
YEAR = {1990..2100}

Ex: Instance

Employee
Employee ID Name Department Salary
001 John Doe HR 45.00$
002 John Smith Sales 55.00$
003 Lona lee IT 66.00$

Works on
Essn Pno Hours
001 101 40
002 102 25
003 101 30

Ex: Schema

Employee ID Name Department Salary Type of PK


Simple PK Composite PK

Essn Pno Hours - 1 attribute is a PK - 2 or more attribute are a PK

Key Constraints

Super Key Candidate Key Primary Key Foreign Key

-unique identifier -After we define the super key, we -After selection the candidate key, -FK Links 2 or more table
-might contain more than select the most appropriate key that we choose the PK. -Match PK
attribute is still able to identify it uniquely - unique value -Can be null
tuples in a relationship (candidate - Can ‘t be null
key)
SQL

CREATE SELECT Clauses


CREATE SELECT Attribute name
Table name
FROM Distinct - Used to remove the frequent
CREATE Database name Table name value from the specific col
WHERE Condition when data showed to the user

Like -Used to perform pattern


ALTER UPDATE matching within a DB query.
ALTER Table Like ‘%_a_%’, ‘%a’, ‘a%’…
Table name UPDATE Col name
ADD Constrain SET Value of attribute u want to change
Not Like -Reverse of Like

WHERE Condition
Is Null -Check if the value of a
specific col is null or not
DROP INSERT
Is Not Null - Reverse of Is Null
DROP Database Database name INSERT INTO Table name (Col names)
DROP Table Table name VALUES (value of each Col)
Limit (Top) - Used to restrict the number of
rows returned in a query result
Between -Used to specify a range of
values in a query.
TRUNCATE DELETE OR - Used to combine multiple
conditions in a query
TRUNCATE Table name DELETE where at least one of the
Col name
FROM Table name
conditions must be true
WHERE AND -like OR but, where at least
Condition
one of the conditions must be
true
AS - Rename the attribute name
Difference between… when it showed to the user
Aggregate Function -min, max, sum, count, avg
Update Modify existing data
&
Modify structure of data Group by - used to sort the result of a
Alter query based on one or more
Delete used to selectively remove specific rows from a table based on specified conditions columns
& Having - Use it when we use group by

Truncate used deletes all the rows in the table without considering any conditions Order by - used to sort the result of a query based on
one or more columns. DESC OR ASC
Used in the SELECT, UPDATE, and DELETE statements & evaluated before any grouping or aggregation takes place
Where
& Where -condition for anything doesn’t
Used in the SELECT statement with grouping & evaluated after the grouping or aggregation has been performed. use groupby
Having
Inner Join Left Join Right Join Full Join Self Join Cross Join

-used to return rows -Return all matching -Return all matching rows, -Return all matching rows, -Combine rows from the -Combines every row from
from both tables rows, plus nonmatching plus nonmatching rows plus nonmatching rows same table based on a one table with every row
that satisfy the given rows from one or both tables from one or both tables related condition, treating from another table.
condition. from one or both tables -Include the right tuple the table as if it were two
-Include the both left and separate entities.
-Include the left tuple even if there’s no match right tuples even if there’s Attribute = n + m
even if there’s no match no match Cardinality = n * m

Ex: Ex: Ex: Ex: Ex: Ex:

select * select * select *


select * from one left join two on from one right join two on from one full join two on
from one inner join two on one.x = two.x; one.x = two.x; one.x = two.x;
one.x = two.x;

Result: Result: Result: Result: Result: Result:

Union Intersect Except Exist Type of Subquery


Used to combine the -Used to retrieve the Used to retrieve the Used to check for the existence of
result-set of common rows between distinct rows that exist rows in a subquery. None - The inner query is first
Correlated evaluated and used in evaluating
two or more SELECT two or more SELECT in the first SELECT the outer query
statements. statements statement but not in the
second SELECT Ex: - Display the employees whose
job title is the same as that of
statement. employee 7369.
Sol: SELECT Name
FROM Customers
WHERE ID IN (SELECT
Ex: Ex: Ex: Ex: DISTINCT Customer
FROM Orders);
Correlated - Outer queries provides values
select name select name select name SELECT column1, column2 for the inner query evaluation
from depositor from depositor from depositor FROM table1
union intersect except WHERE EXISTS (SELECT * Ex: - Retrieve the names of
customers who have placed
select name select name select name FROM table2 orders more than once.
from borrower from borrower WHERE table1.column1 = Sol:
from borrower SELECT Name
table2.column1); FROM Customers c
WHERE (SELECT COUNT(*)
FROM Orders o
Single Operator >, < , =, <=, >= WHERE(o.Customer = c.ID)
> 1;

Multiple Operator In(=Any), Any, All

Note: relations in union operator need to be Compatible Subquery: query inside query
(same num of Col, Similar dataType, same order)
Example
Ex1: create a table with the name ‘CUSTOMERS’ which contains the
personal details of customers including their name, age, address and
salary

CREATE TABLE CUSTOMERS (


ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
);
Alter table CUSTOMERS
Add constraint PRIMARY KEY (ID)

Ex1: write a SQL query to find those employees who earn more than the average
salary and work in the same department as an employee whose first name contains
the letter 'J'. Return employee ID, first name and salary.

Sol:
SELECT employee_id, first_name , salary
FROM employees
WHERE salary > (SELECT AVG (salary)
FROM employees )
AND department_id IN
(SELECT department_id
FROM employees
WHERE first_name LIKE '%J%');

Ex3: Consider the relational database schema.  Question 3 in FinalExam 2018

STUDENTS (ST-ID, Name, Address, Major, GPA)

PROF (PROF-ID, Name, Working-Dept, Salary)

DEPARTMENTS (DCode, DName, DHead)

COURSE (C-Code, CTitle)

PREREQUESTS (C-Code, Pre-Code)

ATTEND (STID, Course#)


TEACHES (P-ID, C-Code)
Q1: Find names of professors who work in ‘IS’ department, each has a salary greater than 1000, and
teach a course that has course CS101 as a prerequisite. (write this query
in the nested form)

Q2: For each major that has more than 100 students, find the major and the average GPA of students in
the major.

Q3: Find pairs of students ids such that the two students have the same GPA, live in
same address but have different major
Sol Q1: SolQ1 without nested form:

SELECT PROF.Name SELECT p.Name


FROM PROF FROM PROF,TEACHES, COURSE, PREREQUISITES
WHERE PROF.Working_Dept = 'IS' WHERE PROF.PROF-ID = TEACHES.P-ID
AND PROF.Salary > 1000 AND TEACHES.C-Code = COURSE.C-Code
AND PROF.PROF_ID IN ( AND COURSE.C-Code = PREREQUISITES.C-Code
SELECT TEACHIES.P_ID AND Working_Dept = 'IS'
FROM TEACHES AND PROF.Salary > 1000
WHERE .C_Code IN ( AND PROF.Pre_Code = 'CS101';
SELECT pr.C_Code
FROM PREREQUISITES pr
WHERE pr.Pre_Code = 'CS101'
)
);

Sol Q2: Sol Q3: Using Self Join:

SELECT Major, AVG(GPA) AS Average_GPA SELECT DISTINCT s1.ST_ID, s2.ST_ID


FROM STUDENTS FROM STUDENTS s1, STUDENTS s2
GROUP BY Major WHERE s1.GPA = s2.GPA
HAVING COUNT(*) > 100; AND s1.Address = s2.Address
AND s1.Major != s2.Major;
Relational Algebra
Unary Operations
Selection(σ) Projection(π) Rename(ρ)

-Used to retrieve rows -Used to retrieve specific columns or -Used to rename


from a relation that attributes from a relation. relations or attributes in
satisfies a specific a relation.
-NOT commutative
condition or predicate.
- commutative
Ex: Ex: Ex:

σ Hoppy = ‘Stamp’(Person) π GPA, ID, major(Student) ρ (All Students, Students)


Condition table name new name old name

Binary Operations Example


Union (∪) Intersect (∩) Except ( ̶ )
Ex1: π Fname, address( σ name = ‘DB’(Dept Dnum=DNo Employee))
-combines two relations -compares two relations and -compares two relations
and returns returns a new relation that and returns a new relation
that contains only the Another way:
a new relation that contains only the tuples that are
tuples that exist in the first
contains all the tuples
common
relation but not in the
R1  σ name = ‘DB’(Dept)
to both relations. second relation. R2  R1 Employee
R3  π Fname, address(R2)
Ex: Ex: Ex:

π name(Student) ∪ R ̶ S Ex2:
retrieve name and GPA where majority = IS or IT and GPA greater than 3.0
π name(Professor)
from student.
Sol: π name, GPA( σ major = ‘IS’ or ‘IT’ and GPA > 3.0 (Dept))
R∩S
Another way: Another way:
Result: Result:
R1 π name(Student) R1  σ major = ‘IS’ or ‘IT’ and GPA > 3.0 (Dept)
R2  π name(Professor) R2  π name, GPA(R1)
R3  R1∪ R2

Transform SQL  Relational Algebra Aggregate Function

Ex1: Ex1: Fined the total amount owed to the credit company
Select name, age  π
From student
Where depno=1  σ Sol:
Sol: Ꞙsum(balance)(credit_acct)

πname, age ( σdeno=1(student)) Ex2: Fined the maximum available credit of any account.

Sol:
Ex2: Ꞙmax(available_credit)( π limit, balance(credit_acct))
Student(sid,name,age,dno)
Department (Did, depname)
Select depname
From student, department
Where dno=did and age > 30

Sol:

π depname( σage>30 (student dno = did department)


DB Design

Conceptual Model ER Model Component


*DB designer usually employs data models as Entities Attributes Relationships
communications tools
- to facilitate the interaction among the -Represented by a -Represented by oval Unary, Binary, Ternary
rectangle
designer, the programmer& the end user.
Type: Type: Type:
*Represent a global view of the data
- Regular(Strong) -Simple, Composite 1 to 1
*It is an enterprise-wide representation of data
- Weak -Derived 1 to *
*The conceptual model forms the basis for the
conceptual schema. - Recursive -single valued * to *
*The conceptual model is independent of both -Associative -Multi valued
software (software, hardware).
*Conceptual schema is a permanent Strong Entity Weak Entity Recursive Entity Associative Entity
description of DB requirements -Independent -Dependent -can exist between -independent
occurrences of the same -serves as a bridge
entity set. between the related
entities.
IE Notation -Has a P K that is -found within a unary -composed of the PK
partially or totally relationship. of each of the entities
derived from the to be connected.
parent entity(Strong
entity)

Participation Cardinality
-Describes the degree to which an -Defines the maximum and minimum number of
entity in one table is related to entities occurrences of related entities.
in another table.

-Optional (partial participation) - (1:1)


-Mandatory (total participation) - (1:N)
Unary Binary Ternary - (N:N)
-Association 2 entities are 3 entities are associated
is maintained associated
Within single
entity
ERD-into-Relation
Steps Ex1:
1) Mapping of regular entity types

2) Mapping of weak entity types

3) Mapping of binary 1:1 relationship types

4) Mapping of binary 1:N relationship types

5) Mapping of binary M:N relationship types

Symbol Meaning Sol:

Strong Entity

Weak Entity

Relationship

Ex2:
Identifying Relationship

Attribute

multivalued attribute

Sol:
Composite attribute

Derived Attribute
ERR Model

Specialization Generalization Completeness Constraint


-process of defining a set of subclasses -Reverse of the specialization process. Total Partial
of a superclass. -Original classes become its subclasses. -Specifies that every entity -allows an entity not to
Ex: Specialization to MANUFACTURED PART and Ex: Generalization to VEHICLE supertype in the superclass must be a belong to any of
PURCHASED PART member of some subclass. the subclasses.
-Shown in EER diagrams -Shown in EER diagrams
by a double line. by a single line.

Disjoin rule(d) Overlap rule(o)


Ex: -Entity instance can belong -(non-disjointness)
to only one subclass at a -Entity instances can
time belong to multiple
subclasses simultaneously

EER Into Relation:


1) Mapping of regular entity types
2) Mapping of weak entity types

Sol: 3) Mapping of binary 1:1 relationship types

4) Mapping of binary 1:N relationship types


5) Mapping of binary M:N relationship types
6) options for mapping specialization or
generalization
7) Step 9: Mapping of union types

Ex: convert schema into ER model

Sol:
Normalization
Normalization is a technique of
organizing the data into multiple
related tables, to minimize data
redundancy

Function Dependency:
describes a relationship
between attributes in a
single relation.
X→Y
Read: X determines Y
OR
Y depend on X

How do we know which stage we are in?


1NF  Table has [PK & Single value]
2NF  Table doesn’t contain [Partial dependency (Composite PK)]
3NF  Table doesn’t contain [transitive dependency]
[non-key attribute refers to another non-key attribute]

Bringing a
In 2NF relation to 3NF

In 3NF
Notes:
Note: Good Identifier division operator:
- used to find values in one table that match all
values in another table based on a specific condition.
-Select stable one
-allows you to retrieve records that have a specific
-rarely change relationship with another set of records.
-Less Composite
-Avoid concatenated (intelligent) Ex:
identifiers Name Skill
-Must be guaranteed Ron Educated
Ron Adaptable
Ron Confident
John Adaptable
Note: Single Operator with Single
Subquery John Educated
Multiple operator with multiple query Lona Adaptable
Lona Confident

Skill
Note: don’t add ORDER BY clause to a
Adaptable
Subquery
Educated

Result:
Note: Attribute should be Atomic Ron
(Can’t put multiple values in same cell) John

Note: Union operator Removes duplicate rows


Union all  preserves the duplicate rows

Note: 1th step in building an ER model is to  identify the entities


2nd step  identify attribute
3rd step  Establish relationships between entities
4th step  Define relationship attributes
5th step  Determine cardinality and participation
6th step  Normalize the model

https://www.indiabix.com/database/questions-and-
answers/

You might also like