Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

3/25/2017.
Data Definition and Integrity Constraints
1 Lecture 5: SQL Schema & Views. 2 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL)
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.
Yong Choi School of Business CSU, Bakersfield
Microsoft Access.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Lecture plan Oracle architecture SQL Data definition Queries
Functional Dependencies and Normalization for Relational Databases
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 5 The Relational Data Model and Relational Database Constraints.
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
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.
Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Database technology Lecture 2: Relational databases and SQL
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.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
SQL SQL (Structured Query Language) is used to define, query, and modify relational databases Every relational database system understands SQL SQL is standard:
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
1 CSE 480: Database Systems Lecture 9: SQL-DDL Reference: Read Chapter of the textbook.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
FEN  Queries: SELECT  Data Manipulation: INSERT, UPDATE, DELETE SQL: Structured Query Language – Part 2.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
Structured Query Language The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4.
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.
SQL: Schema Definition, Constraints, & Queries and Views.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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.
NOEA/IT - FEN: Databases/SQL1 SQL – part 1 SQL: DDL and DML.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Announcements Written Homework 1 due Nov 2 –See course web page –Exercises 5.12, 5.15, 6.17, 6.20, 6.22 (a,c,f only). Today –SQL (chapter 8) –Instructional.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Introduction to Database Systems
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 COMP 1100 Basic SQL David J. Stucki. Outline SQL Overview Retrievals Schema creation Table creation Constraints Inserts Updates 2.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
Chapter 10 SQL DDL.
The SQL Database Grammar
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
11/9/2018.
Company Requirements.
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
CS4222 Principles of Database System
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
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:
Presentation transcript:

Fundamentals of Database Systems Fourth Edition El Masri & Navathe Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

http://mohammedshbier.wordpress.com

Definition SQL commonly expanded as Structured Query Language, is a computer language designed for : Retrieval and management of data in relational database management systems. Database schema creation and modification Database object access control management. Although SQL is both an ANSI and an ISO standard, many database products support SQL with proprietary extensions to the standard language. Queries take the form of a command language that lets you select, insert, update, find out the location of data, and so forth. 3

SQL: CREATE TABLE Specifies a new base relation by giving it a name, and specifying each of its attributes and their data types (INTEGER, FLOAT, DECIMAL(i,j), CHAR(n), VARCHAR(n)). A constraint NOT NULL may be specified on an attribute. CREATE TABLE DEPARTMENT ( DNAME VARCHAR2(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9) ); 4

SQL: CREATE TABLE (Cont.) In SQL2, can use the CREATE TABLE command for specifying the primary key attributes, secondary keys, and referential integrity constraints (foreign keys). Key attributes can be specified via the PRIMARY KEY and UNIQUE phrases CREATE TABLE DEPT ( DNAME VARCHAR2(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP (SSN ); 5

SQL: DROP TABLE Used to remove a relation (base table) and its definition The relation can no longer be used in queries, updates, or any other commands since its description no longer exists Example: DROP TABLE DEPENDENT; 6

SQL: ALTER TABLE The ALTER TABLE command allows you to add, modify, or drop a column from an existing table The new attribute will have NULLs in all the tuples of the relation right after the command is executed; hence, the NOT NULL constraint is not allowed for such an attribute 7

SQL: ALTER TABLE (Cont.) Adding column(s) to a table Syntax #1 To add a column to an existing table, the ALTER TABLE syntax is: ALTER TABLE table_name  ADD column_name column-definition; For example: ALTER TABLE supplier  ADD supplier_name  varchar2(50); This will add a column called supplier_name to the supplier table. 8

SQL: ALTER TABLE (Cont.) Syntax #2 To add multiple columns to an existing table, the ALTER TABLE syntax is: ALTER TABLE table_name ADD ( column_1 column-definition, column_2 column-definition, ... column_n column_definition ); For example: ALTER TABLE supplier ADD ( supplier_name varchar2(50), city varchar2(45) ); This will add two columns (supplier_name and city) to the supplier table. 9

SQL: ALTER TABLE (Cont.) Modifying column(s) in a table Syntax #1 To modify a column in an existing table, the ALTER TABLE syntax is: ALTER TABLE table_name  MODIFY column_name column_type; For example: ALTER TABLE supplier  MODIFY supplier_name   varchar2(100)     not null; This will modify the column called supplier_name to be a data type of varchar2(100) and force the column to not allow null values. 10

SQL: ALTER TABLE (Cont.) Modifying column(s) to a table Syntax #2 To modify multiple columns in an existing table, the ALTER TABLE syntax is: ALTER TABLE table_name MODIFY ( column_1 column_type, column_2 column_type, ... column_n column_type ); For example: ALTER TABLE supplier MODIFY ( supplier_name varchar2(100) not null, city varchar2(75)   ); 11

SQL: ALTER TABLE (Cont.) Drop column(s) in a table Syntax #1 To drop a column in an existing table, the ALTER TABLE syntax is: ALTER TABLE table_name  DROP COLUMN column_name; For example: ALTER TABLE supplier  DROP COLUMN supplier_name; This will drop the column called supplier_name from the table called supplier. 12

SQL: REFERENTIAL INTEGRITY OPTIONS We can specify CASCADE, SET NULL or SET DEFAULT on referential integrity constraints (foreign keys) CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9) DEFAULT 1, MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP ON DELETE SET DEFAULT ON UPDATE CASCADE ); 13

