FDB Lecture05
FDB Lecture05
FDB Lecture05
Fundamentals of
Database
Prepared by: Khudaydad MAHMOODI
Overview
SQL (Structured Query Language)
Database Languages
SQL
SQL (Structured Query Language) was developed in 1970’s in
an IBM laboratory “San Jose Research Laboratory” (now the
Amaden Research center). SQL is derived from the SEQUEL
(Structured English QUEry Language) one of the database
language popular during 1970’s. SQL established itself as the
standard relational database language. Two standard
organization (ANSI) and International standards organization
(ISO) currently promote SQL standards to industry.
SQL
SQL is a language used for storing and managing data in
RDBMS. SQL was the first commercial language introduced
for E.F Codd’s Relational model. Today almost all RDMBS
(MySQL, Oracle, Informix, Sybase, Ms Access, etc) uses SQL
as the standart database language. SQL is used to perform
all type of data operations in RDBMS.
Database Languages
SQL defines some data languages to manipulate data of RDBMS.
DDL (Data Definition Language)
DML (Data Manipulation Langauge)
TCL (Transaction Control Language)
DCL (Data Control Language)
DQL (Data Query Langugae)
Database Languages
DDL (Data Definition Language)
All DDL commands are auto-committed. That means it saves all the
changes permanently in the database. DDL statements are used to define
and manage the objects in the database.
Command Description
create to create new table or database
alter for alteration
truncate delete data from table
drop to drop a table
rename to rename a table
DDL: CREATE
The CREATE statement is used to create a new database or table.
To create database:
CREATE DATABASE `DatabaseName`;
To create table:
CREATE TABLE `TableName` (
`ColumnName1` DataType [Constraint],
`ColumnName2` DataType [Constraint],
`ColumnName3` DataType [Constraint],
...
);
DDL: CREATE
Lets create a sample database and its tables.
CREATE DATABASE `enrollment`;
Command Description
insert to insert a new row
upadate to update existing row
delete to delete a row
merge merging two rows or two tables
DML: INSERT
The INSERT command is used to insert data into a table.
Or,
INSERT INTO `subject` VALUES (NULL, 'Fundamentals of
Database', 4);
Or,
INSERT INTO `subject` (`id`, `title`) VALUES (NULL,
'Fundamentals of Database');
SELECT *
FROM student
WHERE first_name='Ali';
UPDATE student
SET first_name='Omar'
WHERE id=2;
To retrieve all info of students whose cities are Jawzjan and Faryab:
SELECT * FROM student
WHERE city IN ('Jawzjan', 'Faryab');
SQL: BETWEEN
The BETWEEN operator is used when the filtering criteria is a continuous
range with a maximum value and a minimum value. It is always used in
the WHERE clause.
To retrieve all info of students whose ages are between 20 and 25:
SELECT * FROM student
WHERE age BETWEEN 20 AND 25;
Wildcard Operators
There are two wildcard operators that are used in like clause.
1. Percent sign (%) : represents zero, one or more than one character.
2. Underscore sign (_) : represents only one character.
List all students with names that contains 'b‘ as second character:
SELECT * FROM `student` WHERE `first_name` LIKE '_b%';
SQL: Logical Operators
Logical operators are used to combine two or more conditions into a
compound condition.
SQL: Logical Operators
Name Description
AND Combines two expressions. The statement is true only
when both expressions are true. Ex:
WHERE (A=B) AND (C=D)
OR Combines two expressions. The statement is true when
either expression is true. Ex:
WHERE (A=B) OR (C=D)
NOT Reverses the value of an expression. It’s typically used in
conjunction with LIKE, BETWEEN clauses. Ex:
WHERE columnName NOT LIKE ‘ABC%‘
WHERE columnName NOT BETWEEN 90 AND 100
SQL: AND
The AND operator combines two expressions. The statement is true only
when both expressions are true.
AND
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE
SQL: AND
SELECT *
FROM TableName
WHERE exp1 AND exp2 AND exp3 ... ;
Find the employees whose genders are male and ages are 20:
SELECT *
FROM employee
WHERE gender='m' AND age=20;
Find the female employees whose salaries are more than 10000:
SELECT *
FROM employee
WHERE gender='f' AND salary>10000;
SQL: OR
The OR operator combines two expressions. The statement is true when
either expression is true.
OR
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE TRUE
SQL: OR
SELECT *
FROM `TableName`
WHERE exp1 OR exp2 OR exp3 ... ;
Find the employees whose first names are start with 'a‘ or 's':
SELECT *
FROM employee
WHERE first_name LIKE 'a%' OR first_name LIKE 's%';
SQL: NOT
The NOT operator negates an expression. The statement is true when
expression is false or the statement is false when expression is true.
NOT
NOT FALSE TRUE
NOT TRUE FALSE
SQL: NOT
SELECT *
FROM `TableName`
WHERE NOT expression ... ;
Name Description
MIN returns the smallest value in a given column
MAX returns the largest value in a given column
SUM returns the sum of the numeric values in a given column
AVG returns the average value of a given column
COUNT returns the total number of values in a given column
COUNT(*) returns the total number of rows in a table
Sample database table
Assume that we have table called employee as bellow.
SQL: MIN()
SELECT MIN(ColumnName)
FROM TableName;
SELECT ColumnNames
FROM TableName
WHERE Condition
ORDER BY ColumnName [ASC,DESC];
SQL: ORDER BY
List all students by there first name in ascending order:
SELECT * FROM student
ORDER BY first_name ASC;
List all students by there first name ascending order, and by there last
name descending order:
SELECT * FROM student
ORDER BY first_name ASC, last_name DESC;
SQL: ORDER BY
In addition to column name, we may also use column position (based on
the SQL query) to indicate which column we want to apply the ORDER BY
clause. The first column is 1, second column is 2, and so on.
List all students by there first name in descending order:
SELECT * FROM student
ORDER BY 2 DESC;
List all students by there first name ascending order, and by there last
name descending order:
SELECT * FROM student
ORDER BY 2 ASC, 3 DESC;
SQL: ORDER BY
It is also possible to sort the result by an expression. For example, in the
following product table,
id unit_price quantity
1 10 9
2 15 4
3 25 3
4 20 6