Vous êtes sur la page 1sur 66

SQL – Structured Query Language

• SQL –Structured Query Language a standard


that specifies how
– a relational schema is created
– data is inserted / updated in the relation
– data is queried
– transactions are started and stopped
– programs access data in the relations
Components of SQL standard
• Data Definition Language
– Specifies constructs for schema definition, relation definition, integrity
constraints, views and schema modification.
• Data manipulation Language
– Specifies constructs for inserting, updating and querying the data in
the relational instances ( or tables ).
• Embedded SQL and Dynamic SQL
– Specifies how SQL commands can be embedded in a high-level host
language such as C, C++ or Java for programmatic access to the data.
• Transaction Control
Specifies how transactions can be started / stopped, how a set of concurrently
executing transactions can be managed.

• Authorization
Specifies how to restrict a user / set of users to access only certain parts of data,
perform only certain types of queries etc.
DATA TYPES
• char(n) : Fixed length character string, with
user-specified length n.

• varchar(n) : Variable length character strings,


with user-specified maximum length n.

• int. Integer (a finite subset of the integers that


is machine-dependent).

• smallint : Small integer (a machine-


dependent subset of the integer domain type).

• numeric(p , d) : Fixed point number, with user-


specified precision of p digits, with d digits to
the right of decimal point. (ex., numeric(3,1),
allows 44.5 to be stores exactly, but not 444.5
or 0.32)

• float(n) : Floating point number, with user-


