Sunteți pe pagina 1din 4

CONSULTA EM BANCO DE DADOS

use IMPACT
select *from produto

--ENCONTRAR MAIOR VALOR--


select top 1*
from produto
order by val_prod desc

--ENCONTRAR MENOR VALOR--


select top 1*
from produto
order by val_prod

--MAIOR VALOR ENTRE OS ELETRONICOS--


select top 1*
from produto
where tipo_prod = 'eletrônicos'
order by val_prod desc

--PRODUTOS COM MENORES QUANTIDADES--


select top 1 with ties /*ou você utiliza top 2*/*
from produto
order by qtd_prod

--CLASSIFICAR NA COLUNA 3 E NA COLUNA NOME_PROD--


select *
from produto
order by 3,2

--LISTAR O VALORES DE QUANTIDADE 250 - 350--


select*
from produto
where qtd_prod <=350 and qtd_prod >=250
order by 4

select*
from produto
where not (qtd_prod > 350 or qtd_prod <250)
order by 4

--VALORES QUE TERMINAM COM '50' (EXEMPLO: 250,350...)--


select*
from produto
where qtd_prod%100 = 50
order by 4
select*
from produto
where qtd_prod like '%50'
order by 4

--LISTAR nome_prod, tipo_prod, val_prod E O CAMPOR VIRTUAL


-- [VALORC/DESCONTO] COM DESCONTO DE 12% DO PRODUTO ELETRÔNICOS--
select
nome_prod,
tipo_prod,
val_prod,
[Valor c/ Desconto] = val_prod*0.88 /* O campo vitual ja vai com a operação de
desconto*/
from produto
where tipo_prod ='eletrônicos'
order by 4

--AUMENTO P/ TIPO ELETRONICOS E INMATICA--


select
nome_prod,
tipo_prod,
val_prod,
[Valor c/ Aumento] = val_prod+val_prod*0.07 /* ou apenas val_prod*1.07*/
from produto
where tipo_prod ='eletrônicos' or tipo_prod ='informática' /* nao pode ser 'and' pois nada é
eletronico e informatica ao mesmo tempo*/
order by 4

select
nome_prod,
tipo_prod,
val_prod,
[Valor c/ Aumento] = val_prod+val_prod*0.07
from produto
where tipo_prod in ('eletrônicos','informática') /* clausula 'in' faz o trabalho de listar como
seria um between ou and, mas ele lista apenas o que você quer*/
order by 2

--LISTAR nome_prod, qtd_prod, val_prod e o--


-- campo virtual [Total] = qtd_prod*val_prod dos produtos com [total] acima de 300000.--
select
nome_prod,
qtd_prod,
val_prod,
[Total] = qtd_prod*val_prod
from produto
where qtd_prod*val_prod >300000 /* não se usa o campo vitual no where pois o mesmo é
um campo imaginario que você crio*/

Atualizando e excluindo DADOS


--ALTERAR SOMENTE UMA COISA--
update produto
set tipo_prod = 'informatica'
where cod_prod = 4

--COM VALOR ABAIXO DE 650,00.--


begin tran

update produto
set val_prod = val_prod*1.12
where tipo_prod ='informática' and val_prod <650

select*from produto
commit tran
select*from produto

--ALTERAR A QUANTIDADE COM MAIS 53 COM TUDO QUE FOR MAIOR QUE 400--
begin tran

update produto
set qtd_prod = qtd_prod+53
where qtd_prod >= 400

select * from produto


commit tran

--COMANDO DELETE--
begin tran

delete from produto


where tipo_prod = 'informática'

select*from produto

--DELETAR TUDO QUE FOR MENOR QUE 500 EM QUANTIDADE--


begin tran

delete from produto


where qtd_prod <500

select*from produto
commit tran

CONSTRAINTS

create database TESTE_CONSTRAINT


use TESTE_CONSTRAINT

create table tipo_produto


(
cod_tipo int identity not null,
tipo varchar(30) not null,
constraint pk_tipo_produto primary key (cod_tipo),
constraint uq_tipo_produto_tipo unique (tipo)
)

-- testando a constraint UNIQUE--


insert tipo_produto values ('mouse')
insert tipo_produto values ('pen-drive')
insert tipo_produto values ('hard disk')
--vai dar erro porque VIOLA unique constraint--
insert tipo_produto values ('hard disk')

select*from tipo_produto

S-ar putea să vă placă și