Anda di halaman 1dari 5

3/12/2016

Overview
SQL, the Structured Introduction

Query Language
DDL Commands
DML Commands
SQL Statements, Operators, Clauses
Aggregate Functions

I Gede Made Karma

http://www.cs.sjsu.edu/faculty/lee/cs157b/cs157b.html

Structured Query Language (SQL)‫‏‬ Some Facts on SQL


The ANSI standard language for the definition SQL data is case-sensitive, SQL commands are not.
and manipulation of relational database.
First Version was developed at IBM by Donald D.
Includes data definition language (DDL), Chamberlin and Raymond F. Boyce. [SQL]
statements that specify and modify database Developed using Dr. E.F. Codd's paper, “A Relational
schemas. Model of Data for Large Shared Data Banks.”
Includes a data manipulation language (DML), SQL query includes references to tuples variables
statements that manipulate database content. and the attributes of those variables

SQL: DDL Commands SQL: CREATE TABLE Statement


CREATE TABLE: used to create a table. Things to consider before you create your table are:
The type of data
ALTER TABLE: modifies a table after it was created.
the table name
DROP TABLE: removes a table from a database. 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 )
);

1
3/12/2016

SQL: Attributes Types 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;

Table 7.6 pg.164

SQL: DROP TABLE Statement Example:


CREATE TABLE FoodCart (
Has two options: date varchar(10),
food varchar(20), FoodCart
CASCADE: Specifies that any foreign key constraint
profit float
violations that are caused by dropping the table will date food profit
);
cause the corresponding rows of the related table to
be deleted. ALTER TABLE FoodCart ( FoodCart
ADD sold int date food profit sold
RESTRICT: blocks the deletion of the table of any );
foreign key constraint violations would be created. FoodCart
ALTER TABLE FoodCart(
DROP COLUMN profit date food sold
DROP TABLE statement syntax: );
DROP TABLE <table name> [ RESTRICT|CASCADE ];
DROP TABLE FoodCart;

SQL: DML Commands SQL: INSERT Statement


INSERT: adds new rows to a table. To insert a row into a table, it is necessary to
have a value for each attribute, and order
UPDATE: modifies one or more attributes.
matters.
DELETE: deletes one or more rows from a table. 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 hotdog 500 02/26/08 pizza 70

2
3/12/2016

SQL: UPDATE Statement SQL: DELETE Statement


To update the content of the table: To delete rows from the table:
UPDATE statement syntax: DELETE statement syntax:
UPDATE <table name> SET <attr> = <value> DELETE FROM <table name>
WHERE <condition>;
WHERE <selection condition>; Example: DELETE FROM FoodCart
Example: UPDATE FoodCart SET sold = 349 WHERE food = „hotdog‟;
WHERE date = ‟02/25/08‟ AND food = „pizza‟;
FoodCart
FoodCart
date food sold date food sold
date food sold date food sold
02/25/08 pizza 349 02/25/08 pizza 349
02/25/08 pizza 350 02/25/08 pizza 349
02/26/08 hotdog 500 02/26/08 pizza 70
02/26/08 hotdog 500 02/26/08 hotdog 500
02/26/08 pizza 70
02/26/08 pizza 70 02/26/08 pizza 70
Note: If the WHERE clause is omitted all rows of data are deleted from the table.

SQL Statements, Operations, Clauses SQL: SELECT Statement


SQL Statements: A basic SELECT statement includes 3 clauses
Select
SELECT <attribute name> FROM <tables> WHERE <condition>
SQL Operations:
Join SELECT FROM WHERE
Left Join Specifies the Specifies the Specifies the
attributes that are tables that serve selection condition,
Right Join part of the as the input to the including the join
Like resulting relation statement condition.

SQL Clauses:
Order By Note: that you don't need to use WHERE
Group By
Having

SQL: SELECT Statement (cont.)‫‏‬ Example: 1) SELECT *


Person FROM person
Using a “*” in a select statement indicates that Name Age Weight WHERE age > 30;
every attribute of the input table is to be Harry 34 80 Name Age Weight

