Professional Documents
Culture Documents
DATABASE DESIGN
CHAPTER 4
Structured Query Language (SQL)
CHAPTER 4
WHAT YOU WILL LEARN:
SQL commands to a database.
Functions of the basic DDL
commands.
DML statements and
commands.
Functions for SQL advanced
commands.
SQL aggregate functions.
Introduction
• Main characteristics:
SQL is an ANSI and ISO standard computer language
for creating and manipulating databases.
SQL allows the user to create, update, delete, and
retrieve data from a database.
SQL is very simple and easy to learn.
Non-procedural language.
Consists of standard English words.
Advantages And Disadvantages
DISADVANTAGES
in mainframes, PCs, laptops, knowledge of the
servers and even mobile phones.
Easy to learn and understand:
structure of the database.
SQL mainly consists of English • Can provide misleading
statements and it is very easy to results.
learn and understand a SQL • Difficult Interface: SQL
query. has a complex interface that
• Used with any DBMS system
with any vendor.
makes it difficult for some
• No coding needed: It is very users to access it.
easy to manage the database
systems without any need to
write the substantial amount of
code by using the standard SQL.
• Multiple data views: By use of
SQL, different views of structure
and content of a database can
be provided for different users.
SQL STATEMENTS TYPES
SQL
UPDATE,
CREATE,
INSERT…INTO, GRANT, COMMIT,
ALTER, USE,
DELETE, REVOKE ROLLBACK
DROP
SELECT
CREATE THE DATABASE
https://www.tutorialspoint.com/sql/sql-constraints.htm
CONSTRAINT
• Ensures that all values in a column are different.
UNI • The UNIQUE Constraint prevents two records from having identical values in
a column.
QU
E
• The CHECK constraint ensures that all the values in a column satisfies
certain conditions.
CH • The CHECK Constraint enables a condition to check the value being entered
into a record. If the condition evaluates to false, the record violates the
EC constraint and isn't entered the table.
K
ALTER TABLE CUSTOMERS MODIFY (does not work with MySQL as MySQL does
AGE INT NOT NULL not support CHECK constraint)
CHECK (AGE >= 18 );
CONSTRAINT: DEFAULT
OR
• Enter data into selected columns (at least primary key column):
○ Result:
LastName FirstName Address City
Nilsen Fred Matang 22 Kuching
Rahmat Nina Gita 12 Kuching
REMOVE RECORDS - DELETE
◎ DELETE – to delete records in a table.
◎ Syntax:
DELETE FROM table_name
WHERE column_name = some_value;
◎ Consider this table: Person
LastName FirstName Address City
Nilsen Fred Matang 22 Kuching
Rahmat Nina Gita 12 Kuching
Example
address
address
CHERAS
BANGI CHERAS
BANGI BANGI
AMPANG
AMPANG
BANGI
KAJANG KAJANG
1 2
DUPLICATE ROWS (cont.)
OPERATOR DESCRIPTION
+ ADD
- SUBTRACT
* MULTIPLY
/ DIVIDE
EXAMPLE(using MySQL Workbench, database
sakila)
• SELECT first_name,last_name
FROM customer
WHERE customer_id = 1;
• SELECT first_name,last_name
FROM customer
WHERE customer_id <= 10;
BETWEEN…AND… operator
• SELECT title
FROM film
WHERE title like ‘%B%’;
• SELECT title
FROM film
WHERE title like ‘%A’;
Combining Wildcard Characters
◎Logical operator:
AND : returns true if both component conditions are
true.
OR : returns true if either component condition is true
Example
FUNCTION OUTPUT
COUNT Return the number of rows containing not null values
SUM The sum of all values for a selected attribute in a given
column
AVG Average for the specified column
MAX Maximum value encountered in a given column
MIN Minimum value encountered in a given column
AGGREGATE FUNCTIONS
◎COUNT EXAMPLE:
SELECT COUNT( film_id )
FROM film
WHERE special_features LIKE
‘%Deleted Scenes%';
Explain the difference…
Company OrderNumber
Sega 3412
ABC Shop 5678
W3Schools 6798
W3Schools 2312
◎SUM EXAMPLE:
SELECT SUM(amount)
FROM payment;
AGGREGATE FUNCTIONS
◎Syntax:
SELECT column1 [, column2 ]
FROM table1, table2
[WHERE condition]
INTERSECT
SELECT column1 [, column2 ]
FROM table1, table2
[WHERE condition]
EXAMPLE
INTERSECT
khilan
chaitali
Exercise (using MySQL
Workbench, database sakila)
• Based on table film_category, write SQL
codes to find the number of films for each
category.
• Based on table film_category, write SQL
codes to find the sum of payment for each
customer.
• Based on table film_category, write SQL
codes to find which category has more than
60 films.
RIDDLES
Two fathers and two sons went
fishing one day. They were there the
whole day and only caught 3 fish.
One father said, that is enough for
all of us, we will have one each. How
can this be possible?