Você está na página 1de 6

Cria9ao da base-resposta ao exercicio 1

utilizador - logut

Password da base - passbas Nome da base de dados - testes

o utilizador cria no linux , com um editor de texto, 0 ficheiro

CRIABASE.txt abaixo descrito (0 nome a BOLDe s6 para facilitar a legibilidade)

l;lse testes

drop table if exists clientei create table cliente (

codCli tinyint auto increment primary key, nomeCli char(lO), -

moradaCli char(lO),

telCli char(9),

limCred smallint,

contacto char(lO)

} i

Drop table if exists encomendai create table encomenda(

codEnc tinyint auto increment primary key, dataEnc date,

codCli tinyint

) ;

Drop table if exists objectoi create table objector

codObj tinyint auto increment primary key, nomeObj char(lO), -

puObj smallint

) i

drop table if exists linEnci create table linEnc(

codEnc tinyint not null, codObj tinyint not null,

qnt tinyint,

primary key(codEnc,codObj) ) i

chama 0 mysql utilizando 0 ficheiro CRIABASE.txt (um script) como fonte de informat;ao

LINUX >

mysql -u logut -p < criabase.txt

o mysql pede a Password ( mantendo-a secreta) e 0 utilizador digita passbas

Fase 2 Verifica9ao do esquema da base

o utilizador cria um script de verificat;ao a que chama schemaSistEncomendas.txt.

Poderia utilizar urn processo analogo a criayao da base.

