Sei sulla pagina 1di 15

Comandos SQL - Postgres

Programacin en SQL
PL
DML
DDL
Tipos de Datos
Comandos DDL
Lenguaje Definicion de Datos
CREATE TABLE 'TABLA_NOMBRE'
( 'CAMPO_1' INT, 'CAMPO_2' STRING )
ALTER TABLE 'TABLA_NOMBRE' ( ADD NUEVO_CAMPO
INT UNSIGNED meel )
TRUNCATE TABLE ''TABLA_NOMBRE1''
DROP Table Nombre Tabla'
DML
Lenguaje de Manipulacin de Datos
insert into cliente values('1002702197','Edgar','Imbabura')
update cliente set ci='1002702198',nombre='Edgar2',
provincia='Pichincha' where ci='1002702197
delete from cliente where ci='1002702198'
Programacin en SQL
PL/pgSQL
Lenguaje de procedimientos PL/pgSQL
Los objetivos de PL/pgSQL:
Poder ser usado para crear funciones y disparadores (triggers)
Aadir estructuras de control al lenguaje SQL
Poder realizar clculos complejos
Heredar todos los tipos, funciones y operadores definidos por el
usuario
Poder ser definido como un lenguaje "de confianza"
Fcil de usar
Comentarios
Un Comentario es una secuencia arbitraria
de caracteres precedido por un doble guin
hasta final de lnea y no esta incluida en la
ejecucin de los comandos SQL.
-- This is a standard SQL comment
/* multi
line
comment
*/
Tipos de Datos
Operadores Matematicos
Operator Description Example Result
+
addition
2 + 3 5
-
subtraction
2 - 3 -1
*
multiplication
2 * 3 6
/
division (integer
division truncates
the result)
4 / 2 2
%
modulo (remainder)
5 % 4 1
% Truncado % 4.5 4
^
exponentiation
2.0 ^ 3.0 8
|/
square root
|/ 25.0 5
||/
cube root
||/ 27.0 3
!
factorial
5 ! 120
!!
factorial (prefix
operator)
!! 5 120
@
absolute value
@ -5.0 5
Operadores Relacionales y Logicos
Operator Description
< less than
> greater than
<= less than or equal to
>= greater than or equal to
= equal
<> or != not equal
a b a AND b a OR b
TRUE TRUE TRUE TRUE
TRUE FALSE FALSE TRUE
TRUE NULL NULL TRUE
FALSE FALSE FALSE FALSE
FALSE NULL FALSE NULL
NULL NULL NULL NULL
Estructuras de Control
Estructuras de Condicin:
Estructura IF
Estructura por Casos
IF boolean-expression THEN
statements ;
ELSE
statements ;
END IF;
IF boolean-expression THEN
statements
END IF;
CASE search-expression
WHEN expression THEN
statements ;
WHEN expression THEN
statements ;
ELSE
statements ;
END CASE;
CASE x
WHEN 1, 2 THEN
msg := 'one or two';
ELSE
msg := 'other value than one or two';
END CASE;
Estructuras de Control
Estructuras de Repeticin
LOOP -- some computations
IF count > 0 THEN EXIT; -- exit loop
END IF;
END LOOP;
WHILE boolean-expression LOOP
statements;
END LOOP;
FOR i IN 1..10 LOOP
-- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
END LOOP;
FOR i IN REVERSE 10..1 LOOP
-- i will take on the values 10,9,8,7,6,5,4,3,2,1 within the loop
END LOOP;
FOR i IN REVERSE 10..1 BY 2 LOOP -- i will take on the values 10,8,6,4,2 within the
loop
END LOOP;
Manejo de Cadenas
LENGTH Tamao del Texto
Length(Hola)
Extraer parte especifica de una
cadena
substr('This is a test', 6, 2)
Numero a Texto
to_char(125, '999')
Texto a Numero
to_number('-1002702197.00', 'S9999999999D99')
Estructura Creacin de
una Funcin
CREATE [ OR REPLACE ] FUNCTION
nombre_funcion([ [ argmodo ] [ argnombre ]
argtipo [, ...] ]) RETURNS tipo AS
$$
[ DECLARE ] [ declaraciones de variables ]
BEGIN codigo
END;
$$
LANGUAGE plpgsql
Ejemplo 1:
CREATE OR REPLACE FUNCTION sumar(x INTEGER ,y INTEGER)
RETURNS INTEGER AS
$BODY$
DECLARE
suma INTEGER;
BEGIN
suma:=x+y;
return suma;
END
$BODY$
LANGUAGE plpgsql;
Ejemplo 2:
DROP FUNCTION sumar(x INTEGER ,y INTEGER)
CREATE OR REPLACE FUNCTION sumar(x INTEGER ,y INTEGER)
RETURNS INTEGER AS
$$
DECLARE
suma INTEGER:=0;
BEGIN
for i in x..y loop
suma:=suma+i;
end loop;
return suma;
END ;
$$
LANGUAGE plpgsql;
SELECT sumar(1,1000)

Potrebbero piacerti anche