Vous êtes sur la page 1sur 62

Chapter -7 SQL 99 : Schema Definition , Basic Constraints, and Queries

Why a query language?


Given some data,
how should users and computer programs communicate with it?

we need an interface to the data


SQL does the job

Background


History


 

SEQUEL (Structured English QUEry Language) designed and implemented at IBM Research as the interface for an experimental relational database system called SYSTEM R. Joint effort by ANSI(American National Standards Institute) and ISO(International Standards Organization) has led to a standard version of SQL - SQL (ANSI 1986) called SQL1 or SQL86 Revised and much expanded version SQL2 or SQL92 Next version called SQL3 or SQL99

SQL is now the standard language for all commercial relational DBMSs. Sql -99 is divided into a core specification which is supposed to be implemented by all RDBMS vendors that are SQL-99 compliant plus optional specializedpackage SQLthat can be implemented as optional modules to be purchased independently for specific database applications such as data mining, spatial data, temporal data, data warehousing, on-line onanalytical procesing(OLAP), multimedia and so on.

Features of SQL


SQL is an English like language. It uses words such as select, insert, delete as part of its command set. SQL is a non procedural language: we specify what information we require, not how to get it. SQL processes sets of records rather than a single record at a time. The most common form of a set of records is a table. SQL can be used by a range of users including DBAs, application programmers, management personnel and many other types of end users. SQL provides command for a variety of tasks including: - querying data - inserting, updating and deleting rows in a table - creating modifying and deleting database objects - controlling access to the database and database objects - guaranteeing database consistency.

SQLSQL- a comprehensive database language




Data Definition Language (DDL)




Define/reDefine/re-define database structure Updates Queries Views Security, authorization Integrity constraints Transaction constraints Rules for embedding SQL statements into other languages like Java, C\C++, COBOL C\

Data Manipulation Language (DML)


 

Additional facilities
    

7.1 SQL DATA DEFINITION


Theoretical foundation:

The relational data model


relation table  tuple row  attribute column The above terms will be used interchangeably.


column1

columnn <row 2> <row n>

7.1.1 Schema and Catalog Concepts in SQL


 

 

Concept of schema was incorporated from SQL2 To group together tables and other constructs that belong to the same database application An SQL schema is identified by a schema name, and includes an name, authorization identifier to indicate the user or account who owns the schema, as well as descriptors for each element in the schema. Schema elements include tables, constraints, views, domains, and other constructs that describe the schema. A schema can be assigned a name and authorization identifier, and the elements can be defined later. e.g. -CREATE SCHEMA COMPANY AUTHORIZATION RITU; The privilege to create schemas, tables and other constructs must be explicitly granted to the relevant user accounts by the system administrator or DBA
7

 

SQL2 uses the concept of a catalog - a named collection of schemas in an SQL environment A catalog always contain a special schema called INFORMATION_SCHEMA, which provides information on all the schemas in the catalog and all the element descriptors in these schemas. Integrity constraints such as referential integrity constraints can be defined between relations only if they exist in schemas within the same catalog. Schemas within the same catalog can also share certain elements, such as domain definitions
8

Schema diagram for the COMPANY relational database schema

(Page no 106)

7.1.2 CREATE TABLE COMMAND


 

Used to specify a new relation by giving it a name and specifying its attributes and initial constraints Each attribute is given a name, a data type to specify its domain of values and any attribute constraints such as NOT NULL The key , entity integrity and referential integrity constraints can be specified within the CREATE TABLE statement after the attributes are declared, or they can be added later using the ALTER TABLE command
11

Syntax of create command




CREATE TABLE <table name>

( <column name> <data type>[(size)]<column


constraint>, <column name> <data type>[(size)]<column constraint>,. <table constraint>(<column name>,[,<column name>]).

);
12

CREATE TABLE emp ( empno integer NOT NULL, empname varchar(10), job varchar(10) , mgr integer, hiredate date, sal integer, comm integer, deptno integer not null, PRIMARY KEY (empno) );

What is a constraint ?


A constraint is a condition or a check applicable on a field or set of fields. Two basic types of constraints are column constraint and table constraint. Constraints ensure database integrity, thus called database integrity constraints. Few of them are : - NOT NULL constraint - Unique constraint - Primary key constraint - default constraint - check constraint

14

Unique Constraint
Ensures that no two rows have the same value in the specified columns. This constraint can be applied only to columns that have also been declared NOT NULL. CREATE TABLE emp ( empno integer NOT NULL UNIQUE, empname varchar(10), job varchar(10) , mgr integer, hiredate date, sal integer, comm integer, deptno integer not null, );

15

Primary Key Constraint




Does not allow NULL value Similar to unique constraint except that only one column (or one group of column) can be applied in this constraint.

