0% found this document useful (0 votes)
6 views39 pages

Lec 4

This document provides an overview of SQL Data Manipulation Language (DML), detailing its functions such as INSERT, UPDATE, DELETE, and SELECT. It includes examples of how to use these commands to manipulate data within a database, as well as explanations of clauses like WHERE, ORDER BY, and DISTINCT. Additionally, it covers advanced topics like pattern matching and compound conditions for querying data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views39 pages

Lec 4

This document provides an overview of SQL Data Manipulation Language (DML), detailing its functions such as INSERT, UPDATE, DELETE, and SELECT. It includes examples of how to use these commands to manipulate data within a database, as well as explanations of clauses like WHERE, ORDER BY, and DISTINCT. Additionally, it covers advanced topics like pattern matching and compound conditions for querying data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

DATABASE SYSTEMS

Dr. Noha Nagy

Lecture 4 SQL : DML


‫رمضان كريم‬
SQL
3
Structured Query Language

 Data Definition Language (DDL)


 Define relational schemata
 Create/Alter/Drop tables and their attributes

 Data Manipulation Language (DML)


 Insert/Delete/Updatetuples in tables
 Query one or more table
4

SQL AUTO
INCREMENT Field

Note:
Identity(seed,increment)
DML: Data Manipulation Language
5

 DML is used to retrieve, insert, update, and/or delete instances in a


database

▪ INSERT: is used to insert new instances inside a database


▪ UPDATE: is used to update existing instances inside a database
▪ DELETE: is used to delete existing instances inside a database
▪ SELECT: is used to retrieve data from a database
Example
6

INSERT INTO mynewtable Mynewtable


Values (1, ‘Ahmed’, ‘Cairo’); ID Name City

OR ID Name City
1 Ahmed Cairo

INSERT INTO mynewtable (id, name, city)


Values (1, ‘Ahmed’, ‘Cairo’);
Inserting Multiple rows at one time
7

 INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)


VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');
Update Statment
8
Pnum Pname Price Quantity

123 Arial 200 20


Product
124 Persil 180 50

127 OXI 100 11

128 Tide 150 32

Update Product Set Quantity= Quantity – 1 Where Pnum= 123


Pnum Pname Price Quantity

123 Arial 400 19


Product
124 Persil 360 50

127 OXI 200 11

128 Tide 300 32


Delete Statment
9
Employee Enum Ename
Ename phone
phone Pnum

123 Ahmed 01110025878 111

124 Ali 01225929785 254

127 Ola 0102457896 111

Delete From Employee


Where Pnum = 254;
Employee Enum
Enum Ename
Ename phone
phone Pnum

123 Ahmed 01110025878 111

127 Ola 0102457896 111


Difference between delete and drop
10

 Delete all the data from your table “MyCustomers”


 Delete from MyCustomers;

#### Don’t do that on real data!!!


Employee
Enum Ename
Ename phone
phone Pnum

Delete the table itself


Drop table MyCustomers;
Truncate
11
Employee
Enum Ename phone
Delete all data in the table
123 Ahmed 01110025878
No where condition
124 Ali 01225929785 Reset the identity
127 Ola 0102457896

Truncate table Employee;


Employee
Enum Ename phone
The SELECT Statement
12

 Used for queries on single or multiple tables


 Clauses of the SELECT statement:
 SELECT
◼ List the columns (and expressions) that should be returned from the query
 FROM
◼ Indicate the table(s) or view(s) from which data will be obtained
 WHERE
◼ Indicate the conditions under which a row will be included in the result
 GROUP BY
◼ Indicate categorization of results
 HAVING
◼ Indicate the conditions under which a category (group) will be included
 ORDER BY
◼ Sorts the result according to specified criteria
DML - SQL SELECT Statement
13

 The SELECT statement allows you to read data from one or more
tables. To write a SELECT statement in MySQL, you follow this
syntax:

SELECT select_list
FROM table_name
WHERE conditions
SQL SELECT – Single Column
14

 Using the SELECT statement to retrieve data from a single column


