Академический Документы
Профессиональный Документы
Культура Документы
Database
System
1
Herat
University
Computer
Science
Faculty
Foawziah
Naseri
Foawziah_naseri@yahoo.com
April-11-2015
4/13/15
Overview
What
is
SQL?
IntroducOon
to
SQL
CreaOng
Database
in
SQL
CreaOng
Table
Command
in
SQL
ARributes
Data
Types
Alter
Command
InserOng
Data
Specifying
Constrains
in
SQL
Schema
Change
Statements
Joining
with
Form
Where
Clause
Ordering
a
Query
Result
What
is
SQL?
OVen
called
Structured
Query
Language,
although
SQL
is
the
ocial
name
Developed
in
the
1970s
as
the
rst
commercial
languages
for
Codds
relaOonal
model
SQL
is
not
a
procedural
language.
SQL
in
combinaOon
with
a
procedural
language,
such
as
C,
to
create
a
complete
applicaOon.
What
is
a
query?
What is a Query ?
A query is a question posed to a
database
Queries are expressed in a high-level
declarative manner
Algorithms needed to answer the query
are not specified in the query
What
is
a
query?
Examples:
Mouse
click
on
a
map
symbol
(e.g.
road)
may
mean
What
is
the
name
of
road
pointed
to
by
mouse
cursor
?
Typing
a
keyword
in
a
search
engine
(e.g.
google,
yahoo)
means
Which
documents
on
web
contain
given
keywords?
SELECT
S.name
FROM
Senator
S
WHERE
S.gender
=
F
means
What
is
SQL?
The heart of SQL is the select-query:
1. Select A1, A2, .,An == the projection A1, A2, .,An
2. from R1, R2, Rn == the C.product R1, R2 Rn
Where p == the selection ()
This means that such a select statement is
equivalent to the algebra expression:
IntroducOon to SQL
Crea0ng
tables
CREATE
DATABASE
world;
A
database
table
is
a
two-dimensional
array
made
up
of
rows
and
columns.
You
can
create
a
table
by
using
the
SQL
CREATE
TABLE
command.
Within
the
command,
you
specify
the
name
and
data
type
of
each
column.
IntroducOon to SQL
( sid CHAR(9) NOT NULL,
name VARCHAR(20) NOT NULL, Bdate
DATE, Address VARCHAR(30)
age INTEGER;
Each sql statement ends with semicolon
Alter
Command
Is
used
to
change
the
deniOon
of
a
base
table
or
other
named
schema
elements
For
base
tables,
the
possible
alter
table
acOons
include:
oAdding
or
dropping
a
column
oChanging
a
column
deniOon
Ex:
Adding
a
new
column
to
table
Employee
ALTER
table
COMPANY.EMPLOYEE
ADD
COLUMN
Job
VARCHAR(10)
DeleOng
Data
To delete tuples from a table we use:
delete from table where condition
The where clause works like in a select
statement. e.g.
delete from books where author=Tolkien;
DeleOng
a
table
We
can
delete
whole
tables
with
the
drop
table
command.
Example:
drop
table
books;
ARenOon:
the
whole
table
with
all
data
is
deleted
immediately
InserOng
Data
Now
we
can
make
a
table
how
do
we
populate
it
with
data?
insert
into
tablename
values
(tuple)
For
example:
insert
into
books
values
(4,
lord
of
the
Rings,
Tolkien)
Changing data
Where clause
String in where-Clause
SUM
SUM
funcOon
computes
the
total
sum
for
any
specied
aRribute,
using
whatever
condiOon(s)
you
have
imposed.
For
example,
if
you
want
to
compute
the
total
amount
owed
by
your
customers,
you
could
use
the
following
command:
SELECT
SUM(CUS_BALANCE)
AS
TOTBALANCE
FROM
CUSTOMER;
Grouping
Data
The
GROUP
BY
clause
is
generally
used
when
Grouping
Data
SELECT
FROM
GROUP
BY
HAVING
ORDER
BY
V_CODE,
SUM(P_Q
*
P_PRICE)
AS
TOTCOST
PRODUCT
V_CODE
(SUM(P_Q
*
P_PRICE)
>
500)
SUM(P_Q
*
P_PRICE)
DESC;
This
statement
will
do
the
following:
Aggregate
the
total
cost
of
products
grouped
by
V_CODE.
Select
only
the
rows
having
totals
that
exceed
$500.
List
the
results
in
descending
order
by
the
total
cost.
Thats all..