CREATE TABLE emp ( empno integer NOT NULL primary key, empname varchar(10), job varchar(10) , mgr integer, hiredate date, sal integer, comm integer, deptno integer not null, );

16

Default Constraint


When a user does not enter a value for the column (having default value), automatically the defined default value is inserted in the field.

CREATE TABLE emp ( empno integer NOT NULL, empname varchar(10), job varchar(10) , mgr integer, hiredate date, sal integer, comm integer default = 15, deptno integer not null, PRIMARY KEY (empno) );
17

Check Constraint
Limits the value that can be inserted into a column of a table. CREATE TABLE emp ( empno integer NOT NULL, empname varchar(10), job varchar(10) , mgr integer, hiredate date, sal integer check (sal >20000), comm integer, deptno integer not null, PRIMARY KEY (empno) );


18

Table Constraints
When a constraint is to be applied to a group of columns of the table, it is called table constraint.  Table constraint appears at the end of the table definition CREATE TABLE members ( firstname varchar(20) NOT NULL, lastname varchar(20) NOT NULL, city char(10), PRIMARY KEY (firstname,lastname) );

19

INSERT Command (DML)




INSERT INTO tablename [(column, column,..)] VALUES (value, value,..); value,..);

INSERT INTO employee ( empno, ename, job, mgr, hiredate, sal, comm, deptno ) VALUES (7369, 'SMITH', 'CLERK', 7902, '06/13/1983', 800, NULL, 20); or INSERT INTO EMPlOYEE VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '08/15/1983', 1600, 300, 30); Here the order of values matches the order of columns in the CREATE TABLE command. Or INSERT INTO EMPLOYEE (empno, deptno) values(9999,10); Only those columns can be omitted that have either default value defined or they allow NULL values.
20

TABLES TO BE CREATED
EMPLOYEE TABLE EMPNO 7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934 ENAME SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER JOB MGR HIREDATE SAL DEPTNO CLERK 7902 13-Jun-83 800 20 SALESMAN 7698 15-Aug-83 1600 30 SALESMAN 7698 26-Mar-84 1250 30 MANAGER 7839 31-Oct-83 2975 20 SALESMAN 7698 5-Dec-83 1250 30 MANAGER 7839 11-Jun-84 2850 30 MANAGER 7839 14-May-84 2450 10 ANALYST 7566 5-Mar-84 3000 20 PRESIDENT 9-Jul-84 5000 10 SALESMAN 7698 4-Jun-84 1500 30 CLERK 7788 4-Jun-84 1100 20 CLERK 7698 23-Jul-84 950 30 ANALYST 7566 5-Dec-83 3000 20 CLERK 7782 21-Nov-83 1,300 10

DEPARTMENT TABLE DEPTNO DNAME LOCATION 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON .

21

SELECT Command
SELECT A1, A2, , An FROM r1, r2, , rm WHERE P;  A1, A2, , An list of attribute names to be retrieved  r1, r2, , rm List of tables required to process the query  P Conditional expression identifying the tuples to be retrieved


AND, OR, NOT, <, <=, =, >=, >

Result of the query is a table

SQL and the relational data model


   

Projection Cartesian product Selection Set operations


  

Assignment operator


Rename relations

Union Difference Intersection

Join  U join  Equijoin  Natural join

PROJECTION


Q1 To display all the columns of the table employee SELECT * from employee; Q2 To display empno, jab, salary SELECT empno, job, sal from employee; Q3 The order of selection determine the order of display SELECT job, empno, sal from employee; Q4 To display the department numbers to which the employees belong to : SELECT deptno from employee; Q5 To display only the distinct department numbers SELECT distinct deptno from employee;

24

ARITHMETIC EXPRESSIONS


 

Q6 Display the annual salary of all the employees SELECT ename, sal*12 from employee; Q7 SELECT ename, sal + 250 * 12; Q8 SELECT ename, (sal + 250) *12;

In Q7 multiplication(250*12) is evaluated first; then salary value is added to the result of the multiplication. In Q8 first 250 is added to the salary value and then it is multiplied by 12. If arithmetic expression contains more than one operator, the priority is * and / first, then + and second (left to right if there are several operators with the same priority)
25

COLUMN ALIASES


When displaying the result of the query SQL * Plus normally uses the selected columns name as the heading. In many cases it may be meaningless. A column alias gives a column an alternative heading on output. Specify the alias after the column in the select list. Q9 SELECT empname, sal *12 ANNSAL, comm from employee;
or SELECT empname, sal *12 as ANNSAL, comm from employee;
26

CONCATENATION OPERATOR


Q10.SELECT empname || job AS employee_job FROM emp; or

