Department of Computer Science
Fundamentals of database systems
Course code: comp4062
1 04/04/2024
Chapter Five
The SQL Language
2 04/04/2024
What is SQL?
SQL stands for Structured Query Language.
SQL is a standard language for accessing databases.
SQL is an ANSI (American National Standards Institute) standard
3 04/04/2024
What Can SQL 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 a database
SQL can delete records from a 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 a database
SQL can set permissions on tables, procedures, and views
4 04/04/2024
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS
SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables.
A table is a collection of related data entries and it consists of columns and rows.
5 04/04/2024
SQL Commands
The standard SQL commands to interact with
relational databases are CREATE, SELECT,
INSERT, UPDATE, DELETE, and DROP. These
commands can be classified into groups based on
their nature:
6 04/04/2024
Data Definition Language (DDL)
Data Definition Language (DDL) statements are used to define the
database structure or schema. Some examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all
spaces allocated for the records are removed
COMMENT - add comments to the data dictionary
RENAME - rename an object
7 04/04/2024
Data Manipulation Language (DML)
Data Manipulation Language (DML) statements are used for
managing data within schema objects. Some examples:
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
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency
8 04/04/2024
Data Control Language (DCL)
Data Control Language (DCL) statements. examples:
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with
the GRANT command
9 04/04/2024
SQL Data Types for Various DBs
Microsoft Access Data Types
Text AutoNumber
Memo Date/Time
Integer Yes/No
Single Ole Object
Double Lookup Wizard
Currency Hyperlink
Byte
10 04/04/2024
Database SQL Syntax
Tables
A database most often contains one or more tables. Each table is
identified by a name (e.g. "Customers" or "Orders"). Tables contain
records (rows) with data.
Sample database (included in MS Access and MS SQL Server).
Below is a selection from the "Customers" table:
11 04/04/2024
Customers table
Customer Customer Contact Address city Postal Country
ID Name Name Code
1 Alfred's Maria Obere Str. Berlin 12209 Germany
57
2 Ana Ana Avda. de la México 05021 Mexico
Trujillo Trujillo
3 Antonio Antonio Matadors México 05023 Mexico
Moreno 2312
4
Around Thomas 120 Londo WA1 UK
the Hardy Hanover n 1DP
Sq.
5 Berglund Christina Berguvsvä Luleå S-958 Sweden
ss gen 8 22
12 04/04/2024
cont’d…
The table above contains five records (one for each
customer) and seven columns (CustomerID,
Customer Name, Contact Name, Address, City,
Postal Code, and Country).
13 04/04/2024
SQL Statements
Most of the actions you need to perform on a database are
done with SQL statements.
The following SQL statement selects all the records in the
"Customers" table:
• Example:
SELECT * FROM Customers;
Keep in Mind That...
SQL is NOT case sensitive: SELECT is the same as select
14 04/04/2024
Semicolon after SQL Statements?
Semicolon after SQL Statements?
Some database systems require a semicolon at the end
of each SQL statement.
Semicolon is the standard way to separate each SQL
statement in database systems that allow more than
one SQL statement to be executed in the same call to
the server.
Due to this we will use semicolon at the end of each
SQL statement.
15 04/04/2024
Some of The Most Important SQL Commands
CREATE DATABASE :- SELECT :- extracts data from
creates a new database
a database
CREATE TABLE :- creates a
ALTER TABLE :- modifies a
new table
INSERT INTO :- inserts new
table
data into a database DROP TABLE :- deletes a
UPDATE :- updates data in a
table
database
CREATE INDEX :- creates an
DELETE :- deletes data from a
database index (search key)
ALTER DATABASE :- modifies a DROP INDEX :- deletes an
database
index
16 04/04/2024
SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name,column_name
FROM table_name;
and
SELECT * FROM table_name;
17 04/04/2024
SELECT Column Example
The following SQL statement selects the "Customer
Name" and "City" columns from the "Customers"
table:
Example:
SELECT CustomerName,City FROM Customers;
18 04/04/2024
SELECT * Example
The following SQL statement selects all the
columns from the "Customers" table:
Example:
SELECT * FROM Customers;
19 04/04/2024
SQL SELECT DISTINCT statement
The SELECT DISTINCT statement is used to return
only distinct (different) values.
The SQL SELECT DISTINCT Statement
In a table, a column may contain many duplicate values
and sometimes you only want to list the different (distinct)
values.
The DISTINCT keyword can be used to return only
distinct (different) values.
20 04/04/2024
SQL SELECT DISTINCT Syntax
SELECT
DISTINCT column_name,column_n
ame
FROM table_name;
21 04/04/2024
SQL
The WHERE WHERE
clause Clause
is used to filter records.
The WHERE clause is used to extract only those records that fulfill a specified
criterion.
SQL WHERE Syntax
SELECT column_name,column_name
FROM table_name
WHERE column name operator value;
22 04/04/2024
WHERE Clause Example
The following SQL statement selects all the
customers from the country "Mexico", in the
"Customers" table:
SELECT * FROM Customers
WHERE Country='Mexico';
23 04/04/2024
SQL AND & OR Operators
The AND & OR operators are used to filter records based on
more than one condition.
The AND operator displays a record if both the first
condition AND the second condition are true.
The OR operator displays a record if either the first
condition OR the second condition is true.
24 04/04/2024
AND Operator Example
The following SQL statement selects all customers from the country
"Germany" AND the city "Berlin", in the "Customers" table:
Example:
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
25 04/04/2024
OR Operator Example
The following SQL statement selects all customers
from the city "Berlin" OR "München", in the
"Customers" table:
Example:
SELECT * FROM Customers
WHERE City='Berlin'
OR City='München';
26 04/04/2024
Combining AND & OR
You can also combine AND OR (use parenthesis to form complex expressions).
The following SQL statement selects all customers from the country "Germany"
AND the city must be equal to "Berlin" OR "München", in the "Customers" table:
Example:
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');
27 04/04/2024
SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a
table in a database.
Tables are organized into rows and columns; and
each table must have a name.
28 04/04/2024
Cont’d…
SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
29 04/04/2024
Cont’d…
The column_name parameters specify the names of
the columns of the table.
The data_type parameter specifies what type of data
the column can hold (e.g. varchar, integer, decimal,
date, etc.).
The size parameter specifies the maximum length of
the column of the table.
30 04/04/2024
SQL CREATE TABLE Example
Now we want to create a table called "Persons" that contains five
columns: PersonID, Last Name, First Name, Address, and City.
Therefore, We use the following CREATE TABLE statement:
CREATE TABLE Persons
(
PersonID int,
Last Name varchar(255),
First Name varchar(255),
Address varchar(255),
City varchar(255)
);
31 04/04/2024
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form does not specify the column names where the data will be
inserted, only their values:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
32 04/04/2024
cont’d…
Assume we wish to insert a new row in the "Customers" table.
We can use the following SQL statement (without specifying column
names):
INSERT INTO Customers
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway');
or this SQL statement (including column names):
INSERT INTO Customers (CustomerName, ContactName, Address,
City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen
21','Stavanger','4006','Norway');
33 04/04/2024
It is alsoInsert
possibleData
to onlyOnly
insertin Specified
data in specificColumns
columns.
The following SQL statement will insert a new row, but only insert data in the
"CustomerName", "City", and "Country" columns (and the CustomerID field will of
course also be updated automatically):
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
34 04/04/2024
The SQL UPDATE Statement
The UPDATE statement is used to update existing
records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
35 04/04/2024
SQL UPDATE Example
Assume we wish to update the customer "Alfreds
Futterkiste" with a new contact person and city.
We use the following SQL statement:
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
36 04/04/2024
SQL DELETE Statement
The DELETE statement is used to delete rows in a
table.
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value;
37 04/04/2024
SQL DELETE Example
Assume we wish to delete the customer "Alfreds
Futterkiste" from the "Customers" table.
We use the following SQL statement:
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND
ContactName='Maria Anders';
38 04/04/2024
Delete All Data
It is possible to delete all rows in a table without deleting the table.
This means that the table structure, attributes, and indexes will be
intact:
DELETE FROM table_name;
or
DELETE * FROM table_name;
39 04/04/2024
SQL LIKE Operator
The LIKE operator is used to search for a specified
pattern in a column.
SQL LIKE Syntax
SELECT column name(s)
FROM table_name
WHERE column name LIKE pattern;
40 04/04/2024
SQL LIKE Operator Examples
The following SQL statement selects all customers
with a City starting with the letter "s":
Example
SELECT * FROM Customers
WHERE City LIKE 's%';
41 04/04/2024
cont’d…
The following SQL statement selects all customers
with a City ending with the letter "s":
Example
SELECT * FROM Customers
WHERE City LIKE '%s';
42 04/04/2024
The SQL BETWEEN Operator
The BETWEEN operator selects values within a range. The
values can be numbers, text, or dates.
SQL BETWEEN Syntax
SELECT column name(s)
FROM table_name
WHERE column name BETWEEN value1 AND value2;
43 04/04/2024
BETWEEN Operator Example
The following SQL statement selects all products
with a price BETWEEN 10 and 20:
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
44 04/04/2024
BETWEEN Operator with Text Value
The following SQL statement selects all products
with a Product Name beginning with any of the
letter BETWEEN 'C' and 'M':
Example
SELECT * FROM Products
WHERE Product Name BETWEEN 'C' AND 'M';
45 04/04/2024
End of chapter five………
?
46 04/04/2024