Professional Documents
Culture Documents
Database1 Final Revision ٠٤٥٢٢٤
Database1 Final Revision ٠٤٥٢٢٤
Database1 Final Revision ٠٤٥٢٢٤
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
DB States
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
Key Constraints
-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
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
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
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%');
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:
π 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
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:
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.
Strong Entity
Weak Entity
Relationship
Ex2:
Identifying Relationship
Attribute
multivalued attribute
Sol:
Composite attribute
Derived Attribute
ERR 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
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
https://www.indiabix.com/database/questions-and-
answers/