Mas pelo contrario vai entrar na sua base ,vai criar um ficheiro resultados chamado esqBase.txt (utilizando a directiva \T e vai chamar 0 script de verificayao utilizando a directiva \. Nome do fichairo .

Para entrar directamente na base usa 0 modificador -D nome da base e ja nao necessita de 0 escrever no script de verificayao.

Respostas ao exercicio 1

Folha 1 de 6 26w03w2008

Conteudo do script de verifica9ao

show tables; desc encomenda; desc cliente; desc objecto; desc linEnCi

Para entrar na base

LINUX >

mysql -u logut -D testes -p

e responde com 0 password.

mysql> \T esqBase.txt Logging to file 'esqBase.txt'

mysql> \. schemaSistEncomendas.txt

+------------------+

I Tables in_testes I

+------------------+

I cliente

I encomenda J linEnc

I objecto

+------------------+

4 rows in set (0.00 sec)

+---------+------------+------+-----+---------+----------------+

I Field I Type I Null I Key I Default I Extra

+---------+------------+------+-----+---------+----------------+

I codEnc I tinyint(4) I I PRI I NULL I auto increment I

I dataEnc I date I YES I I NULL I I

I codCli I tinyint (4) I YES I I NULL I I

+---------+------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

+-----------+-------------+------+-----+---------+----------------+

I Field I Type I Null I Key I Default I Extra

+-----------+-------------+------+-----+---------+----------------+

I codCli I tinyint(4) I PRI NULL auto increment

I nomeCli I char(lO) YES! NULL

I moradaCli I char(lO) YES I NULL

I telCli I char(9) YES I NULL

I limCred I smallint(6) YES I NULL

I contacto I char(lO) YES I NULL

+-----------+-------------+------+-----+---------+----------------+

6 rows in set (0.00 sec)

+---------+-------------+------+-----+---------+----------------+

I Field I Type J Null I Key I Default I Extra I

+---------+-------------+------+-----+---------+----------------+

codObj tinyint(4) PRI NULL auto increment I

I nomeObj I char ( 10) I YES I NULL I

I puObj I smallint(6) I YES I NULL I

+---------+-------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

Respostas ao exercicio 1 Folha 2 de 6 26~03-2008

+--------+------------+------+--~--+---------+-------+

I Field I Type I Null I Key I Default I Extra I

+--------+------------+------+-----+---------+-------+

I codEnc I tinyint ( 4 ) I I PRI I 0

I codObj I tinyint (4) I I PRI I 0

I qnt I tinyint (4) I YES I I NULL

+--------+------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

Fase 3

Povoamento da base

A partir da linha de comandos do rnysql

mysql> insert into cliente(nomeCli) values ('A.Silva'); Query OK, 1 row affected (0.00 sec)

mysql> insert into cliente(nomeCli) values ('A.Cesar'); Query OK, 1 row affected (0.00 sec)

mysql> insert into encornenda(dataEnc,codCli) values('2001-4-l2',l); Query OK, 1 row affected (0.01 sec)

mysql> insert into encornenda(dataEnc,codCli) values('2001-4-l0',2); Query OK, 1 row affected (0.00 sec)

mysql> insert into encomenda(dataEncrcodCli) values('2001-4-25',l); Query OKr 1 row affected (0.00 sec)

mysql> insert into encomenda(dataEncrcodCli) values('2001-5-2'rl)i Query OK, 1 row affected (0.00 sec)

mysql> desc objecto;

+---------+-------------+------+-----+---------+----------------+

I Field I Type I Null I Key I Default I Extra

+---------+-------------+------+-----+---------+----------------+

codObj tinyint(4) I PRI NULL auto increment

I nomeObj I char (10) I YES I NULL

I puObj I smallint(6) I YES I NULL

+---------+-------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

mysql> insert into objecto values (0, 'cadeira'r200); Query OKr 1 row affected (0.00 sec)

mysql> insert into objecto values (Or 'banco',20)i Query OK, 1 row affected (0.00 sec)

mysql> insert into objecto values (0, 'cama',521); Query OK, 1 row affected (0.00 sec)

mysql> insert into objecto values (Or'mesa'r300)i Query OKr 1 row affected (0.00 sec)

Respostas ao exercicio 1

Folha 3 de 6 26-03-2008

mysgl> select * from objecto;

+--------+---------+-------+

I codObj I nomeObj I puObj I

+--------+---------+-------+

I 1 I cadeira 200

I 2 I banco 20

I 3 I cama 521

I 4 I mesa 300

+--------+---------+-------+

4 rows in set (0.00 sec)

mysgl> select * from encomenda;

+--------+------------+--------+

I codEnc I dataEnc I codeli I

+--------+------------+--------+

I 1 I 2001-04-12 1

I 2 I 2001-04 -10 2

I 3 I 2001-04-25 1

I 4 I 2001-05-02 1

+--------+------------+--------+

4 rows in set (0.00 sec)

mysql> desc linEnc;

+--------+------------+------+-----+---------+-------+

I Field I Type I Null I Key I Default I Extra I

+--------+------------+------+-----+---------+-------+

I codEnc I tinyint (4) I I PRI I 0

I codObj I tinyint (4) I I PRI I 0

I qnt I tinyint (4) I YES I I NULL

+--------+------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> insert into linEnc values (1,1,7); Query OK, 1 row affected (0.00 sec)

mysql> insert into linEnc values (1,2,14); Query OK, 1 row affected (0.00 sec)

mysql> insert into linEnc values (2,3,1); Query OK, 1 row affected (0.00 sec)

mysql> insert into linEnc values (3,1,5); Query OK, 1 row affected (0.00 sec)

mysql> insert into linEnc values (3,4,10); Query OK, 1 row affected (0.00 sec)

mysgl> insert into linEnc values (4,1,7); Query OK, 1 row affected (0.00 sec)

mysql> insert into linEnc values (4,2,14); Query OK, 1 row affected (0.00 sec)

mysql> insert into linEnc values (4,3,2); Query OK, 1 row affected (0.00 sec)

Respostas ao exercicio 1

Folha 4 de 6 26-03-2008

mysql> select * from linEnCi

+--------+--------+------+

I codEnc I codObj I qnt I

+--------+--------+------+

I 1 I 1 7

I 1 I 2 14

12131 13115 I 3 I 4 10 14117

I 4 I 2 14

I 4 I 3 2

+--------+--------+------+

8 rows in set (0.00 sec)

Fase 3 - Explora9ao da base

mysql> select nomeCli,dataEnc,codEnc from encomenda,cliente

-> where encomenda.codCli=cliente.codCli;

+---------+------------+--------+

I nomeCli I dataEnc I codEnc I

+---------+------------+--------+

A.Silva 2001-04-12 I 1

A.Silva 2001-04-25 I 3

A.Silva 2001-05-02 I 4

A.Cesar 2001-04-10 I 2

+---------+------------+--------+

4 rows in set (0.02 sec)

mysql> select sum (puObj *qnt) from objecto,linEnc where linEnc.codObj=

-> objecto.codObj and codEnc=4i

+----------------+

I sum (puObj *qnt)

+----------------+

2722 I

+----------------+

1 row in set (0.00 sec)

mysql> select nomeObj,puObj*qnt from objecto,linEnc where linEnc.codObj=

-> objecto.codObj and codEnc=4i

+---------+-----------+

I nomeObj I pUObj*qnt I

+---------+-----------+

cadeira 1400 I

I banco 280 I

I cama 1042 I

+---------+-----------+

3 rows in set (0.00 sec)

Respostas ao exercicio 1 Folha 5 de 6 26-03-2008

mysql> select codEnc,sum(puObj*qnt) from objecto,linEnc where linEnc.codObj=

-> objecto.codObj group by codenc;

+--------+----------------+

I codEnc I sum (puObj *qnt)

+--------+----------------+

I 1 I 1680

I 2 I 521

I 3 I 4000

I 4 I 2722

+--------+----------------+

4 rows in set (0.00 sec)

mysql> select encomenda.codEnc,dataEnc,sum(puObj*qnt) from objecto,linEnc,encomenda where linEnc.codObj=

-> objecto.codObj and encomenda.codEnc=linEnc.codEnc group by codenc;

+--------+------------+----------------+

I codEnc I dataEnc I sum (puObj *qnt)

+--------+------------+----------------+

I 1 I 2001-04-12 I 1680 t

I 2 t 2001-04-10 I 521 )

I 3 J 2001-04-25 t 4000 I

I 4 I 2001-05-02 I 2722 I

+--------+------------+----------------+

4 rows in set (0.05 sec)

mysql> select nomeCli,encomenda.codEnc,dataEnc,sum(puObj*qnt) from cliente,objecto,linEnc,encomenda where linEnc.codObj=

-> objecto.codObj and encomenda.codEnc=linEnc.codEnc and cliente.codCli=encomenda.codCli group by codenc;

+---------+--------+------------+----------------+

I nomeCli I codEnc I dataEnc I sum(puObj*qnt) I

+---------+--------+------------+----------------+

I A.Silva I 1 I 2001-04-12 I 1680 I

I A.Cesar I 2 I 2001-04-10 I 521 I

I A.Silva I 3 I 2001-04-25 I 4000 I

I A.Silva I 4 I 2001-05-02 I 2722 I

+---------+--------+------------+----------------+

4 rows in set (0.01 sec)

Respostas ao exercicio 1

Folha 6 de 6 26-03-2008