Download SELECT

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

IMDb wikipedia , lookup

Concurrency control wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Functional Database Model wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Ingres (database) wikipedia , lookup

SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

PL/SQL wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Database model wikipedia , lookup

Transcript
1

Group members:
 Ngô
 Lê
Minh Khang - 1450234
Quang Trí - 1551152
 Nguyễn
Chí Bảo - 1552038
2
CONTENTS

History of SQL and SQL standards

SQL Overview:
 DDL:
Create, Alter, Drop
 DML:
Select, Insert, Update, Delete
 Advanced

References
DDL, VIEWs, DCL: Grant, Revoke
3

Developed by at IBM Corporation by Donald D.
Chamberlin and Raymond F. Boyce

Used Codd’s Model

Introduced the first SQL in 1979.

Accept as the standard RDBMS language.
Raymond F. Donald D.
Boyce
Chamberlin
4
5



CREATE statement is used to establish a new database, table, index, or
stored procedure.
 The syntax:

Create [type database] [database name]

Example: Create Database Library_management;
A commonly used CREATE statement is the CREATE TABLE statement
The typical usage:

Create Table [table name] (


);
([column definitions])
6

The COLUMN definitions are:
A
comma-separated list consisting of any of the following
 Column
definition: [column name] [data type] {NULL | NOT
NULL} {column options}
 PRIMARY
KEY definition: PRIMARY KEY ( [comma separated
column list] )
 Constraints:
{CONSTRAINT} [constraint definition]
7
8

The DROP statement DESTROY/DELETE an existing database, table,
index, or view.

The typical usage is simply:

DROP [Object type] [Object name]

For example, the command to drop a table
named employees is:
 DROP

TABLE employees;
Note: Be careful before dropping a table. Deleting a table will
result in loss of complete information stored in the table!
9

The ALTER TABLE statement is:

used to ADD, DELETE, or MODIFY columns in an
existing table.
 also
used to ADD and DROP various constraints on an
existing table.
10

ADD/DROP Column:
 ALTER
TABLE table_name
 ADD/DROP

COLUMN column_name;
ALTER/MODIFY Column (To change the data type of a
column in a table):
 ALTER
TABLE table_name
 ALTER/MODIFY
COLUMN column_name datatype;
11
12

Now we want to add a column named "DateOfBirth" in the "Persons" table.

We use the following SQL statement:


ALTER TABLE Persons