selected.
Sally 28 64 Harry 34 80
George 29 70 Helena 54 54
Example: SELECT * FROM … WHERE …; Helena 54 54 Peter 34 80
Peter 34 80
To get unique rows, type the keyword 3) SELECT distinct weight
2) SELECT weight
DISTINCT after SELECT. FROM person FROM person
Example: SELECT DISTINCT * FROM … WHERE age > 30; WHERE age > 30;
WHERE …; Weight Weight
80 80
54 54
80

3
3/12/2016

SQL: Join operation SQL: Join operation (cont.)‫‏‬


A join can be specified in the FROM clause inner join = join
which list the two input relations and the SELECT *
FROM emp join dept (or FROM emp, dept)‫‏‬
WHERE clause which lists the join condition.
on emp.id = dept.id;
Example:
Emp.ID Emp.State Dept.ID Dept.Division
Emp Dept
1001 MA 1001 IT
ID State ID Division
1002 TN 1002 Sales
1000 CA 1001 IT
1001 MA 1002 Sales
1002 TN 1003 Biotech

SQL: Join operation (cont.)‫‏‬ SQL: Join operation (cont.)‫‏‬


left outer join = left join right outer join = right join
SELECT * SELECT *
FROM emp left join dept FROM emp right join dept
on emp.id = dept.id; on emp.id = dept.id;

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


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

SQL: Like operation SQL: The ORDER BY Clause


Pattern matching selection Ordered result selection
% (arbitrary string)‫‏‬ desc (descending order)‫‏‬
SELECT * SELECT *
FROM emp FROM emp
WHERE ID like „%01‟; order by state desc
 finds ID that ends with 01, e.g. 1001, 2001, etc  puts state in descending order, e.g. TN, MA, CA
_ (a single character)‫‏‬ asc (ascending order)‫‏‬
SELECT * SELECT *
FROM emp FROM emp
WHERE ID like „_01_‟; order by id asc
 finds ID that has the second and third character  puts ID in ascending order, e.g. 1001, 1002, 1003
as 01, e.g. 1010, 1011, 1012, 1013, etc

4
3/12/2016

SQL: The GROUP BY Clause SQL: The HAVING Clause


The function to divide the tuples into groups and The substitute of WHERE for aggregate functions
returns an aggregate for each group. Usually, it is an aggregate function‟s companion
Usually, it is an aggregate function‟s companion SELECT food, sum(sold) as totalSold
SELECT food, sum(sold) as totalSold FROM FoodCart
FROM FoodCart group by food
group by food; having sum(sold) > 450;
FoodCart
FoodCart
date food sold food totalSold date food sold food totalSold
02/25/08 pizza 349 hotdog 500 02/25/08 pizza 349 hotdog 500
02/26/08 hotdog 500 pizza 419 02/26/08 hotdog 500
02/26/08 pizza 70 02/26/08 pizza 70

SQL: Aggregate Functions SQL: Aggregate Functions (cont.)‫‏‬


FoodCart
Are used to provide summarization information for
SQL statements, which return a single value. date food sold
02/25/08 pizza 349
COUNT(attr)‫‏‬ 02/26/08 hotdog 500
SUM(attr)‫‏‬
MAX(attr)‫‏‬ 02/26/08 pizza 70
MIN(attr)‫‏‬
AVG(attr)‫‏‬ COUNT(attr) -> return # of rows that are not null
Ex: COUNT(distinct food) from FoodCart; -> 2
Note: when using aggregate functions, NULL values SUM(attr) -> return the sum of values in the attr
are not considered, except in COUNT(*) . Ex: SUM(sold) from FoodCart; -> 919

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


Ex: MAX(sold) from FoodCart; -> 500

SQL: Aggregate Functions (cont.)‫‏‬ References


FoodCart Riccardi, Greg. Principles of Database Systems with Internet and Java Applications.
Addison Wesley, 2001.
date food sold
Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition.
02/25/08 pizza 349 Sams Publishing, 2003.

02/26/08 hotdog 500 SQL http://en.wikipedia.org/wiki/SQL

02/26/08 pizza 70 W3C http://www.w3schools.com/sql/sql_tryit.asp

MIN(attr) -> return the lowest value from the attr Wikipedia - SQL http://en.wikipedia.org/wiki/SQL
Ex: MIN(sold) from FoodCart; -> 70 Wikipedia - join http://en.wikipedia.org/wiki/Join_(SQL)‫‏‬

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

Anda mungkin juga menyukai