comandos sql e funções

16
Comandos SQL e Funções Professor Esp. Diego André Sant’Ana E-mail: [email protected] Disciplina: Banco de Dados II professordiegosantana.wordpress.com

Upload: anjolie-bradford

Post on 02-Jan-2016

50 views

Category:

Documents


2 download

DESCRIPTION

Comandos SQL e Funções. Professor Esp. Diego André Sant’Ana E-mail: [email protected]. Disciplina: Banco de Dados II. professordiegosantana.wordpress.com. Comandos Básicos. SELECT – Seleciona os registros contidos na tabela INSERT – Insere um registro na tabela - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Comandos SQL e Funções

Comandos SQL e Funções

Professor Esp. Diego André Sant’AnaE-mail: [email protected]: Banco de Dados IIprofessordiegosantana.wordpress.com

Page 2: Comandos SQL e Funções

Comandos BásicosSELECT – Seleciona os registros contidos na tabelaINSERT – Insere um registro na tabelaUPDATE - Atualiza registro da tabelaDELETE – Serve para deletar registros da tabela

Page 3: Comandos SQL e Funções

Comando SELECTComando de consulta ao banco de dados, com ele

é possível extrair os dados desejados.SELECT(SELECIONE) *(TODOS) FROM(DA TABELA)

ESTADO WHERE(ONDE CONDIÇÃO) SIGLA=‘MS’

SELECT * FROM ESTADO

SELECT ID_ESTADO,SIGLA FROM ESTADO

SELECT <CAMPOS> FROM <TABELA>WHERE <CONDIÇÃO>

Page 4: Comandos SQL e Funções

Comando INSERT comando que insere um registro na tabela. INSERT(INSIRA) INTO(DENTRO DA TABELA) ESTADO

(id_estado,nome_estado,sigla) values(?,?,?);

INSERT INTO estado( id_estado, nome_estado, sigla) VALUES (1,’NOVO ACRE’,’NC’);

INSERT INTO <TABELA> (<CAMPOS>) values(<VALORES>);

INSERT INTO estado( nome_estado, sigla) VALUES ('NOVO ACRE','NC') returning id_estado;

Page 5: Comandos SQL e Funções

Comando UPDATEComando que faz alteração de registro em uma tabela. Observação cuidado com WHERE, procure sempre coloca-lo caso contrario atualizará a tabela inteira. UPDATE(ALTERAR TABELA) estado SET(JOGA VALOR NO CAMPO) nome_estado=?,

sigla=? WHERE <condition>;

UPDATE estado SET nome_estado=‘NOVO ACRE’, sigla=‘NC’ WHERE id_estado=28;

UPDATE estado SET nome_estado='NEW ACRE' WHERE ID_ESTADO=30

Page 6: Comandos SQL e Funções

Comando DELETEComando que faz deleção de registro em

uma tabela. Observação cuidado com WHERE, procure sempre coloca-lo caso contrario deletará a tabela inteira.

DELETE(DELETE) FROM(DA TABELA) estado WHERE(ONDE CONDIÇÃO) <condition>;

DELETE FROM estado WHERE ID_ESTADO=28;

Page 7: Comandos SQL e Funções

Operadores= Igual (Exemplo: id_estado=1)< Menor (Exemplo: id_estado>1)> Maior (Exemplo: id_estado<1)>= Maior Igual (Exemplo: id_estado>=1)<= Menor Igual (Exemplo: id_estado<=1)

SELECT 1=1; (TRUE)SELECT 1>10; (FALSE)SELECT 1<10; (TRUE)SELECT 10>=10; (TRUE)SELECT 10<=5; (FALSE)

Page 8: Comandos SQL e Funções

UPPER E LOWER

SELECT UPPER('mato grosso do sul')

SELECT LOWER('MATO GROSSO DO SUL')

Page 9: Comandos SQL e Funções

Operadores

<> DIFERENTE (Exemplo: sigla <> ‘SP’)

