SlideShare uma empresa Scribd logo
Instituto Federal de Educação, Ciência e Tecnologia do Ceará Campus Cedro
Curso: Integrado Informática
Disciplina: Banco De Dado
Exercícios
1. Implementar um BDR para o seguinte modelo:
a) Inserir registros para:
7 clientes (Um deles é o nome Antônio José)
4 Vendedores (Um deles possuirá o nome Francisco Marcos)
15 Compras (Duas delas possuirão a data 12-12-2012)
9 Produtos (Um deles possuirá o nome Rapadura e o outro o nome Farinha)
b) Cada tabela deverá conter com pelo menos 4 campos além das chaves.
c) Criar as seguintes consultas:
 Retornar todos os produtos cujo preço é menor que $15.
 Retornar os clientes que compraram na data 12-12-2012.
 Retornar um histórico de produtos comprados pelo o cliente Antônio José.
 Retornar o histórico de vendas do vendedor Marcos (Apenas palavra chave).
 Retornar todos os clientes atendidos pelo vendedor cujo "Id" é igual a 3.
 Retornar o salário vendedor de "Id" = 2, levando em conta uma comissão de
7%sobre os produtos vendidos por ele somando a $700.
d) Alterar o endereço do cliente cujo "Id" = 6.
e) Incluir o campo "Cliente_Nasc" na tabela "Clientes".
f) Criar a tabela "Fornecedores" relacionando a "Produtos"
g) Crie uma VIEW para cada Consulta.
Respostas
1. //Criando o banco dados:
Create database SuperMercado;
Query OK, 1 row affected (0.00 sec)
Use SuperMercado;
Database changed
//Criando a Tabela Clientes:
create table Clientes(
cliente_Id int not null auto_increment,
cliente_Nome varchar(100) not null,
cliente_Cpf varchar(20) not null,
cliente_End varchar(50) not null,
cliente_Tel varchar(20) not null,
primary key (cliente_Id)
);
Query OK, 0 rows affected (0.06 sec)
describe clientes;
Field Type Null Key Default Extra
cliente_Id int(11) NO PRI NULL auto_increment
cliente_ Nome varchar(100) NO NULL
cliente_ Cpf varchar(20) NO NULL
cliente_ End varchar(20) NO NULL
cliente_ Tel varchar(15) NO NULL
5 rows in set (0.00 sec)
//Criando a Tabela Vendedores:
create table Vendedores(
Vendedor_Id int not null auto_increment,
Vendedor_Nome varchar(100) not null,
Vendedor_Cpf varchar(15) not null,
Vendedor_Tel varchar(10) not null,
Vendedor_Desc varchar(50) null,
primary key (Vendedor_Id)
);
Query OK, 0 rows affected (0.05 sec)
describe vendedores;
Field Type Null Key Default Extra
Vendedor_Id int(11) NO PRI NULL auto_increment
Vendedor_Nome varchar(100) NO NULL
Vendedor_Cpf varchar(15) NO NULL
Vendedor_Tel varchar(10) NO NULL
Vendedor_Desc varchar(50) YES NULL
5 rows in set (0.08 sec)
//Inserindo Dados na Tabela Clientes:
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Antonio Jose','125.432.34-9','Rua: Machado N:162','88 9823-1628');
Query OK, 1 row affected (0.11 sec)
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Francisca Maria','735.432.34-9','Rua: Cacimba N:762','89 8363-1738');
Query OK, 1 row affected (0.11 sec)
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Mariana Costa','892.432.44-9','Rua: Cacimbão N:18','92 7363-1828');
Query OK, 1 row affected (0.11 sec)
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Francinete Martins','826.139.625-2','Rua: Meireles N:83','88 7853-1828');
Query OK, 1 row affected (0.11 sec)
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Geraldo Souza','826.139.863-1','Rua: Rosario N:300','83 9233-1828');
Query OK, 1 row affected (0.11 sec)
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Jucineldo Gaucho','283.139.836-2','Rua: Rodrigues N:100','89 2733-1828');
Query OK, 1 row affected (0.11 sec)
insert into clientes
(cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel)
values
('Marta Farias','937.139.625-4','Rua: Raugil N:987','87 9873-1828');
Query OK, 1 row affected (0.11 sec)
//Resultado da Tabela Clientes:
select * from clientes;
cliente_Nome cliente_Cpf cliente_End cliente_Tel cliente_Id
Antonio Jose 125.432.34-9 Rua: Machado
N:162
88 9823-
1628
1
Francisca Maria 735.432.34-9 Rua: Cacimba
N:762
89 8363-
1738
2
Mariana Costa 892.432.44-9 Rua: Cacimbão
N:18
92 7363-
1828
3
Francinete
Martins
826.139.625-2 Rua: Meireles
N:83
88 7853-
1828
4
Geraldo Souza 826.139.863-1 Rua: RosarioN:300 83 9233-
1828
5
Jucineldo
Gaucho
283.139.836-2 Rua: Rodrigues
N:100
89 2733-
1828
6
Marta Farias 937.139.625-4 Rua: Raugil N:987 87 9873-
1828
7
7 rows in set (0.07 sec)
//Inserindo Dados na Tabela Vendedores:
insert into vendedores
(Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel)
values
('Francisco Airton', '283.73.93-2','9725-2537');
Query OK, 1 row affected (0.08 sec)
insert into vendedores
(Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel)
values
('Jose Marcos', '837.73.93-2','9263-2537');
Query OK, 1 row affected (0.08 sec)
insert into vendedores
(Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel)
values
('Cleiton Chavier', '927.73.93-2','9835-2537');
Query OK, 1 row affected (0.08 sec)
insert into vendedores
(Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel)
values
('Joao Ferreira', '933.73.93-2','9933-2537');
Query OK, 1 row affected (0.08 sec)
//Resultado da Tabela Vendedores:
select * from vendedores;
Vendedor_Id Vendedor_Nome Vendedor_Cpf Vendedor_Tel Vendedor_Salario
1 Francisco Airton 283.73.93-2 9725-2537 NULL
2 Jose Marcos 837.73.93-2 9263-2537 NULL
3 Cleiton Chavier 927.73.93-2 9835-2537 NULL
4 Joao Ferreira 933.73.93-2 9933-2537 NULL
4 rows in set (0.06 sec)
//Criando a Tabela Compras:
create table Compras(
Compra_Id int not null auto_increment,
Compra_Data varchar (10) not null ,
Compra_Hora varchar(10) not null,
Compra_End varchar(100) not null,
Compra_Desc varchar(100) null,
primary key (Compra_Id),
Id_Cliente int not null,
Id_Vendedor int not null,
foreign key (Id_Cliente)
references Clientes (Cliente_Id),
foreign key (Id_Vendedor)
references Vendedores (Vendedor_Id)
);
Query OK, 0 rows affected (0.53 sec)
describe Compras;
Field Type Null Key Default Extra
Compra_Id int(11) NO PRI NULL auto_increment
Compra_Data varchar(10) NO NULL
Compra_Hora varchar(10) NO NULL
Compra_End varchar(100) NO NULL
Compra_Desc varchar(100) YES NULL
Id_Cliente int(11) NO MUL NULL
Id_Vendedor int(11) NO MUL NULL
7 rows in set (0.00 sec)
//Inserindo Dados na Tabela Compras:
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('12-12-2012', '12:00', 'Rua: Machado N:162',1,1);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('12-12-2012', '10:00', 'Rua: Cacimba N:762',2,1);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('11-12-2012', '14:00', 'Rua: Cacimbão N:18',3,1);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('02-02-2012', '13:00', 'Rua: Meireles N:83',4,1);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('01-01-2012', '08:00', 'Rua: RosarioN: 300',5,2);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('20-03-2012', '13:00', 'Rua: Rodrigues N:100',6,2);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('15-05-2012', '17:00', 'Rua: Raugil N:987',7,2);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('02-04-2012', '14:00', 'Rua: Rodrigues N:100',6,3);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('06-02-2012', '10:00', 'Rua: RosarioN: 300',5,3);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('15-11-2012', '11:00', 'Rua: Machado N:162',1,3);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('09-11-2012', '09:00', 'Rua: Cacimbão N:18',3,3);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('10-06-2012', '12:00', 'Rua: Raugil N:987',7,3);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('05-05-2012', '16:00', 'Rua: Rodrigues N:100',6,4);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('09-11-2012', '08:00', 'Rua: Cacimba N:762',2,4);
insert into compras
(Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor)
values
('08-11-2012', '09:00', 'Rua: Cacimba N:762',7,4);
//Resultado da Tabela Compras:
select * from Compras;
Compra_I
d
Compra_Dat
a
Compra_Hor
a
Compra_En
d
Compra_Des
c
Id_Client
e
Id_Vendedo
r
1 12-12-2012 12:00 Rua:
Machado
N:162
NULL 1 1
2 12-12-2012 10:00 Rua:
Cacimba
N:762
NULL 2 1
3 12-12-2012 10:00 Rua:
Cacimba
N:762
NULL 2 1
4 11-12-2012 14:00 Rua:
Cacimbão
N:18
NULL 3 1
5 02-02-2012 13:00 Rua:
Meireles
N:83
NULL 4 1
6 01-01-2012 08:00 Rua:
Rosario
N: 300
NULL 5 2
7 20-03-2012 13:00 Rua:
Rodrigues
N:100
NULL 6 2
8 15-05-2012 17:00 Rua: Raugil
N:987
NULL 7 2
9 02-04-2012 14:00 Rua:
Rodrigues
N:100
NULL 6 3
10 06-02-2012 10:00 Rua:
Rosario
N: 300
NULL 5 3
11 15-11-2012 11:00 Rua:
Machado
N:162
NULL 1 3
12 09-11-2012 09:00 Rua:
Cacimbão
N:18
NULL 3 3
13 10-06-2012 12:00 Rua: Raugil
N:987
NULL 7 3
14 05-05-2012 16:00 Rua:
Rodrigues
N:100
NULL 6 4
15 08-11-2012 09:00 Rua:Cacimb
a
N:762
NULL 7 4
//Criando a Tabela Produtos:
create table Produtos(
Produto_Id int not null auto_increment,
Produto_Nome varchar (100) not null,
Produto_Unid varchar (10) not null,
Produto_Desc varchar (100) null,
Produto_Preco varchar (10) not null,
primary key (Produto_Id)
);
Query OK, 0 rows affected (0.04 sec)
describe produtos;
Field Type Null Key Default Extra
Produto_Id int(11) NO PRI NULL auto_increment
Produto_Nome varchar(100) NO NULL
Produto_Unid varchar(20) NO NULL
Produto_Desc varchar(20) YES NULL
Produto_Preco varchar(15) YES NULL
5 rows in set (0.00 sec)
//Inserindo Dados na Tabela Produtos;
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Rapadura', ' 1kg ', 'Preta', 'R$ 3,00');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Farinha', ' 1kg ', 'Mandioca', ' R$ 3,20');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Arroz', ' 1kg ', 'Branco', ' R$ 3,49');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Arroz', ' 1kg ', 'Parboilizado', ' R$ 2,89 ');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Feijão', ' 1kg ', 'Preto', ' R$ 3,99');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Farinha', ' 1kg ', 'Trigo', ' R$ 2,79');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Farinha', ' 1kg ', 'Mandioca', ' R$ 2,19');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Macarrão', ' 1 pacote ', 'Italiano', 'R$ 3,00');
insert into produtos
(Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco )
values
('Vitamilho', ' 500g ', 'milho', 'R$ 1,10');
//Resultado da Tabela Produtos:
select * from Produtos;
Produto_Id Produto_Nome Produto_Unid Produto_Desc Produto_Preco
1 Rapadura 1kg Preta R$ 3,00
2 Farinha 1kg Mandioca R$ 3,20
3 Arroz 1kg Branco R$ 3,49
4 Arroz 1kg Parboilizado R$ 2,89
5 Feijão 1kg Preto R$ 3,99
6 Farinha 1kg Trigo R$ 2,79
7 Farinha 1kg Mandioca R$ 2,19
8 Macarrão 1 pacote Italiano R$ 3,00
9 Vitamilho 500g Milho R$ 1,10
9 rows in set (0.00 sec)
//Criando a Tabela RTC:
create table RCP(
RCP_Id int not null auto_Increment,
Id_Produto int not null,
Id_Compra int not null,
primary key (RCP_Id),
foreign key (Id_Produto)
references Produtos (Produto_Id),
foreign key (Id_Compra)
references Compras (Compra_Id)
);
Query OK, 0 rows affected (0.20 sec)
describe RCP;
Field Type Null Key Default Extra
RCP_Id int(11) NO PRI NULL auto_increment
Id_Produto int(11) NO MUL NULL
Id_Compra int(11) NO MUL NULL
//Inserindo Dados na Tabela RCP:
insert into RCP
(Id_Produto, Id_Compra )
values
(1,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(2,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(3,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(4,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(5,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(6,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(7,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(8,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(9,1);
insert into RCP
(Id_Produto, Id_Compra )
values
(1,2);
insert into RCP
(Id_Produto, Id_Compra )
values
(3,2);
insert into RCP
(Id_Produto, Id_Compra )
values
(5,2);
insert into RCP
(Id_Produto, Id_Compra )
values
(7,2);
insert into RCP
(Id_Produto, Id_Compra )
values
(9,2);
insert into RCP
(Id_Produto, Id_Compra )
values
(2,3);
insert into RCP
(Id_Produto, Id_Compra )
values
(4,3);
insert into RCP
(Id_Produto, Id_Compra )
values
(6,3);
insert into RCP
(Id_Produto, Id_Compra )
values
(8,3);
insert into RCP
(Id_Produto, Id_Compra )
values
(1,4);
insert into RCP
(Id_Produto, Id_Compra )
values
(3,4);
insert into RCP
(Id_Produto, Id_Compra )
values
(6,4);
insert into RCP
(Id_Produto, Id_Compra )
values
(9,4);
insert into RCP
(Id_Produto, Id_Compra )
values
(2,5);
insert into RCP
(Id_Produto, Id_Compra )
values
(5,5);
insert into RCP
(Id_Produto, Id_Compra )
values
(8,5);
insert into RCP
(Id_Produto, Id_Compra )
values
(3,6);
insert into RCP
(Id_Produto, Id_Compra )
values
(6,6);
insert into RCP
(Id_Produto, Id_Compra )
values
(9,6);
insert into RCP
(Id_Produto, Id_Compra )
values
(1,7);
insert into RCP
(Id_Produto, Id_Compra )
values
(2,8);
insert into RCP
(Id_Produto, Id_Compra )
values
(3,9);
insert into RCP
(Id_Produto, Id_Compra )
values
(4,10);
insert into RCP
(Id_Produto, Id_Compra )
values
(5,11);
insert into RCP
(Id_Produto, Id_Compra )
values
(6,12);
insert into RCP
(Id_Produto, Id_Compra )
values
(7,13);
insert into RCP
(Id_Produto, Id_Compra )
values
(8,14);
insert into RCP
(Id_Produto, Id_Compra )
values
(9,15);
//Resultado da Tabela RCP:
select * from rcp;
RCP_Id Id_Produto Id_Compra
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 6 1
7 7 1
8 8 1
9 9 1
10 1 2
11 3 2
12 5 2
13 7 2
14 9 2
15 2 3
16 4 3
17 6 3
18 8 3
19 1 4
20 3 4
21 6 4
22 9 4
23 2 5
24 5 5
25 8 5
26 3 6
27 6 6
28 9 6
29 1 7
30 2 8
31 3 9
32 4 10
33 5 11
34 6 12
35 7 13
36 8 14
37 9 15
38 rows in set (0.00 sec)
//Realizando a consulta:
Select Produtos.Produto_Nome, Produtos.Produto_preco from Produtos
Where
Produto_preco < 15 and
Produto_id >=1 and
Produto_Id <=9;
//Retornando todos os produtos cujo preço é menor que $15:
Produto_Nome Produto_preco
Rapadura R$ 3,00
Farinha R$ 3,20
Arroz R$ 3,49
Arroz R$ 2,89
Feijão R$ 3,99
Farinha R$ 2,79
Farinha R$ 2,19
Macarrão R$ 3,00
Vitamilho R$ 1,10
9 rows in set (0.12 sec)
//Realizando consulta:
Select Compras.Id_Cliente, Clientes.Cliente_Nome
From Compras, Clientes
Where
Compras.Compra_Data=’12-12-2012’ and
Clientes.Cliente_id=Compras.Id_Cliente;
//Retornando os clientes que compraram na data 12-12-2012:
Id_Cliente Cliente_Nome
1 Antonio Jose
2 Francisca Maria
// Realizando consulta:
select Clientes.Cliente_Nome, Compras.Id_Cliente, Produtos.Produto_Nome,
RCP.Id_Produto, Compras.Compra_id
from Clientes, Compras,Produtos, RCP
where Clientes.Cliente_Nome=’Antonio Jose’ and
Clientes.Cliente_Id =Compras.Id_Cliente and
Compras.Compra_id=RCP.Id_Compra and
Produtos.Produto_id=RCP.Id_Produto;
//Retornando um histórico de produtos comprados pelo o cliente Antônio José:
Cliente_Nome Id_Cliente Produto_Nome Id_Produto Compra_id
Antonio Jose 1 Rapadura 1 1
Antonio Jose 1 Farinha 2 1
Antonio Jose 1 Arroz 3 1
Antonio Jose 1 Arroz 4 1
Antonio Jose 1 Feijão 5 1
Antonio Jose 1 Farinha 6 1
Antonio Jose 1 Farinha 7 1
Antonio Jose 1 Macarrão 8 1
Antonio Jose 1 Vitamilho 9 1
Antonio Jose 1 Arroz 4 10
// Realizando consulta:
select Vendedores.Vendedor_Nome, Compras.Compra_id, Compras.Compra_Data
from Vendedores, Compras
where Vendedores.Vendedor_Nome like ’%Marcos%’ and
Vendedores.Vendedor_Id = Compras.Id_Vendedor;
//Retornando o histórico de vendas do vendedor Marcos (Apenas palavra chave):
Vendedor_Nome Compra_id Compra_Data
Jose Marcos 5 01-01-2012
Jose Marcos 6 20-03-2012
Jose Marcos 7 15-05-2012
// Realizando consulta:
Select Clientes.Cliente_Nome, Vendedores.Vendedor_Id
From Clientes, Vendedores ,Compras
Where Vendedor_Id = 3 and
Compras.Id_Cliente=Clientes.Cliente_Id and
Compras.Id_Vendedor=Vendedores.Vendedor_Id;
//Retornando todos os clientes atendidos pelo vendedor cujo "Id" é igual a 3:
Cliente_Nome Vendedor_Id
Jucineldo Gaucho 3
Geraldo Souza 3
Antonio Jose 3
Mariana Costa 3
Marta Farias 3
// Realizando consulta:
Não sei 
//Retornando o salário vendedor de "Id" = 2, levando em conta uma comissão de
7% sobre os produtos vendidos por ele somando a $700:

//Alterando o endereço do cliente "Id" = 6:
update Clientes set Cliente_End = 'Rua: Belo Horizonte N: 23' Where Cliente_Id = 6;
select * from Clientes;
cliente_Nome cliente_Cpf cliente_End cliente_Tel cliente_Id
Antonio Jose 125.432.34-9 Rua: Machado N:162 88 9823-
1628
1
Francisca Maria 735.432.34-9 Rua: Cacimba N:762 89 8363-
1738
2
Mariana Costa 892.432.44-9 Rua: Cacimbão N:18 92 7363-
1828
3
Francinete
Martins
826.139.625-
2
Rua: Meireles N:83 88 7853-
1828
4
Geraldo Souza 826.139.863-
1
Rua: RosarioN:300 83 9233-
1828
5
Jucineldo Gaucho 283.139.836-
2
Rua: Belo Horizonte N:
23
89 2733-
1828
6
Marta Farias 937.139.625-
4
Rua: Raugil N:987 87 9873-
1828
7
// Incluindo o campo "Cliente_Nasc" na tabela "Clientes":
alter table Clientes add Cliente_Nasc varchar(8);
cliente_Nome cliente_Cpf cliente_End cliente_Tel cliente_Id Cliente_Nasc
Antonio Jose 125.432.34-9 Rua: Machado
N:162
88 9823-
1628
1 NULL
Francisca
Maria
735.432.34-9 Rua: Cacimba
N:762
89 8363-
1738
2 NULL
Mariana Costa 892.432.44-9 Rua: Cacimbão
N:18
92 7363-
1828
3 NULL
Francinete
Martins
826.139.625-
2
Rua: Meireles
N:83
88 7853-
1828
4 NULL
Geraldo Souza 826.139.863-
1
Rua:
RosarioN:300
83 9233-
1828
5 NULL
Jucineldo
Gaucho
283.139.836-
2
Rua: Belo
Horizonte N: 23
89 2733-
1828
6 NULL
Marta Farias 937.139.625-
4
Rua: Raugil
N:987
87 9873-
1828
7 NULL
// Criar a tabela "Fornecedores" relacionando a "Produtos":
Create Table Fornecedores (
Forne_Id int not null auto_increment,
Forne_Data Varchar(10),
Forne_Hora Varchar(10),
Id_Produto int not null,
Primary key (Forne_Id),
foreign key (Id_Produto) references Produtos (Produto_Id));
describe Fornecedores;
Field Type Null Key Default Extra
Forne_Id int(11) NO PRI NULL auto_increment
Forne_Data varchar(10) YES NULL
Forne_Hora varchar(10) YES NULL
Id_Produto int(11) NO MUL NULL
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 8
Server version: 5.1.36-community MySQL Community Server (GPL)
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql> use SM;
Database changed
mysql> update Clientes
-> set Cliente_End='Rua: Matias N: 400'
-> where
-> Cliente_Id=6;
Query OK, 1 row affected (0.36 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> altar table Clientes add Cliente_Nasc varchar(20) not null;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'altar
table Clientes add Cliente_Nasc varchar(20) not null' at line 1
mysql> altar table Clientes add Cliente_Nasc varchar(20);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'altar
table Clientes add Cliente_Nasc varchar(20)' at line 1
mysql> alter table Clientes add Cliente_Nasc varchar(20) not null;
Query OK, 7 rows affected (0.27 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> describe Clientes
-> ;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| cliente_Id | int(11) | NO | PRI | NULL | auto_increment |
| cliente_Nome | varchar(100) | NO | | NULL | |
| cliente_Cpf | varchar(20) | NO | | NULL | |
| cliente_End | varchar(50) | NO | | NULL | |
| cliente_Tel | varchar(20) | NO | | NULL | |
| Cliente_Nasc | varchar(20) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)
mysql> create view PrecoMenorquinze
-> as select Produto_Nome, Produto_Preco
-> from Produtos
-> where
-> Produto_Preco < '15';
Query OK, 0 rows affected (0.08 sec)
mysql> select * from PrecoMenorQuinze;
+--------------+---------------+
| Produto_Nome | Produto_Preco |
+--------------+---------------+
| Farinha | R$ 3,20 |
| Arroz | R$ 3,49 |
| Arroz | R$ 2,89 |
| Feijão | R$ 3,99 |
| Farinha | R$ 2,79 |
| Farinha | R$ 2,19 |
+--------------+---------------+
6 rows in set (0.00 sec)
mysql> create view ComprasNaData12_12_2012
-> As select Cliente_Nome, Compra_Data
-> from Clientes, Compras
-> where
-> Compra_Data = '12-12-2012' and
-> cliente_Id = Id_Cliente;
Query OK, 0 rows affected (0.09 sec)
mysql> Select * from ComprasNaData12_12_2012;
+-----------------+-------------+
| Cliente_Nome | Compra_Data |
+-----------------+-------------+
| Antonio Jose | 12-12-2012 |
| Francisca Maria | 12-12-2012 |
+-----------------+-------------+
2 rows in set (0.00 sec)
mysql> create view PCCAntonioJose
-> As select Produto_Nome
-> from Clientes, Compras,Produtos, RCP
-> where Cliente_Nome='Antonio Jose' and
-> Cliente_Id = Id_Cliente and
-> Compra_id = Id_Compra and
-> Produto_id = Id_Produto;
Query OK, 0 rows affected (0.34 sec)
mysql> select * from PCCAntonioJose;
+--------------+
| Produto_Nome |
+--------------+
| Rapadura |
| Farinha |
| Arroz |
| Arroz |
| Arroz |
| Feijão |
| Farinha |
| Farinha |
| Macarrão |
| Vitamilho |
+--------------+
10 rows in set (0.00 sec)
mysql> create view HistoricoDeVendaMarcos
-> As select Compra_Id, Vendedor_Nome
-> from Vendedores, Compras
-> where
-> Vendedor_Nome Like '%Marcos%'and
-> Vendedor_Id = Id_Vendedor;
Query OK, 0 rows affected (0.09 sec)
mysql> select * from HistoricoDeVendaMarcos;
+-----------+---------------+
| Compra_Id | Vendedor_Nome |
+-----------+---------------+
| 5 | Jose Marcos |
| 6 | Jose Marcos |
| 7 | Jose Marcos |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> create view CAVI3
-> As select Cliente_Nome, Vendedor_Id
-> from Clientes, Compras, Vendedores
-> where
-> Vendedor_Id = 3 and
-> Vendedor_id = Id_Vendedor and
-> Cliente_Id = Id_Cliente;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from CAVI3;
+------------------+-------------+
| Cliente_Nome | Vendedor_Id |
+------------------+-------------+
| Jucineldo Gaucho | 3 |
| Geraldo Souza | 3 |
| Antonio Jose | 3 |
| Mariana Costa | 3 |
| Marta Farias | 3 |
+------------------+-------------+
5 rows in set (0.02 sec)
mysql> create view SalarioVI2
-> As Select Sum(Produto_Preco * 0.07 + 700)
-> from Vendedores, Produtos, Compras, RCP
-> where
-> Vendedor_Id = 2 and
-> Vendedor_Id = Id_Vendedor and
-> Produto_Id = Id_Produto and
-> Compra_Id = Id_Compra;
Query OK, 0 rows affected (0.09 sec)
mysql> select * from SalarioVI3;
ERROR 1146 (42S02): Table 'sm.salariovi3' doesn't exist
mysql> select * from SalarioVI2;
+---------------------------------+
| Sum(Produto_Preco * 0.07 + 700) |
+---------------------------------+
| 4900 |
+---------------------------------+
1 row in set (0.01 sec)
mysql>

Mais conteúdo relacionado

Destaque

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Destaque (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Implementar Um Banco de Dados

  • 1. Instituto Federal de Educação, Ciência e Tecnologia do Ceará Campus Cedro Curso: Integrado Informática Disciplina: Banco De Dado Exercícios 1. Implementar um BDR para o seguinte modelo: a) Inserir registros para: 7 clientes (Um deles é o nome Antônio José) 4 Vendedores (Um deles possuirá o nome Francisco Marcos) 15 Compras (Duas delas possuirão a data 12-12-2012) 9 Produtos (Um deles possuirá o nome Rapadura e o outro o nome Farinha) b) Cada tabela deverá conter com pelo menos 4 campos além das chaves. c) Criar as seguintes consultas:  Retornar todos os produtos cujo preço é menor que $15.  Retornar os clientes que compraram na data 12-12-2012.  Retornar um histórico de produtos comprados pelo o cliente Antônio José.  Retornar o histórico de vendas do vendedor Marcos (Apenas palavra chave).  Retornar todos os clientes atendidos pelo vendedor cujo "Id" é igual a 3.  Retornar o salário vendedor de "Id" = 2, levando em conta uma comissão de 7%sobre os produtos vendidos por ele somando a $700. d) Alterar o endereço do cliente cujo "Id" = 6.
  • 2. e) Incluir o campo "Cliente_Nasc" na tabela "Clientes". f) Criar a tabela "Fornecedores" relacionando a "Produtos" g) Crie uma VIEW para cada Consulta.
  • 3. Respostas 1. //Criando o banco dados: Create database SuperMercado; Query OK, 1 row affected (0.00 sec) Use SuperMercado; Database changed //Criando a Tabela Clientes: create table Clientes( cliente_Id int not null auto_increment, cliente_Nome varchar(100) not null, cliente_Cpf varchar(20) not null, cliente_End varchar(50) not null, cliente_Tel varchar(20) not null, primary key (cliente_Id) ); Query OK, 0 rows affected (0.06 sec) describe clientes; Field Type Null Key Default Extra cliente_Id int(11) NO PRI NULL auto_increment cliente_ Nome varchar(100) NO NULL cliente_ Cpf varchar(20) NO NULL cliente_ End varchar(20) NO NULL cliente_ Tel varchar(15) NO NULL 5 rows in set (0.00 sec) //Criando a Tabela Vendedores: create table Vendedores( Vendedor_Id int not null auto_increment, Vendedor_Nome varchar(100) not null, Vendedor_Cpf varchar(15) not null, Vendedor_Tel varchar(10) not null, Vendedor_Desc varchar(50) null, primary key (Vendedor_Id)
  • 4. ); Query OK, 0 rows affected (0.05 sec) describe vendedores; Field Type Null Key Default Extra Vendedor_Id int(11) NO PRI NULL auto_increment Vendedor_Nome varchar(100) NO NULL Vendedor_Cpf varchar(15) NO NULL Vendedor_Tel varchar(10) NO NULL Vendedor_Desc varchar(50) YES NULL 5 rows in set (0.08 sec) //Inserindo Dados na Tabela Clientes: insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Antonio Jose','125.432.34-9','Rua: Machado N:162','88 9823-1628'); Query OK, 1 row affected (0.11 sec) insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Francisca Maria','735.432.34-9','Rua: Cacimba N:762','89 8363-1738'); Query OK, 1 row affected (0.11 sec) insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Mariana Costa','892.432.44-9','Rua: Cacimbão N:18','92 7363-1828'); Query OK, 1 row affected (0.11 sec) insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Francinete Martins','826.139.625-2','Rua: Meireles N:83','88 7853-1828'); Query OK, 1 row affected (0.11 sec)
  • 5. insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Geraldo Souza','826.139.863-1','Rua: Rosario N:300','83 9233-1828'); Query OK, 1 row affected (0.11 sec) insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Jucineldo Gaucho','283.139.836-2','Rua: Rodrigues N:100','89 2733-1828'); Query OK, 1 row affected (0.11 sec) insert into clientes (cliente_Nome, cliente_Cpf, cliente_End, cliente_Tel) values ('Marta Farias','937.139.625-4','Rua: Raugil N:987','87 9873-1828'); Query OK, 1 row affected (0.11 sec) //Resultado da Tabela Clientes: select * from clientes; cliente_Nome cliente_Cpf cliente_End cliente_Tel cliente_Id Antonio Jose 125.432.34-9 Rua: Machado N:162 88 9823- 1628 1 Francisca Maria 735.432.34-9 Rua: Cacimba N:762 89 8363- 1738 2 Mariana Costa 892.432.44-9 Rua: Cacimbão N:18 92 7363- 1828 3 Francinete Martins 826.139.625-2 Rua: Meireles N:83 88 7853- 1828 4 Geraldo Souza 826.139.863-1 Rua: RosarioN:300 83 9233- 1828 5 Jucineldo Gaucho 283.139.836-2 Rua: Rodrigues N:100 89 2733- 1828 6 Marta Farias 937.139.625-4 Rua: Raugil N:987 87 9873- 1828 7 7 rows in set (0.07 sec) //Inserindo Dados na Tabela Vendedores: insert into vendedores
  • 6. (Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel) values ('Francisco Airton', '283.73.93-2','9725-2537'); Query OK, 1 row affected (0.08 sec) insert into vendedores (Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel) values ('Jose Marcos', '837.73.93-2','9263-2537'); Query OK, 1 row affected (0.08 sec) insert into vendedores (Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel) values ('Cleiton Chavier', '927.73.93-2','9835-2537'); Query OK, 1 row affected (0.08 sec) insert into vendedores (Vendedor_Nome,Vendedor_Cpf, Vendedor_Tel) values ('Joao Ferreira', '933.73.93-2','9933-2537'); Query OK, 1 row affected (0.08 sec) //Resultado da Tabela Vendedores: select * from vendedores; Vendedor_Id Vendedor_Nome Vendedor_Cpf Vendedor_Tel Vendedor_Salario 1 Francisco Airton 283.73.93-2 9725-2537 NULL 2 Jose Marcos 837.73.93-2 9263-2537 NULL 3 Cleiton Chavier 927.73.93-2 9835-2537 NULL 4 Joao Ferreira 933.73.93-2 9933-2537 NULL 4 rows in set (0.06 sec) //Criando a Tabela Compras: create table Compras( Compra_Id int not null auto_increment, Compra_Data varchar (10) not null , Compra_Hora varchar(10) not null, Compra_End varchar(100) not null,
  • 7. Compra_Desc varchar(100) null, primary key (Compra_Id), Id_Cliente int not null, Id_Vendedor int not null, foreign key (Id_Cliente) references Clientes (Cliente_Id), foreign key (Id_Vendedor) references Vendedores (Vendedor_Id) ); Query OK, 0 rows affected (0.53 sec) describe Compras; Field Type Null Key Default Extra Compra_Id int(11) NO PRI NULL auto_increment Compra_Data varchar(10) NO NULL Compra_Hora varchar(10) NO NULL Compra_End varchar(100) NO NULL Compra_Desc varchar(100) YES NULL Id_Cliente int(11) NO MUL NULL Id_Vendedor int(11) NO MUL NULL 7 rows in set (0.00 sec) //Inserindo Dados na Tabela Compras: insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('12-12-2012', '12:00', 'Rua: Machado N:162',1,1); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('12-12-2012', '10:00', 'Rua: Cacimba N:762',2,1); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('11-12-2012', '14:00', 'Rua: Cacimbão N:18',3,1);
  • 8. insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('02-02-2012', '13:00', 'Rua: Meireles N:83',4,1); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('01-01-2012', '08:00', 'Rua: RosarioN: 300',5,2); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('20-03-2012', '13:00', 'Rua: Rodrigues N:100',6,2); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('15-05-2012', '17:00', 'Rua: Raugil N:987',7,2); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('02-04-2012', '14:00', 'Rua: Rodrigues N:100',6,3); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('06-02-2012', '10:00', 'Rua: RosarioN: 300',5,3); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('15-11-2012', '11:00', 'Rua: Machado N:162',1,3); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values
  • 9. ('09-11-2012', '09:00', 'Rua: Cacimbão N:18',3,3); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('10-06-2012', '12:00', 'Rua: Raugil N:987',7,3); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('05-05-2012', '16:00', 'Rua: Rodrigues N:100',6,4); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('09-11-2012', '08:00', 'Rua: Cacimba N:762',2,4); insert into compras (Compra_Data , Compra_Hora , Compra_End , Id_Cliente, Id_Vendedor) values ('08-11-2012', '09:00', 'Rua: Cacimba N:762',7,4); //Resultado da Tabela Compras: select * from Compras; Compra_I d Compra_Dat a Compra_Hor a Compra_En d Compra_Des c Id_Client e Id_Vendedo r 1 12-12-2012 12:00 Rua: Machado N:162 NULL 1 1 2 12-12-2012 10:00 Rua: Cacimba N:762 NULL 2 1 3 12-12-2012 10:00 Rua: Cacimba N:762 NULL 2 1 4 11-12-2012 14:00 Rua: Cacimbão N:18 NULL 3 1
  • 10. 5 02-02-2012 13:00 Rua: Meireles N:83 NULL 4 1 6 01-01-2012 08:00 Rua: Rosario N: 300 NULL 5 2 7 20-03-2012 13:00 Rua: Rodrigues N:100 NULL 6 2 8 15-05-2012 17:00 Rua: Raugil N:987 NULL 7 2 9 02-04-2012 14:00 Rua: Rodrigues N:100 NULL 6 3 10 06-02-2012 10:00 Rua: Rosario N: 300 NULL 5 3 11 15-11-2012 11:00 Rua: Machado N:162 NULL 1 3 12 09-11-2012 09:00 Rua: Cacimbão N:18 NULL 3 3 13 10-06-2012 12:00 Rua: Raugil N:987 NULL 7 3 14 05-05-2012 16:00 Rua: Rodrigues N:100 NULL 6 4 15 08-11-2012 09:00 Rua:Cacimb a N:762 NULL 7 4 //Criando a Tabela Produtos: create table Produtos( Produto_Id int not null auto_increment, Produto_Nome varchar (100) not null, Produto_Unid varchar (10) not null, Produto_Desc varchar (100) null, Produto_Preco varchar (10) not null, primary key (Produto_Id)
  • 11. ); Query OK, 0 rows affected (0.04 sec) describe produtos; Field Type Null Key Default Extra Produto_Id int(11) NO PRI NULL auto_increment Produto_Nome varchar(100) NO NULL Produto_Unid varchar(20) NO NULL Produto_Desc varchar(20) YES NULL Produto_Preco varchar(15) YES NULL 5 rows in set (0.00 sec) //Inserindo Dados na Tabela Produtos; insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Rapadura', ' 1kg ', 'Preta', 'R$ 3,00'); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Farinha', ' 1kg ', 'Mandioca', ' R$ 3,20'); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Arroz', ' 1kg ', 'Branco', ' R$ 3,49'); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Arroz', ' 1kg ', 'Parboilizado', ' R$ 2,89 '); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Feijão', ' 1kg ', 'Preto', ' R$ 3,99');
  • 12. insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Farinha', ' 1kg ', 'Trigo', ' R$ 2,79'); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Farinha', ' 1kg ', 'Mandioca', ' R$ 2,19'); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Macarrão', ' 1 pacote ', 'Italiano', 'R$ 3,00'); insert into produtos (Produto_Nome , Produto_Unid , Produto_Desc , Produto_Preco ) values ('Vitamilho', ' 500g ', 'milho', 'R$ 1,10'); //Resultado da Tabela Produtos: select * from Produtos; Produto_Id Produto_Nome Produto_Unid Produto_Desc Produto_Preco 1 Rapadura 1kg Preta R$ 3,00 2 Farinha 1kg Mandioca R$ 3,20 3 Arroz 1kg Branco R$ 3,49 4 Arroz 1kg Parboilizado R$ 2,89 5 Feijão 1kg Preto R$ 3,99 6 Farinha 1kg Trigo R$ 2,79 7 Farinha 1kg Mandioca R$ 2,19 8 Macarrão 1 pacote Italiano R$ 3,00 9 Vitamilho 500g Milho R$ 1,10 9 rows in set (0.00 sec) //Criando a Tabela RTC: create table RCP( RCP_Id int not null auto_Increment, Id_Produto int not null,
  • 13. Id_Compra int not null, primary key (RCP_Id), foreign key (Id_Produto) references Produtos (Produto_Id), foreign key (Id_Compra) references Compras (Compra_Id) ); Query OK, 0 rows affected (0.20 sec) describe RCP; Field Type Null Key Default Extra RCP_Id int(11) NO PRI NULL auto_increment Id_Produto int(11) NO MUL NULL Id_Compra int(11) NO MUL NULL //Inserindo Dados na Tabela RCP: insert into RCP (Id_Produto, Id_Compra ) values (1,1); insert into RCP (Id_Produto, Id_Compra ) values (2,1); insert into RCP (Id_Produto, Id_Compra ) values (3,1); insert into RCP (Id_Produto, Id_Compra ) values (4,1); insert into RCP (Id_Produto, Id_Compra )
  • 14. values (5,1); insert into RCP (Id_Produto, Id_Compra ) values (6,1); insert into RCP (Id_Produto, Id_Compra ) values (7,1); insert into RCP (Id_Produto, Id_Compra ) values (8,1); insert into RCP (Id_Produto, Id_Compra ) values (9,1); insert into RCP (Id_Produto, Id_Compra ) values (1,2); insert into RCP (Id_Produto, Id_Compra ) values (3,2); insert into RCP (Id_Produto, Id_Compra ) values (5,2);
  • 15. insert into RCP (Id_Produto, Id_Compra ) values (7,2); insert into RCP (Id_Produto, Id_Compra ) values (9,2); insert into RCP (Id_Produto, Id_Compra ) values (2,3); insert into RCP (Id_Produto, Id_Compra ) values (4,3); insert into RCP (Id_Produto, Id_Compra ) values (6,3); insert into RCP (Id_Produto, Id_Compra ) values (8,3); insert into RCP (Id_Produto, Id_Compra ) values (1,4); insert into RCP (Id_Produto, Id_Compra ) values
  • 16. (3,4); insert into RCP (Id_Produto, Id_Compra ) values (6,4); insert into RCP (Id_Produto, Id_Compra ) values (9,4); insert into RCP (Id_Produto, Id_Compra ) values (2,5); insert into RCP (Id_Produto, Id_Compra ) values (5,5); insert into RCP (Id_Produto, Id_Compra ) values (8,5); insert into RCP (Id_Produto, Id_Compra ) values (3,6); insert into RCP (Id_Produto, Id_Compra ) values (6,6); insert into RCP
  • 17. (Id_Produto, Id_Compra ) values (9,6); insert into RCP (Id_Produto, Id_Compra ) values (1,7); insert into RCP (Id_Produto, Id_Compra ) values (2,8); insert into RCP (Id_Produto, Id_Compra ) values (3,9); insert into RCP (Id_Produto, Id_Compra ) values (4,10); insert into RCP (Id_Produto, Id_Compra ) values (5,11); insert into RCP (Id_Produto, Id_Compra ) values (6,12); insert into RCP (Id_Produto, Id_Compra ) values
  • 18. (7,13); insert into RCP (Id_Produto, Id_Compra ) values (8,14); insert into RCP (Id_Produto, Id_Compra ) values (9,15); //Resultado da Tabela RCP: select * from rcp; RCP_Id Id_Produto Id_Compra 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 8 1 9 9 1 10 1 2 11 3 2 12 5 2 13 7 2 14 9 2 15 2 3 16 4 3 17 6 3 18 8 3 19 1 4 20 3 4 21 6 4 22 9 4 23 2 5
  • 19. 24 5 5 25 8 5 26 3 6 27 6 6 28 9 6 29 1 7 30 2 8 31 3 9 32 4 10 33 5 11 34 6 12 35 7 13 36 8 14 37 9 15 38 rows in set (0.00 sec) //Realizando a consulta: Select Produtos.Produto_Nome, Produtos.Produto_preco from Produtos Where Produto_preco < 15 and Produto_id >=1 and Produto_Id <=9; //Retornando todos os produtos cujo preço é menor que $15: Produto_Nome Produto_preco Rapadura R$ 3,00 Farinha R$ 3,20 Arroz R$ 3,49 Arroz R$ 2,89 Feijão R$ 3,99 Farinha R$ 2,79 Farinha R$ 2,19 Macarrão R$ 3,00 Vitamilho R$ 1,10 9 rows in set (0.12 sec) //Realizando consulta:
  • 20. Select Compras.Id_Cliente, Clientes.Cliente_Nome From Compras, Clientes Where Compras.Compra_Data=’12-12-2012’ and Clientes.Cliente_id=Compras.Id_Cliente; //Retornando os clientes que compraram na data 12-12-2012: Id_Cliente Cliente_Nome 1 Antonio Jose 2 Francisca Maria // Realizando consulta: select Clientes.Cliente_Nome, Compras.Id_Cliente, Produtos.Produto_Nome, RCP.Id_Produto, Compras.Compra_id from Clientes, Compras,Produtos, RCP where Clientes.Cliente_Nome=’Antonio Jose’ and Clientes.Cliente_Id =Compras.Id_Cliente and Compras.Compra_id=RCP.Id_Compra and Produtos.Produto_id=RCP.Id_Produto; //Retornando um histórico de produtos comprados pelo o cliente Antônio José: Cliente_Nome Id_Cliente Produto_Nome Id_Produto Compra_id Antonio Jose 1 Rapadura 1 1 Antonio Jose 1 Farinha 2 1 Antonio Jose 1 Arroz 3 1 Antonio Jose 1 Arroz 4 1 Antonio Jose 1 Feijão 5 1 Antonio Jose 1 Farinha 6 1 Antonio Jose 1 Farinha 7 1 Antonio Jose 1 Macarrão 8 1 Antonio Jose 1 Vitamilho 9 1 Antonio Jose 1 Arroz 4 10 // Realizando consulta: select Vendedores.Vendedor_Nome, Compras.Compra_id, Compras.Compra_Data from Vendedores, Compras where Vendedores.Vendedor_Nome like ’%Marcos%’ and
  • 21. Vendedores.Vendedor_Id = Compras.Id_Vendedor; //Retornando o histórico de vendas do vendedor Marcos (Apenas palavra chave): Vendedor_Nome Compra_id Compra_Data Jose Marcos 5 01-01-2012 Jose Marcos 6 20-03-2012 Jose Marcos 7 15-05-2012 // Realizando consulta: Select Clientes.Cliente_Nome, Vendedores.Vendedor_Id From Clientes, Vendedores ,Compras Where Vendedor_Id = 3 and Compras.Id_Cliente=Clientes.Cliente_Id and Compras.Id_Vendedor=Vendedores.Vendedor_Id; //Retornando todos os clientes atendidos pelo vendedor cujo "Id" é igual a 3: Cliente_Nome Vendedor_Id Jucineldo Gaucho 3 Geraldo Souza 3 Antonio Jose 3 Mariana Costa 3 Marta Farias 3 // Realizando consulta: Não sei  //Retornando o salário vendedor de "Id" = 2, levando em conta uma comissão de 7% sobre os produtos vendidos por ele somando a $700:  //Alterando o endereço do cliente "Id" = 6: update Clientes set Cliente_End = 'Rua: Belo Horizonte N: 23' Where Cliente_Id = 6; select * from Clientes; cliente_Nome cliente_Cpf cliente_End cliente_Tel cliente_Id Antonio Jose 125.432.34-9 Rua: Machado N:162 88 9823- 1628 1 Francisca Maria 735.432.34-9 Rua: Cacimba N:762 89 8363- 1738 2
  • 22. Mariana Costa 892.432.44-9 Rua: Cacimbão N:18 92 7363- 1828 3 Francinete Martins 826.139.625- 2 Rua: Meireles N:83 88 7853- 1828 4 Geraldo Souza 826.139.863- 1 Rua: RosarioN:300 83 9233- 1828 5 Jucineldo Gaucho 283.139.836- 2 Rua: Belo Horizonte N: 23 89 2733- 1828 6 Marta Farias 937.139.625- 4 Rua: Raugil N:987 87 9873- 1828 7 // Incluindo o campo "Cliente_Nasc" na tabela "Clientes": alter table Clientes add Cliente_Nasc varchar(8); cliente_Nome cliente_Cpf cliente_End cliente_Tel cliente_Id Cliente_Nasc Antonio Jose 125.432.34-9 Rua: Machado N:162 88 9823- 1628 1 NULL Francisca Maria 735.432.34-9 Rua: Cacimba N:762 89 8363- 1738 2 NULL Mariana Costa 892.432.44-9 Rua: Cacimbão N:18 92 7363- 1828 3 NULL Francinete Martins 826.139.625- 2 Rua: Meireles N:83 88 7853- 1828 4 NULL Geraldo Souza 826.139.863- 1 Rua: RosarioN:300 83 9233- 1828 5 NULL Jucineldo Gaucho 283.139.836- 2 Rua: Belo Horizonte N: 23 89 2733- 1828 6 NULL Marta Farias 937.139.625- 4 Rua: Raugil N:987 87 9873- 1828 7 NULL // Criar a tabela "Fornecedores" relacionando a "Produtos": Create Table Fornecedores ( Forne_Id int not null auto_increment, Forne_Data Varchar(10), Forne_Hora Varchar(10), Id_Produto int not null, Primary key (Forne_Id), foreign key (Id_Produto) references Produtos (Produto_Id));
  • 23. describe Fornecedores; Field Type Null Key Default Extra Forne_Id int(11) NO PRI NULL auto_increment Forne_Data varchar(10) YES NULL Forne_Hora varchar(10) YES NULL Id_Produto int(11) NO MUL NULL Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 8 Server version: 5.1.36-community MySQL Community Server (GPL) Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> use SM; Database changed mysql> update Clientes -> set Cliente_End='Rua: Matias N: 400' -> where -> Cliente_Id=6; Query OK, 1 row affected (0.36 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> altar table Clientes add Cliente_Nasc varchar(20) not null; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'altar table Clientes add Cliente_Nasc varchar(20) not null' at line 1 mysql> altar table Clientes add Cliente_Nasc varchar(20); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'altar table Clientes add Cliente_Nasc varchar(20)' at line 1 mysql> alter table Clientes add Cliente_Nasc varchar(20) not null;
  • 24. Query OK, 7 rows affected (0.27 sec) Records: 7 Duplicates: 0 Warnings: 0 mysql> describe Clientes -> ; +--------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+----------------+ | cliente_Id | int(11) | NO | PRI | NULL | auto_increment | | cliente_Nome | varchar(100) | NO | | NULL | | | cliente_Cpf | varchar(20) | NO | | NULL | | | cliente_End | varchar(50) | NO | | NULL | | | cliente_Tel | varchar(20) | NO | | NULL | | | Cliente_Nasc | varchar(20) | NO | | NULL | | +--------------+--------------+------+-----+---------+----------------+ 6 rows in set (0.02 sec) mysql> create view PrecoMenorquinze -> as select Produto_Nome, Produto_Preco -> from Produtos -> where -> Produto_Preco < '15'; Query OK, 0 rows affected (0.08 sec) mysql> select * from PrecoMenorQuinze; +--------------+---------------+ | Produto_Nome | Produto_Preco | +--------------+---------------+ | Farinha | R$ 3,20 | | Arroz | R$ 3,49 | | Arroz | R$ 2,89 | | Feijão | R$ 3,99 | | Farinha | R$ 2,79 | | Farinha | R$ 2,19 | +--------------+---------------+ 6 rows in set (0.00 sec) mysql> create view ComprasNaData12_12_2012
  • 25. -> As select Cliente_Nome, Compra_Data -> from Clientes, Compras -> where -> Compra_Data = '12-12-2012' and -> cliente_Id = Id_Cliente; Query OK, 0 rows affected (0.09 sec) mysql> Select * from ComprasNaData12_12_2012; +-----------------+-------------+ | Cliente_Nome | Compra_Data | +-----------------+-------------+ | Antonio Jose | 12-12-2012 | | Francisca Maria | 12-12-2012 | +-----------------+-------------+ 2 rows in set (0.00 sec) mysql> create view PCCAntonioJose -> As select Produto_Nome -> from Clientes, Compras,Produtos, RCP -> where Cliente_Nome='Antonio Jose' and -> Cliente_Id = Id_Cliente and -> Compra_id = Id_Compra and -> Produto_id = Id_Produto; Query OK, 0 rows affected (0.34 sec) mysql> select * from PCCAntonioJose; +--------------+ | Produto_Nome | +--------------+ | Rapadura | | Farinha | | Arroz | | Arroz | | Arroz | | Feijão | | Farinha | | Farinha | | Macarrão |
  • 26. | Vitamilho | +--------------+ 10 rows in set (0.00 sec) mysql> create view HistoricoDeVendaMarcos -> As select Compra_Id, Vendedor_Nome -> from Vendedores, Compras -> where -> Vendedor_Nome Like '%Marcos%'and -> Vendedor_Id = Id_Vendedor; Query OK, 0 rows affected (0.09 sec) mysql> select * from HistoricoDeVendaMarcos; +-----------+---------------+ | Compra_Id | Vendedor_Nome | +-----------+---------------+ | 5 | Jose Marcos | | 6 | Jose Marcos | | 7 | Jose Marcos | +-----------+---------------+ 3 rows in set (0.00 sec) mysql> create view CAVI3 -> As select Cliente_Nome, Vendedor_Id -> from Clientes, Compras, Vendedores -> where -> Vendedor_Id = 3 and -> Vendedor_id = Id_Vendedor and -> Cliente_Id = Id_Cliente; Query OK, 0 rows affected (0.05 sec) mysql> select * from CAVI3; +------------------+-------------+ | Cliente_Nome | Vendedor_Id | +------------------+-------------+ | Jucineldo Gaucho | 3 | | Geraldo Souza | 3 | | Antonio Jose | 3 |
  • 27. | Mariana Costa | 3 | | Marta Farias | 3 | +------------------+-------------+ 5 rows in set (0.02 sec) mysql> create view SalarioVI2 -> As Select Sum(Produto_Preco * 0.07 + 700) -> from Vendedores, Produtos, Compras, RCP -> where -> Vendedor_Id = 2 and -> Vendedor_Id = Id_Vendedor and -> Produto_Id = Id_Produto and -> Compra_Id = Id_Compra; Query OK, 0 rows affected (0.09 sec) mysql> select * from SalarioVI3; ERROR 1146 (42S02): Table 'sm.salariovi3' doesn't exist mysql> select * from SalarioVI2; +---------------------------------+ | Sum(Produto_Preco * 0.07 + 700) | +---------------------------------+ | 4900 | +---------------------------------+ 1 row in set (0.01 sec) mysql>