Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Learning Objectives:
Understand basic commands and functions of
SQL
Understand the use of SQL for data
administration (to create tables and indexes)
Understand the use of SQL for data
manipulation (to add, modify, delete, and
retrieve data)
Understand the use of SQL to query a database
for useful information
2
Introduction to SQL
What SQL can do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in database
SQL can delete records in database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in database
SQL can set permissions on tables, procedures 3and
views
SQL statement
4
Data Definition Language (DDL)
DDL manages table and index structure.
The most basic items of DDL:
CREATE – create an object (i.e. table) in the
database
DROP – deletes an object in the database, usually
irretrievably
ALTER – modifies the structure of an existing object
in various ways, for example, adding a column to an
existing table
5
Data Manipulation Language
(DML)
DML is the subset of SQL used to add, update and delete
data.
The acronym CRUD refers to all of the major functions that
need to be implemented in a relational database application
to consider it complete.
Each letter in the acronym can be mapped to a standard SQL
statement:
6
7
(1) CREATE TABLE statement
by 1
Foreign key
11
12
13
(2) ALTER TABLE statement
14
(3) INSERT INTO statement
Is to insert new row in a table
Can be written in 2 ways:
Method (1) does not specify the column names where the data will be
inserted, only their values:
15
(3) INSERT INTO statement…
cont
Method (2)
16
(4) UPDATE statement
The UPDATE statement is used to update existing records in a
table.
17
(5) DELETE statement
18
DELETE statement
19
(6) SELECT statement
This statement is used for retrieving rows from the
database and enables the selection of one or many rows
or columns from one or many table in the database
This is probably the most used SQL command.
20
SELECT statement
21
(7) ORDER BY keyword
22
ORDER BY … desc keyword
23
(8) SELECT DISTINCT statement
In a table, some of the columns may contain duplicate
values.
The DISTINCT keyword can be used to return only distinct
values.
24
(9) WHERE clause
25
Operators
26
Like operator (1)
27
Like operator (2)
28
Example:
29
Example:
31
Wildcards - examples
32
AND & OR operators
33
Combination used of AND & OR
operators
34
SELECT TOP clause
The TOP clause is used to specify the number of records to
return.
The TOP clause can be very useful on large tables with
thousands of records. Returning a large number of records
can impact on performance
35
Alias – you can give a table or a column
another name by using alias.
Example 1:
Select last_name, first_name, phone
From employees
AS contactList
Example 2:
Select first_name, Last_name AS Name
36
From employees
JOINS
SQL joins are used to query data from one or more tables,
based on a relationship between certain columns in these
tables.
The JOIN is used to join the primary key in one table with
the foreign key in another table
Different SQL joins:
JOIN: Return rows when there is at least one match in both
tables
LEFT JOIN: Return all rows from the left table, even if there are
no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there
are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the
tables
37
38
JOIN
39
40
41
42
43
44
45
EXERCISE…
46