0% found this document useful (0 votes)
54 views

DBMS SQL Commands CH 7

This document provides an overview of Structured Query Language (SQL) including its data definition language (DDL) and data manipulation language (DML). It discusses SQL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, INSERT, UPDATE, DELETE. It also covers SQL statements, operators, and clauses like SELECT, JOIN, ORDER BY, GROUP BY. Examples are provided to illustrate how to use SQL statements and clauses to define schemas, manipulate data, and retrieve results from relational databases.

Uploaded by

abegaz
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

DBMS SQL Commands CH 7

This document provides an overview of Structured Query Language (SQL) including its data definition language (DDL) and data manipulation language (DML). It discusses SQL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, INSERT, UPDATE, DELETE. It also covers SQL statements, operators, and clauses like SELECT, JOIN, ORDER BY, GROUP BY. Examples are provided to illustrate how to use SQL statements and clauses to define schemas, manipulate data, and retrieve results from relational databases.

Uploaded by

abegaz
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 7:

SQL, the Structured


Query Language
By: Atnafu L.

11/30/22 Ch-7,SQL.......by Atnafu 1


Overview

Introduction
DDL Commands
DML Commands
SQL Statements, Operators, Clauses
Aggregate Functions

11/30/22 Ch-7,SQL.......by Atnafu 2


Structured Query Language (SQL)

The ANSI standard language for the definition


and manipulation of relational database.

Includes data definition language (DDL),


statements that specify and modify database
schemas.

Includes a data manipulation language (DML),


statements that manipulate database content.

11/30/22 Ch-7,SQL.......by Atnafu 3


Some Facts on SQL

SQL data is case-sensitive, SQL commands are not.

First Version was developed at IBM by Donald D.


Chamberlin and Raymond F. Boyce. [SQL]

Developed using Dr. E.F. Codd's paper, “A Relational


Model of Data for Large Shared Data Banks.”

SQL query includes references to tuples variables


and the attributes of those variables

11/30/22 Ch-7,SQL.......by Atnafu 4


SQL: DDL Commands
CREATE TABLE: used to create a table.

ALTER TABLE: modifies a table after it was created.

DROP TABLE: removes a table from a database.

11/30/22 Ch-7,SQL.......by Atnafu 5


SQL: CREATE TABLE Statement
Things to consider before you create your table are:
The type of data
the table name
what column(s) will make up the primary key
the names of the columns
CREATE TABLE statement syntax:
CREATE TABLE <table name>
( field1 datatype ( NOT NULL ),
field2 datatype ( NOT NULL )
);

11/30/22 Ch-7,SQL.......by Atnafu 6


SQL: Attributes Types

11/30/22
Table
Ch-7,SQL.......by Atnafu 7.6 pg.164 7
SQL: ALTER TABLE Statement
To add or drop columns on existing tables.
ALTER TABLE statement syntax:
ALTER TABLE <table name>
ADD attr datatype;
or
DROP COLUMN attr;

11/30/22 Ch-7,SQL.......by Atnafu 8


SQL: DROP TABLE Statement
Has two options:
CASCADE: Specifies that any foreign key constraint
violations that are caused by dropping the table will
cause the corresponding rows of the related table to
be deleted.

RESTRICT: blocks the deletion of the table of any


foreign key constraint violations would be created.

DROP TABLE statement syntax:


DROP TABLE <table name> [ RESTRICT|CASCADE ];

11/30/22 Ch-7,SQL.......by Atnafu 9


Example:
CREATE TABLE FoodCart (
date varchar(10),
food varchar(20), FoodCart
profit float
date food profit
);

ALTER TABLE FoodCart ( FoodCart


ADD sold int
); date food profit sold

ALTER TABLE FoodCart( FoodCart


DROP COLUMN profit date food sold
);

DROP TABLE FoodCart;


11/30/22 Ch-7,SQL.......by Atnafu 10
SQL: DML Commands
INSERT: adds new rows to a table.