SQL: REFERENTIAL INTEGRITY OPTIONS (Cont.) CREATE TABLE EMP ( ENAME VARCHAR(30) NOT NULL, ESSN CHAR(9), BDATE DATE, DNO INTEGER DEFAULT 1, SUPERSSN CHAR(9), PRIMARY KEY (ESSN), FOREIGN KEY (DNO) REFERENCES DEPT ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY (SUPERSSN) REFERENCES EMP ON DELETE SET NULL ON UPDATE CASCADE ); 14

SQL: Some Data Types in SQL2 and SQL-99 . Data Type Syntax Explanation (if applicable) integer numeric numeric(p,s) Where p is a precision value; s is a scale value. For example, numeric(6,2) is a number that has 4 digits before the decimal and 2 digits after the decimal. real Single-precision floating point number float float(p) Where p is a precision value. character char(x) Where x is the number of characters to store. This data type is space padded to fill the number of characters specified. character varying varchar2(x) Where x is the number of characters to store. This data type does NOT space pad. bit bit(x) Where x is the number of bits to store. date Stores year, month, and day values. time Stores the hour, minute, and second values. timestamp Stores year, month, day, hour, minute, and second values. 15

Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement Important distinction between SQL and the formal relational model; SQL allows a table (relation) to have two or more tuples that are identical in all their attribute values Hence, an SQL relation (table) is a multi-set (sometimes called a bag) of tuples; it is not a set of tuples SQL relations can be constrained to be sets by specifying PRIMARY KEY or UNIQUE attributes, or by using the DISTINCT option in a query 16

Retrieval Queries in SQL (Cont.) Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block SELECT <attribute list> FROM <table list> WHERE <condition> <attribute list> is a list of attribute names whose values are to be retrieved by the query <table list> is a list of the relation names required to process the query <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query 17

Retrieval Queries in SQL (Cont.) Note: All queries from now are on the company database, Book page No. :137. 18

Simple SQL Queries Basic SQL queries correspond to using the SELECT, PROJECT, and JOIN operations of the relational algebra All subsequent examples use the COMPANY database Example of a simple query on one relation Query 0: Retrieve the birthdate and address of the employee whose name is 'John B. Smith'. SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME='John' AND MINIT='B’ AND LNAME='Smith’; 19

Simple SQL Queries (Cont.) Query 1: Retrieve the name and id of the projects which are performed in Houston, Statford. SELECT PNAME, PNUMBER FROM PROJECT WHERE PLOCATION=‘Houston’ OR PLOCATON=‘Statford’; 20

Simple SQL Queries (Cont.) Query 2: Retrieve the full name of the employees who are working in department No. 5 and have more than 1000$ monthly salary. SELECT FNAME, MINIT, LNAME FROM EMPLOYEE WHERE DNO=5 AND SALARY>1000; 21

Simple SQL Queries (Cont.) Query 3: Retrieve the name of each dependent who is a son of some employee. SELECT DEPENDENT_NAME FROM DEPENDET WHERE RELATIONSHIP=‘SON’; 22

Simple SQL Queries (Cont.) Query 4: Retrieve the name of each employee and his/her department name. SELECT FNAME, MINIT, LNAME, DNAME FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER; 23

Simple SQL Queries (Cont.) Query 5: Retrieve the name and address of all employees who work for the 'Research' department. SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME='Research‘; 24

Simple SQL Queries (Cont.) Query 6: Retrieve the name of each son and the name of his father/mother name SELECT DEPENDENT_NAME FROM DEPENDENT, EMPLOYEE WHERE ESSN=SSN AND RELATIONSHIP=‘SON’; 25

Simple SQL Queries (Cont.) Query 7: Retrieve the name of each department and the name if its manager. SELECT DNAME, FNAME, MINIT, LNAME FROM DEPARTMENT, EMPLOYEE WHERE MGRSSN=SSN; 26

Simple SQL Queries (Cont.) Query 8: Retrieve the name of each project and the name of its controlling department. SELECT PNAME, DNAME FROM PROJECT, DEPARTMENT, WHERE DNUM=DNUMBER; 27

Simple SQL Queries (Cont.) Query 9: Retrieve the name managers. SELECT FNAME, MINIT, LNAME FROM EMPLOYEE, DEPARTMENT WHERE SSN=MGRSSN; 28

Simple SQL Queries (Cont.) Query 10: Retrieve the name of each project and the name of its controlling department and the name of the department manager. SELECT PNAME, DNAME, FNAME, MINIT, LNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN; 29

Simple SQL Queries (Cont.) Query 11: For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate.. SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford’; 30

USE OF * To retrieve all the attribute values of the selected tuples, a * is used, which stands for all the attributes Examples: SELECT * FROM EMPLOYEE WHERE DNO=5; SELECT * FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research‘ AND DNO=DNUMBER; 31