Upload
molly-mccormick
View
232
Download
2
Tags:
Embed Size (px)
Citation preview
Eng: Ahmed Aly SaadEng: Ahmed Aly Saad
A Lecturer Assistant A Lecturer Assistant
Systems & Biomedical Eng. Systems & Biomedical Eng. Dept.,Dept.,
Faculty of EngineeringFaculty of Engineering
Cairo UniversityCairo University
Course DescriptionCourse Description
Structured Query Language (SQL) is Structured Query Language (SQL) is the standard for accessing data in the standard for accessing data in relational databases . Knowing SQL is relational databases . Knowing SQL is essential to using products from essential to using products from Microsoft , Oracle , IBM , and other Microsoft , Oracle , IBM , and other leading companiesleading companies
Course ObjectivesCourse Objectives
Relational Database Fundamentals.Relational Database Fundamentals. Database Design & Implementation using Database Design & Implementation using
SQL server 2000SQL server 2000 SQL basics.SQL basics. Use join techniques.Use join techniques. Understand database objects and Understand database objects and
constraints.constraints. Write useful SELECT , INSERT , UPDATE.Write useful SELECT , INSERT , UPDATE.
and DELETE statements.and DELETE statements.
TodayToday
Relational Database FundamentalsRelational Database Fundamentals Database Design & ImplementationDatabase Design & Implementation
Importance of DatabasesImportance of Databases
Traditional DB ApplicationsTraditional DB Applications Telephone index , bank , hotelTelephone index , bank , hotel Healthcare Information System (HIS),..Healthcare Information System (HIS),.. New DB ApplicationsNew DB Applications
Multimedia DB Multimedia DB Geographic Information System (GIS)Geographic Information System (GIS)
Data warehouse and Online Analytical Data warehouse and Online Analytical Processing (OLAP).Processing (OLAP).
Online Library , Email Servers… Online Library , Email Servers…
Definition & Properties Definition & Properties
Def: Collection of Related DataDef: Collection of Related Data Represents some aspects of the real Represents some aspects of the real
worldworld
(miniworld)(miniworld) Logically Coherent collection of data Logically Coherent collection of data
with some inherent meaningwith some inherent meaning Designed , built and populated with Designed , built and populated with
data for a specific purposedata for a specific purpose
Actors of the SceneActors of the Scene
Database AdministratorsDatabase Administrators Database DesignersDatabase Designers End usersEnd users System Analysts and application System Analysts and application
programmerprogrammer
Different Storage FacilitiesDifferent Storage Facilities
File Based SystemFile Based System DB based SystemDB based System
Database Management System Database Management System (DBMS)(DBMS)
Definition.Definition. Types of DBMS.Types of DBMS. How to choose your DBMS.How to choose your DBMS.
Design ProblemDesign Problem
You are asked to design a database You are asked to design a database for a company with the following for a company with the following specs.specs.
CompanyCompany
Requirements of the Company (oversimplified for illustrative purposes)
The company is organized into Departments. Each department has a name, number and an employee who manages the department. We keep track of the start date of the department manager.A department may have several locations.
Each department controls a number of Projects. Each project has a name, number and is located at a single location.
Company (Cont.)Company (Cont.)
We store each Employee's name, social security number, address, salary, sex, and birthdate. Each employee works for one department but may work on several projects. We keep track of
the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee
Each employee may have a number of Dependents. For each dependent, we keep track of their name, sex, birthdate, and relationship to employee
Design StepsDesign Steps
Step 1: Determine the main EntitiesStep 1: Determine the main Entities
Employee Department
Dependent Project
Design Steps (Cont.)Design Steps (Cont.)
Step 2: Determine the basic Step 2: Determine the basic independent attributes for each independent attributes for each entityentity
Employee Department
Dependent Project
Name
Number
Name
Number
Location
Name
SSN
Address
Salary
Sex Birthdate
First Name
SexBirthdate
FName
Minit
LName
Location
Relationship
Types of AttributesTypes of Attributes
Single versus Composite attributes.Single versus Composite attributes. Single Valued Attributes.Single Valued Attributes. Multi-Valued AttributesMulti-Valued Attributes
Design Steps (Cont.)Design Steps (Cont.)
Step 3: Determine the Primary Key Step 3: Determine the Primary Key for Each Entityfor Each Entity
Employee Department
Dependent Project
Name
Number
Name
Number
Location
Name
SSN
Address
Salary
Sex Birthdate
First Name
SexBirthdate
FName
Minit
LName
Location
Relationship
Primary Keys and Weak Primary Keys and Weak EntitiesEntities
Choice of Primary KeyChoice of Primary Key Composite Primary KeyComposite Primary Key Definition of Weak EntitiesDefinition of Weak Entities
Design Steps (Cont.)Design Steps (Cont.)
Step 4: Determine the Relationships Step 4: Determine the Relationships between different Entitiesbetween different Entities
Employee Department
Dependent Project
Name
Number
Name
Number
Location
Name
SSN
Address
Salary
Sex
Birthdate
First Name
SexBirthdate
FName
Minit
LName Works For
Mange
ControlsWorks_on
Dependent of
Supervise
Location
Relationship
Cardinality RatioCardinality Ratio
One – to – One (mange Relationship)One – to – One (mange Relationship)““each employee manges one department and each employee manges one department and
each department has one manger.”each department has one manger.” One – to – many ( works_for Relationship)One – to – many ( works_for Relationship)““each employee can work into one each employee can work into one
department but each department contains department but each department contains of several employeesof several employees”.”.
Many – to – Many (works_on Relationship)Many – to – Many (works_on Relationship)““each employee works on several projects each employee works on several projects
and each project contains several and each project contains several employeesemployees
Design Steps (Cont.)Design Steps (Cont.)
Step 5: Determine the Cardinality Step 5: Determine the Cardinality ratio for each relationshipratio for each relationship
Employee Department
Dependent Project
Name
Number
Name
Number
Location
Name
SSN
Address
Salary
Sex
Birthdate
First Name
SexBirthdate
FName
Minit
LName Works For
Mange
ControlsWorks_on
Dependent of
Supervise
N
1
N
M
1 1
1N1N
1
N
Location
Relationship
Design Steps (Cont.)Design Steps (Cont.)
Step 6: Determine the attributes of Step 6: Determine the attributes of the relationshipsthe relationships
Employee Department
Dependent Project
Name
Number
Location
Name
Number
Location
Name
SSN
Address
Salary
Sex
Birthdate
First Name
SexBirthdate
FName
Minit
LName Works For
Mange
ControlsWorks_on
Dependent of
Supervise
N
1
N
M
1 1
1N1N
1
N
hours
Start
Relationship
Relational Database Relational Database TerminologyTerminology
Table : a basic structure where in data Table : a basic structure where in data stored stored
DB has at least one tableDB has at least one table Field : “Column”Field : “Column” (max 1024 fields per table )(max 1024 fields per table ) Record : “Row”Record : “Row” (no physical limit on the # of (no physical limit on the # of
rows but the performance is affected) rows but the performance is affected)
Relational Database Relational Database Terminology (Cont.)Terminology (Cont.)
Key : field has a special meaningKey : field has a special meaning
Primary key : uniquely identifies a Primary key : uniquely identifies a record within a table.record within a table.
Foreign key : establish a Foreign key : establish a relationship between 2 tables “bases relationship between 2 tables “bases of Referential-data integrity”.of Referential-data integrity”.
Other concepts will be discussed later.Other concepts will be discussed later.
Table ExampleTable Example
EmployeEmployeee
FnameFname LnameLname SSNSSN AddressAddress
Ahmed Ahmed SaadSaad 1234512345 HaramHaram
AshrafAshraf AlyAly 3344533445 MokateMokatemm
RamyRamy AdelAdel 9876398763 ZamalekZamalek
Convert ER diagram to Convert ER diagram to TablesTables
In order to implement the ER In order to implement the ER diagram into any DBMS (i.e. SQL diagram into any DBMS (i.e. SQL server) . We need to convert the ER server) . We need to convert the ER diagram to Tablesdiagram to Tables
Mapping RulesMapping Rules
Rule1: Entity-------------Rule1: Entity-------------TableTable
EmployeeEmployee
DepartmeDepartmentnt
ProjectProject
DependenDependentt
Mapping Rules (Cont.)Mapping Rules (Cont.)
Rule2: simple and composite attribute Rule2: simple and composite attribute ------------------------ColumnsColumns
EmployeeEmployee SSNSSN FNamFNamee
MiniMinitt
LNameLName BDateBDate AddresAddresss
SalarySalary SexSex
ProjectProject PNumberPNumber PNamePName PLocationPLocation
DependenDependentt
Dependent_namDependent_namee
SexSex BirthdatBirthdatee
RelationshipRelationship
DepartmeDepartmentnt
DNumberDNumber DNameDName
Mapping Rules (Cont.)Mapping Rules (Cont.)
Rule3: convert the PKs to PKs into table Rule3: convert the PKs to PKs into table and the weak entity with P.K and F.Kand the weak entity with P.K and F.K
EmployeeEmployee SSNSSN FNamFNamee
MiniMinitt
LNameLName BDateBDate AddresAddresss
SalarySalary SexSex
ProjectProject PNumberPNumber PNamePName PLocationPLocation
DependenDependentt
Dependent_namDependent_namee
ESSNESSN SexSex BirthdateBirthdate RelationshiRelationshipp
DepartmeDepartmentnt
DNumberDNumber DNameDName
Mapping Rules (Cont.)Mapping Rules (Cont.)
Rule 4: multiValue Rule 4: multiValue new Table with new Table with F.KF.K
Dept_locationsDept_locations DNumbeDNumberr
DLocationDLocation
DepartmeDepartmentnt
DNumberDNumber DNameDName
ProjectProject PNumberPNumber PNamePName PLocationPLocation
EmployeeEmployee SSNSSN FNamFNamee
MiniMinitt
LNameLName BDateBDate AddresAddresss
SalarySalary SexSex
DependenDependentt
Dependent_namDependent_namee
ESSNESSN SexSex BirthdateBirthdate RelationshiRelationshipp
Mapping Rules (Cont.)Mapping Rules (Cont.) Rule 5: 1-1 and 1- N Rule 5: 1-1 and 1- N F.K in the many F.K in the many
table with relationship attributes table with relationship attributes Dept_locationsDept_locations DNumberDNumber DLocationDLocation
DepartmentDepartment DNumberDNumber DNameDName MGRSSNMGRSSN MgrStartDateMgrStartDate
ProjectProject PNumberPNumber PNamePName PLocationPLocation DNUMDNUM
EmployeeEmployee SSNSSN FNameFName MinitMinit LNameLName BDateBDate AddresAddresss
SalarySalary SexSex SuperSSNSuperSSN DNODNO
DependentDependent Dependent_nameDependent_name ESSNESSN SexSex BirthdateBirthdate RelationshipRelationship
Mapping Rules (Cont.)Mapping Rules (Cont.) Rule 6: M-N relationship - > new Table with 2 Rule 6: M-N relationship - > new Table with 2
F.KF.KDept_locationsDept_locations DNumberDNumber DLocationDLocation
DepartmentDepartment DNumberDNumber DNameDName MGRSSNMGRSSN MgrStartDateMgrStartDate
ProjectProject PNumberPNumber PNamePName PLocationPLocation DNUMDNUM
EmployeeEmployee SSNSSN FNameFName MinitMinit LNameLName BDateBDate AddresAddresss
SalarySalary SexSex SuperSSNSuperSSN DNODNO
DependentDependent Dependent_nameDependent_name ESSNESSN SexSex BirthdateBirthdate RelationshipRelationship
Works_OnWorks_On PNOPNO ESSNESSN HoursHours