Você está na página 1de 7

-- exercício 1

create database dbJoaoArthurAlice;


use dbJoaoArthurAlice;

create table tbUsuario(


IdUsuario int primary key,
NomeUsuario varchar(45) ,
DataNascimento date );

create table tbEstado(


Id int primary key,
Uf char(2));

create table tbCliente(


CodigoCli smallint primary key,
Nome varchar(50) ,
Endereco varchar(60));

create table tbProduto(


Barras decimal(13,0) primary key,
Valor float (8,4),
Descricao varchar (150) );

use dbJoaoArthurAlice;
describe tbProduto;
show tables;
show databases;

use dbJoaoArthurAlice;
alter table tbCliente modify Nome varchar(58);
alter table tbProduto add Qtd int;
drop table tbEstado;
alter table tbUsuario drop DataNascimento;

/* Exercício 2 */
create database dbEvandroAndre;
use dbEvandroAndre;
create table tbProduto(
IdProp int primary key,
NomeProd varchar(50) not null,
Qtd int,
DataValidade date not null,
Preco decimal(8,2) not null);

create table tbCliente(


codigo int primary key,
Nomecli varchar(50) not null,
DataNascimento date);

/* Exercício 3 */
create database dbcomercio;
use dbcomercio;
create table tbCliente(
Id int primary key,
NomeCli varchar(200) not null,
NumEnd decimal(6,0) not null,
CompEnd varchar(50));

create table tbClientePF(


CPF decimal(11,0) primary key,
RG decimal (9,0) ,
Rgdig char(1),
Nascimento date not null);

/*Exercício 4*/
create database dbJaniceAntonia;

use dbJaniceAntonia;
create table tbproduto(
Idprop int primary key,
NomeProd varchar(50) not null,
Qtd int,
DataValidade date not null,
Preco decimal(8,2) not null);

use dbJaniceAntonia;
alter table tbproduto add Peso decimal(6,3);
alter table tbproduto add Cor varchar(50);
alter table tbproduto add Marca varchar(50) not null;

use dbJaniceAntonia;
alter table tbproduto drop Cor;
alter table tbproduto modify Peso decimal(6,3) not null;
alter table tbproduto drop DataValidade;

/*Exercício 5*/
create database dbisabelcaroll;
use dbisabelcaroll;

create table tbvenda(


NF int primary key auto_increment,
DataValidade date not null
);
alter table tbvenda add (Preco decimal(8,2) not null, Qtd tinyint);
alter table tbvenda drop DataValidade;
alter table tbvenda add DataVenda date default (current_date());

use dbisabelcaroll;
create table tbproduto(
CodigoB decimal(13,0) primary key,
NomeProd varchar(50) not null);

alter table tbvenda add (Codigo decimal(13,0), foreign key (Codigo) references
tbproduto(CodigoB));

/* exercicio 6 */
create database dbdesenvolvimento;
use dbdesenvolvimento;
create table tbproduto(
IdProp int primary key,
NomeProd varchar(50) not null,
Qtd int,
DataValidade date not null,
Preco decimal(8,2) not null);

use dbdesenvolvimento;
alter table tbproduto add (Peso decimal(8,3) , Cor varchar(50) , Marca varchar(50)
not null);
alter table tbproduto drop Cor;
alter table tbproduto modify Peso decimal(8,3) not null;
/*Não é possível apagar a coluna Cor da tabela tbproduto por:
a coluna já foi apagada*/

create database dbLojaGrande;

use dbdesenvolvimento;
alter table tbproduto add(Cor varchar(50));

create database dblojica;


use dblojica;
create table tbcliente(
NomeCli varchar(50) not null,
CodigoCli int primary key,
DataCadastro date not null);

use dbLojaGrande;
create table tbfuncionario(
NomeFunc varchar(50) not null,
CodigoFunc int primary key,
DataCadastro datetime not null);
drop database dbLojaGrande;

use dblojica;
alter table tbcliente add CPF decimal(11,0) not null unique;

/*Exercício 7*/
/*João Arthur
Alice Gonzalez*/
create database dbescola;
use dbescola;
create table tbcliente(
IdCli int primary key,
NomeCli varchar(50) not null,
NumEnd smallint ,
DataCadastro datetime default(current_timestamp())
);

use dbescola;
alter table tbcliente add CPF decimal(11,0) unique not null;
alter table tbcliente add Cep decimal(5,0);

create database dbempresa;

use dbescola;
create table tbendereco(
Cep decimal(5,0) primary key,
Logradouro varchar(250) not null,
IdUf tinyint );
alter table tbcliente add constraint Fk_Cep_TbCliente foreign key (Cep) references
tbendereco (Cep);
use dbescola;
describe tbcliente;
/* 3 Principais Dificuldades: tipos de dados para limitar a quantidade de digítos
númericos;
utilizar default junto do current_timestamp;
salvar a base de dados na nuvem para abrir em outro computador*/
show databases;
drop database dbEmpresa;

