Escolar Documentos
Profissional Documentos
Cultura Documentos
Criando as tabelas
create table pessoa
(pessoa_cod integer not null,
NOME varchar(6),
fone varchar(10),
primary key (pessoa_cod));
create table turma
(turma_cod character(1) not null,
NOME varchar(10),
profe integer,
primary key (turma_cod));
create table participante
(pessoa integer not null,
turma character(1) not null,
primary key (pessoa, turma));
alter table turma add foreign key (profe) references pessoa
(pessoa_cod);
alter table participante add foreign key (pessoa) references pessoa
(pessoa_cod);
alter table participante add foreign key (turma) references turma
(turma_cod);
fone)
insert
values
insert
values
insert
values
insert
values
insert
values
fone)
fone)
fone)
insert
values
insert
values
insert
values
Exemplo 1
SQL> select * from pessoa;
4 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
------------------------------SQL> select * from turma;
3 rows selected
------------------------------------|TURMA_cod|
NOME
| PROFE
|
------------------------------------|
A
| Volei |
4
|
|
C
| Natao |
2
|
|
B
|Karate|
4
|
------------------------------------SQL> select * from participante;
5 rows selected
--------------------| PESSOA
|TURMA|
--------------------|
1
|
A
|
|
1
|
B
|
|
1
|
C
|
|
2
|
C
|
|
3
|
A
|
---------------------
Exemplo 2
SQL> select NOME from pessoa;
4 rows selected
-------| NOME |
-------|Silva |
|Cabral|
|Lobato|
|OBilac|
--------
Exemplo 3
SQL> select * from pessoa
where NOME = 'Lobato';
1 row selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
4
|Lobato| 174590 |
-------------------------------
Exemplo 4
SQL> select * from pessoa
where first_NOME = Lobato;
[interbase.interclient] Dynamic SQL Error
SQL error code = -206
Column unknown
FIRST_NOME
SQL> select * from PARTIC
where NOME = Lobato;
[interbase.interclient] Dynamic SQL Error
SQL error code = -204
Table unknown
PARTIC
Exemplo 5
SQL> select NOME, fone from pessoa
where NOME like 'S%';
2 rows selected
------------------| NOME |FONE |
------------------|Silva | 282677 |
|Cabral| 260088 |
-------------------
Exemplo 6
SQL> select NOME, fone from pessoa
where NOME = 'S%';
No rows selected
------------------| NOME |FONE |
-------------------
Exemplo 7
Exemplo 8
SQL> select * from pessoa
where NOME like 'S%' and (pessoa_cod <= 2
or fone = '174590');
1 row selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
-------------------------------
Exemplo 9
SQL> select * from turma;
3 rows selected
------------------------------------|TURMA_cod|
NOME
| PROFE
|
------------------------------------|
A
| Volei |
4
|
|
C
| Natao |
2
|
|
B
|Karate|
4
|
------------------------------------SQL> select * from pessoa;
4 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
-------------------------------
Exemplo 10
SQL> select profe from turma
where NOME = 'Volei';
1 row selected
-------------
| PROFE
|
------------|
4
|
-------------
Exemplo 11
SQL> select NOME from pessoa
where pessoa_cod = 4;
1 row selected
-------| NOME |
-------|Lobato|
--------
Exemplo 12
SQL> select NOME from pessoa
where pessoa_cod = (select profe from turma
where NOME = 'Volei');
1 row selected
-------| NOME |
-------|Lobato|
--------
Exemplo 13
SQL> select * from turma, pessoa;
12 rows selected
------------------------------------------------------------------|TURMA_cod|
NOME
| PROFE
| PESSOA_COD | NOME |FONE |
------------------------------------------------------------------|
A
| Volei |
4
|
2
|Silva | 282677 |
|
C
| Natao |
2
|
2
|Silva | 282677 |
|
B
|Karate|
4
|
2
|Silva | 282677 |
|
A
| Volei |
4
|
3
|Cabral| 260088 |
|
C
| Natao |
2
|
3
|Cabral| 260088 |
|
B
|Karate|
4
|
3
|Cabral| 260088 |
|
A
| Volei |
4
|
4
|Lobato| 174590 |
|
C
| Natao |
2
|
4
|Lobato| 174590 |
|
B
|Karate|
4
|
4
|Lobato| 174590 |
|
A
| Volei |
4
|
1
|OBilac| 260088 |
|
C
| Natao |
2
|
1
|OBilac| 260088 |
|
B
|Karate|
4
|
1
|OBilac| 260088 |
-------------------------------------------------------------------
Exemplo 14
SQL> select * from turma, pessoa
where profe = pessoa_cod;
3 rows selected
------------------------------------------------------------------|TURMA_cod|
NOME
| PROFE
| PESSOA_COD | NOME |FONE |
------------------------------------------------------------------|
C
| Natao |
2
|
2
|Silva | 282677 |
|
A
| Volei |
4
|
4
|Lobato| 174590 |
|
B
|Karate|
4
|
4
|Lobato| 174590 |
-------------------------------------------------------------------
Exemplo 15
SQL> select * from turma, pessoa
where profe = pessoa_cod
and turma.NOME = 'Volei';
1 row selected
------------------------------------------------------------------|TURMA_cod|
NOME
| PROFE
| PESSOA_COD | NOME |FONE |
------------------------------------------------------------------|
A
| Volei |
4
|
4
|Lobato| 174590 |
-------------------------------------------------------------------
Exemplo 16
SQL> select pessoa.NOME from turma, pessoa
where profe = pessoa_cod
and turma.NOME = 'Volei';
1 row selected
-------| NOME |
-------|Lobato|
--------
Exemplo 17
SQL> select pessoa_cod
from pessoa
where NOME = 'O-Bilac';
1 row selected
------------| PESSOA_COD |
------------|
1
|
-------------
Exemplo 18
SQL> select turma from participante
where pessoa = 1;
3 rows selected
---------
|TURMA|
--------|
A
|
|
B
|
|
C
|
---------
Exemplo 19
SQL> select NOME from turma
where turma_cod = 'A'
or turma_cod = 'B'
or turma_cod = 'C';
3 rows selected
-----------|
NOME
|
-----------| Volei |
| Natao |
|Karate|
------------
Exemplo 20
SQL> select NOME from turma
where turma_cod in ('A', 'B', 'C');
3 rows selected
-----------|
NOME
|
-----------| Volei |
| Natao |
|Karate|
------------
Exemplo 21
SQL> select NOME from turma
where turma_cod = (select turma from participante
where pessoa = 1);
[interbase.interclient] multiple rows in singleton select
SQL> select NOME from turma
where turma_cod = (select turma from participante
where pessoa = (select pessoa_cod from pessoa
where NOME = O-Bilac));
[interbase.interclient] multiple rows in singleton select
Exemplo 22
SQL> select NOME from turma
where turma_cod in (select turma from participante
Exemplo 23
SQL> select NOME from turma, participante, pessoa
where turma_cod = turma
and pessoa = pessoa_cod
and NOME = 'O-Bilac';
3 rows selected
-------| NOME |
-------|OBilac|
|OBilac|
|OBilac|
--------
Exemplo 24
SQL> select turma.NOME
from turma, participante, pessoa
where turma_cod = turma
and pessoa = pessoa_cod
and pessoa.NOME = O-Bilac;
3 rows selected
-----------|
NOME
|
-----------| Volei |
|Karate|
| Natao |
------------
Exemplo 25
SQL> select * from pessoa
where pessoa_cod in (select pessoa from participante);
3 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
1
|OBilac| 260088 |
-------------------------------
Exemplo 26
4 SQL> select * from pessoa
where pessoa_cod not in (select pessoa from participante);
1 row selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
4
|Lobato| 174590 |
-------------------------------
Exemplo 27
SQL> select max(pessoa_cod) from pessoa;
1 row selected
------------|
MAX
|
------------|
4
|
-------------
Exemplo 28
SQL> select * from pessoa
where pessoa_cod = max(pessoa_cod);
[interbase.interclient] Dynamic SQL Error
SQL error code = -104
Invalid aggregate reference
Exemplo 29
SQL> select * from pessoa
where pessoa_cod = (select max(pessoa_cod) from pessoa);
1 row selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
4
|Lobato| 174590 |
-------------------------------
Exemplo 30
SQL> select max(pessoa_cod) from pessoa
where NOME like 'S%';
1 row selected
------------|
MAX
|
-------------
|
3
|
-------------
Exemplo 31
SQL> select turma.NOME, pessoa.NOME
from turma, pessoa
where profe = pessoa_cod;
3 rows selected
------------------|
NOME
| NOME |
------------------| Natao |Silva |
| Volei |Lobato|
|Karate|Lobato|
-------------------
Exemplo 32
SQL> create view prof_pessoa
as select turma.NOME, pessoa.NOME
from turma, pessoa
where profe = pessoa_cod;
[interbase.interclient] unsuccessful metadata update
STORE RDB$RELATION_FIELDS failed
attempt to store duplicate value (visible to active transactions) in
unique index "RDB$INDEX_15"
Exemplo 33
SQL> create view prof_pessoa (turma_NOME, pessoa_codME)
as select turma.NOME, pessoa.NOME
from turma, pessoa
where profe = pessoa_cod;
Ok
Exemplo 34
SQL> select * from prof_pessoa;
3 rows selected
-------------------------|TURMA_NOME|PESSOA_CODME|
-------------------------| Natao |
Silva
|
| Volei
|
Lobato |
| Karate |
Lobato |
-------------------------SQL> select PESSOA_CODME
from prof_pessoa
where turma_NOME = 'Volei';
1 row selected
------------|PESSOA_CODME|
------------|
Lobato |
-------------
Exemplo 35
SQL> select * from pessoa;
4 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
------------------------------SQL> insert into pessoa
values (4, 'Isaac', 281000);
[interbase.interclient] violation of PRIMARY or UNIQUE KEY constraint
"INTEG_16" on table "PESSOA"
Exemplo 36
create table pessoa
(pessoa_cod integer not null,
NOME varchar(6),
fone varchar(10),
primary key (pessoa_cod));
Ok
Exemplo 37
SQL> insert into pessoa
values (7, 'Isaac', 281000);
Ok (1 row affected)
SQL> select * from pessoa;
5 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
|
7
|Isaac | 281000 |
-------------------------------
Exemplo 38
4 SQL> insert into pessoa values
(8, 'Nelson');
Exemplo 39
SQL> insert into pessoa (pessoa_cod, NOME)
values (8, 'Nelson');
Ok (1 row affected)
SQL> select * from pessoa;
6 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
|
7
|Isaac | 281000 |
|
8
|Nelson|
|
-------------------------------
Exemplo 40
SQL> select * from pessoa where fone is null;
1 row selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
8
|Nelson|
|
-------------------------------
Exemplo 41
SQL>
No rows selected
------------------------------| PESSOA_COD | NOME |FONE |
-------------------------------
Exemplo 42
SQL> commit;
Transaction committed
Exemplo 43
SQL> delete from pessoa where NOME = 'Isaac';
Ok (1 row affected)
SQL> select * from pessoa;
5 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
|
8
|Nelson|
|
-------------------------------
Exemplo 44
SQL> rollback;
Transaction aborted
SQL>
6 rows selected
------------------------------| PESSOA_COD | NOME |FONE |
------------------------------|
2
|Silva | 282677 |
|
3
|Cabral| 260088 |
|
4
|Lobato| 174590 |
|
1
|OBilac| 260088 |
|
7
|Isaac | 281000 |
|
8
|Nelson|
|
-------------------------------
Exemplo 45
SQL> delete from pessoa where NOME = Lobato;
[interbase.interclient] violation of FOREIGN KEY constraint "INTEG_36"
on table "TURMA"
Exemplo 46
alter table turma add foreign key (profe) references pessoa
(pessoa_cod);
Exemplo 47
SQL> update turma
set NOME = 'Futebol'
Exerccio II
Criando as tabelas
create table empregado
(emp_cod integer not null,
NOME varchar(6),
fone varchar(10),
salario integer,
chefe integer,
depto character(1),
primary key (emp_cod));
create table depto
(depto_cod character(1) not null,
NOME varchar(12),
responsavel integer,
primary key (depto_cod));
alter table
foreign key
alter table
foreign key
alter table
foreign key
depto)
chefe, depto)
chefe, depto)
chefe, depto)
chefe, depto)
chefe, depto)
chefe, depto)
insert
values
insert
values
insert
values
Exemplo inicial
SQL> select * from empregado;
7 rows selected
-----------------------------------------------------------------| EMP_COD
| NOME |FONE | SALARIO
|
CHEFE
|DEPTO|
-----------------------------------------------------------------|
2
|Silva |
2677
|
30000
|
0
|
H
|
|
3
|Cabral|
1088
|
22000
|
2
|
S
|
|
4
|Lobato|
4590
|
28000
|
2
|
H
|
|
1
|OBilac|
2688
|
20000
|
3
|
S
|
|
8
|Maria |
2690
|
25000
|
4
|
C
|
|
9
|Antunes |
2698
|
26000
|
8
|
C
|
|
10
|Petter|
2645
|
22000
|
8
|
C
|
-----------------------------------------------------------------SQL> select * from depto;
3 rows selected
-----------------------------------------|DEPTO_cod|
NOME
|RESPONSAVEL|
-----------------------------------------|
H
|Sede|
2
|
|
S
| Seguranca |
3
|
|
C
| Computao |
8
|
------------------------------------------
Exemplo 1
SQL> select depto.NOME, empregado.NOME
from depto, empregado
where depto.responsavel = empregado.emp_cod;
3 rows selected
--------------------|
NOME
| NOME |
--------------------|Sede|Silva |
| Seguranca |Cabral|
| Computao |Maria |
---------------------
Exemplo 2
SQL> select avg(salario)
from empregado;
1 row selected
-------------
|
AVG
|
------------|
24714
|
-------------
Exemplo 3
SQL> select chefe, avg(salario)
from empregado
group by chefe;
5 rows selected
------------------------|
CHEFE
|
AVG
|
------------------------|
2
|
25000
|
|
3
|
20000
|
|
4
|
25000
|
|
8
|
24000
|
|
0
|
30000
|
-------------------------
Exemplo 4
SQL> select depto, avg(salario)
from empregado
group by depto;
3 rows selected
-----------------------|DEPTO|
AVG
|
-----------------------|
C
|
24333
|
|
H
|
29000
|
|
S
|
21000
|
------------------------
Exemplo 5
SQL> select depto, avg(salario)
from empregado
where salario > 20000
group by depto;
3 rows selected
-----------------------|DEPTO|
AVG
|
-----------------------|
C
|
24333
|
|
H
|
29000
|
|
S
|
22000
|
------------------------
Exemplo 6
SQL> select depto, avg(salario)
from empregado
group by depto
having avg(salario) > 22000;
2 rows selected
-----------------------|DEPTO|
AVG
|
-----------------------|
C
|
24333
|
|
H
|
29000
|
------------------------
Exemplo 7
SQL> select depto, avg(salario)
from empregado
where salario > 20000
group by depto
having avg(salario) > 22000;
2 rows selected
-----------------------|DEPTO|
AVG
|
-----------------------|
C
|
24333
|
|
H
|
29000
|
------------------------
Exemplo 8
SQL> select NOME, salario, NOME, salario
from empregado, empregado
where chefe = emp_cod
and salario > salario;
No rows selected
--------------------------------------| NOME | SALARIO
| NOME | SALARIO
|
---------------------------------------
Exemplo 9
SQL> select asalariado.NOME, asalariado.salario, chefe.NOME,
chefe.salario
from empregado asalariado, empregado chefe
where asalariado.chefe = chefe.emp_cod
and asalariado.salario > chefe.salario;
1 row selected
--------------------------------------| NOME | SALARIO
| NOME | SALARIO
|
--------------------------------------|Antunes |
26000
|Maria |
25000
|
---------------------------------------