Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Ihr Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries."— Presentation transcript:

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

2 Your Logo

3 Your Logo 3 Definition SQL 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.

4 Your Logo 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 (DNAMEVARCHAR2(10)NOT NULL, DNUMBERINTEGERNOT NULL, MGRSSNCHAR(9), MGRSTARTDATECHAR(9) );

5 Your Logo 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 (DNAMEVARCHAR2(10)NOT NULL, DNUMBERINTEGERNOT NULL, MGRSSNCHAR(9), MGRSTARTDATECHAR(9), PRIMARY KEY (DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGRSSN) REFERENCES EMP (SSN );

6 Your Logo 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;

7 Your Logo 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

8 Your Logo 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_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.

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

10 Your Logo 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_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.

11 Your Logo 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) );

12 Your Logo 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_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.

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

14 Your Logo 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 DEPT ON DELETE SET DEFAULT ON UPDATE CASCADE, FOREIGN KEY (SUPERSSN) REFERENCES EMP ON DELETE SET NULL ON UPDATE CASCADE );

15 Your Logo 15. Data TypeSyntaxExplanation (if applicable) integer numericnumeric(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 floatfloat(p)Where p is a precision value. characterchar(x) Where x is the number of characters to store. This data type is space padded to fill the number of characters specified. character varyingvarchar2(x) Where x is the number of characters to store. This data type does NOT space pad. bitbit(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. SQL: Some Data Types in SQL2 and SQL-99

16 Your Logo 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

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

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

19 Your Logo 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’;

20 Your Logo 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 WHEREPLOCATION=‘Houston’ OR PLOCATON=‘Statford’;

21 Your Logo 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 WHEREDNO=5 AND SALARY>1000;

22 Your Logo 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 WHERERELATIONSHIP=‘SON’;

23 Your Logo 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 WHEREDNO=DNUMBER;

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

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

26 Your Logo 26 Simple SQL Queries (Cont.) Query 7: Retrieve the name of each department and the name if its manager. SELECTDNAME, FNAME, MINIT, LNAME FROM DEPARTMENT, EMPLOYEE WHEREMGRSSN=SSN;

27 Your Logo 27 Simple SQL Queries (Cont.) Query 8: Retrieve the name of each project and the name of its controlling department. SELECTPNAME, DNAME FROM PROJECT, DEPARTMENT, WHEREDNUM=DNUMBER;

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

29 Your Logo 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, LNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHEREDNUM=DNUMBER AND MGRSSN=SSN;

30 Your Logo 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 FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN ANDPLOCATION='Stafford’;

31 Your Logo 31 USE OF * To retrieve all the attribute values of the selected tuples, a * is used, which stands for all the attributes Examples: SELECT * FROMEMPLOYEE WHEREDNO=5; SELECT* FROMEMPLOYEE, DEPARTMENT WHEREDNAME='Research‘ AND DNO=DNUMBER;


Download ppt "Ihr Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries."

Similar presentations


Ads by Google