!= DIFERENTE (Exemplo: sigla != ‘SP’)

SELECT 5<>5SELECT 5!=5

Page 10: Comandos SQL e Funções

Operadores

BETWEEN (id_estado BETWEEN 1 AND 10)NOT BETWEEN (id_estado NOT BETWEEN 1

AND 10)

select * from material where valor BETWEEN 2.00 AND 3.00

select * from material where valor NOT BETWEEN 2.00 AND 3.00

Page 11: Comandos SQL e Funções

Operadores

IN esta em( id_estado in (1,2,4) )NOT INT não esta ( id_estado not in

(1,2,4))

SELECT * FROM ESTADO WHERE SIGLA IN ('MS','MT','SP')

SELECT * FROM ESTADO WHERE SIGLA NOT IN ('MS','MT','SP')

Page 12: Comandos SQL e Funções

Operadores LIKE Igual (Exemplo: sigla LIKE ‘SP’) ILIKE Igual maiúscula e minúscula (Exemplo: sigla ILIKE ‘sP’)

SELECT * FROM ESTADO WHERE NOME_ESTADO = 'MATO GROSSO'

SELECT * FROM ESTADO WHERE NOME_ESTADO LIKE 'MATO GROSSO'

SELECT * FROM ESTADO WHERE NOME_ESTADO LIKE 'MaTO GROSSO'

SELECT * FROM ESTADO WHERE UPPER(NOME_ESTADO) LIKE UPPER('MaTO GROSSO')

SELECT * FROM ESTADO WHERE NOME_ESTADO ILIKE 'MaTO GROSSO'

Page 13: Comandos SQL e Funções

Operadores Usando o ~; 'abc' ~ 'abc' true 'abc' ~ '^a' true 'abc' ~ '(b|d)' true 'abc' ~ '^(b|c)' false

select * from cidade where nome_cidade ~ 'Grande'

select * from cidade where nome_cidade ~ '^Ca'

select * from cidade where nome_cidade ~ '(Po|Gro)' select * from cidade where nome_cidade ~ '^(A|C)'

Page 14: Comandos SQL e Funções

FunçõesFunção Retorno Descrição Exemplo

to_char(timestamp, text) textconverte time stamp para string

to_char(current_timestamp, 'HH12:MI:SS')

to_char(interval, text) textconverte interval para string

to_char(interval '15h 2m 12s', 'HH24:MI:SS')

to_char(int, text) textconverte integer para string to_char(125, '999')

to_char(double precision, text) text

converte real/double precision para string

to_char(125.8::real, '999D9')

to_char(numeric, text) text converte numeric para string

to_char(-125.8, '999D99S')

to_date(text, text) date converte string para dateto_date('05 Dec 2000', 'DD Mon YYYY')

to_number(text, text) numericconverte string para numeric

to_number('12,454.8-', '99G999D9S')

to_timestamp(text, text) timestamp with time zoneconverte string para time stamp

to_timestamp('05 Dec 2000', 'DD Mon YYYY')

to_timestamp(double precision) timestamp with time zone

converte Unix epoch para time stamp

to_timestamp(1284352323)

Page 15: Comandos SQL e Funções

Funções

select to_char(data_nasc,'dd/mm/yyyy') as data_nascimento from cliente

select nome_cliente,count(com.id_cliente) from cliente cli inner join compra com on cli.id_cliente=com.id_clientegroup by nome_clienteorder by count(com.id_cliente) desc

select SIGLA,COUNT(CIDADE.ID_ESTADO) AS QTD from estado inner join cidade on estado.id_estado=cidade.id_estadoGROUP BY SIGLA

Page 16: Comandos SQL e Funções

REFERÊNCIAS

http://www.postgresql.org/docs/9.0/static/functions-matching.html

http://www.codigofonte.net/dicas/bancodedados/561_utilizando-o-postgres-dicas-de-comandos-sql-e-essenciais-para-a-manipulacao-de-dados