specified precision of at least n digits.
DATA DEFINITION LANGUAGE
CREATE TABLE CONSTRUCT
• CREATE TABLE instructor (
ID char(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
• CREATE TABLE department(
dept_id int,
dept_name
varchar(20))
INTEGRITY CONSRAINTS IN CREATE
• not null
TABLE
• primary key (A1, ..., An )
• foreign key (Am, ..., An ) references r
• Default
• Unique
• check

Example:
CREATE TABLE instructor (
ID char(5),
name varchar(20) not null unique,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references
department(dept_name),
check (ID < 50));

primary key declaration on an attribute automatically ensures not null


SQL – ALTER TABLE
• To add a column to a table
• ALTER TABLE table_name ADD COLUMN
column_name datatype

• To change datatype of a column


• ALTER TABLE table_name ALTER COLUMN
column_name datatype

• To drop a column from a table


• ALTER TABLE table_name DROP COLUMN
column_name
ALTERING TABLES TO ADD/REMOVE
CONSTRAINTS
• To add primary key constraint
• ALTER TABLE table_name ADD CONSTRAINT pk_name PRIMARY KEY (column_name)
• To drop a primary key constraint
• ALTER TABLE table_name DROP CONSTRAINT pk_name
• To add foreign key
• ALTER TABLE table_name ADD CONSTRAINT fk_name FOREIGN KEY (colum_name)
REFERNCES referenced_tablename(column_name)
• To remove foreign key
• ALTER TABLE table_name DROP CONSTRAINT fk_name
• To add a check constraint
– ALTER TABLE table_name
ADD CONSTRAINT chk_tablename CHECK (table_attribute1 >0 )
• To drop a check constraint
– ALTER TABLE table_name
DROP CONSTRAINT chk_tablename
ALTERING TABLES TO ADD/REMOVE
CONSTRAINTS
• Adding a default constraint
– ALTER TABLE department ALTER COLUMN dept_id
int NOT NULL;
– ALTER TABLE department ADD CONSTRAINT dft_id
DEFAULT 0 FOR dept_id;

• To drop a default constraint


• ALTER TABLE department DROP CONSTRAINT
dft_id
DROPING A TABLE
• DROP TABLE
– Deletes table structure
– Cannot be recovered
– Use with caution
SQL Command : DROP TABLE tablename;
TRUNCATING A TABLE
• Structure of a table is retained but all rows are
deleted.

TRUNCATE TABLE table_name


RETREIVAL QUERIES IN SQL
• SQL has one basic statement for retrieving information
from a database; the SELECT statement
• 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
Relational Database Schema
Populated
Database
SIMPLE SQL QUERIES
• Example of a simple query on one relation
• 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’
UNSPECIFIED WHERE-clause
• A missing WHERE-clause indicates no condition;
hence, all tuples of the relations in the FROM-
clause are selected.
• This is equivalent to the condition WHERE TRUE
• Retrieve the SSN values for all employees.
SELECT SSN FROM EMPLOYEE
USE OF *
• To retrieve all the attribute values of the
selected tuples, a * is used, which stands for
all the attributes
SELECT * FROM EMPLOYEE WHERE DNO=5
USE OF DISTINCT
• To eliminate duplicate tuples in a query result,
the keyword DISTINCT is used
• E.g.
– SELECT SALARY FROM EMPLOYEE
Will fetch all duplicate tuples containing same
values
SELECT DISTINCT SALARY FROM EMPLOYEE
Will fetch distinct values of salary without any
duplicate tuple
GETTING VALUES FROM MULTIPLE
TABLES
• A join Combines data from multiple tables using
foreign key references
• E.g.
GETTING VALUES FROM MULTIPLE
TABLES
• Syntax
SELECT column1, column2, …
FROM table1, table2
WHERE table1.joincolumn = table2.joincolumn
AND search_condition(s);
SELECT student.s_id, student.s_last, faculty.f_last
FROM student, faculty
WHERE student.f_id = faculty.f_id
AND f_last IN (‘Marx’, ‘Zhulin’);
• Must qualify column name in SELECT clause
– Specify name of table that contains column followed by period then
column name
– Example: SELECT s_id, s_last, student.f_id
• Join condition
– Specifies table names to be joined and column names on which to join
tables
– Example: WHERE student.f_id = faculty.f_id
ALIASES

• Some queries need to refer to the same relation twice


• In this case, aliases are given to the relation name
• E.g. For each employee, retrieve the employee's name, and
the name of his or her immediate supervisor.

SELECT E.FNAME, E.LNAME, S.FNAME,


S.LNAME FROM
EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.SUPERSSN=S.SSN
SET OPERATIONS
• SQL has directly incorporated some set operations

• There is a union operation (UNION), and in some


versions of SQL there are set difference (MINUS)
and intersection (INTERSECT) operations

• The resulting relations of these set operations are


sets of tuples; duplicate tuples are eliminated
from the result

• The set operations apply only to union compatible


relations ; the two relations must have the same
attributes and the attributes must appear in the
same order
SET OPERATIONS
• E.g. Make a list of all project numbers for projects that
involve an employee whose last name is 'Smith' as a
worker or as a manager of the department that controls
the project.
(SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME='Smith')
UNION
(SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND
LNAME='Smith')
SUBSTRING COMPARISON
• The LIKE comparison operator is used to compare
partial strings
• Two reserved characters are used: '%' (or '*' in
some implementations) replaces an arbitrary
number of characters, and '_' replaces a single
arbitrary character
• Retrieve all employees whose address is in
Houston, Texas. Here, the value of the ADDRESS
attribute must contain the substring 'Houston,TX'.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE '%Houston,TX%’
SUBSTRING COMPARISON (cont.)
• Retrieve all employees who were born during
the 1950s. Here, '5' must be the 7th 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 BDATE LIKE '_______5_’
ARITHMETIC OPERATIONS
• The standard arithmetic operators '+', '-‘, '*', and '/' (for
addition, subtraction, multiplication, and division,
respectively) can be applied to numeric values in an SQL
query result.

• Show the effect of giving all employees who work on the


'ProductX' project a 10% raise.

SELECT FNAME, LNAME, 1.1*SALARY


FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER AND
PNAME='ProductX’
ORDER BY
• The ORDER BY clause is used to sort the tuples in a query
result based on the values of some attribute(s)

• Retrieve a list of employees and the projects each works in,


ordered by the employee's department, and within each
department ordered alphabetically by employee last name.

e.g. SELECT * FROM instructor ORDER by


dept_name , salary
Where instructor table has attributes ID, name, dept_name,
salary
• The default order is in ascending order of values.
• We can specify the keyword DESC if we want a descending
order; the keyword ASC can be used to explicitly specify
ascending order, even though it is the default.
AGGREGATE FUNCTIONS
• Include COUNT, SUM, MAX, MIN, and AVG
• E.g. Find the maximum salary, the minimum
salary, and the average salary among all
employees.
SELECT MAX(SALARY), MIN(SALARY),AVG(SALARY)
FROM EMPLOYEE
AGGREGATE FUNCTIONS (cont.)
• Retrieve the total number of employees in the
company, and the number of employees in the
'Research' department.
SELECT COUNT (*) FROM EMPLOYEE

SELECT COUNT (*) FROM EMPLOYEE,


DEPARTMENT
WHERE DNO=DNUMBER AND
DNAME='Research’
GROUPING
• In many cases, we want to apply the aggregate functions to subgroups of
tuples in a relation.
• Each subgroup of tuples consists of the set of tuples that have the same value
for the grouping attribute(s).
• The function is applied to each subgroup independently.

• SQL has a GROUP BY-clause for specifying the grouping attributes, which must
also appear in the SELECT-clause.
• For each department, retrieve the department number, the number of
employees in the department, and their average salary.
SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO
– The EMPLOYEE tuples are divided into groups--each group having the same value for the
grouping attribute DNO
– The COUNT and AVG functions are applied to each such group of tuples separately
– The SELECT-clause includes only the grouping attribute and the functions to be applied on
each group of tuples
– A join condition can be used in conjunction with grouping
GROUPING (cont.)
• For each project, retrieve the project number,
project name, and the number of employees
who work on that project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
In this case, the grouping and functions are applied
after the joining of the two relations
THE HAVING-CLAUSE
• Sometimes we want to retrieve the values of these
functions for only those groups that satisfy certain
conditions.
• The HAVING-clause is used for specifying a selection
condition on groups (rather than on individual tuples)
• For each project on which more than two employees
work , retrieve the project number, project name, and the
number of employees who work on that project.
– SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
HAVING COUNT (*) > 2
NESTING OF QUERIES
• A complete SELECT query, called a nested query , can be specified
within the WHERE-clause of another query, called the outer query.
• Many of the previous queries can be specified in an alternative
form using nesting.
• Retrieve the name and address of all employees who work for the
'Research' department.

SELECT FNAME, LNAME, ADDRESS


FROM EMPLOYEE
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research' )
NESTING OF QUERIES (cont.)
• The nested query selects the number of the
'Research' department.

• The outer query select an EMPLOYEE tuple if its


DNO value is in the result of either nested query.

• The comparison operator IN compares a value v


with a set (or multi-set) of values V, and evaluates
to TRUE if v is one of the elements in V.

• In general, we can have several levels of nested


queries
CORRELATED NESTED QUERIES
• If a condition in the WHERE-clause of a nested query
references an attribute of a relation declared in the
outer query , the two queries are said to be correlated.
• Retrieve the name of each employee who has a
dependent with the same first name as the employee.
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT ESSN
FROM DEPENDENT
WHERE ESSN=E.SSN AND
E.FNAME=DEPENDENT_NAME)
THE EXISTS FUNCTION
• EXISTS is used to check whether the result of a
correlated nested query is empty (contains no
tuples) or not.
• Retrieve the name of each employee who has a
dependent with the same first name as the
employee.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN AND
FNAME=DEPENDENT_NAME)
THE EXISTS FUNCTION (cont.)
• Retrieve the names of employees who have no
dependents.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE NOT EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN)
The correlated nested query retrieves all DEPENDENT
tuples related to an EMPLOYEE tuple. If none exist ,
the EMPLOYEE tuple is selected
NULLS IN SQL QUERIES
• SQL allows queries that check if a value is NULL
(missing or undefined or not applicable).
• SQL uses IS or IS NOT to compare NULLs.
• E.g. Retrieve the names of all employees who do not
have supervisors.
– SELECT FNAME,LNAME
FROMEMPLOYEE
WHERE SUPERSSN IS NULL
SPECIFYING UPDATES IN SQL
• There are three SQL commands to modify the
database
– INSERT
– DELETE
– UPDATE
INSERTING VALUES IN TABLE
• INSERT Command
• In its simplest form, it is used to add one or more
tuples to a relation
• Attribute values should be listed in the same order as
the attributes were specified in the CREATE TABLE
command
• Only the constraints specified in the DDL commands
are automatically enforced by the DBMS when updates
are applied to the database.
• Another variation of INSERT allows insertion of
multiple tuples resulting from a query into a relation
INSERTING VALUES IN TABLE