UPDATE: modifies one or more attributes.

DELETE: deletes one or more rows from a table.

11/30/22 Ch-7,SQL.......by Atnafu 11


SQL: INSERT Statement
To insert a row into a table, it is necessary to
have a value for each attribute, and order matters.
INSERT statement syntax:
INSERT into <table name>
VALUES ('value1', 'value2', NULL);
Example: INSERT into FoodCart
VALUES (’02/26/08', ‘pizza', 70 );

FoodCart
date food sold
date food sold 02/25/08 pizza 350
02/25/08 pizza 350 02/26/08 hotdog 500
02/26/08
11/30/22
hotdog 500Ch-7,SQL.......by 02/26/08
Atnafu
pizza 70 12
SQL: UPDATE Statement
To update the content of the table:
UPDATE statement syntax:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
Example: UPDATE FoodCart SET sold = 349
WHERE date = ’02/25/08’ AND food = ‘pizza’;
FoodCart

date food sold date food sold


02/25/08 pizza 350 02/25/08 pizza 349
02/26/08 hotdog 500 02/26/08 hotdog 500
02/26/08 pizza 70 02/26/08 pizza 70
11/30/22 Ch-7,SQL.......by Atnafu 13
SQL: DELETE Statement

To delete rows from the table:


DELETE statement syntax:
DELETE FROM <table name>
WHERE <condition>;
Example: DELETE FROM FoodCart
WHERE food = ‘hotdog’;
FoodCart

date food sold date food sold


02/25/08 pizza 349 02/25/08 pizza 349
02/26/08 hotdog 500 02/26/08 pizza 70
02/26/08 pizza 70
Note: If the WHERE clause is omitted
11/30/22 all rows
Ch-7,SQL.......by of data are deleted from the table.
Atnafu 14
SQL Statements, Operations, Clauses

SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
Group By
Having
11/30/22 Ch-7,SQL.......by Atnafu 15
SQL: SELECT Statement

A basic SELECT statement includes 3 clauses

SELECT <attribute name> FROM <tables> WHERE <condition>

SELECT FROM WHERE

Specifies the Specifies the Specifies the


attributes that are tables that serve selection condition,
part of the as the input to the including the join
resulting relation statement condition.

Note: that you don't need to use WHERE

11/30/22 Ch-7,SQL.......by Atnafu 16


SQL: SELECT Statement (cont.)

Using a “*” in a select statement indicates that


every attribute of the input table is to be
selected.
Example: SELECT * FROM … WHERE …;

To get unique rows, type the keyword


DISTINCT after SELECT.
Example: SELECT DISTINCT * FROM …
WHERE …;

11/30/22 Ch-7,SQL.......by Atnafu 17


Example: 1) SELECT *
Person FROM person
Name Age Weight WHERE age > 30;
Harry 34 80 Name Age Weight
Sally 28 64 Harry 34 80
George 29 70 Helena 54 54
Helena 54 54 Peter 34 80
Peter 34 80

3) SELECT distinct weight


2) SELECT weight
FROM person FROM person
WHERE age > 30; WHERE age > 30;
Weight Weight
80 80
54 54

11/30/22
80 Ch-7,SQL.......by Atnafu 18
SQL: Join operation
A join can be specified in the FROM clause
which list the two input relations and the
WHERE clause which lists the join condition.
Example:

Emp Dept
ID State ID Division
1000 CA 1001 IT
1001 MA 1002 Sales
1002 TN 1003 Biotech
11/30/22 Ch-7,SQL.......by Atnafu 19
SQL: Join operation (cont.)
inner join = join
SELECT *
FROM emp join dept (or FROM emp, dept)
on emp.id = dept.id;

Emp.ID Emp.State Dept.ID Dept.Division


1001 MA 1001 IT
1002 TN 1002 Sales

11/30/22 Ch-7,SQL.......by Atnafu 20


SQL: Join operation (cont.)
left outer join = left join
SELECT *
FROM emp left join dept
on emp.id = dept.id;