SELECT empname + " + job AS employee_job FROM emp;

27

SQL Operators


There are four SQL operators which operate with all data types : BETWEEN..AND between two values (inclusive) NOT BETWEENAND not between two given values IN(list) match any of the list values NOT IN not in the given list of values LIKE match a character pattern NOT LIKE does not match a character pattern IS NULL is a null value IS NOT NULL is not a null value

       

28

BETWEENAND


Q11(a). Those employees whose salary is between 1000 and 2000.

Select ename,sal from employee where sal between 1000 and 2000;


Q11(b). Those employees whose salary is not between 1000 and 2000.

Select ename,sal from employee where sal not between 1000 and 2000;
29

IN opeartor


Q12 To list the employees who belong to department no 10 or 30.

Select eno,ename from employee where deptno in(10,30);




Q13 List the details of the employees with employee number 7788,7369,7902,7876.

Select * from employee where eno in(7788,7369,7902,7876);

30

LIKE Operator
 

% is used for any sequence of zero or more characters - is used for any single character

Q14(a). List all the employees whose name start with S. Select ename from employee where ename like S%; Q14(b). List all the employees whose job does not start with M. Select ename from employee where ename not like M%; Q15. List all employees whose name has 4 characters. Select ename from employee where ename like ----; ----;
31

IS NULL operator
Q16(a). List the employees who have no manager. Select ename,mgr from employee where mgr is null;


Q16(b). List the empoyees who have Manager. Select ename,mgr from employee where mgr is not null;


32

Querying with multiple conditions




Q17(a) To find all clerks who earn between 1000 and 2000. Select eno,ename,job,sal from employee where job=CLERK and sal between 1000 and 2000; Q17(b) To find all employees who are either clerks or who earn between 1000 and 2000. Select eno, ename, job, sal from employee where job=CLERK or sal between 1000 and 2000;

Q18(a) Find all managers with salariws over 1500 and all salesman. Select eno,ename,job,sal from employee where sal>1500 and job=MANAGER or jab=SALESMAN; Q18(b) Find all managers and salesmen with salaries over 1500. Select eno,ename,job,sal from employee where sal>1500 and (job=MANAGER or jab=SALESMAN);

33

ORDER BY clause


Used to sort the rows. If used, it must be last clause in the SELECT statement. The default order is ascending.

Q19. To sort the table on name. select * from employee order by ename; Q20. To display employees with recently joined first. select ename,job,hiredate from employee order by date desc; desc; Q21. Make a departmentwise list of employees with employee having the highest salary on the top; Select deptno,job,ename from employee order by deptno,sal desc;
34

Aggregate Functions


They operate on a group of tuples and return a single value. avg min max Sum count count(*) stddev variance

       

35

Q22. To calculate the total salary of all managers. select sum(sal) from employee where job=MANAGER; Q23. Find the minimum salary earned by the clerk. select min(sal) from employee where job=CLERK; Q24. To count the total number of employees in department 10. select count(*) from employee where deptno=10; Q25. List the minimum, maximum, average salary of all the employees. select max(sal), min(sal), avg(sal) from employee; Q26. Find out how many managers are there. select count(*) from employee where job=MANAGER;


36

The SQL WHERE clause




Ex 1, Look for employee info > select * from employee where fname=John; Ex 2, Look for employee info > select * from employee where bdate > 1955-01-01 1955-01and salary between 30000 and 50000;

Ex 3, vector length!
> select x,y,z from vectors where x > 10 and x*x+y*y+z*z < 200;

Rel. algebra Cartesian product


Similar to Cartesian product of two vectors

v1

v2 -

vn v

v1 1 1 n ! / v n 1

v1 vn

The Cartesian product forms all possible pairs of the elements of the operands

The SQL FROM clause


Similarly, given two database tables
persons Alex John Mike cars Audi select * from persons, cars; Alex John Mike Alex John Mike Alex John Mike Audi Audi Audi BMW BMW BMW Mercedes Mercedes Mercedes
More #

BMW Mercedes

, this SQL query generates all possible persons-cars combinations.

Select from where

revisited
Basic SQL query: three clauses
select <projection-predicate> <projectionfrom <table list> where <selection-predicate> <selection-

Relational algebra  Cartesian product  Selection  Projection

Select from where


Ex 1: Find all employees working at research dept
SELECT FROM WHERE EMPLOYEE.LNAME, ADDRESS EMPLOYEE, DEPARTMENT DEPARTMENT.NAME=Research AND DNUMBER=DNO;

Ex 2: All employees and their managers


SELECT FROM WHERE E.FNAME, E.LNAME, S.FNAME, S.LNAME EMPLOYEE E, EMPLOYEE S E.SUPERSSN=S.SSN;

