Top Banner
SQL SQL Structured Query Structured Query Language Language Programming Course Programming Course
33
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL Structured Query Language Programming Course.

SQL SQL Structured Query LanguageStructured Query Language

Programming Course Programming Course

Page 2: SQL Structured Query Language Programming Course.

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

[email protected][email protected]

Page 3: SQL Structured Query Language Programming Course.

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

Page 4: SQL Structured Query Language Programming Course.

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.

Page 5: SQL Structured Query Language Programming Course.

TodayToday

Relational Database FundamentalsRelational Database Fundamentals Database Design & ImplementationDatabase Design & Implementation

Page 6: SQL Structured Query Language Programming Course.

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…

Page 7: SQL Structured Query Language Programming Course.

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

Page 8: SQL Structured Query Language Programming Course.

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

Page 9: SQL Structured Query Language Programming Course.

Different Storage FacilitiesDifferent Storage Facilities

File Based SystemFile Based System DB based SystemDB based System

Page 10: SQL Structured Query Language Programming Course.

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.

Page 11: SQL Structured Query Language Programming Course.

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.

Page 12: SQL Structured Query Language Programming Course.

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.

Page 13: SQL Structured Query Language Programming Course.

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

Page 14: SQL Structured Query Language Programming Course.

Design StepsDesign Steps

Step 1: Determine the main EntitiesStep 1: Determine the main Entities

Employee Department

Dependent Project

Page 15: SQL Structured Query Language Programming Course.

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

Page 16: SQL Structured Query Language Programming Course.

Types of AttributesTypes of Attributes

Single versus Composite attributes.Single versus Composite attributes. Single Valued Attributes.Single Valued Attributes. Multi-Valued AttributesMulti-Valued Attributes

Page 17: SQL Structured Query Language Programming Course.

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

Page 18: SQL Structured Query Language Programming Course.

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

Page 19: SQL Structured Query Language Programming Course.

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

Page 20: SQL Structured Query Language Programming Course.

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

Page 21: SQL Structured Query Language Programming Course.

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

Page 22: SQL Structured Query Language Programming Course.

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

Page 23: SQL Structured Query Language Programming Course.

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)

Page 24: SQL Structured Query Language Programming Course.

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.

Page 25: SQL Structured Query Language Programming Course.

Table ExampleTable Example

EmployeEmployeee

FnameFname LnameLname SSNSSN AddressAddress

Ahmed Ahmed SaadSaad 1234512345 HaramHaram

AshrafAshraf AlyAly 3344533445 MokateMokatemm

RamyRamy AdelAdel 9876398763 ZamalekZamalek

Page 26: SQL Structured Query Language Programming Course.

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

Page 27: SQL Structured Query Language Programming Course.

Mapping RulesMapping Rules

Rule1: Entity-------------Rule1: Entity-------------TableTable

EmployeeEmployee

DepartmeDepartmentnt

ProjectProject

DependenDependentt

Page 28: SQL Structured Query Language Programming Course.

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

Page 29: SQL Structured Query Language Programming Course.

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

Page 30: SQL Structured Query Language Programming Course.

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

Page 31: SQL Structured Query Language Programming Course.

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

Page 32: SQL Structured Query Language Programming Course.

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

Page 33: SQL Structured Query Language Programming Course.

??QUESTIONSQUESTIONS