1 / 31

Fundamentals of Database Systems Fourth Edition El Masri & Navathe

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 :

gaenor
Download Presentation

Fundamentals of Database Systems Fourth Edition El Masri & Navathe

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamentals ofDatabase SystemsFourth EditionEl Masri & Navathe Chapter 8SQL-99: Schema Definition, Basic Constraints, and Queries

  2. http://mohammedshbier.wordpress.com

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

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

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

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

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

  8. 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_nameADD column_name column-definition; • For example: • ALTER TABLE supplierADD supplier_name  varchar2(50); • This will add a column called supplier_name to the supplier table. 8

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

  10. 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_nameMODIFY column_name column_type; • For example: • ALTER TABLE supplierMODIFY 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

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

  12. 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_nameDROP COLUMN column_name; • For example: • ALTER TABLE supplierDROP COLUMN supplier_name; • This will drop the column called supplier_name from the table called supplier. 12

  13. 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 EMPON DELETE SET DEFAULT • ON UPDATE CASCADE ); 13

  14. 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 DEPTON DELETE SET DEFAULT ON UPDATE CASCADE,FOREIGN KEY (SUPERSSN) REFERENCES EMPON DELETE SET NULL ON UPDATE CASCADE ); 14

  15. SQL: Some Data Types in SQL2 and SQL-99 15

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

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

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

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

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

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

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

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

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

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

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

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

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

  29. 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; 29

  30. 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’; 30

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

More Related