INSERT INTO EMPLOYEE


VALUES ('Richard','K’, 'Marini', '653298653', '30-DEC-52',
'98 Oak Forest,Katy,TX', 'M', 37000,'987654321', 4 )
• An alternate form of INSERT specifies explicitly the attribute names
that correspond to the values in the new tuple
• Attributes with NULL values can be left out
• Example: Insert a tuple for a new EMPLOYEE for whom we only know
the FNAME, LNAME, and SSN attributes.
U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)
VALUES ('Richard', 'Marini', '653298653')

NOTE : Insertion of multiple tuples in a single insert query are also


allowed
INSERT (cont.)
• Suppose we want to create a table that has the name, number of
employees, and total salaries for each department. A table DEPTS_INFO is
created by Q1, and is loaded with the summary information retrieved
from the database by the query in Q2.

Q1: CREATE TABLE DEPTS_INFO


(DEPT_NAME VARCHAR(10),
NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);

Q2 : INSERT INTO DEPTS_INFO (DEPT_NAME,


NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME ;
DELETE
• Removes tuples from a relation
• Includes a WHERE-clause to select the tuples to be
deleted
• Tuples are deleted from only one table at a time
• A missing WHERE-clause specifies that all tuples
in the relation are to be deleted; the table then
becomes an empty table
• The number of tuples deleted depends on the
number of tuples in the relation that satisfy the
WHERE-clause
• Referential integrity should be enforced
DELETE (cont.)
1:DELETE FROM EMPLOYEE
WHERE LNAME='Brown’
2:DELETE FROM EMPLOYEE
WHERE SSN='123456789’
3:DELETE FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
4:DELETE FROM EMPLOYEE
UPDATING VALUES IN A TABLE
• Used to modify attribute values of one or more selected
tuples
• A WHERE-clause selects the tuples to be modified
• An additional SET-clause specifies the attributes to be
modified and their new values
• Each command modifies tuples in the same relation
• Referential integrity should be enforced
• Change the location and controlling department number
of project number 10 to ‘chandigarh' and 5, respectively.
UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER=10
UPDATE (cont.)
• Give all employees in the 'Research'
department a 10% raise in salary.
UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
• In this request, the modified SALARY value depends on the
original SALARY value in each tuple
• The reference to the SALARY attribute on the right of = refers
to the old SALARY value before modification
• The reference to the SALARY attribute on the left of = refers to
the new SALARY value after modification
SELECT from Multiple Tables
• Often you need to combine information from
two or more tables
• You can get the effect of a product by using
SELECT * FROM Table1, Table2...
• If the tables have columns with the same name
ambiguity results
• You resolve this by referencing columns with
the table name
TableName.Column
SELECT from Multiple Tables
Student
SELECT
ID First Last
First, Last, Mark
S103 John Smith
FROM Student, Grade S104 Mary Jones
S105 Jane Brown
WHERE S106 Mark Jones
(Student.ID = S107 John Brown

Grade.ID) AND
Grade
(Mark >= 40) ID Code Mark
S103 DBS 72
S103 IAI 58
S104 PR1 68
S104 IAI 65
S106 PR2 43
S107 PR1 76
S107 PR2 60
S107 IAI 35
SELECT from Multiple Tables
SELECT ... FROM Student, Grade WHERE...

Are
matched
with the first
entry from All of the
the Student entries from
table... the Grade
table

And then
with the
second…

and so on
SELECT from Multiple Tables
SELECT ... FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND
...
SELECT from Multiple Tables
• SELECT ... FROM Student, Grade
WHERE (Student.ID = Grade.ID) AND
(Mark >= 40)

ID First Last ID Code Mark


S103 John Smith S103 DBS 72
S103 John Smith S103 IAI 58
S104 Mary Jones S104 PR1 68
S104 Mary Jones S104 IAI 65
S106 Mark Jones S106 PR2 43
S107 John Brown S107 PR1 76
S107 John Brown S107 PR2 60
SELECT from Multiple Tables
• SELECT First, Last, Mark FROM Student,
Grade WHERE (Student.ID = Grade.ID)
AND(Mark >= 40)

First Last Mark


John Smith 72
John Smith 58
Mary Jones 68
Mary Jones 65
Mark Jones 43
John Brown 76
John Brown 60
SELECT from Multiple Tables
• When selecting from multiple tables you
almost always use a WHERE clause to find
entries with common values

SELECT * FROM Student, Grade, Course WHERE


Student.ID = Grade.ID AND
Course.Code = Grade.Code
SELECT from Multiple Tables
JOINs
• JOINs can be used to combine tables
– There are many types of JOIN
• CROSS JOIN
• INNER JOIN
• NATURAL JOIN
• OUTER JOIN
• A CROSS JOIN B
– returns all pairs of rows from A and B
• A NATURAL JOIN B
– returns pairs of rows with common values for identically
named columns and without duplicating columns
• A INNER JOIN B
– returns pairs of rows satisfying a condition
CROSS JOIN
SELECT * FROM Student CROSS JOIN
Enrolment
NATURAL JOIN
SELECT * FROM Student NATURAL JOIN Enrolment
CROSS and NATURAL JOIN
• SELECT * FROM A CROSS JOIN B
is the same as
SELECT * FROM A, B

• SELECT * FROM A NATURAL JOIN B


is the same as
SELECT A.col1,… A.coln, [and all other
columns apart from B.col1,…B.coln]
FROM A, B
WHERE A.col1 = B.col1
AND A.col2 = B.col2
...AND A.coln = B.col.n
(this assumes that col1… coln in A and B
have common names)
INNER JOIN
• INNER JOINs specify a condition which the pairs of
rows satisfy
SELECT * FROM A INNER JOIN B
ON <condition>
• Can also use
SELECT * FROM A INNER JOIN B
USING (col1, col2,…)
Chooses rows where the given columns are equal
INNER JOIN
• SELECT * FROM A INNER JOIN B
ON <condition>
is the same as
SELECT * FROM A, B WHERE <condition>

• SELECT * FROM A INNER JOIN B USING(col1,


col2,...)
is the same as
SELECT * FROM A, B WHERE A.col1 = B.col1
AND A.col2 = B.col2 AND ...
JOINs vs WHERE Clauses
• JOINs (so far) are not needed
– You can have the same effect by selecting from multiple
tables with an appropriate WHERE clause
– So should you use JOINs or not?
OUTER JOINS
Implicit joins in SQL = “inner joins”:
Product(name, category)
Purchase(prodName, store)
• SELECT Product.name, Purchase.store FROM Product JOIN Purchase ON
Product.name = Purchase.prodName

Same as:
• SELECT Product.name, Purchase.store FROM Product, Purchase WHERE
Product.name = Purchase.prodName

But Products that never sold will be lost !


Left outer joins in SQL:
Product(name, category)
Purchase(prodName, store)

• SELECT Product.name, Purchase.store FROM Product LEFT


OUTER JOIN Purchase ON Product.name = Purchase.prodName
OUTERJOINS
APPLICATION
Compute, for each product, the total number of sales in
‘September’
Product(name, category)
Purchase(prodName, month, store)

• SELECT Product.name, count(*) FROM Product,


Purchase WHERE Product.name =
Purchase.prodName AND Purchase.month =
‘September’ GROUP BY Product.name
APPLICATION
Compute, for each product, the total number of sales in
‘September’
Product(name, category)
Purchase(prodName, month, store)

• SELECT Product.name, count(*) FROM Product LEFT OUTER


JOIN Purchase ON Product.name = Purchase.prodName
AND Purchase.month = ‘September’ GROUP BY Product.name

Now we also get the products who sold in 0 quantity


OUTER JOINS
• Left outer join:
– Include the left tuple even if there’s no match
• Right outer join:
– Include the right tuple even if there’s no match
• Full outer join:
– Include the both left and right tuples even if there’s no
match

Vous aimerez peut-être aussi