example:

 SELECT “column” FROM “tablename”;


 SELECT lastname From employees;

 Using the SELECT statement to query data from multiple columns


example:

 SELECT lastname, firstname, jobtitle FROM


employees;
Retrieve Specific Columns
15

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT PName, Price


FROM Product
PName Price
Gizmo $19.99
Powergizmo $29.99
“projection” SingleTouch $149.99
MultiTouch $203.99
SQL SELECT - DISTINCT Keyword
16

 Distinct allow you to remove all the duplicates from the result.

 Select lastName from employees;

 Select distinct lastName from employees;


DISTINCT: Eliminating Duplicates
17

Category
SELECT DISTINCT Category
FROM Product Gadgets
Photography
Household
Versus
Category
SELECT Category Gadgets
FROM Product Gadgets
Photography
Household

17
SQL SELECT – All Attributes
18

 Using the MySQL SELECT statement to retrieve data from all columns
example:

SELECT * FROM employees

 Often called “select star” or “select *”


Retrieve All Columns and All Rows
19

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT Pname, Price, Category, Manufacturer


FROM Product
OR
SELECT *
FROM Product
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SQL WHERE Clause - Operators
20
Retrieve Specific Rows
21

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT *
FROM Product
WHERE category=‘Gadgets’

PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks

“selection” Powergizmo $29.99 Gadgets GizmoWorks


Retrieve Specific Columns and Rows
22

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100

PName Price Manufacturer


“selection” and SingleTouch $149.99 Canon
“projection” MultiTouch $203.99 Hitachi
Multiple Conditions with AND
23

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100 and Manufacturer= ‘Canon’

PName Price Manufacturer


SingleTouch $149.99 Canon
Multiple Conditions with OR
24

Product PName Price Category Manufacturer


Gizmo $19.99 Gadgets Samsung
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT PName, Price, Manufacturer


FROM Product
WHERE Price > 100 OR Manufacturer= ‘Samsung’

PName Price Manufacturer


SingleTouch $149.99 Canon
MultiTouch $203.99 Hitachi
$19.99
Gizmo Samsung
LIKE: Simple String Pattern Matching
25

SELECT *
FROM Products
WHERE PName LIKE ‘%Touch%’

 S LIKE p: pattern matching on strings


 S is a column name
 p may contain two special symbols:
 % = zero, one, or multiple characters
 _ = any single character
Employee Enum Ename phone

LIKE Cont, 123 Ahmed 01110025878

124 Ali 01225929785


26
127 Ola 0102457896

 selects all Employees with a Name that start with “A“


Enum Ename phone
SELECT *
FROM Employee 123 Ahmed 01110025878
WHERE Ename LIKE ’A%';
124 Ali 01225929785

 selects all Employees with a Name that does NOT start


with “A"
Enum Ename phone
SELECT *
127 Ola 0102457896
FROM Employee
WHERE Ename NOTLIKE ’A%';
ORDER BY: Sorting the Results
27
 The column specified in the ORDER BY clause does not need to be
included in the SELECT clause
 Null values are ordered as the lowest value
Employee Enum Ename Salary Gender

123 Ahmed 10000 M


SELECT Ename, Salary 124 Ali 5000 M
FROM Employee
WHERE gender=‘M’ 127 Ola 30000 F
ORDER BY Salary DESC
SELECT Ename
FROM Employee Ename
ORDER BY Salary DESC
Ename Salary Ola

Ahmed 10000 Ahmed

Ali 5000 Ali


ORDER BY Cont
28

 Order by several columns


SELECT Lname, Fname, Salary
FROM Employee
WHERE Sex=‘F’
ORDER BY Fname, Lname

SELECT PName, Price, Manufacturer


FROM Product
WHERE Category=‘gizmo’ AND Price > 50
ORDER BY Price ASC, Pname DESC
Order by Expression
29

 You can order using an expression.


 Retrieve orders with their order number, order line number
