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

Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Jan 10, 2016

Download

Documents

gaenor

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 : - PowerPoint PPT Presentation
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: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Ihr Logo

Fundamentals of

Database SystemsFourth Edition

El Masri & Navathe

Chapter 8SQL-99: Schema Definition,

Basic Constraints, and Queries

Page 2: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo

http://mohammedshbier.wordpress.com

Page 3: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo3

DefinitionSQL commonly expanded as Structured Query Language, is a computer language designed for :

1. Retrieval and management of data in relational database management systems.

2. Database schema creation and modification

3. 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.

Page 4: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo4

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) );

Page 5: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo5

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 );

Page 6: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo6

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;

Page 7: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo7

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

Page 8: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo8

SQL: ALTER TABLE (Cont.)Adding column(s) to a tableSyntax #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.

Page 9: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo9

SQL: ALTER TABLE (Cont.)Syntax #2To 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.

Page 10: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo10

SQL: ALTER TABLE (Cont.)Modifying column(s) in a tableSyntax #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.

Page 11: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo11

SQL: ALTER TABLE (Cont.)Modifying column(s) to a tableSyntax #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)   );

Page 12: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo12

SQL: ALTER TABLE (Cont.)Drop column(s) in a tableSyntax #1To 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.

Page 13: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo13

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 );

Page 14: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo14

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 );

Page 15: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo15

.

Data Type Syntax Explanation (if applicable)integer 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 real Single-precision floating point numberfloat 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 date Stores year, month, and day values.time time Stores the hour, minute, and second values.

timestamp timestamp Stores year, month, day, hour, minute, and second values.

SQL: Some Data Types in SQL2 and SQL-99

Page 16: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo16

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

Page 17: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo17

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

Page 18: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo18

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

Page 19: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo19

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, ADDRESSFROM EMPLOYEEWHERE FNAME='John' AND MINIT='B’

AND LNAME='Smith’;

Page 20: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo20

Simple SQL Queries (Cont.)

• Query 1: Retrieve the name and id of the projects which are performed in Houston, Statford.

SELECT PNAME, PNUMBERFROM PROJECTWHERE PLOCATION=‘Houston’

OR PLOCATON=‘Statford’;

Page 21: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo21

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, LNAMEFROM EMPLOYEEWHERE DNO=5

AND SALARY>1000;

Page 22: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo22

Simple SQL Queries (Cont.)

• Query 3: Retrieve the name of each dependent who is a son of some employee.

SELECT DEPENDENT_NAMEFROM DEPENDETWHERE RELATIONSHIP=‘SON’;

Page 23: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo23

Simple SQL Queries (Cont.)

• Query 4: Retrieve the name of each employee and his/her department name.

SELECT FNAME, MINIT, LNAME, DNAMEFROM EMPLOYEE, DEPARTMENTWHERE DNO=DNUMBER;

Page 24: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo24

Simple SQL Queries (Cont.)

• Query 5: Retrieve the name and address of all employees who work for the 'Research' department.

SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHERE DNO=DNUMBER

AND DNAME='Research‘;

Page 25: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo25

Simple SQL Queries (Cont.)

• Query 6: Retrieve the name of each son and the name of his father/mother name

SELECT DEPENDENT_NAMEFROM DEPENDENT, EMPLOYEEWHERE ESSN=SSN

AND RELATIONSHIP=‘SON’;

Page 26: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo26

Simple SQL Queries (Cont.)

• Query 7: Retrieve the name of each department and the name if its manager.

SELECT DNAME, FNAME, MINIT, LNAMEFROM DEPARTMENT,

EMPLOYEEWHERE MGRSSN=SSN;

Page 27: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo27

Simple SQL Queries (Cont.)

• Query 8: Retrieve the name of each project and the name of its controlling department.

SELECT PNAME, DNAMEFROM PROJECT, DEPARTMENT,WHERE DNUM=DNUMBER;

Page 28: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo28

Simple SQL Queries (Cont.)

• Query 9: Retrieve the name managers.

SELECT FNAME, MINIT, LNAMEFROM EMPLOYEE, DEPARTMENTWHERE SSN=MGRSSN;

Page 29: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo29

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, LNAMEFROM PROJECT, DEPARTMENT, EMPLOYEE

WHERE DNUM=DNUMBER AND MGRSSN=SSN;

Page 30: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo30

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, EMPLOYEEWHERE DNUM=DNUMBER AND MGRSSN=SSN

AND PLOCATION='Stafford’;

Page 31: Fundamentals of Database Systems Fourth Edition El Masri & Navathe

Your Logo31

USE OF *• To retrieve all the attribute values of the selected

tuples, a * is used, which stands for all the attributesExamples:

SELECT *FROM EMPLOYEEWHERE DNO=5;

SELECT *FROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research‘ AND DNO=DNUMBER;