Para comenzar configuraremos el ambiente de trabajo.
- Oracle 10g Express Edition
- PL-SQL Developer v8.0
PASOS:
I- CREACIÓN TABLAS:
CREATE TABLE TB_DEMO_A( ID NUMBER, DESCRIPCION VARCHAR2( 10 ) );
CREATE TABLE TB_DEMO_B( ID NUMBER, DESCRIPCION VARCHAR2( 10 ) );
II- CREACIÓN INSERTS:
INSERT INTO TB_DEMO_A VALUES( 1, 'PRIMERO' );
INSERT INTO TB_DEMO_A VALUES( 2, 'SEGUNDO' );
INSERT INTO TB_DEMO_A VALUES( 3, 'TERCERO' );
INSERT INTO TB_DEMO_A VALUES( 4, 'CUARTO' );
INSERT INTO TB_DEMO_B VALUES( 1, 'PRIMERO' );
INSERT INTO TB_DEMO_B VALUES( 2, 'SEGUNDO' );
INSERT INTO TB_DEMO_B VALUES( 5, 'QUINTO' );
INSERT INTO TB_DEMO_B VALUES( 6, 'SEXTO' );
III- SELECTS:
SELECT a.* FROM TB_DEMO_A a;
SELECT b.* FROM TB_DEMO_B b;
IV.- AMARRES X CASO:
4.1.- "CONJUNTO A" INTERSECCION "CONJUNTO B":
4.1.1.- FORMA #1:
SELECT a.*, b.*
FROM TB_DEMO_A a,
TB_DEMO_B b
WHERE a.ID = b.ID;
4.1.1.- FORMA #2:
SELECT a.*, b.*
FROM TB_DEMO_A a
INNER JOIN TB_DEMO_B b
ON a.ID = b.ID;
4.2.- ("CONJUNTO A" INTERSECCION "CONJUNTO B") + ( "CONJUNTO A" MENOS CONJUNTO B" ):
4.2.1.- FORMA #1:
SELECT a.*, b.*
FROM TB_DEMO_A a,
TB_DEMO_B b
WHERE a.ID = b.ID(+);
4.2.2.- FORMA #2:
SELECT a.*, b.*
FROM TB_DEMO_A a
LEFT JOIN TB_DEMO_B b
ON a.ID = b.ID;
4.3.- ("CONJUNTO A" INTERSECCION "CONJUNTO B") + ( "CONJUNTO B" MENOS CONJUNTO A" ):
4.3.1.- FORMA #1:
SELECT a.*, b.*
FROM TB_DEMO_A a,
TB_DEMO_B b
WHERE a.ID(+) = b.ID;
4.3.2.- FORMA #2:
SELECT a.*, b.*
FROM TB_DEMO_A a
RIGHT JOIN TB_DEMO_B b
ON a.ID = b.ID;
4.4.- ( "CONJUNTO A" MENOS CONJUNTO B" ):
4.4.1.- FORMA #1:
SELECT a.*, b.*
FROM TB_DEMO_A a,
TB_DEMO_B b
WHERE a.ID = b.ID(+) AND
b.ID IS NULL;
4.4.2.- FORMA #2:
SELECT a.*, b.*
FROM TB_DEMO_A a
LEFT JOIN TB_DEMO_B b
ON a.ID = b.ID
WHERE b.ID IS NULL;
4.4.3.- FORMA #3:
SELECT a.*
FROM TB_DEMO_A a
WHERE a.ID
NOT IN ( SELECT b.ID
FROM TB_DEMO_B b
);
4.4.4.- FORMA #4:
SELECT a.*
FROM TB_DEMO_A a
WHERE NOT EXISTS( SELECT b.ID
FROM TB_DEMO_B b
WHERE a.ID = b.ID
);
4.5.- ( "CONJUNTO B" MENOS CONJUNTO A" ):
4.5.1.- FORMA #1:
SELECT a.*, b.*
FROM TB_DEMO_A a,
TB_DEMO_B b
WHERE a.ID(+) = b.ID AND
a.ID IS NULL;
4.5.2.- FORMA #2:
SELECT a.*, b.*
FROM TB_DEMO_A a
LEFT JOIN TB_DEMO_B b
ON a.ID = b.ID
WHERE a.ID IS NULL;
4.5.3.- FORMA #3:
SELECT b.*
FROM TB_DEMO_B b
WHERE b.ID
NOT IN ( SELECT a.ID
FROM TB_DEMO_A a
);
4.5.4.- FORMA #4:
SELECT b.*
FROM TB_DEMO_B b
WHERE NOT EXISTS( SELECT a.ID
FROM TB_DEMO_A a
WHERE b.ID = a.ID
);
4.6.- ( "CONJUNTO A" MÁS CONJUNTO B" ) Ó ( "CONJUNTO B" MÁS CONJUNTO A" ): (NO REPETIDOS)
4.6.1.- FORMA #1:
SELECT a.ID, a.DESCRIPCION
FROM TB_DEMO_A a
UNION
SELECT b.ID, b.DESCRIPCION
FROM TB_DEMO_B b;
4.7.- ( "CONJUNTO A" MÁS CONJUNTO B" ) Ó ( "CONJUNTO B" MÁS CONJUNTO A" ):(CON REPETIDOS)
4.7.1.- FORMA #1:
SELECT a.ID, a.DESCRIPCION
FROM TB_DEMO_A a
UNION ALL
SELECT b.ID, b.DESCRIPCION
FROM TB_DEMO_B b;
No hay comentarios:
Publicar un comentario