and the amount of money paid in each order ordered y the
largest amount paid first.
SELECT : with ALIAS Enum Ename phone
Employee
30
123 Ahmed 01110025878

124 Ali 01225929785


 Alias is an alternative column or table name
127 Ola 0102457896

Name Employee ID

Ahmed 123
SELECT Ename as Name, Enum as Employee ID Ali 124
FROM Employee
Ola 127

SELECT Cust.Customer_Name as Name, Cust.Customer_address


FROM Customer Cust
WHERE Customer_Name= ‘Home Furnishings’;
Comparisons Involving NULL
31

 SQL allows queries that check whether an attribute


value is NULL
 IS NULL or IS NOT NULL
Retrieve the names of all employees who don’t have
supervisors.
Employee Fname Lname ID Super_ssn
Ahmed Fahmy 111 113

Select Fname,Lname Ali Zidan 112 114

From Employee
Mark Antony 113 114
Amr Moussa 114 Null
Where Super_ssn is null;
Fname Lname
Amr Moussa
Compound Comparison Search Conditions
32

Customer Fname Lname ID City


Ahmed Fahmy 111 London
Ali Zidan 112 Paris
Mark Antony 113 London
Amr Moussa 114 Madrid

 List all Customer Details for customers who live in London


or Paris
Fname Lname ID City

SELECT * Ahmed
Ali
Fahmy
Zidan
111
112
London
Paris
FROM Customer Mark Antony 113 London

WHERE City = ‘London’ OR City = ‘Paris’


Range Search Conditions
33

Product(PID, Product_name, Standard_Price)


 Select all Products with Standard Price between $100 and

$300
SELECT Product_name
From Product
Where Standard_Price Between 100 and 300
OR

SELECT Product_name
From Product
Where Standard_Price >= 100 and Standard_Price < = 300
Using specific values for an attribute
34

Customer (CID, Customer_Name,City,State)


 List all Customer names, cities, and States for all customers
who lives in the following states (Fl, Tx, Ca, Hi)
 Sort the results first by STATE, and within a state by
CUSTOMER_NAME

SELECT Customer_Name, City, State


FROM Customer
WHERE State In (‘Fl’, ‘Tx’, ‘Ca’, ‘Hi’)
ORDER BY State, Customer_Name
Note: the IN operator in this example allows you to include rows whose STATE
value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions
SQL SYNTAX
35

SELECT <Column list>


FROM <table names>
[WHERE <Condition>]
[GROUP BY <Column list>]
[HAVING <Condition>]
[ORDER BY <Column list>]
Constants and Arithmetic
36

 As well as column names, you SELECT Name,Code,Mark/100


can select constants, compute FROM Grades
arithmetic expressions and
evaluate functions in a
SELECT statement

Grades
Grades
Name Code Mark
Name Code Mark
John DBS 56
John DBS 0.56
John IAI 72
John IAI 0.72
Mary DBS 60
Mary DBS 0.60
Mark PR1 43
Mark PR1 0.43
Mark PR2 35
Mark PR2 0.35
Jane IAI 54
Jane IAI 0.54
LIMIT Keyword
37

 The limit keyword is used to limit the number of rows


returned in a query result.

•"LIMIT N" is the keyword and N is any number starting from


0, putting 0 as the limit does not return any records in the
query. Putting a number say 5 will return five records. If the
records in the specified table are less than N, then all the
records from the queried table are returned in the result set.
LIMIT Example
38

 Do you have any employee with last names “Fadi” ?

 select * from employees where lastName=‘Fadi’ limit 1;

 In some DBMS
Select top 1 * from employees where lastName=‘Fadi’
Question
40

1. Create a relation “MyCustomers”


 MyCustomers( customer_id, customer_name, city)
 City must have a value
2. In your newly created “MyCustomers” table
 Insert two customers with names “Ahmed” and “Mohamed” who live in
“Paris” with autoincrement ID start from 1
 Update all customers living in Paris, set their name to your name
 Update the city of customer number 4 to Alexandria

You might also like