bdtema05-sql.

Anuncio
0$1,38/$&,Ï1'('$726
/(1*8$-(65(/$&,21$/(6 64/
6WUXFWXUHG4XHU\/DQJXDJHOHQJXDMHHVWUXFWXUDGRGHFRQVXOWD
‡ 3ULPHUOHQJXDMHGH%'GHDOWRQLYHO$xRV
± 'LVHxDGRHLPSOHPHQWDGRHQ IBM Research6DQ-RVp&DOLIRUQLD
SDUDHO6*%'5HODFLRQDOH[SHULPHQWDO SYSTEM R
‡ 'HILQLFLyQGHXQOHQJXDMHHVWiQGDU
$16,American National Standars Institute
,62Organization for International Standardization
² 64/$16,H[WHQGLGRHQ 64/
± 64/64/\
² 64/H[WHQVLRQHVGH2ULHQWDFLyQD2EMHWRV\RWURVFRQFHSWRVUHFLHQWHV
GHQRPLQDGR 64/
0RGHOR5HODFLRQDOGHGDWRV64/
64/
‡ /HQJXDMHGH%DVHVGH'DWRV&203/(721RVyOR³GHFRQVXOWD´
± 'HILQLFLyQ\0DQLSXODFLyQGH'DWRV ''/'0/
± 'HILQLFLyQGH9LVWDV9'/
± &UHDFLyQ\'HVWUXFFLyQGH ÌQGLFHVDXQTXHHQ64/³\DQRH[LVWHQ´
± ,QFRUSRUDFLyQGH64/GHQWURGHFyGLJRHVFULWRFRQXQ/HQJXDMHGH3URJUDPDFLyQGH
SURSyVLWRJHQHUDOPascal, CHWF
‡ /RVSURYHHGRUHVGH6*%'5FRPHUFLDOHVLPSOHPHQWDQYDULDFLRQHVGH64/
± DOJXQRVLQFOX\HQFDUDFWHUtVWLFDVTXHQRHVWiQHVWDQGDUL]DGDV WULJJHUVUHJODVDFWLYDV
‡ 1LYHOHVGHFRPSDWLELOLGDGFRQHOHVWiQGDUGH64/
– Entry SQL
– Intermediate SQL
– Full SQL
0RGHOR5HODFLRQDOGHGDWRV64/
(VWUXFWXUDGHOVHPLQDULRVREUH64/
‡ &RQVXOWDVR6HOHFFLyQGHGDWRV
‡ 0RGLILFDFLyQGHGDWRV
‡ 9LVWDV
‡ 'HILQLFLyQ\$OWHUDFLyQGHGDWRV
² (VTXHPDV'RPLQLRV5HODFLRQHV
‡ 5HVWULFFLRQHVGH,QWHJULGDG*HQHUDOHV$VHUWRV
‡ 6HJXULGDG\&RQWUROGH$FFHVR
$QH[RUHVXPHQGHODVLQWD[LVRHVWUXFWXUDGH64/
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6(164/
‡&OiXVXOD 6(/(&7:
,QVWUXFFLyQEiVLFDGH
REWHQFLyQGHLQIRUPDFLyQ
GRQGH
<lista atributos>
<lista tablas>
<condición>
SELECT <lista atributos>
FROM <lista tablas>
WHERE <condición>
DWULEXWRVFX\RVYDORUHVYDDREWHQHUODFRQVXOWD
UHODFLRQHVQHFHVDULDVSDUDUHDOL]DUODFRQVXOWD
H[SUHVLyQERROHDQDSDUDLGHQWLILFDUWXSODVTXHREWHQGUiODFRQVXOWD
H[SUHVLyQGHUHXQLyQ\RGHVHOHFFLyQ
)HFKDGHQDFLPLHQWR\GLUHFFLyQGHOHPSOHDGROODPDGR-RVp%6LOYD
SELECT fechan, dirección
FROM EMPLEADO
WHERE nombrep = ‘José’ AND inic = ‘B’ AND apellido = ‘Silva’;
‡ /DFRQVXOWDVHOHFFLRQDWXSODVGH<lista tablas>
TXHVDWLVIDFHQ<condición>\
SUR\HFWDHOUHVXOWDGRVREUHORVDWULEXWRVGH<lista atributos>
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6(164/
/DFOiXVXOD SELECTFROM ... WHERE...
‡ 1RHVLJXDODODRSHUDFLyQ5HVWULFFLyQ GHOÈOJHEUD5HODFLRQDO
σ
SELECTGH64/WLHQHPXFKDVPiVRSFLRQHV\PDWLFHV
‡ 3DUDHOFDVRGHXQD~QLFDWDEOD7HQlista tablas!HVHTXLYDOHQWHD
ΠOLVWDDWULEXWRV!σFRQGLFLyQ!7!
‡ 64/YV0RGHOR5HODFLRQDO)RUPDO05)
‡ 64/SHUPLWHTXHODVUHODFLRQHVWDEODVWHQJDQ RPiVWXSODV
LGpQWLFDVHQWRGRVORVYDORUHVGHVXVDWULEXWRVHO 05)QR
⇒7DEOD64/ ≠FRQMXQWRGHWXSODVHQJHQHUDO
7DEOD64/ 0XOWLFRQMXQWREDJEROVDVDFRGHWXSODV
‡ (VSRVLEOHIRU]DUTXHODVWDEODV64/VHDQFRQMXQWRVGHWXSODV
PHGLDQWHRSFLyQDISTINCTHQXQDSELECTR
FRQUHVWULFFLRQHVGHFODYH
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6(164/
1RPEUHDSHOOLGR\GLUHFFLyQGHWRGRVORVHPSOHDGRVGHOGHSWRGH,QYHVWLJDFLyQ
SELECT nombrep, apellido, dirección
FROM EMPLEADO, DEPARTAMENTO
→FRQGLFLyQGHVHOHFFLyQ
WHERE nombred=‘Investigación’
→FRQGLFLyQGHUHXQLyQHQWUHODVWDEODV
AND númerod=nd;
‡ &XDOTXLHUQ~PHURGHFRQGLFLRQHVVHOHFFLyQUHXQLyQHQSELECT
3DUDFDGDSUR\HFWRXELFDGRHQ6DQWLDJRREWHQHUHOQžGHOSUR\HFWRQžGHOGHSWRTXH
ORFRQWUROD\HODSHOOLGRGLUHFFLyQ\IHFKDGHQDFLPLHQWRGHOJHUHQWHGHHVHGHSWR
SELECT númerop, númd, apellido, dirección, fechan
FROM PROYECTO, DEPARTAMENTO, EMPLEADO
WHERE númd=númerod AND nssgte=nss AND lugarp=‘Santiago’;
‡ 8QDSELECTSXHGHREWHQHUWXSODVUHSHWLGDV
6DODULRGHORVHPSOHDGRVGHORVGHSDUWDPHQWRVGH$GPLQLVWUDFLyQH,QYHVWLJDFLyQ
SELECT salario
FROM EMPLEADO, DEPARTAMENTO
WHERE (nombred=‘Administración’ OR nombred=‘Investigación’)
AND númerod=nd;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$620,6,Ï1'(WHERE, y
‡ 2PLVLyQGHWHERE⇒6(/(&&,Ð1,1&21',&,21$/≡WHERE TRUE
⇒VHOHFFLyQGHWRGDVODVWXSODV
± GHODUHODFLyQ~QLFDGHODFOiXVXODFROMR
± GHOSURGXFWRFDUWHVLDQRRFUX]DGRVL FROMFRQWLHQHYDULDVWDEODV
6HOHFFLRQDUWRGRVORVQVVGHHPSOHDGRV
SELECT nss FROM EMPLEADO;
6HOHFFLRQDUWRGDVODVFRPELQDFLRQHVGHQVVGHHPSOHDGRV\QRPEUHVGHGHSWRV
SELECT nss, nombred
FROM EMPLEADO, DEPARTAMENTO;
‡ 2EWHQFLyQGHORVYDORUHVGHWRGRVORVDWULEXWRVGHODVWXSODVVHOHFFLRQDGDV
± 8VRGHOVtPEROR72'26/26$75,%8726
± 1RHVQHFHVDULROLVWDUWRGRVORVQRPEUHVWUDVFOiXVXODSELECT
SELECT * FROM EMPLEADO WHERE nd=5;
SELECT * FROM EMPLEADO,DEPARTAMENTO
WHERE nombred=‘Investigación’ AND nd=númerod;
SELECT * FROM EMPLEADO, DEPARTAMENTO;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6&$'(1$6'(&$5$&7(5(6
‡ &203$5$&,Ð1'(68%&$'(1$6'(&$5$&7(5(6
2SHUDGRULIKE
± &DUDFWHUHVUHVHUYDGRV ‘%’\‘_’FRPRGLQHV
1RPEUHV\DSHOOLGRVGHORVHPSOHDGRVFX\DGLUHFFLyQHVWpHQ+LJXHUDVHVWDGRGH0p[LFR
SELECT apellido, nombrep FROM EMPLEADO
WHERE dirección LIKE ‘%Higueras, MX%’ ;
‡ &21&$7(1$&,Ð1'(&$'(1$6'(&$5$&7(5(6
2SHUDGRU||
1RPEUHVFRPSOHWRVGHORVHPSOHDGRVFX\DGLUHFFLyQHVWpHQ+LJXHUDVHVWDGRGH0p[LFR
SELECT nombrep || inic || apellido FROM EMPLEADO
WHERE dirección LIKE ‘%Higueras, MX%’ ;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6$5,70e7,&$\7,(032
‡ 23(5$&,21(6$5,70e7,&$6
± $SOLFDFLyQGHRSHUDGRUHVDULWPpWLFRV + - * /VREUHYDORUHVQXPpULFRV
6DODULRVGHORVHPSOHDGRVTXHWUDEDMDQHQHOSUR\HFWRProductoX,WUDVXQDXPHQWRGHO
SELECT apellido, nombrep, 1.1*salario
FROM EMPLEADO, TRABAJA_EN, PROYECTO
WHERE nss=nsse AND númp=númerop AND nombrepr=‘ProductoX’ ;
RMRHOYDORUrealGHORVVDODULRVHQODWDEOD(03/($'2QRFDPELD
‡ 23(5$&,21(6&21)(&+$6+25$60$5&$6'(7,(032(,17(59$/26
‡ ,QFUHPHQWR+\'HFUHPHQWR-GHDWULEXWRVWLSRDATE, TIME, TIMESTAMP
HQXQintervaloFRPSDWLEOHFRQHOWLSR
‡ (VSHFLILFDFLyQGHO YDORUGHXQINTERVAL
FRPRGLIHUHQFLDGHGRVYDORUHVDATETIME o TIMESTAMP
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6&$/,),&$&,Ï1
)(Q64/ORVQRPEUHVGHDWULEXWRVGHEHQVHU~QLFRVGHQWURGHFDGDUHODFLyQ
‡ &RQVXOWDTXHUHIHUHQFLDDYDULRVDWULEXWRVGHLJXDOQRPEUH
SHURGHUHODFLRQHVGLVWLQWDV
⇒$0%,*h('$'6ROXFLyQ&$/,),&$&,Ð1
(MHPSORGLVWLQWRDOGHOHVTXHPD&203$fÌ$
&yGLJRQRPEUH\GLUHFFLRQGHORVHPSOHDGRVGHOGHSDUWDPHQWRGH0DUNHWLQJ
EMPLEADO(codEmp, nombre, codDepto, dirección, ...)
DEPARTAMENTO(codDepto, nombre, ...)
SELECT codEmp, EMPLEADO.nombre, dirección
FROM EMPLEADO, DEPARTAMENTO
WHERE DEPARTAMENTO.nombre =‘Marketing’
AND DEPARTAMENTO.codDepto = EMPLEADO.codDepto;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$66(8'Ï1,026
‡ 3XHGHXWLOL]DUVHORVVHXGyQLPRVSDUDDFRUWDUQRPEUHVGHUHODFLyQ
GHQWURGHODVFRQVXOWDVFRQFDOLILFDFLyQ
EMPLEADO(codEmp, nombre, codDepto, dirección, ...)
DEPARTAMENTO(codDepto, nombre, ...)
SELECT codEmp, E.nombre, dirección
FROM EMPLEADO E, DEPARTAMENTO D
WHERE D.nombre =‘Investigación’
AND D.codDepto = E.codDepto;
‡ &RQVXOWDTXHVHUHILHUH '26YHFHVDODPLVPDUHODFLyQUHFXUVLYLGDGGHQLYHO
⇒$0%,*h('$'6ROXFLyQ6(8'Ð1,026
2EWHQHUQRPEUH\DSHOOLGRGHFDGDHPSOHDGR\GHVXVXSHUYLVRUMHIHLQPHGLDWR
SELECT E.nombrep, E.apellido, S.nombrep, S.apellido
FROM EMPLEADO E, EMPLEADO AS S
WHERE E.nsssuper=S.nss;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6&$0%,2'(120%5('($75,%8726
‡ (QHOUHVXOWDGRGHHYDOXDUODFRQVXOWD
1RPEUHVGHFDGDHPSOHDGR\VXVXSHUYLVRUFDPELDQGRDOPLVPRWLHPSRORVQRPEUHV
GHORVDWULEXWRVUHVXOWDQWHVDnombre_empleado\nombre_supervisor
SELECT E.nombrep AS nombre_empleado,
S.nombrep AS nombre_supervisor
FROM EMPLEADO AS E, EMPLEADO AS S
WHERE E.nsssuper = S.nss;
⇒1XHYDFDEHFHUDSDUDODUHODFLyQUHVXOWDGR
‡ 6HXGyQLPRVGHDWULEXWRV\RUHODFLyQHQFOiXVXODFROM
EMPLEADO(codEmp, nombre, codDepto, dirección, ...)
DEPARTAMENTO(codDepto, nombre, ...)
SELECT cod, nom, dir
FROM EMPLEADO E( cod, nom, dep , dir,...), DEPARTAMENTO
WHERE nombre =‘Investigación’ AND dep = codDepto;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6%È6,&$6
25'(1'(35(6(17$&,Ï1'(/$6783/$6
‡ 64/SHUPLWHSUHVHQWDUODVWXSODVUHVXOWDGRGHXQDFRQVXOWDGHIRUPDRUGHQDGD
± 2UGHQDFLyQ VHJ~QYDORUHVGHXQRRYDULRV DWULEXWRV
² $VFHQGHQWHASCSRUGHIHFWRR 'HVFHQGHQWHDESC
± 6XHOHVHUXQDRSHUDFLyQPX\ FRVWRVD
)ODVWXSODV12VHRUGHQDQHQGLVFR VHYHQRUGHQDGDVSHURQRORHVWiQ
1RPEUH\DSHOOLGRGHORVHPSOHDGRV\SUR\HFWRVHQORVTXHWUDEDMDQHQRUGHQGHVFHQGHQWHSRU
GHSDUWDPHQWRV\GHQWURGHFGHSWRHQRUGHQDOIDEpWLFR\DVFHQGHQWHSRUDSHOOLGR\QRPEUH
SELECT nombred, apellido, nombrep, nombrepr
FROM DEPARTAMENTO, EMPLEADO, TRABAJA_EN, PROYECTO
WHERE númerod=nd AND nss=nsse AND númp=númerop
ORDER BY nombred DESC, apellido ASC, nombrep ASC;
0RGHOR5HODFLRQDOGHGDWRV64/
7$%/$6&202&21-81726(164/
‡ 64/QRHOLPLQDWXSODVUHSHWLGDV GHOUHVXOWDGRGHXQDFRQVXOWDSRUTXH
± /DHOLPLQDFLyQGHGXSOLFDGRVHV FRVWRVDRUGHQDUUHFRUUHUHOLPLQDU
± (OXVXDULRSXHGHGHVHDUYHUODVWXSODVUHSHWLGDVHQHOUHVXOWDGRGHXQDFRQVXOWD
± 6LVHDSOLFDXQDIXQFL yQDJUHJDGDDWXSODVUDUDYH]GHEHQHOLPLQDUVHGXSOLFDGRV
• DISTINCT3DUDHOLPLQDUGXSOLFDGRVGHOUHVXOWDGRGHXQDFRQVXOWD64/
ª (OUHVXOWDGR 5HODFLyQGHO0RGHOR5HODFLRQDO)RUPDOFRQMXQWRGHWXSODV
6DODULRGHWRGRVORVHPSOHDGRV
SELECT salario
FROM EMPLEADO;
6DODULRVGLVWLQWRVGHORVHPSOHDGRVVLQLPSRUWDUFXiQWRVSHUFLEDQFDGDFDQWLGDG
SELECT DISTINCT salario
FROM EMPLEADO;
0RGHOR5HODFLRQDOGHGDWRV64/
7$%/$6&202&21-81726
23(5$&,21(6'(&21-81726
– UNION( ∪ ), INTERSECT( ∩ ), EXCEPT ( — )
± 5HVXOWDGRFRQMXQWRVGHWXSODVODVWXSODVUHSHWLGDVVHHOLPLQDQ
± 3DUD12HOLPLQDUGXSOLFDGRVUNION ALL, INTERSECT ALL, EXCEPT ALL
± /DVUHODFLRQHVKDQGHVHUFRPSDWLEOHV
LJXDOQžGHDWULEXWRVHQHOPLVPRRUGHQ\FRQHOPLVPRGRPLQLR
1~PHURVGHORVSUR\HFWRVHQORVTXHSDUWLFLSDHOHPSOHDGRGHDSHOOLGR6LOYD\DVHD
FRPRWUDEDMDGRURELHQFRPRJHUHQWHGHOGHSDUWDPHQWRTXHFRQWURODHOSUR\HFWR
( SELECT númerop
FROM PROYECTO, DEPARTAMENTO, EMPLEADO
WHERE númd=númerod AND nssgte=nss AND apellido=‘Silva’ )
UNION
( SELECT númerop
FROM PROYECTO, TRABAJA_EN, EMPLEADO
WHERE númerop=númp AND nsse=nss AND apellido=‘Silva’ );
0RGHOR5HODFLRQDOGHGDWRV64/
7$%/$6&202&21-81726&21-81726(;3/Ë&,726
&21-8172(;3/Ì&,72'(9$/25(6HQFOiXVXODWHERE
‡ /LVWDGHYDORUHVHQFHUUDGDHQWUHSDUpQWHVLV
QVVGHORVHPSOHDGRVTXHWUDEDMDQHQORVSUR\HFWRVy
SELECT DISTINCT nsse FROM TRABAJA_EN
WHERE númp IN (1, 2, 3);
‡ 2SHUDGRU,1
v IN VLQGLFDVLHOYDORUSHUWHQHFHDOFRQMXQWRGHYDORUHVV
± 'HYXHOYHTRUEVLDOJ~QHOHPHQWRe GHVFXPSOHTXHv = e
QVVGHORVHPSOHDGRVTXHWUDEDMDQHQDOJ~QSUR\HFWRTXHQRVHDHOQLHO
SELECT DISTINCT nsse FROM TRABAJA_EN
WHERE númp NOT IN (4, 6);
0RGHOR5HODFLRQDOGHGDWRV64/
7$%/$6&202&21-81726&21-81726(;3/Ë&,726
‡ 2SHUDGRU$1<R620(
v <op> ANY V o v <op> SOME V GRQGH<op> ∈ { >, ≥, <, ≤, <>, = }
± &RPSDUDXQYDORULQGLYLGXDOvFRQXQFRQMXQWRV
± 'HYXHOYHTRUEVLDOJ~QHOHPHQWRe GHVFXPSOHTXHv <op> e
QVVGHORVHPSOHDGRVTXHWUDEDMDQHQDOJXQRGHORVSUR\HFWRVy
SELECT DISTINCT nsse FROM TRABAJA_EN
WHERE númp = ANY (1, 2, 3);
‡ 2SHUDGRU$//
v <op> ALL VGRQGH<op> ∈ { >, ≥, <, ≤, <>, = }
± &RPSDUDXQYDORUv FRQFRQMXQWRV
± 'HYXHOYHTRUEVLSDUDWRGRHOHPHQWRe GHVVHFXPSOHTXHv <op> e
QVVGHORVHPSOHDGRVTXHQRWUDEDMDQHQQLQJXQRGHORVWUHVSUR\HFWRV\
SELECT DISTINCT nsse FROM TRABAJA_EN
WHERE númp <> ALL (1, 2, 3);
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6$1,'$'$6(164/
&2168/7$$1,'$'$
‡ YDORUHVREWHQLGRVGHOD%'TXHVHXVDQHQODFRQGLFLyQGHRWUDFRQVXOWDSDUD
REWHQHURWURVGDWRV
‡ (VXQDFRQVXOWDSELECTFRPSOHWDGHQWURGHFOiXVXODWHEREGHRWUD
FRQVXOWDFRQVXOWDH[WHULRU
‡ (VSRVLEOHWHQHUYDULRVQLYHOHVGHFRQVXOWDVDQLGDGDV
1~PHURVGHORVSUR\HFWRVHQORVTXHSDUWLFLSDHOHPSOHDGRGHDSHOOLGR6LOYDFRPRWUDEDMDGRUR
FRPRJHUHQWHGHOGHSDUWDPHQWRTXHFRQWURODHOSUR\HFWR
SELECT DISTINCT númerop FROM PROYECTO
WHERE númerop IN ( SELECT númerop
FROM PROYECTO,DEPARTAMENTO,EMPLEADO
WHERE númd=númerod
AND nssgte=nss AND apellido=‘Silva’ )
OR
númerop IN (SELECT númp
FROM TRABAJA_EN, EMPLEADO
WHERE nsse=nss AND apellido=‘Silva’ );
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6$1,'$'$6&203$5$&,Ï1'(&21-81726
‡ 2SHUDGRU,1RWURXVRGHOPLVPRRSHUDGRU
t IN SLQGLFDVLODWXSODWSHUWHQHFHDOFRQMXQWRGHWXSODVS VXEFRQVXOWD
1RPEUH\GLUHFFLyQGHORVHPSOHDGRVTXHWUDEDMDQHQDOJ~QSUR\HFWR
SELECT nombrep, dirección FROM EMPLEADO
WHERE nss IN ( SELECT nsse FROM TRABAJA_EN );
1žVGHVHJXULGDGVRFLDOGHDTXHOORVHPSOHDGRVTXHWUDEDMDQHQDOJ~QSUR\HFWRHQHOTXHWUDEDMH
HOHPSOHDGR-RVp%6LOYDGHIRUPDWDOTXHDPERVWHQJDQODPLVPDFRPELQDFLyQSUR\HFWR
KRUDVHVGHFLUWRGRHPSOHDGRTXHWUDEDMHODVPLVPDVKRUDVTXH-RVp%6LOYDHQFDGD
SUR\HFWRHQHOTXHWUDEDMHQDPERV(OQVVGH-RVp%6LOYDHV¶·
SELECT DISTINCT nsse FROM TRABAJA_EN
WHERE (númp, horas) IN ( SELECT númp, horas FROM TRABAJA_EN
WHERE nsse=‘123456789’);
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6$1,'$'$6&203$5$&,Ï1'(&21-81726
‡ 2SHUDGRU$1<R620(RWURXVRGHOPLVPRRSHUDGRU
t <op> ANY S o t <op> SOME SGRQGH<op> ∈ { >, ≥, <, ≤, ≠, = }
± &RPSDUDXQDWXSODtFRQODVWXSODVUHVXOWDGRGHXQDFRQVXOWDDQLGDGD
± 'HYXHOYHTRUEVLDOJXQDWXSODe GHSFXPSOHTXHt <op> e
‡ 2SHUDGRU$//RWURXVRGHOPLVPRRSHUDGRU
t <op> ALL SGRQGH<op> ∈ { >, ≥, <, ≤, ≠, = }
± &RPSDUDXQDWXSODtFRQODVWXSODVUHVXOWDGRGHXQDFRQVXOWDDQLGDGD
± 'HYXHOYHTRUEVLSDUDWRGDWXSODe GHSVHFXPSOHTXHt <op> e
1RPEUHV\DSHOOLGRVGHORVHPSOHDGRVFX\RVDODULRHVPHQRUTXHHOGH72'26ORVHPSOHDGRVGHO
GHSDUWDPHQWR
SELECT nombrep, apellido FROM EMPLEADO
WHERE salario < ALL ( SELECT salario
FROM EMPLEADO
WHERE nd=5 );
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6$1,'$'$6$75,%8726$0%,*826
‡ &ROLVLyQGHQRPEUHVGHDWULEXWRVHQWUHFRQVXOWDH[WHULRU\DQLGDGD ⇒$PELJHGDG
1RPEUHV\DSHOOLGRVGHFDGDHPSOHDGRFRQGHSHQGLHQWHVGHLJXDOQRPEUH\VH[RTXHpO
SELECT nombrep, apellido FROM EMPLEADO E
WHERE nss IN ( SELECT nsse FROM DEPENDIENTE
WHERE nsse=nss AND nombrep=nombre_dep
AND sexo=sexo);
6ROXFLyQ
‡ 5(*/$³XQDUHIHUHQFLDDXQDWULEXWRQRFDOLILFDGRVHUHILHUHDOD
UHODFLyQGHFODUDGDHQODFRQVXOWDDQLGDGDPiVLQWHULRU´
‡ 6LHVQHFHVDULR KDFHUUHIHUHQFLDGHVGHXQDFRQVXOWDDQLGDGD
DDWULEXWRVGHUHODFLRQHVGHFODUDGDVHQXQDFRQVXOWD H[WHUQD⇒&$/,),&$5
1RPEUHV\DSHOOLGRVGHFDGDHPSOHDGRFRQGHSHQGLHQWHVGHLJXDOQRPEUH\VH[RTXHpO
SELECT nombrep, apellido FROM EMPLEADO E
WHERE nss IN ( SELECT nsse FROM DEPENDIENTE
WHERE nss=nsse AND nombrep=nombre_dep
AND E.sexo=sexo);
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6$1,'$'$6&255(/$&,Ï1
‡ &2168/7$6&255(/$&,21$'$6
± 8QDFRQGLFLyQGHODFRQVXOWDDQLGDGDKDFHUHIHUHQFLDD
XQDWULEXWRGHXQDUHODFLyQGHFODUDGDHQODFRQVXOWDH[WHULRU
− /DFRQVXOWDDQLGDGDVHHYDO~DXQDYH]SDUDFDGDWXSOD
RFRPELQDFLyQGHWXSODVGHODFRQVXOWDH[WHULRU
SELECT nombrep, apellido FROM EMPLEADO E
WHERE nss IN ( SELECT nsse FROM DEPENDIENTE
WHERE nss=nsse AND nombrep=nombre_dep AND E.sexo=sexo);
 (YDOXDUODFRQVXOWDDQLGDGDSDUDFDGDWXSODGH EMPLEADO
 6LHOYDORUGHnssGHODWXSODEMPLEADOHVWiHQHOUHVXOWDGRGHODFRQVXOWD
DQLGDGDVHOHFFLRQDUODWXSODEMPLEADOSDUDHOUHVXOWDGRWRWDO
‡ 8QD&2168/7$$1,'$'$TXHXVHHORSHUDGRU RIN
VLHPSUHSXHGHH[SUHVDUVHFRPRXQDÒ1,&$&2168/7$
SELECT E.nombrep, E.apellido
FROM EMPLEADO E, DEPENDIENTE D
WHERE E.nss=D.nsse AND E.nombrep=D.nombre_dep AND E.sexo=D.sexo;
0RGHOR5HODFLRQDOGHGDWRV64/
&2168/7$6$1,'$'$6EXISTS\UNIQUE
‡ &20352%$&,Ð1'(5(/$&,21(69$&Ì$6EXISTS (S)
± 'HYXHOYHTRUEVLODUHODFLyQS FRQWLHQHDOPHQRVXQDWXSOD
± 'HYXHOYHFALSEVLS HVXQDUHODFLyQYDFtD VLQWXSODV
6VXHOHVHUXQDFRQVXOWDDQLGDGDFRUUHODFLRQDGD
1RPEUHV\DSHOOLGRVGHHPSOHDGRVFRQDOJ~QGHSHQGLHQWHVGHLJXDOQRPEUH\VH[RTXHpO
SELECT E.nombrep, E.apellido FROM EMPLEADO E
WHERE EXISTS ( SELECT * FROM DEPENDIENTE
WHERE E.nss=nsse AND sexo=E.sexo AND E.nombrep=nombre_dep);
1RPEUHVGHHPSOHDGRVVLQSHUVRQDVGHSHQGLHQWHV
SELECT nombrep, apellido FROM EMPLEADO E
WHERE NOT EXISTS ( SELECT * FROM DEPENDIENTE WHERE nss=nsse );
‡ &20352%$&,Ð1'(783/$6'83/,&$'$6UNIQUE (S)
± 'HYXHOYHTRUEVL12KD\WXSODVUHSHWLGDVHQS
6VXHOHVHUXQDFRQVXOWDDQLGDGDFRUUHODFLRQDGD
1RPEUHV\DSHOOLGRVGH(PSOHDGRVTXHWUDEDMDQHQXQ~QLFRSUR\HFWR
SELECT nombrep, apellido FROM EMPLEADO
WHERE UNIQUE ( SELECT nsse FROM TRABAJA_EN WHERE nss=nsse );
0RGHOR5HODFLRQDOGHGDWRV64/
9$/25(618/26NULL )
5HFRUGDPRV
&DGDNULLHVGLVWLQWRGHWRGRVORVGHPiVNULL
⇒HQXQD5(81,Ð1ODVWXSODVFRQNULLHQORVDWULEXWRVGHUHXQLyQ12VH
LQFOX\HQHQHOUHVXOWDGRH[FHSWRVL5(81,Ð1(;7(51$
• 2SHUDGRU,618//,612718//
→HVTRUEVLHOYDORUvHVNULLLQIRUPDFLyQGHVFRQRFLGD
v IS NULL
v IS NOT NULL →HVTRUEVLHOYDORUvHVXQYDORUQR NULL
1RPEUHVGHHPSOHDGRVVLQVXSHUYLVRUHV
SELECT nombrep, apellido FROM EMPLEADO
WHERE nsssuper IS NULL;
0RGHOR5HODFLRQDOGHGDWRV64/
)81&,21(6$*5(*$'$6
‡ 2SHUDFLyQCOUNT( )
± &XHQWDHOQ~PHURGHWXSODVRYDORUHVHVSHFLILFDGRVHQXQDFRQVXOWD
‡ 2SHUDFLyQSUM( ), MAX( ), MIN( ), AVG( )
± 6XPDPi[LPRPtQLPR\PHGLDDULWPpWLFD
± $SOLFDGDVDXQPXOWLFRQMXQWREDJVDFRGHYDORUHVQXPpULFRV
‡ 3XHGHQVHUXVDGDVHQFOiXVXOD SELECTRHQHAVINGVHYHUi
6XPDGHORVVDODULRVVDODULRPi[LPRVDODULRPtQLPR\VDODULRPHGLRGHORVHPSOHDGRV
SELECT SUM(salario), MAX(salario), MIN(salario), AVG(salario)
FROM EMPLEADO;
6XPDGHORVVDODULRVVDODULRPi[LPRVDODULRPtQLPR\VDODULRPHGLRSHURGHORVHPSOHDGRVGHO
GHSDUWDPHQWRGH,QYHVWLJDFLyQ
SELECT SUM(salario), MAX(salario), MIN(salario), AVG(salario)
FROM EMPLEADO, DEPARTAMENTO
WHERE nd=númerod AND nombred=‘Investigación’;
0RGHOR5HODFLRQDOGHGDWRV64/
)81&,21(6$*5(*$'$6
‡ 862'(* (1/$6)81&,21(6$*5(*$'$6
1~PHURWRWDOGHHPSOHDGRVGHODFRPSDxtD
SELECT COUNT(*) FROM EMPLEADO
FXHQWDILODVRWXSODV
&RQWDUHOQ~PHURGHHPSOHDGRVGHODFRPSDxtDTXHWLHQHQXQMHIH
SELECT COUNT(nsssuper) FROM EMPLEADO;
QRFXHQWDWXSODVFRQnsssuperDNULL
1~PHURGHHPSOHDGRVHQHOGHSDUWDPHQWRGH,QYHVWLJDFLyQ
SELECT COUNT(*) FROM EMPLEADO, DEPARTAMENTO
WHERE nd=númerod AND nombred=‘Investigación’;
‡ 862'(DISTINCT (1/$6)81&,21(6$*5(*$'$6
&RQWDUHOQ~PHURGHYDORUHVGLVWLQWRVGHVDODULRTXHSXHGHQFREUDUORVHPSOHDGRV
SELECT COUNT(salario) FROM EMPLEADO;
(UURU12VHHOLPLQDQGXSOLFDGRVDVtTXHCOUNT(salario) ≡ COUNT(*)
SELECT COUNT(DISTINCT salario) FROM EMPLEADO; 2.
0RGHOR5HODFLRQDOGHGDWRV64/
)81&,21(6$*5(*$'$6
‡ )81&,21(6$*5(*$'$6(1&2168/7$6&255(/$&,21$'$6
± 6XEFRQVXOWDTXHLQFOX\HODIXQFLyQGHQWURGHO WHEREGHODFRQVXOWDH[WHULRU
1RPEUHVGHORVHPSOHDGRVFRQRPiVGHSHQGLHQWHV
SELECT apellido, nombrep FROM EMPLEADO
WHERE 2 ≤ ( SELECT COUNT(*)
FROM dependiente
WHERE nss=nsse );
0RGHOR5HODFLRQDOGHGDWRV64/
$*583$&,Ï1
‡ &OiXVXODGROUP BY
± 6XEJUXSRVGHWXSODVGHQWURGHXQDUHODFLyQ
± /RVJUXSRVVHIRUPDQVHJ~QHOYDORUGHDOJXQRVDWULEXWRV DWULEXWRVGHDJUXSDFLyQ
± /DVWXSODVGHFDGDJUXSRWHQGUiQLJXDOYDORUHQORVDWULEXWRVGHDJUXSDFLyQ
‡ $3/,&$&,Ð1'()81&,21(6$*5(*$'$6$*58326
3DUDFDGDGHSDUWDPHQWRREWHQHUVXQž\QRPEUHHOQžGHHPSOHDGRVGHGLFKRGHSDUWDPHQWR\HO
VDODULRPHGLRGHORVHPSOHDGRVGHOPLVPR
SELECT nd, COUNT(*), AVG(salario)
FROM EMPLEADO
GROUP BY nd ;
² /RVDWULEXWRVGHDJUXSDFLyQGHEHQDSDUHFHUHQFOiXVXODSELECT
SDUDTXHVXYDORU~QLFRSDUDFDGDJUXSRDSDUH]FDMXQWRDOUHVXOWDGR
GHDSOLFDUODIXQFLyQDFDGDJUXSR
0RGHOR5HODFLRQDOGHGDWRV64/
$*583$&,Ï1
‡ &OiXVXOD+$9,1*
± 6LHPSUHMXQWRDGROUP BY
± &RQGLFLyQTXHGHEHQFXPSOLUORVJUXSRVGHWXSODVDVRFLDGRVDFDGD
YDORUGHORVDWULEXWRVGHDJUXSDFLyQ
± 8QJUXSRTXH12FXPSOHODFRQGLFLyQQRVHVHOHFFLRQDSDUDHOUHVXOWDGR
3DUDFDGDSUR\HFWRHQHOTXHWUDEDMHQPiVGHGRVHPSOHDGRVREWHQHUQ~PHUR\QRPEUHGHO
SUR\HFWR\HOQžGHHPSOHDGRVTXHWUDEDMDQHQpO
SELECT númerop, nombrepr, COUNT(*)
FROM PROYECTO, TRABAJA_EN
WHERE númerop=númp
GROUP BY númerop, nombrepr
HAVING COUNT(*) > 2 ;
0RGHOR5HODFLRQDOGHGDWRV64/
$*583$&,Ï1
• WHEREVHDSOLFDDWXSODVLQGLYLGXDOHV
• HAVINGVHDSOLFDDJUXSRVGHWXSODV
1žWRWDOGHHPSOHDGRVFX\RVVDODULRVUHEDVDQORVHQFDGDGHSDUWDPHQWR
SHURVyORHQHOFDVRGHGHSDUWDPHQWRVHQORVTXHWUDEDMHQPiVGHHPSOHDGRV
&RQVXOWDLQFRUUHFWD¢SRUTXp"
&RQVXOWDFRUUHFWD
SELECT nombred, COUNT(*)
FROM DEPARTAMENTO, EMPLEADO
WHERE númerod=nd
AND salario>40000
GROUP BY nombred
HAVING COUNT(*) > 5 ;
SELECT nombred, COUNT(*)
FROM DEPARTAMENTO, EMPLEADO
WHERE númerod=nd
AND salario>40000
AND nd IN (SELECT nd
FROM EMPLEADO
GROUP BY nd
HAVING COUNT(*) > 5)
GROUP BY nombred ;
SLVWDRUGHQGHHMHFXFLyQ
0RGHOR5HODFLRQDOGHGDWRV64/
5(81,Ï1'(5(/$&,21(6-2,1
1RPEUHV\GLUHFFLyQGHHPSOHDGRVGHOGHSDUWDPHQWRGH,QYHVWLJDFLyQ
SELECT nombrep, apellido, dirección
FROM EMPLEADO, DEPARTAMENTO → UHXQLyQMRLQGHWDEODV
WHERE nombred=‘Investigacion’ AND nd=númerod; → FRQGLFLyQGHUHXQLyQ
‡ 5(/$&,21(6R7$%/$65(81,'$6
QXHYRHQ64/
(VSHFLILFDFLyQHQODFOiXVXODFROMGHODWDEODUHVXOWDQWHGHXQD5(81,Ï1
± $QWHVVHHVSHFLILFDEDHQFOiXVXODV FROM y WHERE
± &RQVXOWDPiVFRPSUHQVLEOHQRPH]FODFRQGLFLRQHVGHUHXQLyQ\GHVHOHFFLyQ
1RPEUHV\GLUHFFLyQGHHPSOHDGRVGHOGHSDUWDPHQWRGH,QYHVWLJDFLyQ
SELECT nombrep, apellido, dirección
FROM (EMPLEADO JOIN DEPARTAMENTO ON nd=númerod)→ ~QLFD WDEODUHXQLGD
WHERE nombred=‘Investigacion’;
0RGHOR5HODFLRQDOGHGDWRV64/
5(81,Ï1'(5(/$&,21(6-2,1
‡ $1,'$0,(172'((63(&,),&$&,21(6'(5(81,Ð1
3DUDFDGDSUR\HFWRXELFDGRHQ6DQWLDJRREWHQHUQžGHSUR\HFWRQžGHOGHSDUWDPHQWRTXHOR
FRQWUROD\DSHOOLGRGLUHFFLyQ\IHFKDGHQDFLPLHQWRGHOJHUHQWHGHHVHGHSDUWDPHQWR
SELECT númerop, númd, apellido, dirección, fechan
FROM ( ( PROYECTO JOIN DEPARTAMENTO ON númd=númerod )
JOIN EMPLEADO ON nssgte=nss )
WHERE lugarp=‘Santiago’;
0RGHOR5HODFLRQDOGHGDWRV64/
5(81,Ï1,17(51$'(5(/$&,21(6
‡ 5(81,Ð1,17(51$LQQHUMRLQ
± 7LSRGHUHXQLyQSRUGHIHFWR
SELECT ...
FROM (R1JOIN R2 ON <cond_reunión>)
WHERE ...
² 6LH[LVWHXQDWXSODWHQXQDGHODVUHODFLRQHV\RWUDWXSODWHQODRWUD
WDOHVTXHFXPSOHQODFRQGLFLyQGHUHXQLyQODWDEODUHVXOWDGRUHXQLGD
LQFOXLUiODWXSODUHVXOWDGRGHFRPELQDUW\W
SELECT E.nombrep AS nombre_empleado, S.nombrep AS nombre_supervisor
FROM (EMPLEADO E JOIN EMPLEADO S ON E.nsssuper = S.nss);
‡ 6RQH[FOXLGDVODVWXSODV EMPLEADOFRQNULLHQnsssuper
± 7DPELpQSXHGHHVSHFLILFDUVHFRPRR1INNER JOIN R2 ON <cond_reunión>
0RGHOR5HODFLRQDOGHGDWRV64/
5(81,Ï11$785$/'(5(/$&,21(6
‡ 5(81,Ð11$785$/QDWXUDOMRLQ
± 6,1FRQGLFLyQGHUHXQLyQ
SELECT ...
FROM ( R11$785$/JOIN R2 )
WHERE ...
² (48,55(81,Ð1,03/Ì&,7$
SDUDFDGDSDUGHDWULEXWRVGHLJXDOQRPEUHHQXQD\RWUDUHODFLyQ
9 &DGDSDUGHDWULEXWRVVyORVHLQFOX\H81$9(=HQHOUHVXOWDGR
9 6LQRFRLQFLGHQORVQRPEUHVGHORVDWULEXWRV
5(120%5$5XQRGHORVDWULEXWRVFRQ ASHQODFOiXVXODFROM
SELECT nombrep, apellido, dirección
FROM (EMPLEADO
NATURAL JOIN (DEPARTAMENTO AS DEP(nombred, nd, nssg, fech)))
WHERE nombred=‘Investigacion’;
0RGHOR5HODFLRQDOGHGDWRV64/
5(81,Ï1(;7(51$'(5(/$&,21(6
‡ 5(81,Ð1(;7(51$RXWHUMRLQ
± 6HQHFHVLWDFRQVLGHUDUWXSODVFRQYDORU NULLHQORVDWULEXWRVGHUHXQLyQ
SELECT E.nombrep AS nombre_empleado, S.nombrep AS nombre_supervisor
FROM (EMPLEADO E LEFT OUTER JOIN EMPLEADO S ON nsssuper=nss);
‡ ,1&/8,'$6ODVWXSODVGHEMPLEADO EFRQNULLHQnsssuper
· LEFT [OUTER] JOIN
5HXQLyQH[WHUQDL]TXLHUGD
· RIGHT [OUTER] JOIN 5HXQLyQH[WHUQDGHUHFKD
· FULL [OUTER] JOIN
5HXQLyQH[WHUQDFRPSOHWDRWRWDO
0RGHOR5HODFLRQDOGHGDWRV64/
(9$/8$&,Ï1'(&2168/7$664/
(QXQDFRQVXOWD64/
‡ +D\XQPi[LPRGHFOiXVXODV
‡ 6yORVRQREOLJDWRULDVSELECT\FROM
‡ 25'(1'((63(&,),&$&,Ð1
SELECT <lista atributos>
FROM <lista tablas>
WHERE <condición para tuplas>
GROUP BY <lista atributos agrupación>
HAVING <condición para grupos>
ORDER BY <lista atributos ordenación>
DWULEXWRVRIXQFLRQHVTXHVHYDQDREWHQHU
UHODFLRQHVQHFHVDULDVLQFOXVRODVUHXQLGDV
FRQGLFLRQHVSDUDVHOHFFLyQGHWXSODV
HVSHFLILFDFLyQGHODJUXSDPLHQWRGHWXSODV
FRQGLFLyQSDUDVHOHFFLyQGHJUXSRVGHWXSODV
RUGHQGHSUHVHQWDFLyQGHOUHVXOWDGRGHODFRQVXOWD
‡ 25'(1'((9$/8$&,Ð1
FROM HVGHFLUUHXQLyQRMRLQGHUHODFLRQHVVLVHHVSHFLILFDPiVGHXQD
WHERE
GROUP BY
HAVING
ORDER BY
0RGHOR5HODFLRQDOGHGDWRV64/
(9$/8$&,Ï1'(&2168/7$664/
‡ )/(;,%,/,'$''(/$6&2168/7$664/
² PXFKDVIRUPDVGHHVSHFLILFDUODPLVPDFRQVXOWD
-HOXVXDULRHOLJHODWpFQLFDRHQIRTXHPiVFyPRGR
(MHPSORHVSHFLILFDFLyQGHXQDFRQVXOWDFRQ
Dcondiciones de reuniónHQFOiXVXODWHERER
Erelaciones reunidasHQODFOiXVXODFROMR
Fconsultas anidadas\HORSHUDGRUGHFRPSDUDFLyQIN
/
 &RQIXVLyQGHOXVXDULR ¢TXpWpFQLFDXVR"
 $OJXQDVWpFQLFDVVRQ PiVHILFLHQWHVTXHRWUDV
⇒HOXVXDULRGHEHGHWHUPLQDUFXiO
&RQVHMRRSWLPL]DFLyQGHFRQVXOWDV
&RQVXOWDVFRQPtQLPRDQLGDPLHQWR
FRUUHODFLRQDGR\PtQLPR
RUGHQDPLHQWRLPSOtFLWR
± (QFRQGLFLRQHVLGHDOHV
² 8VXDULRVyORGHEHUtDSUHRFXSDUVHGH HVSHFLILFDUODFRQVXOWDFRUUHFWDPHQWH
² 6*%'VHRFXSDGH HMHFXWDUODFRQVXOWDGHPDQHUD HILFLHQWH
± (QODSUiFWLFD12HVDVt
⇒PHMRUVLHOXVXDULRVDEHTXpWLSRVGHFRQVXOWDVVRQPiV\PHQRVFRVWRVRV
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726HQ64/,16(5&,Ð1
‡ 2UGHQINSERT
± $xDGHXQDWXSODFRPSOHWDDXQDUHODFLyQ
± ,QFOX\HQRPEUHGHODUHODFLyQ\OLVWDGHYDORUHVSDUDORVDWULEXWRV
HVFULWRVHQLJXDORUGHQDOHVSHFLILFDGRHQODLQVWUXFFLyQCREATE TABLE
INSERT INTO EMPLEADO
VALUES ( “Ricardo”, ‘C’, “Martínez”, “653298653”, “30-DIC-52”,
“Olmo 98, Cedros, MX”, ‘M’, 37000, “987654321”, 4 ) ;
± 6LVHGHVHDSRQHUORVYDORUHVGHORVDWULEXWRVHQFXDOTXLHURUGHQKD\TXH
HVSHFLILFDUORVQRPEUHVGHORVDWULEXWRVHQHVHPLVPRRUGHQ
INSERT INTO EMPLEADO
(nombrep, inic,apellido,nss,nd,salario, nsssuper, direccion, fechan, sexo)
VALUES ( “Ricardo”, ‘C’, “Martínez”, “653298653”, 4, 37000, “987654321”
“Olmo 98, Cedros, MX”, “30-DIC-52”, ‘M’ );
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726,16(5&,Ð1
‡ ,16(5&,Ð1'(9$5,$6783/$6HQXQDVRODLQVWUXFFLyQINSERT
± 7XSODVVHSDUDGDVSRUFRPDV
± &DGDWXSODVHHQFLHUUDHQWUHSDUpQWHVLV
‡ (63(&,),&$&,Ð1(;3/Ì&,7$'($/*8126$75,%8726\QRWRGRV
² 2PLVLyQGHDWULEXWRVFX\RYDORUVHGHVFRQRFH
± /RVDWULEXWRVQRHVSHFLILFDGRVWRPDUiQHOYDORU
 3RURPLVLyQYDORUWRPDGRGHVXFOiXVXOD DEFAULTRELHQ
· NULLVLQRVHGHILQLyFOiXVXOD DEFAULT SDUDHODWULEXWR\SHUPLWHQXORV
,QVHUFLyQGHXQQXHYRHPSOHDGRGHOTXHVyORVHFRQRFHVXQRPEUHDSHOOLGRV\QVV
INSERT INTO EMPLEADO (nombrep, apellido, nss)
VALUES ( “Ricardo”, “Martínez”, “653298653” ) ;
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726,16(5&,Ð1
‡ 0$1(-2<0$17(1,0,(172'(5(675,&&,21(6'(,17(*5,'$'
96*%'FRQLPSOHPHQWDFLyQWRWDOGH64/
± (O6*%'PDQHMDHLPSRQH WRGD5,HVSHFLILFDGDFRQ''/
96*%'FRQLPSOHPHQWDFLyQGHDOJXQDV5,PHQRUFRPSOHMLGDGPD\RUHILFLHQFLD
± 6*%'LPSOHPHQWDFRPSUREDFLRQHVSDUDKDFHUFXPSOLUODV 5,TXHVtPDQHMD
INSERT INTO EMPLEADO (nombrep, apellido, nss, nd)
VALUES ( “Roberto”, “Huertas”, 2 ) ;
ª ,QVHUFLyQUHFKD]DGDQRVHLQFOX\HYDORUSDUDHODWULEXWRnss, GHFODUDGRNOT NULL
± 3URJUDPDGRUDVHJXUDOD12YLRODFLyQGHODV5,QRYHULILFDGDVSRUHO'%06
INSERT INTO EMPLEADO (nombrep, apellido, nss, nd)
VALUES ( “Roberto”, “Huertas”, “980760540”, 2 ) ;
‡ 6LHO6*%'12PDQHMDOD,QWHJULGDG5HIHUHQFLDO⇒LQVHUFLyQSHUPLWLGD
£££(OSURJUDPDGRUGHEHDVHJXUDUTXHHVWR12SDVH
‡ 6LHO6*%'6ÌPDQHMDOD,QWHJULGDG5HIHUHQFLDO⇒LQVHUFLyQUHFKD]DGD
QRH[LVWHGHSDUWDPHQWRFRQnúmerod=2
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726,16(5&,Ð1
‡ ,16(5&,Ð1'(783/$65(68/7$'2'(81$&2168/7$
² &DUJDGHXQDUHODFLyQFRQLQIRUPDFLyQVLQySWLFDGHOD%'
6HDXQDUHODFLyQ,1)2B'(3726YDFtDHQODTXHTXHUHPRVDOPDFHQDUORVQRPEUHVGHFDGD
GHSDUWDPHQWRVXQžGHHPSOHDGRV\HOVDODULRFRQMXQWRGHORVHPSOHDGRVGHOPLVPR
INFO_DEPTOS ( nombre_depto, núm_emps, sal_total)
INSERT INTO INFO_DEPTOS ( nombre_depto, núm_emps, sal_total )
SELECT nombred, COUNT(*), SUM(salario)
FROM DEPARTAMENTO, EMPLEADO
WHERE númerod=nd
GROUP BY nombred ;
$KRUDHVSRVLEOHKDFHU SELECT ... FROM INFO_DEPTOS ...
2MRODWDEODINFO_DEPTOSSXHGHFRQWHQHULQIRUPDFLyQ12DFWXDOL]DGD
6LVHPRGLILFDLQIRUPDFLyQHQEMPLEADO\RDEPARTAMENTO
ORVFDPELRV12VHUHIOHMDUiQHQODUHODFLyQINFO_DEPTOS
ª (VQHFHVDULRFUHDUXQDUHODFLyQGHHVWHWLSRPHGLDQWHXQD 9,67$
SDUDTXHHVWpVLHPSUHFRQWHQJDORVGDWRVPiVDFWXDOHV
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726(/,0,1$&,Ð1
‡ 2UGHQDELETE
± (OLPLQDWXSODVFRPSOHWDVGHXQD~QLFDUHODFLyQ
± 6yORUHODFLyQHQFOiXVXODFROM
± &OiXVXODWHERESDUDVHOHFFLRQDUODVWXSODVTXHHOLPLQDU
‡ 6LQRKD\ WHEREVHHOLPLQDQWRGDVODVWXSODV
‡ /DWDEODSHUPDQHFHSHURTXHGD YDFtD
‡ 3523$*$&,Ð1'((/,0,1$&,21(6
± 6HJ~QDFFLRQHVHVSHFLILFDGDVHQFDGD5HVWULFFLyQGH,QWHJULGDG5HIHUHQFLDO
HVFULWDVFRQ''/HQORVCREATE TABLE
DELETE FROM EMPLEADO WHERE apellido=‘Bojórquez’;
HOLPLQDWXSODV
DELETE FROM EMPLEADO WHERE nss=‘123456789’ ;
HOLPLQDWXSOD
DELETE FROM EMPLEADO
WHERE nd IN ( SELECT númerod FROM DEPARTAMENTO
WHERE nombre=‘Investigación’) ;
HOLPLQDWXSODV
DELETE FROM EMPLEADO ;
HOLPLQDWRGDVODVWXSODV
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726$&78$/,=$&,Ð1
‡ 2UGHQUPDATE
± 0RGLILFDYDORUHVGHDWULEXWRVHQXQDRPiVWXSODVGHXQDUHODFLyQ
± 6HPRGLILFDQWXSODVGHXQD~QLFDWDEODDODYH]
² &OiXVXODSETHVSHFLILFDDWULEXWRVTXHPRGLILFDU\QXHYRVYDORUHV
² &OiXVXODWHERESDUDVHOHFFLRQDUWXSODVTXHDFWXDOL]DU
‡ 6LQRKD\WHEREVHDSOLFDODPRGLILFDFLyQD WRGDVODVWXSODV
‡ 3523$*$&,Ð1'(02',),&$&,21(6
6LFDPELDHOYDORUGHFODYHFDQGLGDWDHVWHFDPELRVHSURSDJDDYDORUHV
GHFODYHDMHQDGHWXSODVGHRWUDVUHODFLRQHVVLDVtVHHVSHFLILFyHQODV
5,V5HIHUHQFLDOHQODGHILQLFLyQGHODWDEODFRQCREATE TABLE
3DUDHOSUR\HFWRQ~PHURFDPELDUHOOXJDUD%HOHQ\HOQžGHOGHSDUWDPHQWRFRQWURODGRUDO
UPDATE PROYECTO SET lugarp = ‘Belen’, númd = 5
WHERE numerop=10 ;
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'('$726$&78$/,=$&,Ð1
‡ 02',),&$&,Ð1'(9$5,$6783/$6$/$9(=(1 UPDATE
2WRUJDUDWRGRVORVHPSOHDGRVGHOGHSDUWDPHQWRGH,QYHVWLJDFLyQXQDXPHQWRVDODULDOGHO
UPDATE EMPLEADO SET salario = salario*1.1
WHERE nd IN (SELECT númerod
FROM DEPARTAMENTO
WHERE nombred=‘Investigación’) ;
‡ 18//2'()$8/7&20218(929$/25'(81$75,%872
UPDATE EMPLEADO SET salario = DEFAULT;
UPDATE EMPLEADO SET nsssuper = NULL WHERE ... ;
0RGHOR5HODFLRQDOGHGDWRV64/
'(),1,&,Ï1'('$726''/(164/
‡ (648(0$GH%DVHGH'DWRV5HODFLRQDO
$JUXSDUHODFLRQHVWDEODV\RWURVHOHPHQWRVGHXQD PLVPDDSOLFDFLyQ
DVYHUVLRQHVGH64/WRGDVODVWDEODVGHQWURGHOXQHVTXHPD~QLFR\
JOREDODWRGDVODVDSOLFDFLRQHVTXHDFFHGtDQDOD%'
‡ 2UGHQCREATE SCHEMAVHQWHQFLDGHGHILQLFLyQFUHDFLyQGHHVTXHPDV
CREATE SCHEMA COMPAÑÍA AUTHORIZATION JSILVA ;
8 QRPEUHGHHVTXHPDLGHQWLILFDHOHVTXHPD(COMPAÑÍA)
8 LGHQWLILFDGRUGHDXWRUL]DFLyQXVXDULRFXHQWDSURSLHWDULRGHOHVTXHPD(JSILVA)
8 &RQMXQWRGHGHILQLFLRQHVGHFDGDHOHPHQWRFRQWHQLGRHQHOHVTXHPD
‡ (OHPHQWRVGHO(VTXHPD
7DEODV9LVWDV'RPLQLRVDXWRUL]DFLRQHVDVHUWRVHWF
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ 2UGHQCREATE TABLE
± 'HILQHFUHDXQDUHODFLyQQRPEUHDWULEXWRV\UHVWULFFLRQHV
± 3DUDFDGDDWULEXWR
‡ QRPEUHWLSRGHGDWRVGRPLQLR\UHVWULFFLRQHVGHDWULEXWR
² 5HVWULFFLRQHVGHWDEOD
‡ GHFODYHFDQGLGDWDHLQWHJULGDGGHHQWLGDG
‡ GHLQWHJULGDGUHIHUHQFLDOR
‡ UHVWULFFLRQHVGH RWURWLSR
CREATE TABLE EMPLEADO (
nombrep ...
inic ...
apellido ...
nss ...
fechan ...
direccion ...
sexo ...
salario ...
nsssuper ...
nd ...
);
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ ,1',&$&,Ð1'(/(648(0$$/48(3(57(1(&(81$5(/$&,Ð1
± (VTXHPD([SOtFLWR
CREATE TABLE COMPAÑÍA.EMPLEADO
± (VTXHPD,PSOtFLWRHQHOFRQWH[WR
CREATE TABLE EMPLEADO
‡ 25'(1$0,(172'($75,%8726<783/$6(164/
± $WULEXWRVRUGHQDGRVWDOFRPRDSDUHFHQHQCREATE TABLE
± /DVILODVWXSODV12HVWiQRUGHQDGDV
‡ /DVUHODFLRQHVFUHDGDVFRQ CREATE TABLEVRQUHODFLRQHV%$6(
± (O6*%'ODVDOPDFHQDItVLFDPHQWHHQDOJ~QILFKHURGHODEDVHGHGDWRV
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ (63(&,),&$&,Ð1'(/7,32'('$726'(81$75,%872
(VSHFLILFDUGLUHFWDPHQWHHO7,32'('$72WUDVHOQRPEUHGHODWULEXWR
CREATE TABLE EMPLEADO (
nombrep VARCHAR(15) ...
... );
'HFODUDUXQ'20,1,2\XVDUVXQRPEUHFRPR³WLSRGHGDWRV´
)DFLOLWDFDPELRGHOWLSRGHGDWRVXVDGRSRUPXFKRVDWULEXWRV
(VTXHPDPiVFRPSUHQVLEOH
CREATE DOMAIN NOMBRES AS CHAR(15);
...
CREATE TABLE EMPLEADO (
nombrep NOMBRES ...
... );
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ 7,326'('$726
1XPpULFRV
 (QWHURV\5HDOHV INTEGER, SMALLINT, FLOAT, REAL, DOUBLE PRECISION
DECIMAL(p,e)( DEC(p,e), NUMERIC(p,e) )
 &RQIRUPDWR
SSUHFLVLyQHHVFDOD9DORUSRURPLVLyQHVFDODe &DGHQDGHFDUDFWHUHV
 /RQJLWXGILMD
CHAR(n)
 /RQJLWXGYDULDEOHVARCHAR(n)
QQžFDUDFWHUHV
QPi[LPRQžFDUDFWHUHV
&DGHQDGH%LWV
 /RQJLWXGILMD
 /RQJLWXGYDULDEOH
BIT(n)QQžELWV
BIT VARYING(n)QPi[QžELWV9DORUSRURPLVLyQn - DATESRVLFLRQHV YEAR , MONTH , DAY
\\\\PPGG
- TIMESRVLFLRQHV HOUR , MINUTE , SECOND
KKPPVV
 6yORSHUPLWLGDVIHFKDV\KRUDVYiOLGDV
- TIMESTAMPPDUFDGHWLHPSR DATE, TIMEIUDFFLRQHVGHVHJXQGR\GHVSOD]DPLHQWR
UHVSHFWRDOKXVRKRUDULRHVWiQGDU WITH TIME ZONE
- INTERVAL9DORUUHODWLYRSDUDLQFUGHFUHPHQWDUHOYDGHIHFKDKRUDRWLPHVWDPS
 6HFDOLILFDFRQYEAR/MONTH ó DAY/TIMESDUDLQGLFDUVXQDWXUDOH]D
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'('20,1,26
‡ '20,1,26'('$726
CREATE DOMAIN <nombre dominio> <tipo de datos>
[DEFAULT <valor>]
[ <lista de definición de restricciones de dominio> ] ;
(O<tipo de datos>KDGHVHUXQRGHORVSURSRUFLRQDGRVSRUHOVLVWHPD EXLOWLQ
9DORUSRUGHIHFWRRSFLRQDO
(VSHFLILFDHOYDORUSRURPLVLyQSDUDDWULEXWRVGHILQLGRVGHHVWHGRPLQLR
6HUiDVLJQDGRDFDGDFROXPQDGHILQLGDVREUHHOGRPLQLRVLQRWLHQH\DVXSURSLD DEFAULT
'HILQLFLyQGH5HVWULFFLRQHVGH,QWHJULGDGGH'RPLQLRRSFLRQDO
&RQMXQWRGH5,TXHVHDSOLFDQDFDGDFROXPQDGHILQLGDVREUHHOGRPLQLR
&DGDUHVWULFFLRQSXHGHWHQHUXQQRPEUHFOiXVXODCONSTRAINT <nombre_RI>
(MHPSORHQXPHUDFLyQGHSRVLEOHVYDORUHVFRPSRQHQWHVGHOGRPLQLR
CREATE DOMAIN COLOR VARCHAR(8) DEFAULT ‘sinColor’
CONSTRAINT color_valido
CHECK (VALUE IN ( ‘rojo’, ’amarillo’ , ’azul’, ’verde’, ’ sinColor’ ) ) ;
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ (63(&,),&$&,Ð1'(5(675,&&,21(6'($75,%872
23&,Ð1'(18/2
,QGLFDVLXQDWULEXWRSXHGHFRQWHQHU NULL
5HVWULFFLyQNULL o NOT NULL
CREATE TABLE EMPLEADO (
nombrep VARCHAR(15) NOT NULL, ... );
± /DUHVWULFFLyQ NOT NULLHVREOLJDWRULDSDUDDWULEXWRVGHFODYHSULPDULD
9$/2532520,6,Ð1RSRUGHIHFWR
&OiXVXODDEFAULT <valor>
CREATE TABLE EMPLEADO ( ...
salario DECIMAL(10,2) NULL DEFAULT 100000 ... );
6LXQDWULEXWRQRWLHQHFOiXVXOD DEFAULTVXYDORUSRUGHIHFWRHV
 (OGHVXGRPLQLRVLVXWLSRGHGDWRVHVXQGRPLQLR\pVWHLQFOX\H DEFAULT
 (ONULLHQFXDOTXLHURWURFDVRVLHPSUHTXHHODWULEXWRSHUPLWD NULL
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ (63(&,),&$&,Ð1'(5(675,&&,21(6'(7$%/$
- PRIMARY KEY (...)
(VSHFLILFDDWULEXWRVTXHFRPSRQHQOD &ODYH3ULPDULD
- UNIQUE (...)
,QGLFD&ODYHV$OWHUQDWLYDV
- FOREIGN KEY (...)
(VSHFLILFD&ODYHV([WHUQDV,QWHJULGDG5HIHUHQFLDO
- CHECK (...)
,QGLFDFRQGLFLyQTXHGHEHFXPSOLUWRGDWXSODGHODWDEOD
CREATE TABLE EMPLEADO (
nombrep VARCHAR(15) NOT NULL,
...,
nss
CHAR (9)
NOT NULL,
nif
CHAR(9)
NOT NULL,
...,
nsssuper CHAR(9)
NULL,
nd
INTEGER
NOT NULL,
PRIMARY KEY ( nss ),
UNIQUE ( nif ),
CHECK ( nsssuper <> nss ),
FOREIGN KEY (nsssuper) REFERENCES EMPLEADO(nss),
FOREIGN KEY (nd) REFERENCES DEPARTAMENTO(numerod)
);
0RGHOR5HODFLRQDOGHGDWRV64/
''/'(),1,&,Ï1'(7$%/$6
‡ $&&,21(6',63$5$'$6325,17(*5,'$'5()(5(1&,$/
3ROtWLFDVGHPDQWHQLPLHQWRGHOD,QWHJULGDG5HIHUHQFLDO
ON DELETE <acción>
ON UPDATE <acción>
GRQGH<acción> ∈ { NO ACTION, SET NULL, CASCADE, SET DEFAULT }
‡ (63(&,),&$&,Ð1'(5(675,&&,21(6'(7$%/$120%5$'$6
CONSTRAINT <nombre_RI> <restricción CHECK>
(VRSFLRQDO
1RPEUHVGHUHVWULFFLyQ ~QLFRVGHQWURGHOPLVPR HVTXHPD
,GHQWLILFDQXQDUHVWULFFLyQSRUVLGHVSXpVGHEHVHUVXVWLWXLGDSRURWUD
CREATE TABLE EMPLEADO (
...,
CONSTRAINT
CONSTRAINT
CONSTRAINT
CONSTRAINT
PRIMARY KEY ( nss ),
pk_empleado
UNIQUE ( nif ),
nif_unico
CHECK ( nsssuper <> nss ),
no_auto_ jefe
jefe_emp FOREIGN KEY (nsssuper) REFERENCES EMPLEADO(nss)
ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT dep_emp FOREIGN KEY (nd) REFERENCES DEPARTAMENTO(numerod)
ON DELETE SET NULL ON UPDATE CASCADE
);
0RGHOR5HODFLRQDOGHGDWRV64/
9,67$6(164/
‡ 9,67$VIEW
± (VXQD5HODFLyQGHULYDGDGHRWUDVUHODFLRQHV
± /DVWDEODVRYLVWDVGHODVTXHVHGHULYDODYLVWD VRQVXV7DEODV%DVH
± 6RQUHODFLRQHVWDEODVYLUWXDOHVQRQHFHVDULDPHQWH H[LVWHQHQIRUPDItVLFD
CREATE VIEW <nombre_vista> [ <lista_nombres_atributos> ]
AS <consulta_de_definición>
± /DFRQVXOWDGHGHILQLFLyQ GHWHUPLQDHOFRQWHQLGRGHODYLVWD
CREATE VIEW EMPLEADO_DEPENDIENTE
AS SELECT nombrep, nombre_dependiente, parentesco
FROM EMPLEADO, DEPENDIENTE
WHERE nss = nsse;
‡ /DVYLVWDVSXHGHQXWLOL]DUVHFRPR
± 0HFDQLVPRGHVLPSOLILFDFLyQGHFRQVXOWDV
± 0HFDQLVPRGHVHJXULGDG
‡ &DUDFWHUtVWLFDIXQGDPHQWDOGHODVYLVWDV
² $FWXDOL]DFLyQ3HUPDQHQWHUHVSRQVDEOHHO6*%'
ª /DYLVWD12VHFUHDFXDQGRVHGHILQHVLQRFXDQGRVHFRQVXOWD
0RGHOR5HODFLRQDOGHGDWRV64/
(63(&,),&$&,Ï1'(9,67$6CREATE VIEW
‡ 3RUGHIHFWRODYLVWD KHUHGDORVQRPEUHVGHORVDWULEXWRV
9VHOHFFLRQDGRVGHVGHODVWDEODVEDVHWDPELpQOODPDGDV³WDEODVGHGHILQLFLyQ´
9VLHPSUHTXHQLQJ~QDWULEXWRVHDHOUHVXOWDGRGHXQDIXQFLyQXRSHUDFLyQDULWPpWLFD
CREATE VIEW VISTA_TRABAJA_EN
AS SELECT nombrep, apellido, nombrepr, horas
FROM EMPLEADO, PROYECTO, TRABAJA_EN
WHERE nss = nsse AND númp = númerop ;
CREATE VIEW INFO_DEPTO (nombre_depto, núm_de_emps, sal_total)
AS SELECT nombred, COUNT(*), SUM(salario)
FROM DEPARTAMENTO, EMPLEADO
WHERE númerod = nd
GROUP BY nombred ;
‡ 1RWLHQHQQLQJXQDOLPLWDFLyQHQRSHUDFLRQHVGH&RQVXOWD
$SHOOLGR\QRPEUHGHHPSOHDGRVTXHWUDEDMDQHQHOSUR\HFWROODPDGR´ProductoX´
SELECT nombrepr, nombrep, apellido
FROM VISTA_TRABAJA_EN
WHERE nombrepr=‘ProductoX’ ;
‡ 7LHQHQDOJXQDVOLPLWDFLRQHVHQRSHUDFLRQHVGH0RGLILFDFLyQ
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'(9,67$6(164/
‡ (VFRPSOLFDGD\SXHGHVHUDPELJXD
0RGLILFDUYLVWDVGHILQLGDVVREUHP~OWLSOHVWDEODVVXHOHGDUSUREOHPDV
UPDATE VISTA_TRABAJA_EN
SET nombrepr = ‘ProductoY’
WHERE apellido = ‘Silva’ AND nombrep=‘José’ AND nombrepr = ‘ProductoX’ ;
3XHGHWUDGXFLUVHD '26SRVLEOHVPRGLILFDFLRQHVGHODVUHODFLRQHVEDVH
DUPDATE TRABAJA_EN
SET númp = (SELECT númerop FROM PROYECTO WHERE nombrepr = ‘ProductoY’)
WHERE nsse = (SELECT nss FROM EMPLEADO
WHERE apellido = ‘Silva’ AND nombrep = ‘José’)
AND númp = (SELECT númerop FROM PROYECTO
WHERE nombrepr = ‘ProductoX’) ;
ª 5HODFLRQDJosé SilvaFRQODWXSOD‘ProductoY’GHPROYECTO\QRFRQODGHµProductoX’
EUPDATE PROYECTO SET nombrepr = ‘ProductoY’
WHERE nombrepr = ‘ProductoX’ ;
ª 0LVPRHIHFWRTXHDSHURPRGLILFDQGRHOnombreprHQPROYECTO
⇒3(52PRGLILFDWRGDVODVWXSODVGHODYLVWDFX\Rnombrepr ‘ProductoX’£/
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'(9,67$6(164/
‡ $OJXQDVPRGLILFDFLRQHVGHYLVWDVFDUHFHQGHVHQWLGR
UPDATE INFO_DEPTO
SET sal_total = 100000
WHERE nombred=‘Investigación’ ;
3XHVsal_totalVHGHILQHFRPR¶VXPDGHORVVDODULRVLQGLYLGXDOHVGHORVHPSOHDGRV·
\PXFKDVDFWXDOL]DFLRQHVGHODVWDEODVEDVHVDWLVIDUtDQHVWDDFWXDOL]DFLyQ
‡ 12VHJDUDQWL]DTXH³WRGDYLVWDVHDDFWXDOL]DEOH´
‡ 8QDYLVWDVHUtDDFWXDOL]DEOH
96LLPSOLFDUDXQD~QLFDDFWXDOL]DFLyQSRVLEOHGHODVWDEODVGHILQLFLyQ
96LH[LVWLHUDQYDULDVSRVLEOHVDFWXDOL]DFLRQHVSHURH[LVWLHUDXQ
SURFHGLPLHQWRHVSHFtILFRGHDFWXDOL]DFLyQGHODVWDEODVEDVHWDOTXH
 (OXVXDULRSXGLHUDHOHJLUHOSURFHGLPLHQWRHVSHFLILFiQGRORHQODGHILQLFLyQGHODYLVWD
RELHQ
 (O6*%'SXGLHUDHOHJLUHOSURFHGLPLHQWRVHJ~QOD DFWXDOL]DFLyQPiVSUREDEOH
0RGHOR5HODFLRQDOGHGDWRV64/
02',),&$&,Ï1'(9,67$6(164/
(QJHQHUDO
‡ 8QDYLVWDFRQ81$~QLFDWDEODGHGHILQLFLyQ
$FWXDOL]DEOHVLORVDWULEXWRVFRQWLHQHQODFODYHSULPDULDXRWUDFDQGLGDWDGH
ODUHODFLyQEDVHVHHVWDEOHFHXQDWUDQVIRUPDFLyQGHFDGDWXSODGHODYLVWDD
XQD~QLFDWXSODEDVH
‡ 8QDYLVWDGHILQLGDVREUH0Ó/7,3/(6WDEODVPHGLDQWHUHXQLRQHV
12HVDFWXDOL]DEOH
‡ 8QDYLVWDGHILQLGDPHGLDQWH$*583$&,Ð1\IXQFLRQHV$*5(*$'$6
12HVDFWXDOL]DEOH
‡ 23&,Ð1'(9(5,),&$&,Ð1'(9,67$6
&OiXVXODWITH CHECK OPTION
± $OILQDOGHODGHILQLFLyQGHWRGDYLVWDTXHVHYD\DDXWLOL]DUSDUDPRGLILFDFLyQ
± 3HUPLWHDOVLVWHPD
±&RPSUREDUVLODYLVWDHV DFWXDOL]DEOH\
±3ODQHDUXQDHVWUDWHJLDGHHMHFXFLyQGHDFWXDOL]DFLRQHV
0RGHOR5HODFLRQDOGHGDWRV64/
,03/(0(17$&,Ï1'(9,67$6(164/
'RVHQIRTXHV
(675$7(*,$'($&78$/,=$&,Ð1'(&2168/7$6'('(),1,&,Ð1
± &RQVXOWDVREUHODYLVWDVHWUDGXFHDXQD&RQVXOWDVREUH7DEODV'HILQLFLyQ
± /DYLVWDVHUHOOHQDGHWXSODVDSDUWLUGHODHMHFXFLyQGHODFRQVXOWD
± 'HVYHQWDMDSRFRHILFLHQWHFXDQGR
ODYLVWDVHGHILQHFRQ FRQVXOWDFRPSOHMDFRQWLHPSRGHHMHFXFL yQDSUHFLDEOH
\VHDSOLFDQPXFKDVFRQVXOWDVVREUHODYLVWDHQSRFRWLHPSR
(675$7(*,$'(0$7(5,$/,=$&,Ð1'(9,67$6
± &RQVXOWDVREUHODYLVWD⇒FUHDFLyQGH7DEOD7HPSRUDO)tVLFD
± 6HFRQVHUYDODWDEODSDUDSRVWHULRUHVFRQVXOWDVVREUHODYLVWD
± 1HFHVDULDHVWUDWHJLDSDUDDFWXDOL]DFLyQLQFUHPHQWDOGHOD7DEOD7HPSRUDO WUDV
FXDOTXLHUPRGLILFDFLyQGHWDEODVEDVH⇒DFWXDOL]DFLyQSHUPDQHQWH
± 6LQRVHKDFHUHIHUHQFLDDODYLVWDWUDVXQWLHPSR HOVLVWHPDODHOLPLQDUi
\ODUHFDOFXODUiHQXQDFRQVXOWDIXWXUD
0RGHOR5HODFLRQDOGHGDWRV64/
''/$/7(5$&,Ï1'(7$%/$6
‡ 02',),&$&,21'(/$(6758&785$'(/$6
5(/$&,21(6%$6('(/(648(0$
‡ 3RVLELOLGDGHV
±$GLFLyQ\(OLPLQDFLyQGH$WULEXWRVFROXPQDV
±0RGLILFDFLyQGHOD'HILQLFLyQGH$WULEXWRV
±$GLFLyQ\(OLPLQDFLyQGH5HVWULFFLRQHVGH7DEOD
ALTER TABLE <nombre_relacion> ... ;
0RGHOR5HODFLRQDOGHGDWRV64/
''/$/7(5$&,Ï1'(7$%/$6
‡ $',&,Ð1'($75,%872&2/801$
ALTER TABLE <nombre_relacion> ADD <definición_atributo> ;
$xDGLUDODUHODFLyQEMPLEADOGHOHVTXHPDCOMPAÑÍAXQDWULEXWRSDUDFRQWHQHUHO
SXHVWRGHWUDEDMR
ALTER TABLE COMPAÑÍA.EMPLEADO ADD puesto VARCHAR(12);
± 7RGDVODVWXSODVGH EMPLEADO WHQGUiQ puesto DNULL
⇒12HVWiSHUPLWLGDODUHVWULFFL yQNOT NULLHQODGHILQLFLyQGHOQXHYRDWULEXWR
VLHVQHFHVDULDSRGUiHVWDEOHFHUVHGHVSXpV
± 3DUDLQWURGXFLUXQYDORUSDUDpuestoHQFDGDWXSODH[LVWHQWHHQEMPLEADO
(a) (VSHFLILFDUODFOiXVXOD DEFAULT DODxDGLUHODWULEXWR
ALTER TABLE EMPLEADO ADD puesto VARCHAR(12) DEFAULT “aprendiz”;
(b)8WLOL]DUGHVSXpVXQDRUGHQ UPDATEVHYHUi
0RGHOR5HODFLRQDOGHGDWRV64/
''/$/7(5$&,Ï1'(7$%/$6
‡ (/,0,1$&,Ð1'( 81$75,%872&2/801$
ALTER TABLE <nombre_relacion> DROP <nombre_atributo> <opcion> ;
<opcion> SXHGHVHU
CASCADEHOLPLQDHODWULEXWR\WRGDUHVWULFFLyQRYLVWDTXHOHKDJDUHIHUHQFLD
RESTRICTVyORHOLPLQDHODWULEXWRVLQLQJXQDYLVWDQLUHVWULFFLyQOHUHIHUHQFLD
(OLPLQDFLyQGHODWULEXWRdirecciónGHODUHODFLyQEMPLEADOGHOHVTXHPDCOMPAÑÍA
ALTER TABLE COMPAÑÍA.EMPLEADO DROP dirección CASCADE;
0RGHOR5HODFLRQDOGHGDWRV64/
''/$/7(5$&,Ï1'(7$%/$6
‡ 02',),&$&,21'(/$'(),1,&,Ð1'(81$75,%872&2/801$
ALTER TABLE <nombre_relacion> ALTER <nombre_atributo> <accion> ;
0RGLILFDFLyQGHODFOiXVXODSRURPLVLyQ
± (OLPLQDFLyQGHODFOiXVXODDEFAULTH[LVWHQWH
ALTER TABLE COMPAÑÍA.DEPARTAMENTO ALTER nssgte DROP DEFAULT;
± 'HILQLFLyQGHXQDQXHYDcláusula por omisión
ALTER TABLE DEPARTAMENTO ALTER nssgte SET DEFAULT “123456789”;
0RGHOR5HODFLRQDOGHGDWRV64/
''/$/7(5$&,Ï1'(7$%/$6
± /DUHVWULFFLyQGHWDEODTXHVHGHVHDPRGLILFDUGHEHWHQHUXQQRPEUH
‡ (/,0,1$&,Ð1'(81$5(675,&&,Ð1'(7$%/$
ALTER TABLE <nom_relacion> DROP CONSTRAINT <nombre_RI> <opcion>;
ALTER TABLE COMPAÑÍA.EMPLEADO DROP CONSTRAINT jefe_emp CASCADE;
‡ $',&,Ð1'(81$5(675,&&,Ð1'(7$%/$
ALTER TABLE <nom_relacion> ADD CONSTRAINT <nombre_RI> <defin_RI>;
ALTER TABLE EMPLEADO ADD CONSTRAINT salario_ok CHECK (salario > 0);
0RGHOR5HODFLRQDOGHGDWRV64/
''/$/7(5$&,Ï1'('20,1,26
‡ 2UGHQALTER DOMAIN <nombre_dominio> <acción>;
(OLPLQDFLyQ\5HHPSOD]RGHXQDGHILQLFLyQSRUGHIHFWR
ALTER DOMAIN <nom_dominio> DROP DEFAULT;
ALTER DOMAIN <nom_dominio> SET DEFAULT <valor>;
(OLPLQDFLyQ\RHVSHFLILFDFLyQGHQXHYDVUHVWULFFLRQHVGHGRPLQLR
ALTER DOMAIN <nom_dominio> DROP CONSTRAINT <nom_RI_dominio>;
ALTER DOMAIN <nom_dominio>
ADD [CONSTRAINT <nom_RI>] <restricción_CHECK>;
0RGHOR5HODFLRQDOGHGDWRV64/
''/(/,0,1$&,Ï1'((/(0(1726'(/(648(0$
‡ (/,0,1$&,Ð1'(81$9,67$2UGHQDROP VIEW
± 'HVWUX\HXQDUHODFLyQGHULYDGDMXQWRFRQVXGHILQLFLyQHQHOFDWiORJR
DROP VIEW <nombre_vista> ;
‡ (/,0,1$&,Ð1'(81'20,1,22UGHQDROP DOMAIN
± 'HVWUX\HXQGRPLQLRGHGDWRVMXQWRFRQVXGHILQLFLyQHQHOFDWiORJR
DROP DOMAIN <nombre_dominio> <opción> ;
<opcion> SXHGHVHU
· RESTRICT:VyORGHVWUX\HHOGRPLQLRVL QRKD\QLQJ~QDWULEXWRGHILQLGRVREUHpO
· CASCADE:VHGHVWUX\HHOGRPLQLR\WRGR DWULEXWRGHILQLGRVREUHpO
SDVDDWHQHUHOtipo de datosVREUHHOTXHVHKDEtDGHILQLGRHOGRPLQLR
0RGHOR5HODFLRQDOGHGDWRV64/
''/(/,0,1$&,Ï1'((/(0(1726'(/(648(0$
‡ (/,0,1$&,Ð1'(81$5(/$&,Ð12UGHQDROP TABLE
± 'HVWUX\HXQDUHODFLyQEDVHMXQWRFRQVXGHILQLFLyQHQHOFDWiORJR
DROP TABLE <nombre_relación> <opcion>;
<opcion>SXHGHVHU
· RESTRICT:'HVWUX\HODWDEOD VyORVLQRVHOHKDFHUHIHUHQFLDGHVGHQLQJXQD
RWUDUHODFLyQFODYHDMHQDQLHVWDEODEDVHGHXQDYLVWD
· CASCADE:(OLPLQDODWDEODMXQWRFRQ UHVWULFFLRQHV\YLVWDVTXHODUHIHUHQFLDQ
‡ (/,0,1$&,Ð1'(81(648(0$2UGHQDROP SCHEMA
± 'HVWUX\HXQHVTXHPDGH%'MXQWRFRQVXGHILQLFLyQHQHOFDWiORJR
DROP SCHEMA <nombre_esquema> <opcion>;
<opcion>SXHGHVHU
· RESTRICT:'HVWUX\HHOHVTXHPD VyORVLQRFRQWLHQHQLQJ~QHOHPHQWR
· CASCADE:(OLPLQDHOHVTXHPD\ORVWDEODVGRPLQLRV\GHPiVHOHPHQWRV
FRQWHQLGRVHQHOHVTXHPD
0RGHOR5HODFLRQDOGHGDWRV64/
''/&$7È/2*25(/$&,21$/
‡ &$7É/2*2GHXQD%DVHGH'DWRV5HODFLRQDOQXHYRFRQFHSWRHQ64/
&ROHFFLyQQRPEUDGDGHHVTXHPDVHQXQHQWRUQR64/
8([LVWHXQHVTXHPDHVSHFLDOINFORMATION_SCHEMA,FRQ
GDWRVVREUHODGHILQLFLyQGHWRGRVORVHOHPHQWRVGHWRGRVORV
HVTXHPDVH[LVWHQWHVHQHOFDWiORJR
6yORSXHGHQGHILQLUVH5HVWULFFLRQHVGH,QWHJULGDG5,5HIHUHQFLDOHWF
HQWUHUHODFLRQHVTXHH[LVWDQHQHVTXHPDVGHOPLVPRFDWiORJR
(VSRVLEOHFRPSDUWLUHOHPHQWRVGHILQLFLRQHVGHGRPLQLRHWFHQWUH
HVTXHPDVGHOPLVPRFDWiORJR
0RGHOR5HODFLRQDOGHGDWRV64/
Descargar