OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.

Slides:



Advertisements
Similar presentations
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Advertisements

SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Database System - Assignment #3 Sept. 2012Yangjun Chen ACS Assignment #3 due Wed., Nov. 14, (30) Exercise 7.17 on Page 235 Show the result.
Defined by Edgar Codd in 1970 Defined by Edgar Codd in 1970 Considered ingenious but impractical Considered ingenious but impractical Conceptually simple.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database technology Lecture 2: Relational databases and SQL
Data Definition Languages Atif Farid Mohammad UNCC.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Design -- Basic SQL
Exploring Microsoft Access 2003 Chapter 4 Proficiency: Relational Databases, External Data, Charts, Pivot, and the Switchboard.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
- relation schema, relations - database schema, database state
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
Oracle8 - The Complete Reference. Koch & Loney1 Chapter 14. Changing Data: Insert, Update, Delete Presented by Victor M. Matos.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
Review: Application of Database Systems
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Relational Algebra - Chapter (7th ed )
Onsdag The concepts in a relation data model SQL DDL DML.
Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. p10ssn ← (Π essn (σ hours > 10 (works-on.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
How to build ER diagrams
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Logical Design database design. Dr. Mohamed Osman Hegaz2 Conceptual Database Designing –Provides concepts that are close to the way many users perceive.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Structured Query Language
Introduction to Database Systems
1 Database Systems Basic SQL. 2Outline  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries in SQL  INSERT,
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Fundamentals of DBMS Notes-1.
Chapter 10 SQL DDL.
The SQL Database Grammar
Chapter 4 Basic SQL.
Database Systems Basic SQL
Database Design The Relational Model Text Ch5
376a. Database Design Dept. of Computer Science Vassar College
376a. Database Design Dept. of Computer Science Vassar College
Mapping ER Diagrams to Tables
376a. Database Design Dept. of Computer Science Vassar College
11/9/2018.
Outline: Relational Data Model
Joining Tables ضم الجداول وإستخراج مناظر views منها الهدف : 1- استخراج المعلومات من جدولين أو اكثر بالإستفادة من الرابط بينهما وبإستخدام SQL 2- شروط قواعد.
Company Requirements.
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
CS4222 Principles of Database System
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Review: Application of Database Systems
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
1. Explain the following concepts: (a) superkey (b) key
1.(5) Describe the working process with a database system.
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column constraints: Not Null; Default and; Unique Step3: Multi-column constraints: Primary key Foreign key Step 4: Business process constraints (Check constraints) PART II GOAL: Introduction to SQL Language Unrestricted results Unordered or Ordered. Projection on certain specific columns of table. Restricted results using ‘where clause’

S TEP 1: C OLUMNS OF THE T ABLES AND D ATA TYPES Create Table Employee( Fname Varchar(15), Minit Char, Lname Varchar(15), Ssn Char(9), Bdate Date, Address Varchar(30), Sex Char, Salary Decimal(10,2), Super_ssn char(9), Dno INT);  Create Table Department( Dname Varchar(15), Dnumber INT, Mgr_ssn char(9), Mgr_start_date Date);

S TEP 1: C OLUMNS OF THE T ABLES AND D ATA TYPES  Create Table Dependent ( Essn Char(9), Dependent_name Varchar(15), Sex Char, Bdate Date, Relationship Varchar(8)); Downside: Poor quality data might get entered into the database. E.g. Too many NULLs are possible. Duplicate entries of employees and departments.

Create Table Employee( Fname Varchar(15) NOT NULL, Minit Char, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Bdate Date, Address Varchar(30), Sex Char, Salary Decimal(10,2), Super_ssn char(9), Dno INT NOT NULL DEFAULT 9);  Create Table Department( Dname Varchar(15) NOT NULL, Dnumber INT NOT NULL, Mgr_ssn char(9) NOT NULL DEFAULT , Mgr_start_date Date, Unique (Dname) ); Sometime we may want use default values for some fields to maintain consistency S TEP 2: S INGLE COLUMN CONSTRAINTS : Not NULL constraint help us to maintain data quality. Ensure each Dname is Unique, No duplicates are allowed

Create Table Employee( Fname Varchar(15) NOT NULL, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Super_ssn char(9), Dno INT NOT NULL DEFAULT 9, Primary Key (Ssn) );  Create Table Department( Dname Varchar(15) NOT NULL, Dnumber INT NOT NULL, Mgr_ssn char(9) NOT NULL DEFAULT , Mgr_start_date Date, Primary Key (Dnumber), Unique (Dname) ); Primary Key Vs Unique Constraint Primary Key can also defined as Unique + Not Null Constraint Many DBMS build an index on Primary Key (main advantage) S TEP 3: M ULTI - COLUMN CONSTRAINT : P RIMARY K EY

S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY Ref: Elmasari, Navathe, “Fundamentals of Database Systems” 6 th edition

Create Table Employee( Fname Varchar(15) NOT NULL, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Super_ssn char(9), Dno INT NOT NULL DEFAULT 9, Primary Key (Ssn), Foreign Key (Super_ssn) References Employee (Ssn), Foreign Key (Dno) References Department (Dnumber) );  Create Table Department( Dname Varchar(15) NOT NULL, Dnumber INT NOT NULL, Mgr_ssn char(9) NOT NULL DEFAULT , Mgr_start_date Date, Primary Key (Dnumber), Unique (Dname), Foreign Key (Mgr_ssn) References Employee (Ssn) ); Need to be careful while putting the constraints. Otherwise data entry becomes hard. Does this schema create any trouble? Circular reference Employee and Department refer to each other Consider adding some constraints later using ALTER Table command S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY

Create Table Employee( Fname Varchar(15) NOT NULL, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Super_ssn char(9), Dno INT NOT NULL DEFAULT 9, Primary Key (Ssn), Foreign Key (Super_ssn) References Employee (Ssn), Foreign Key (Dno) References Department (Dnumber) On Delete On Update ); SET NULL SET DEFAULT CASCADE RESTRICT/ NO ACTION SET NULL SET DEFAULT CASCADE RESTRICT/ NO ACTION S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY

S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY (S ET NULL / DEFAULT ) SET NULL: sets the value of the referenced column to NULL SET DEFAULT: Sets the value of the referenced column to the DEFAULT Value Create Table Employee( Fname Varchar(15) NOT NULL, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Super_ssn char(9), Dno INT NOT NULL DEFAULT 1, Primary Key (Ssn), Foreign Key (Super_ssn) References Employee (Ssn), Foreign Key (Dno) References Department (Dnumber) On Delete SET DEFAULT On Update SET NULL );

S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY (S ET NULL / DEFAULT ) What rows in Employee table are modified when Dnumber = 5 is deleted What rows in Employee table modified when Dnumber = 1 is changed to Dnumber = 3

S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY ( RESTRICT VS CASCADE ) CASCADE: cascades the effect to tuple containing the foreign key: For e.g. When used with on delete, if the tuple containing the referenced key is delete, then all the tuples with that foreign key are also deleted. RESTRICT: Produces an error indicating that the deletion or updating can violate a foreign key constraint. Create Table Employee( Fname Varchar(15) NOT NULL, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Super_ssn char(9), Dno INT NOT NULL DEFAULT 1, Primary Key (Ssn), Foreign Key (Super_ssn) References Employee (Ssn) On Delete RESTRICT On Update CASCADE, Foreign Key (Dno) References Department (Dnumber) );

S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY ( RESTRICT VS CASCADE ) What happens when record of Employee ‘Franklin Wong’ is deleted ? What happens when SSN of Employee ‘James Borg’ is updated to ‘ ’ ?

Ref: Elmasari, Navathe, “Fundamentals of Database Systems” 6 th edition S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY : E XERCISE

S TEP 3: M ULTI - COLUMN CONSTRAINT : F OREIGN K EY E XERCISE  Create Table Department( Dname Varchar(15) NOT NULL, Dnumber INT NOT NULL, Mgr_ssn char(9) NOT NULL, Mgr_start_date Date, Primary Key (Dnumber), Unique (Dname), Foreign Key (Mgr_ssn) References Employee (Ssn) );  Create Table Project( Pname Varchar(15) NOT NULL, Pnumber INT NOT NULL, Plocation Varchar(15), Dnum INT, Primary Key (Pnumber), Foreign Key (Dnum) References Department (Dnumber) On Delete Cascade On Update Cascade );  Create Table Works_on( Essn char(9) NOT NULL, Pno INT NOT NULL, Hours Decimal (3,1) NOT NULL, Primary Key (Essn,Pno), Foreign Key (Essn) References Employee(ssn), Foreign Key (Pno) Reference Project(Pnumber) On Delete Cascade On Update Cascade );

Identify the affected rows when record with Dnumber =4 is deleted

Create Table Employee( Fname Varchar(15) NOT NULL, Minit Char, Lname Varchar(15) NOT NULL, Ssn Char(9) NOT NULL, Bdate Date, Address Varchar(30), Sex Char, Salary Decimal(10,2), Super_ssn char(9), Dno INT NOT NULL DEFAULT 9, Constraint EMPSAL Check (Salary > 0.0) );  Create Table Department( Dname Varchar(15) NOT NULL, Dnumber INT NOT NULL, Dept_create_date Date NOT NULL, Mgr_ssn char(9) NOT NULL DEFAULT , Mgr_start_date Date, Constraint MGRDATE Check (Dept_create_date <= Mgr_start_date) ); Step 4: Business process constraints (Check constraints) Check constraints for maintaining the sanity of data

OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column constraints: Not Null; Default and; Unique Step3: Multi-column constraints: Primary key Foreign key Step 4: Business process constraints (Check constraints) PART II GOAL: Introduction to SQL Language Unrestricted results Unordered or Ordered. Projection on certain specific columns of table. Restricted results using ‘where clause’

“Hello world” example in SQL Language: SELECT * FROM Employee; ; Outputs the entire contents of the Table Employee Ordered results: SELECT * FROM Employee ORDER BY Lname ASC ; Outputs the entire contents of Employee ordered (in ascending order) on last name. DESC -> descending order Can give multiple column names e.g. ORDER BY Lname ASC, Fname ASC Part II: Introduction to SQL Language: Unrestricted Results

Projected only few columns in SQL Language: SELECT Ssn, Bdate, Address FROM Employee; Outputs the Ssn, Bdate and Address columns of Employee Ordered results: SELECT Lname, Ssn, Dno FROM Employee ORDER BY Lname ASC ; Outputs the Lname, Ssn and Dno of Employee ordered (in ascending order) on last name. Part II: Introduction to SQL Language: Unrestricted Results with Projection

Part II: Introduction to SQL Language: Restricted Results using where Clause Can use where clause to filter some unnecessary results: For instance if we only need the details of employees from Dept no 5 SELECT * FROM Employee WHERE Dno = 5; Outputs all the details of employees belonging to Dept no 5 Other logical comparators in SQL:, >=, <> Use ‘AND’ for conjunction of multiple conditions inside a where clause. SELECT * FROM Employee WHERE Dno = 5 AND Salary >100000;

Part II: Introduction to SQL Language: Restricted Results: Substring Pattern Matching Use the ‘LIKE’ comparison operator to specify comparison operations on only parts of the string. SELECT Fname, Lname FROM Employee WHERE Address LIKE ‘%Minneapolis,MN %’; Retrieves the employees who live in Minneapolis, MN % Arbitrary number of zero or more characters. _ Replaces with a single charector. \ Escape character for using _ and % as a literal character. “ Escape character for single quotation (‘).