You are on page 1of 31

.

CHAR(5)

CHAR(20)

CHAR(30)

. DATE

(create)
(alter)
(insert)

((update)
p
)
(delete)
(drop)


(create)
(
)
Create table table_name (
Field_name type [not null] [PRIMARY KEY],
Fi ld
Field_name
t
type
[not
[ t null],
ll]
[Primary key(field_name1, field_name2, field_namen)],
[constraint constraint_name
constraint name foreign key(field_name)
key(field name) references
table_name],
[constraint constraint_name check (field_name condition)] )

create table Sailors (


sid integer not null primary key,
sname char(20),
rating integer);

create table boats


(bid integer not null,
bname char(20),
color char(20)
char(20),
primary key(bid),
constraint b_color check ((color in ( 'red',, 'green',
g
, 'blue',, 'yellow'))
y
)) )

create table Reserves


(sid integer not null,
bid integer not null,
day1 date not null,
primary
p
y key
y ((sid, bid, day1),
y )
constraint r_sid foreign key(sid) references Sailors,
constraint r_bid foreign
g key(bid)
y( ) references boats);
);

10

(alter)

alter table sailors


add column age real;

11

(insert)

Insert into table_name (field_name1, , field_namen)


values
l
((value
l 1, , value
l n),
) (value
( l 11, , value
l 1n)
insert into sailors(sid, sname, rating, age)
values (22, Dustin, 7, 45.0);

12

(update)

update Sailors S
set s.age = s.age + 1, s.rating = s.rating 1
where s.sid = 22;

13

(delete)

delete
from sailors s
where s.sname = Dustin;

14

(drop)

drop table sailors;

15


SQL
SELECT
FROM
WHERE

16

1o:
SELECT
BHMA 1

FROM Sailors
S il
S R
S,
Reserves R
WHERE

17

2o:
SELECT
BHMA 1

FROM Sailors
S il
S R
S,
Reserves R

BHMA 2

WHERE S.sid = R.sid

18

3o:
SELECT
BHMA 1

FROM Sailors
S il
S R
S,
Reserves R

BHMA 2

WHERE S.sid = R.sid

and S.age < 20

19

4o:
4

SELECT S.name, S.surname

BHMA 1

FROM Sailors
S il
S R
S,
Reserves R

BHMA 2

WHERE S.sid = R.sid

and S.age < 20

20

21

y
y
y
y
y
y
y
y
y

select
Where
Order
Group by
Having
Nested
In
Exists
Forall (double not exists)
.

22

1:
SELECT S.name, S.surname
FROM Sailors S

23

2:

SELECT R.sid
FROM Boats B, Reserves R
WHERE B.bid = R.bid and B.color = red
red

24

3:

SELECT S.age
S age
FROM Sailors S
WHERE S.sname like B_%

25

4:
rating

SELECT S.sid
FROM Sailors S
WHERE S.rating >
>= ALL (SELECT S2.rating
FROM Sailors S2)

26

5
5:
(nested )

SELECT S.sname
S
FROM Sailors S
WHERE S
S.sid
sid NOT IN
(SELECT R.sid
FROM Reserves R
WHERE R.bid IN
(SELECT B.bid
FROM Boats B
WHERE B.color = red))
))
.

27

6: sid
(-) (-)

.
SELECT S.sid
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid and R.bid = B.bid
and B.color = red
EXCEPT
SELECT S.sid
S sid
FROM Sailors S, Reserves R, Boats B
WHERE S.sid = R.sid and R.bid = B.bid
and B.color = green
.

28

7:
.
SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS(
SELECT *
FROM Boats B
WHERE NOT EXISTS(
SELECT *
FROM Reserves R
WHERE S.sid = R.sid and R.bid = B.bid ))
.

29

8:
(>18 ) rating

.
g MIN(S.age)
(
g ) AS minage
g
SELECT S.rating,
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT(*) > 1

30

You might also like