ADD DateofBirth date;
The “Persons” table will now look like this:
13
o
SELECT - retrieve data from the a database
o
INSERT - insert data into a table
o
UPDATE - updates existing data within a table
o
DELETE - deletes all records from a table, the space for the records remain
14
o The SELECT statement retrieves data from a database.
o The data is returned in a table-like structure called a
result-set.
o SELECT is the most frequently used action on a
database.
15
•
•
•
•
•
•
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
Specifies which columns are to appear in output
Specifies table(s) to be used
Filters rows
Forms groups of rows with same column value
Filters groups subject to some condition
Specifies the order of the output
16
Basic form of the SQL SELECT statement
is called a mapping or a SELECT-FROMWHERE block
SELECT <attribute list>
FROM <table list>
WHERE <condition>
17
General syntax:
1. SELECT column-names
2. FROM table-name
To select all columns use *:
1. SELECT *
2. FROM table-name
18
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
EXAMPLE: List all customers
SELECT *
FROM Customer
Id
FirstName
LastName
City
Country
Phone
1
Maria
Anders
Berlin
Germany
030-0074321
2
Ana
Trujillo
México D.F. Mexico
(5) 555-4729
3
Antonio
Moreno
México D.F. Mexico
(5) 555-3932
4
Thomas
Hardy
London
UK
(171) 555-7788
5
Christina
Berglund
Luleå
Sweden
0921-12 34 65
19
EXAMPLE:
SELECT FirstName, LastName,
City
FROM Customer
FirstName
Maria
Ana
Antonio
Thomas
Christina
LastName
Anders
Trujillo
Moreno
Hardy
Berglund
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
City
Berlin
México D.F.
México D.F.
London
Luleå
20
o SELECT DISTINCT returns only distinct
(different) values(eliminates duplicate
records from the results).
o DISTINCT can be used with aggregates:
COUNT, AVG, MAX, etc.
21
General syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name
Can be used with COUNT and other
aggregates
SELECT COUNT (DISTINCT column-name)
FROM table-name
22
SUPPLIER
EXAMPLE: List all supplier countries
SELECT DISTINCT Country
FROM Supplier
Country
Australia
Brazil
Canada
Denmark
... 16 rows
Id
CompanyName
ContactName
SELECT COUNT (DISTINCT
Country)
FROM Supplier
Count
16
City
Country
Phone
Fax
23
o The WHERE clause is used to extract
only those records that fulfill a
specified condition.
o WHERE is used with SELECT, UPDATE,
and DELETE.
24
General syntax:
SELECT column-names
FROM table-name
WHERE condition
25
Example: List the customers in Sweden
SELECT Id, FirstName, LastName,
City, Country, Phone
FROM Customer
WHERE Country = 'Sweden'
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Id
FirstName
LastName City
Country
Phone
5
Christina
Berglund
Luleå
Sweden
0921-12 34 65
24
Maria
Larsson
Bräcke Sweden
0695-34 67 21
26
Operators
can be
used in the
WHERE
clause:
Operator
Description
=
Equal
<>
Not equal. Note: In some versions of SQL this
operator may be written as !=
>
Greater than
<
Less than
>=
Greater than or equal
<=
Less than or equal
BETWEEN
Between an inclusive range
LIKE
Search for a pattern
IN
To specify multiple possible values for a column
27
o The GROUP BY statement is often used with
aggregate functions (COUNT, MAX, MIN,
SUM, AVG) to group the result-set by one or
more columns.
o General syntax:
SELECT column-names
FROM table-name
WHERE condition
GROUP BY column-names
28
Example: List the number of customers in
each country
CUSTOMER
SELECT COUNT(Id), Country
Id
FROM Customer
FirstName
GROUP BY Country
LastName
Count
Country
3
Argentina
2
Austria
2
Belgium
9
Brazil
3
Canada
City
Country
Phone
29
o HAVING filters records that work on summarized GROUP
BY results.
=> HAVING requires that a GROUP BY clause is present.
o HAVING applies to summarized group records, whereas
WHERE applies to individual records.
o Only the groups that meet the HAVING criteria will be
returned.
o WHERE and HAVING can be in the same query.
30
General syntax:
SELECT column-names
FROM table-name
WHERE condition
GROUP BY column-names
HAVING condition
31
Example: List the number of customers in each country. Only
include countries with more than 10 customers.
SELECT COUNT(Id),
Country
FROM Customer
GROUP BY Country
HAVING COUNT(Id) > 10
Count
Country
11
France
11
Germany
13
USA
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
32
o The ORDER BY keyword is used to sort the resultset in ascending or descending order.
o The ORDER BY keyword sorts the records in
ascending order by default. To sort the records
in descending order, use the DESC keyword.
33
Example: List the number of customers in
each country sorted high to low
SELECT COUNT(Id), Country
FROM Customer
GROUP BY Country
ORDER BY COUNT(Id) DESC
Count
Country
13
USA
11
France
11
Germany
9
Brazil
7
UK
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
34
o The INSERT INTO statement is used to insert
new records in a table.
o General syntax:
INSERT INTO table_name (column1, column2,
column3, ...)
VALUES (value1, value2, value3, ...);
35
Example:
INSERT INTO Customers (CustomerName,
ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
Custome Customer ContactN Address
rID
Name
ame
City
PostalCo
de
Country
91
Wolski
Zbyszek
ul.
Filtrowa
68
Walla
01-012
Poland
92
Cardinal
Tom B.
Erichsen
Skagen
21
Stavange 4006
r
Norway
36
o The UPDATE statement is used to modify
the existing records in a table
General syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
37
Example: Customer
Christina(Id = 5) has moved:
update city, phone and
country.
UPDATE Customer
SET City = ‘Berlin', Phone = '(0)1-953530',
Country=‘Germany’
WHERE Id = 5
Id
FirstName
LastName
City
Country
Phone
1
Maria
Anders
Berlin
Germany
030-0074321
2
Ana
Trujillo
México D.F. Mexico
(5) 555-4729
3
Antonio
Moreno
México D.F. Mexico
(5) 555-3932
4
Thomas
Hardy
London
UK
(171) 555-7788
5
Christina
Berglund
Berlin
Germany
(0)1953530
38
o The DELETE statement is used to
delete existing records in a table
General syntax:
DELETE FROM table_name
WHERE condition;
39
CustomerID
CustomerName
ContactName
City
Country
1
Alfreds Futterkiste
Maria Anders
Berlin
Germany
2
Ana Trujillo
Emparedados y helados
Ana Trujillo
México
D.F.
Mexico
3
Antonio Moreno
Taquería
Antonio Moreno
México
D.F.
Mexico
40
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
CustomerID
CustomerName Contact
Name
City
Country
2
Ana Trujillo
Emparedados y
helados
Ana
Trujillo
México
D.F.
Mexico
3
Antonio Moreno
Taquería
Antonio
Moreno
México
D.F.
Mexico
41

ASSERTIONs to express constraints that do not fit in the
basic SQL categories

Mechanism: CREATE ASSERTION
 Components
include: a constraint name, followed by
CHECK, followed by a condition
42

“The salary of an employee must not be greater than the salary of the
manager of the department that the employee works for’’
CREATE ASSERTION SALARY_CONSTRAINT
CHECK (NOT EXISTS (SELECT *
FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D
WHERE E.SALARY>M.SALARY AND E.DNO=D.NUMBER
D.MGRSSN=M.SSN))
AND
43

Data Definition Language (DDL) Triggers are operation that gets executed
automatically when a DDL Statements like CREATE, ALTER, DROP
statements are executed.
44

A view is a “virtual” table that is derived from other tables

Allows for limited update operations (since the table may not physically be
stored)

Allows full query operations

A convenience for expressing certain operations
45

SQL command: CREATE VIEW
a view (table) name
 a possible list of attribute names
 a query to specify the view contents


Specify a different WORKS_ON table (view)
CREATE VIEW WORKS_ON_NEW AS
SELECT FNAME, LNAME, PNAME, HOURS
FROM
EMPLOYEE, PROJECT, WORKS_ON
WHERE
SSN=ESSN AND PNO=PNUMBER
GROUP
BY PNAME;
46

We can specify SQL queries on a newly create table (view):
SELECT FNAME, LNAME FROM WORKS_ON_NEW
WHERE PNAME=‘Seena’;

When no longer needed, a view can be dropped:
DROP VIEW WORKS_ON_NEW;
47

Allows database administrators to configure security access to relational
databases.

DCL:

GRANT: add new permissions to a database user. Use the GRANT command
carefully because you money.

REVOKE: remove database access from a user previously granted such access.
48

Data Definition Language (DDL) statements are used to define the database structure or
schema.

CREATE - to create objects in the database

ALTER - alters the structure of the database

DROP - delete objects from the database

Data Manipulation Language (DML) statements are used for managing data within schema
objects.

SELECT - retrieve data from the a database

INSERT - insert data into a table

UPDATE - updates existing data within a table

DELETE - deletes all records from a table, the space for the records remain
49

https://www.youtube.com/watch?v=FR4QIeZaPeM for video.

http://www.dofactory.com

https://www.w3schools.com

Chapter 6 and 7 in R. Elmasri & S.B. Navathe (2016): Fundamentals of
Database Systems, 7th Edition, Addison-Wesley. (p177 – 238).
50
51