Sql-Structured Query Language
Sql-Structured Query Language
QUERY LANGUAGE
INTRODUCTION
SQL is the ANSI standard language for the
definition and manipulation of relational
database.
A database is a collection of related data.
A DBMS is a collection of programs that
enables us to create and maintain a
database.
A system that is able to manage its
database through its relational capabilities
is called RDBMS or relational database.
BASIC STRUCTURE
SQL ENVIRONMENT
Catalog
A set of schemas that constitute the description of a
database
Schema
The structure that contains descriptions of objects created
by a user (base tables, views, constraints)
DDL
DDL is Data Definition Language statements. Some
examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP removes a table from the database.All the tables
rows, space and privileges will also be removed.Cannot be rolled
back.
TRUNCATE - remove all rows from a table, including all spaces
allocated for the records are removed.CANNOT BE
ROLLEDBACK.You cant use conditions in truncate.
COMMENT - add comments to the data dictionary
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT
command
);
EXAMPLE
Example:
CREATE TABLE FoodCart (
date varchar(10),
food varchar(20),
profit float
);
DELETE:ADELETEstatementcanbefine-tunedusingapredicatevia
eitherWHEREorJOINtodeletesomeorallrowsinatableBecauseofthe
transactionaloverhead,DELETEcanbe"slow"(thisisrelative),butsaferbecause
itisfine-grained.
.
TRUNCATE: TRUNCATEcannotbefinegrained.Ifsuccessful,itwillremove
alltherowsfromyourtable.Becauseitis(typically)notloggedanddoesnotuse
predicates,aTRUNCATEwillbefasterthanaDELETE,butlesssafe.
DROP:DROPTABLEgoesfurtherthanaTRUNCATEinthatitactuallyremoves
thetablefromthedatabasecompletely.Thisincludesremovingallassociated
objects,suchasindexesandconstraints.Ifyoudroppedatable,notonlywouldyou
removeallthedata,butthestructureaswell.
DML
DML is Data Manipulation Language
statements. Some examples:
SELECT - retrieve data from the a
database
INSERT - insert data into a table
UPDATE - updates existing data within a
table
DELETE - deletes all rows from a table, the
space for the records remain,can be rolled
back.
DML
INSERT: adds new rows to a table.
UPDATE: modifies one or more
attributes.
DELETE: deletes one or more rows
from a table.
EXAMPLE TABLE
date
food
sold
02/25/08
pizza
350
02/26/08
hotdog
500
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
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
DELETE STATEMENT
To delete rows from the table:
DCL
DCL is Data Control Language
statements. Some examples:
COMMIT - save work done
SAVEPOINT - identify a point in a
transaction to which you can later
roll back
ROLLBACK - restore database to
original since the last COMMIT
SQL
STATEMENTS,OPERATIONS,CLAUSES
SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
Group By
Having
FROM
WHERE
Specifies the selection condition, including the join condition.
Example:
Person
Name
Age
Weight
Harry
34
80
Sally
28
George
1) SELECT *
FROM person
WHERE age >
30;
Name
Age
Weight
64
Harry
34
80
29
70
Helena
54
54
Helena
54
54
Peter
34
80
Peter
34
80
2) SELECT weight
FROM person
WHERE age >
30;
Weight
Weight
80
80
54
54
80
ID
1000
1001
1002
Dept
State
CA
MA
TN
ID
1001
1002
1003
Divisio
n
IT
Sales
Biotec
h
Emp.I
D
1001
1002
Emp.Stat
e
MA
TN
Dept.I
D
1001
1002
Dept.Divisio
n
IT
Sales
Emp.I
D
1000
1001
1002
Emp.Stat
e
CA
MA
TN
Dept.I
D
null
1001
1002
Dept.Divisio
n
null
IT
Sales
Emp.I
D
1001
1002
null
Emp.Stat
e
MA
TN
null
Dept.I
D
1001
1002
1003
Dept.Divisio
n
IT
Sales
Biotech
date
02/25/0
8
02/26/0
8
02/26/0
food
pizza
hotdog
pizza
sold
349
500
70
food
hotdo
g
pizza
totalSol
d
500
419
date
02/25/0
8
02/26/0
8
02/26/0
food
pizza
hotdog
pizza
sold
349
500
70
food totalSol
hotdo d
500
g
date
02/25/0
8
02/26/0
8
02/26/0
8
food
pizza
hotdog
pizza
sold
349
500
70
date
02/25/0
8
02/26/0
8
02/26/0
8
food
pizza
hotdog
pizza
sold
349
500
70