1 / 33

SQL-99: Schema Definition, Constraints, Queries, and Views

SQL-99: Schema Definition, Constraints, Queries, and Views. Ms. Hatoon Al- Sagri CCIS – IS Department. Database Design. Steps in building a database for an application:. Real-world domain. Conceptual model. DBMS data model. Create Schema (DDL). Modify data (DML).

colin
Download Presentation

SQL-99: Schema Definition, Constraints, Queries, and Views

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. SQL-99: Schema Definition, Constraints, Queries, and Views Ms. Hatoon Al-Sagri CCIS – IS Department

  2. Database Design Steps in building a database for an application: Real-world domain Conceptual model DBMS data model Create Schema (DDL) Modify data (DML)

  3. Data Manipulation Language (DML) Statements The main SQL data manipulation language statements are: • SELECT • INSERT INTO • UPDATE • DELETE FROM

  4. Notations Notations to define SQL statements: • UPPER-CASE letters represents reserved words • Lower-case letters represents user-defined words • | indicates a choice among alternatives; (e.g. a | b | c) • { } indicates a required element • [ ] indicates an optional element • … indicates optional repetition of an item zero or mote times • Underlined words represent default values

  5. SELECT Syntax SELECT [DISTINCT|ALL]{*|column|column_expression [AS new_name][,…]} FROM table_name [alias] [, … ] [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list [ASC|DESC]] • column represents a column name • column_expression represents an expression on a column • table_name is the name of an existing database table or view • FROM specifies the table(s) to be used • WHERE filters the rows subject to some condition • GROUP BY forms groups of rows with the same column name • SELECT specifies which column are to appear in the output • ORDER BY specifies the order of the output • Order of the clauses in the SELECT statement can not be changed • The result of a query is another table • Asterisk (*) means all columns.

  6. Retrieve all columns & rows • Syntax SELECT {* | column| column_expression [,…]} FROM table_name; • Example: Staff(StaffNo, fname,lname,position,sex,dob,salary, bno) Retrieve all staff information SELECT StaffNo,fname,lname,position,sex,dob,salary, bno FROM staff; OR SELECT * FROM staff;

  7. Retrieve all columns & rows StaffNo LName position Sex DOB Salary BrnNo FName SL21 John White Manager M 1-Oct-45 30000 B005 SG37 Ann Beech Assistant 10-Nov-60 F 12000 B003 SG14 David M Ford Supervisor 18000 B003 24-Mar-58 Mary SA9 9000 B007 Howe Assistant F 19-Feb-70 SG5 Susan Brand Manager 13-Jun-40 24000 B003 F

  8. Retrieve specific columns & all rows • Example: List salaries of all staff, showing only the staff number, the first and last name, and salary. SELECT StaffNo, fname, lname, salary FROM staff; StaffNo lName salary fName SL21 John White 30000 SG37 Ann Beech 12000 SG14 David Ford 18000 Mary SA9 9000 Howe SG5 Susan Brand 24000

  9. Use of DISTINCT • DISTINCT eliminates duplicated tuples. • SQL does not treat a relation as a set; the result of the query may contain duplicate tuples. Syntax SELECT [DISTINCT|ALL] {* | column |column_expression [,…]} FROM table_name; Example: List the available positions for staff SELECTDISTINCT position FROM staff;

  10. Use of DISTINCT position position Manager Manager Assistant Assistant Supervisor Supervisor Assistant Manager SELECT DISTINCT position FROM staff; SELECT position FROM staff;

  11. Use of DISTINCT Examples: SELECTALLSalary FROM EMPLOYEE; SELECT DISTINCTSalary FROM EMPLOYEE;

  12. Calculated fields • The SQL expression in the SELECT list specifies a derived field. • Columns referenced in the arithmetic expression must have a numeric type • SQL expression can involve +, - , * , / , ( , ) • AS caluse is used to name the derived column Syntax SELECT {* | column| column_expression [AS new_name] [,…]} FROM table_name;

  13. Calculated fields Example: List the monthly salaries for all staff, showing the staff number, the first and last names and salary. SELECT sno, fname, lname, salary/12 AS monthlySalary FROM staff; StaffNo LName MonthlySalary FName SL21 John White 2500 SG37 Ann Beech 1000 SG14 David Ford 1500 Mary SA9 Howe 750 SG5 Susan Brand 2000

  14. Calculated fields Example: Show the effect of giving all employees who work on the 'ProductX' project a 10% raise. SELECT Fname, Lname, 1.1*Salary AS Increased_sal FROM EMPLOYEE, WORKS_ON, PROJECT WHERE Ssn=EssnANDPno=PnumberANDPname='ProductX’;

  15. Row selection (WHERE clause) • 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.

  16. Row selection (WHERE clause) WHERE clause consists of five basic search conditions: • Comparison: Compare the value of one expression to the value of another expression (= , <, >, <=, >=, <>). • Range: Test whether the value of an expression falls within a specified range of values (BETWEEN/ NOT BETWEEN). • Set membership: Test whether the value of an expression equals one of a set of values (IN/ NOT IN). • Pattern match: Test whether a string matches a specified pattern (LIKE/ NOT LIKE). • NULL: Test whether a column has null value (IS NULL/ IS NOT NULL).

  17. WHERE clause Comparison search condition Comparison operators: = , <, >, <=, >=, <> Syntax SELECT [DISTINCT|ALL] {* | column| [column_expression [AS new_name]] [,…]} FROM table_name [WHERE condition]; Example: List all staff with a salary greater than 10,000. SELECT sno, fname, lname, position, salary FROM staff WHERE salary > 10000;

  18. WHERE clause Comparison search condition Example: List all staff with a salary greater than 10,000. SELECT sno, fname, lname, position, salary FROM staff WHERE salary > 10000; StaffNo LName Salary FName SL21 John White 30000 SG37 Ann Beech 12000 SG14 David Ford 18000 SG5 Susan Brand 24000

  19. WHERE clause Compound comparison search condition Compound comparison operators: AND, OR, NOT,( ) • Order of evaluation: • Expression is evaluated left to right • Between brackets • NOT • AND • OR

  20. WHERE clause Compound comparison search condition StaffNo LName position FName Example: List all staff who work as managers or assistants. SELECT sno, fname, lname, position FROM staff WHERE position = ‘Manager’ OR position =‘Assistant’; SL21 John White Manager SG37 Ann Beech Assistant Mary SA9 Howe Assistant SG5 Susan Brand Manager

  21. WHERE clause Compound comparison search condition Example: Retrieve the birth date 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’;

  22. WHERE clause BETWEEN/ NOT BETWEEN StaffNo LName Salary FName • BETWEEN checks if a value is within a range. • NOT BETWEEN checks if a value is outside a range. Example: List all staff with a salary between 20000 and 30000. SELECT sno, fname, lname, salary FROM staff WHERE salary BETWEEN20000AND30000; This would be expressed as: SELECT sno, fname, lname, salary FROM staff WHERE salary >= 20000 ANDsalary <= 30000; SL21 John White 30000 SG5 Susan Brand 24000

  23. WHERE clause BETWEEN/ NOT BETWEEN Example: Retrieve all employee in department 5 whose salary is between $30,000 and $40,000 SELECT * FROM EMPLOYEE WHERE (Salary BETWEEN30,000AND40,000)ANDDno=5;

  24. WHERE clause EXPLICIT SETS (IN/ NOT IN ) • IN tests whether a data value matches one of a list values. • NOT IN checks for data values that do not lie in a specific list of values. Example: List all Managers and Assistants. SELECT sno, fname, lname, position FROM staff WHERE position IN (‘Manager’, ‘Assistant’); • This would be expressed as: SELECT sno, fname, lname, position FROM staff WHERE position = ‘Manager’ OR position = ’Assistant’;

  25. WHERE clause IN/ NOT IN StaffNo LName position FName SL21 John White Manager SG37 Ann Beech Assistant Mary SA9 Howe Assistant SG5 Susan Brand Manager

  26. WHERE clause IN/ NOT IN • Example: Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN(1, 2, 3);

  27. WHERE clause SUBSTRING COMPARISON (LIKE/ NOT LIKE) • SQL has special pattern matching symbol: • % represents any sequence of zero or more character (wildcard) • _ represents any single character • Example • Address LIKE ‘H%’ means that the first character must be H, but the rest can be anything • Address LIKE ‘H_ _ _’ means that there must be exactly four characters in the string, the first of which must be H • Address LIKE ‘%e’ means any sequence of characters, of length at least 1, with the last character an e • Address LIKE ‘%Glasgow%’ means a sequence of characters of any length containing Glasgow • Address NOT LIKE ‘H%’ means the first character can not be H

  28. WHERE clause LIKE/ NOT LIKE StaffNo LName address Example: List all staff with the string ‘Glasgow’ in their address SELECT sno, fname, lname, address FROM staff WHERE address LIKE‘%Glasgow%’ ; SL21 John White Achray St,Glasgow G32 9DX SG37 Ann Beech Well St, Glasgow G42 FName

  29. WHERE clause LIKE/ NOT LIKE • Example: Retrieve all employees who were born during the 1950s. Here, '5' must be the third character of the string (according to our format for date), so the BDATE value is ‘_ _ 5_ _ _ _ _ _ _ ', with each underscore as a place holder for a single arbitrary character. SELECT Fname, Lname FROM EMPLOYEE WHERE BdateLIKE ‘_ _ 5_ _ _ _ _ _ _ ‘;

  30. WHERE clause IS NULL/ IS NOT NULL • A NULL value can not be tested with = or<> to another string. • We have to test for NULL explicitly. Example: Retrieve the names of all employees who do not have supervisors. SELECT Fname, Lname FROM EMPLOYEE WHERE Super_ssnIS NULL;

  31. Ordering of Query Results Allows the retrieved records to be ordered in ascending (ASC) or descending order (DESC) on any column or combination of columns. Syntax SELECT {* | [column_expression] [,…]} FROM table_name [ORDER BY column_list [ASC|DESC]] ORDER BYDnameDESC, LnameASC, FnameASC;

  32. Ordering of Query Results Single Column ordering: Produce a list of salaries for all staff, arranged in descending order of salary. SELECT sno, fname, lname, salary FROM staff ORDER BY salary DESC; Multiple columns ordering: Produce a list arranged in order of property type SELECT propertyNo, type, rooms, rent FROM property ORDER BY type, rent DESC;

  33. Ordering of Query Results PropertNo Rooms Rent Type SELECT propertyNo, type, rooms, rent FROM property ORDER BY type, rent DESC; PG16 Flat 4 450 PL94 Flat 4 400 PG36 Flat 3 370 House PG4 3 650 House PA14 6 600

More Related