SQL and the relational data model


SELECT FROM WHERE


Assignment operator


projection, cartesian product, selection Set operations


  

Rename relations

Union Difference Intersection

Join  U join  Equijoin  Natural join

Operands must be union compatible

Examples of set operations




Retrieve all first names in the database


> select fname from employee union select dependent_name from dependent;

Are there any projects in a town without departments?


> select plocation FROM project p except select dlocation FROM dept_locations; #

SQL and the relational data model


SELECT FROM WHERE


Assignment operator


projection, cartesian product, selection




Rename relations

Set operations
  

Join  U join  Equijoin  Natural join

Union union Difference except Intersection intersect

Rename, assignment


Rename: as
> select distinct superssn as manager social security number from employee;

Assignment: create table as select


> create table names as select fname from employee union select dependent_name from dependent;

SQL and the relational data model


SELECT FROM WHERE


Assignment operator


projection, cartesian product, selection




Rename relations

Set operations
  

Join  U join  Equijoin  Natural join

Union union Difference except Intersection intersect

Join
 

Relational algebra notation: R C join condition




C is on the form AR U AS U is one of {=, <, >, , , {} Several terms can be connected as C1 C2CK. Equijoin: U is = Natural join: All identically named attributes in relations R and S have matching values

Special cases
 

SQL join


Recall this query


SELECT FROM WHERE EMPLOYEE.LNAME, ADDRESS EMPLOYEE, DEPARTMENT DEPARTMENT.NAME=Research AND DNUMBER=DNO;

Equijoin
 

of employee and department tables w.r.t. employee.dnumber and department.dno.

Joins are cartesian products with some selection criteria

SQL join


Another way:


alter table project change pnumber pno int(10);

One more example




Show the resulting salaries if every employee working on the ProductX project is given a 10 percent raise
FNAME, LNAME, 1.1*SALARY AS INC_SAL EMPLOYEE, WORKS_ON, PROJECT SSN=ESSN AND PNO=PNUMBER AND PNAME=ProductX;

SELECT FROM WHERE

Special comparison


Matching string patterns


  

Use LIKE % for any number of arbitrary symbol _ for any symbol
select * from employee where address like %Houston%;

Approx math equality




Use abs(x-x1) < I abs(xselect * from employee where abs(salary-30000) < 8000; abs(salary-

Use BETWEEN:
select * from employee where salary between 22000 and 38000;

NULL values


Sometimes an attribute is
  

Unknown Unavailable/withheld Not applicaple

(date of birth unknown) (refuses to list home phone #) (last college degree)

Need to represent these cases in a DB! Solution: NULL.




What about logical operations involving NULL?

Need to extend logic

3-valued logic
AND TRUE FALSE UNKNOWN OR TRUE FALSE UNKNOWN NOT TRUE TRUE FALSE UNKNOWN TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE UNKNOWN FALSE TRUE UNKNOWN UNKNOWN FALSE UNKNOWN UNKNOWN TRUE UNKNOWN UNKNOWN UNKNOWN UNKNOWN

Comparison of NULL values




=, {, >, <, LIKE,




wont work. NULL is UNDEFINED!

SQL check for NULL


 

IS NULL IS NOT NULL

JOIN operations


Tuples with NULL values in the join columns Not included in result Exception: OUTER JOIN (E/N 8.5.6)

NULL


Find out who is The Big Boss


select fname, lname from employee where superssn is NULL;

Aggregate functions
    

Avg Min Max Sum Count

average value minimum value maximum value sum of values number of values

Aggregate functions group by




Average salary
select avg(salary) from employee;

Average salary at each department


select dname, avg(salary) from employee, department where dno=dnumber group by dno;

Aggregate functions HAVING




Find the projects that more than two employees are assigned to:
  

retrieve the project number, its name, and the number of its employees SELECT project.pnumber, pname , count(*) FROM project, works_on WHERE project.pnumber = works_on.pno GROUP BY project.pnumber, pname HAVING count(*)>2;

Summary


Clauses:
SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY <attribute list> <table list> <condition>] <grouping attributes> <group condition>] <attribute list>] list>]

More Than One Way To Do It

Views


Frequently posed queries should be expressed as views.

> create view tax_view as select ssn, salary, salary*.327 from employee; > select * from tax_view;

Views


Creating a view will not result in a new table. Views are not tables themselves they are views of the underlying tables. A view query will return the state of the underlying tables. Consequence: underlying tables are changed the view will change

Views


Ex 1: > update table employee set salary = 1000000 where ssn = 123456; > select * from tax_view;

Ex 2: We are removing one column!


> alter table employee drop salary;

The view will not work any more


> select * from tax_view;