/* Exercício 8 */
use dbescola;
create table tbest(
IdUf tinyint primary key,
NomeUfs char(2) not null,
NomeEstado varchar(40) not null);
alter table tbendereco add constraint Fk_IdUF_TbEndereco foreign key (IdUf)
references tbest(IdUf);
alter table tbest drop NomeEstado;
rename table tbest to tbestado;
alter table tbestado rename column NomeUfs to NomeUf;
alter table tbestado add IdCid mediumint;

use dbescola;
create table tbcidade(
IdCid mediumint primary key,
NomeCidade varchar(50) not null);
alter table tbcidade modify NomeCidade varchar(250) not null;
alter table tbendereco add IdCid mediumint;
alter table tbendereco add constraint Fk_IdCid_TbEndereco foreign key (IdCid)
references tbcidade(IdCid);

-- Exercício 9

create database dbBanco;

use dbBanco;
create table tbCliente(
cpf bigint primary key,
nome varchar (50) not null,
sexo char(1) not null,
endereco varchar(50) not null
);

create table tbTelefone_Cliente(


cpf bigint, foreign key (cpf) references tbCliente(cpf),
telefone int primary key
);

create table tbBanco(


codigo int primary key,
nome varchar(50) not null
);

create table tbAgencia(


codBanco int, foreign key (codBanco) references tbBanco(codigo),
numeroagencia int primary key,
endereco varchar(50) not null
);

create table tbConta(


numeroconta int primary key,
saldo decimal(7,2),
tipoconta smallint,
numagencia int not null, foreign key (numagencia) references
tbAgencia(numeroagencia)
);

create table tbhistorico(


cpf bigint, foreign key (cpf) references tbCliente(cpf),
numeroconta int, foreign key (numeroconta) references tbConta (numeroconta),
primary key (cpf,numeroconta),
datainicio date
);

-- inserir valores na tabela tbBanco


insert into tbBanco (codigo, nome)
values (1, 'banco do brasil');

insert into tbBanco (codigo, nome)


values (104, 'Caixa Economica do Brasil');

insert into tbBanco (codigo, nome)


values (801, 'Banco Escola');

insert into tbBanco (codigo, nome)


values ( null, null);

-- inserir valores na tabela tbAgencia


insert into tbAgencia
value (1,123,'Av Paulista , 78');

insert into tbAgencia


value (104,159,'Rua liberdade, 124');

insert into tbAgencia


value (801,401,'Rua vinte três, 23');

insert into tbAgencia


value (801,485,'Rua Marechal, 68');

select *
from tbAgencia;

-- inserir valores na tabela tbCliente


insert into tbCliente
value (12345678910, 'Enildo', 'M', 'Rua Grande,75'),
(12345678911, 'Astrogildo', 'M', 'Rua Pequena,789'),
(12345678912, 'Monica', 'F', 'Av Larga,148'),
(12345678913, 'Cascão', 'M', 'Av Principal,389');

select Cpf,nome,sexo,endereco
from tbCliente;

-- inserir valores na tabela tbConta


insert into tbConta
value (9876,456.05,1,123),
(9877,321.00,1,123),
(9878,100.00,2,485),
(9879,5589.48,1,401);

select numeroConta,Saldo,TipoConta,NumAgencia
from tbConta;

-- inserir valores na tabela tbHistorico

insert into tbHistorico


value (12345678910,9876,20010415),
(12345678911,9877,20110310),
(12345678912,9878,20210311),
(12345678913,9879,20000705);

select Cpf, NumeroConta, Datainicio


from tbHistorico;

-- inserir valores na tabela tbTelefone_Cliente

insert into tbTelefone_Cliente


value (12345678910, 912345678),
(12345678911,912345679),
(12345678912,912345680),
(12345678913,912345681);

select cpf, telefone


from tbTelefone_Cliente;

alter table tbCliente add email varchar(50);

select cpf,endereco
from tbcliente
where nome='monica';

select numeroAgencia,endereco
from tbAgencia
where CodBanco=801;

select *
from tbCliente
where sexo = 'M';

-- Exercicios 10

delete from tbTelefone_Cliente


where cpf=12345678911;

update tbConta
set TipoConta = 2
where NumeroConta = 9879;

set sql_safe_updates = 0;
update tbCliente
set email = 'Astro@Escola.com'
where sexo = 'F';

/* nn sei fazer a 4
vo me explodi */
select nome, email, endereco
from tbCliente
where nome = 'monica';

update tbCliente
set nome = 'Enildo Candido', email = 'enildo@escola.com'
where nome = 'enildo';

-- tbm nn sei fazer o 7 HAHAHAHAHAHHAAHAHAHAHAHAHAHAHAH

/* impossivel fazer o exercicio 8


pois não podemos deletar a chave estrangeira sem deletarmos antes da outra tabela
*/

Você também pode gostar