Emp.ID Emp.State Dept.ID Dept.Division


1000 CA null null
1001 MA 1001 IT
1002 TN 1002 Sales

11/30/22 Ch-7,SQL.......by Atnafu 21


SQL: Join operation (cont.)
right outer join = right join
SELECT *
FROM emp right join dept
on emp.id = dept.id;

Emp.ID Emp.State Dept.ID Dept.Division


1001 MA 1001 IT
1002 TN 1002 Sales
null null 1003 Biotech

11/30/22 Ch-7,SQL.......by Atnafu 22


SQL: Like operation
Pattern matching selection
% (arbitrary string)
SELECT *
FROM emp
WHERE ID like ‘%01’;
 finds ID that ends with 01, e.g. 1001, 2001, etc
_ (a single character)
SELECT *
FROM emp
WHERE ID like ‘_01_’;
 finds ID that has the second and third character
as 01, e.g. 1010, 1011,Ch-7,SQL.......by
11/30/22 1012, 1013, Atnafu etc 23
SQL: The ORDER BY Clause

Ordered result selection


desc (descending order)
SELECT *
FROM emp
order by state desc
 puts state in descending order, e.g. TN, MA, CA
asc (ascending order)
SELECT *
FROM emp
order by id asc
 puts ID in ascending order, e.g. 1001, 1002, 1003
11/30/22 Ch-7,SQL.......by Atnafu 24
SQL: The GROUP BY Clause
The function to divide the tuples into groups and returns
an aggregate for each group.
Usually, it is an aggregate function’s companion
SELECT food, sum(sold) as totalSold
FROM FoodCart
group by food;
FoodCart

date food sold food totalSold


02/25/08 pizza 349 hotdog 500
02/26/08 hotdog 500 pizza 419
02/26/08
11/30/22
pizza 70
Ch-7,SQL.......by Atnafu 25
SQL: The HAVING Clause
The substitute of WHERE for aggregate functions
Usually, it is an aggregate function’s companion
SELECT food, sum(sold) as totalSold
FROM FoodCart
group by food
having sum(sold) > 450;
FoodCart

date food sold food totalSold


02/25/08 pizza 349 hotdog 500
02/26/08 hotdog 500
02/26/08
11/30/22
pizza 70 Atnafu
Ch-7,SQL.......by 26
SQL: Aggregate Functions
Are used to provide summarization information for
SQL statements, which return a single value.

COUNT(attr)
SUM(attr)
MAX(attr)
MIN(attr)
AVG(attr)

Note: when using aggregate functions, NULL values


are not considered, except in COUNT(*) .

11/30/22 Ch-7,SQL.......by Atnafu 27


SQL: Aggregate Functions (cont.)
FoodCart

date food sold


02/25/08 pizza 349
02/26/08 hotdog 500
02/26/08 pizza 70
COUNT(attr) -> return # of rows that are not null
Ex: COUNT(distinct food) from FoodCart; -> 2

SUM(attr) -> return the sum of values in the attr


Ex: SUM(sold) from FoodCart; -> 919

MAX(attr) -> return the highest value from the attr


Ex: MAX(sold) from FoodCart;
11/30/22
-> 500
Ch-7,SQL.......by Atnafu 28
SQL: Aggregate Functions (cont.)
FoodCart

date food sold


02/25/08 pizza 349
02/26/08 hotdog 500
02/26/08 pizza 70
MIN(attr) -> return the lowest value from the attr
Ex: MIN(sold) from FoodCart; -> 70

AVG(attr) -> return the average value from the attr


Ex: AVG(sold) from FoodCart; -> 306.33
Note: value is rounded to the precision of the datatype

11/30/22 Ch-7,SQL.......by Atnafu 29


Thank You for your Great
Participation

This is end of the chapter and also end


of the class
Please be free to give any comment

regarding to the class as well as the


course
Thanks one again

Have nice exam

11/30/22 Ch-7,SQL.......by Atnafu 30

You might also like