SQL Structured Query Language Programming Course
Eng: Ahmed Aly Saad A Lecturer Assistant Systems & Biomedical Eng. Dept., Faculty of Engineering Cairo University
Course Description Structured Query Language (SQL) is the standard for accessing data in relational databases. Knowing SQL is essential to using products from Microsoft, Oracle, IBM, and other leading companies Structured Query Language (SQL) is the standard for accessing data in relational databases. Knowing SQL is essential to using products from Microsoft, Oracle, IBM, and other leading companies
Course Objectives Relational Database Fundamentals. Relational Database Fundamentals. Database Design & Implementation using SQL server 2000 Database Design & Implementation using SQL server 2000 SQL basics. SQL basics. Use join techniques. Use join techniques. Understand database objects and constraints. Understand database objects and constraints. Write useful SELECT, INSERT, UPDATE. Write useful SELECT, INSERT, UPDATE. and DELETE statements. and DELETE statements.
Today Relational Database Fundamentals Relational Database Fundamentals Database Design & Implementation Database Design & Implementation
Importance of Databases Traditional DB Applications Traditional DB Applications Telephone index, bank, hotel Telephone index, bank, hotel Healthcare Information System (HIS),.. Healthcare Information System (HIS),.. New DB Applications New DB Applications Multimedia DB Geographic Information System (GIS) Data warehouse and Online Analytical Processing (OLAP). Geographic Information System (GIS) Data warehouse and Online Analytical Processing (OLAP). Online Library, Servers… Online Library, Servers…
Definition & Properties Def: Collection of Related Data Represents some aspects of the real world Represents some aspects of the real world (miniworld) (miniworld) Logically Coherent collection of data with some inherent meaning Logically Coherent collection of data with some inherent meaning Designed, built and populated with data for a specific purpose Designed, built and populated with data for a specific purpose
Actors of the Scene Database Administrators Database Administrators Database Designers Database Designers End users End users System Analysts and application programmer System Analysts and application programmer
Different Storage Facilities File Based System File Based System DB based System DB based System
Database Management System (DBMS) Definition. Definition. Types of DBMS. Types of DBMS. How to choose your DBMS. How to choose your DBMS.
Design Problem You are asked to design a database for a company with the following specs. You are asked to design a database for a company with the following specs.
Company 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.) 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 Steps Step 1: Determine the main Entities Step 1: Determine the main Entities Employee Department DependentProject
Design Steps (Cont.) Step 2: Determine the basic independent attributes for each entity Step 2: Determine the basic independent attributes for each entity Employee Department Dependent Project Name Number Name Number Location Name SSN Address Salary SexBirthdate First Name Sex Birthdate FName Minit LName Location Relationship
Types of Attributes Single versus Composite attributes. Single versus Composite attributes. Single Valued Attributes. Single Valued Attributes. Multi-Valued Attributes Multi-Valued Attributes
Design Steps (Cont.) Step 3: Determine the Primary Key for Each Entity Step 3: Determine the Primary Key for Each Entity Employee Department Dependent Project Name Number Name Number Location Name SSN Address Salary SexBirthdate First Name Sex Birthdate FName Minit LName Location Relationship
Primary Keys and Weak Entities Choice of Primary Key Choice of Primary Key Composite Primary Key Composite Primary Key Definition of Weak Entities Definition of Weak Entities
Design Steps (Cont.) Step 4: Determine the Relationships between different Entities Step 4: Determine the Relationships between different Entities Employee Department Dependent Project Name Number Name Number Location Name SSN Address Salary Sex Birthdate First Name Sex Birthdate FName Minit LName Works For Mange Controls Works_on Dependent of Supervise Location Relationship
Cardinality Ratio One – to – One (mange Relationship) One – to – One (mange Relationship) “each employee manges one department and each department has one manger.” One – to – many ( works_for Relationship) One – to – many ( works_for Relationship) “each employee can work into one department but each department contains of several employees”. Many – to – Many (works_on Relationship) Many – to – Many (works_on Relationship) “each employee works on several projects and each project contains several employees
Design Steps (Cont.) Step 5: Determine the Cardinality ratio for each relationship Step 5: Determine the Cardinality ratio for each relationship Employee Department Dependent Project Name Number Name Number Location Name SSN Address Salary Sex Birthdate First Name Sex Birthdate FName Minit LName Works For Mange Controls Works_on Dependent of Supervise N 1 N M N 1N 1 N Location Relationship
Design Steps (Cont.) Step 6: Determine the attributes of the relationships Step 6: Determine the attributes of the relationships Employee Department Dependent Project Name Number Location Name Number Location Name SSN Address Salary Sex Birthdate First Name Sex Birthdate FName Minit LName Works For Mange Controls Works_on Dependent of Supervise N 1 N M N 1N 1 N hours Start Relationship
Relational Database Terminology Table : a basic structure where in data stored Table : a basic structure where in data stored DB has at least one table DB 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 rows but the performance is affected) (no physical limit on the # of rows but the performance is affected)
Relational Database Terminology (Cont.) Key : field has a special meaning Key : field has a special meaning Primary key : uniquely identifies a record within a table. Foreign key : establish a relationship between 2 tables “bases of Referential- data integrity”. Other concepts will be discussed later. Other concepts will be discussed later.
Table Example EmployeeFnameLnameSSNAddress AhmedSaad12345Haram AshrafAly33445Mokatem RamyAdel98763Zamalek
Convert ER diagram to Tables In order to implement the ER diagram into any DBMS (i.e. SQL server). We need to convert the ER diagram to Tables In order to implement the ER diagram into any DBMS (i.e. SQL server). We need to convert the ER diagram to Tables
Mapping Rules Rule1: Entity Table Rule1: Entity Table Employee Department Project Dependent
Mapping Rules (Cont.) Rule2: simple and composite attribute Columns Rule2: simple and composite attribute Columns EmployeeSSNFNameMinitLNameBDateAddressSalarySex ProjectPNumberPNamePLocation DependentDependent_nameSexBirthdateRelationship DepartmentDNumberDName
Mapping Rules (Cont.) Rule3: convert the PKs to PKs into table and the weak entity with P.K and F.K Rule3: convert the PKs to PKs into table and the weak entity with P.K and F.K EmployeeSSNFNameMinitLNameBDateAddressSalarySex ProjectPNumberPNamePLocation DependentDependent_nameESSNSexBirthdateRelationship DepartmentDNumberDName
Mapping Rules (Cont.) Rule 4: multiValue new Table with F.K Rule 4: multiValue new Table with F.K Dept_locationsDNumberDLocation DepartmentDNumberDName ProjectPNumberPNamePLocation EmployeeSSNFNameMinitLNameBDateAddressSalarySex DependentDependent_nameESSNSexBirthdateRelationship
Mapping Rules (Cont.) Rule 5: 1-1 and 1- N F.K in the many table with relationship attributes Rule 5: 1-1 and 1- N F.K in the many table with relationship attributes Dept_locationsDNumberDLocation DepartmentDNumberDNameMGRSSNMgrStartDate ProjectPNumberPNamePLocationDNUM EmployeeSSNFNameMinitLNameBDateAddressSalarySexSuperSSNDNO DependentDependent_nameESSNSexBirthdateRelationship
Mapping Rules (Cont.) Rule 6: M-N relationship - > new Table with 2 F.K Rule 6: M-N relationship - > new Table with 2 F.K Dept_locationsDNumberDLocation DepartmentDNumberDNameMGRSSNMgrStartDate ProjectPNumberPNamePLocationDNUM EmployeeSSNFNameMinitLNameBDateAddressSalarySexSuperSSNDNO DependentDependent_nameESSNSexBirthdateRelationship Works_OnPNOESSNHours
? QUESTIONS