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H72'$6ODV5,GHILQLGDV
,QWHJULGDGLQWHUHVDQWHHQODVHWDSDVGH
‡ ',6(f2
(VWUXFWXUDVGH'DWRVFRQYHQLHQWHV
5HJODVGH,QWHJULGDGDGHFXDGDV
‡ (-(&8&,Ð1FRUUHFFLyQGHODLQIRUPDFLyQ
5,PDQWHQLGDVHQHO&DWiORJRGHO6LVWHPD
SUBSISTEMA DE INTEGRIDADGHO'%06
‡ FRQWUROGHRSHUDFLRQHVGHXVXDULRINSERTUPDATE, DELETE
‡ SDUDDVHJXUDUTXH12YLRODQODV5HJODVGH,QWHJULGDG
,QWHJULGDGHQVLVWHPDVGH%'5
5(*/$6'(,17(*5,'$'&RPSRQHQWHV
actor_cache_ok
1RPEUH
5HJODDOPDFHQDGDHQHO&DWiORJRGHO6LVWHPDEDMRHVHQRPEUH
$SDUHFHUiHQGLDJQyVWLFRVSURGXFLGRVSRUHOVLVWHPDFRPRUHVSXHVWDD
LQWHQWRVGHYLRODFLyQGHODUHJODPHQVDMHVGHHUURUDOXVXDULR
5HVWULFFLyQ
NOT EXISTS (SELECT * FROM ACTOR WHERE cache≤0)
([SUHVLyQERROHDQD127$5HVWULFFLyQGH,QWHJULGDG⊆5HJODGH,QWHJULGDG
/D5HJOD VHVDWLVIDFH ⇔ODUHVWULFFLyQHVTRUE
HVYLRODGD
⇔ODUHVWULFFLyQHVFALSE
5HVSXHVWDDXQLQWHQWRGHYLRODFLyQGHODUHJOD
,QGLFDDOVLVWHPDTXpKDFHUVLXQXVXDULRLQWHQWDXQDRSHUDFLyQTXHYLRODOD5,
3RUGHIHFWRREJECTUHFKD]DURUHKXVDU TXHLPSOLFD
à 'HVKDFHUORVSRVLEOHVGDxRVFDXVDGRVSRUODRSHUDFLyQ
 0RVWUDULQIRUPDFLyQGH GLDJQyVWLFRPHQVDMH
3RGUtDVHUXQprocedimientoGHFRPSOHMLGDGDUELWUDULD ... tratarError(...)
,QWHJULGDGHQVLVWHPDVGH%'5
5(*/$6'(,17(*5,'$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XQHUURUVLQWiFWLFR\VHGHWHFWDHQWLHPSRGHFRPSLODFLyQ
9LRODFLyQGH5,GRPLQLRVHGHWHFWDHQWLHPSRGHHMHFXFLyQDQWHVLPSRVLEOH
64/UHFKD]DWRGRLQWHQWRGHINSERTRUPDATEVREUHXQDYLVWDVLVHYLRODOD
FRQGLFLyQGHGHILQLFLyQGHODYLVWDVLVHHVSHFLILFyODRSFLyQ CHECK
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH'20,1,2
‡ '(),1,&,Ð1GHOFRQMXQWRGHYDORUHVFRPSRQHQWHVGHXQGRPLQLR
(QXPHUDFLyQGHYDORUHVSRVLEOHV(‘marron’,‘gris’,‘azul’,‘verde’,‘negro’,‘no-se’)
([SUHVLyQGHGHILQLFLyQcache > 0 AND cache < 100
‡ &5($&,Ð1'(81'20,1,2
CREATE DOMAIN <nomDominio> [ AS ] <tipoDatos>
[ DEFAULT <valor> ]
[ [ CONSTRAINT <nomRestricción> ] CHECK (<condición>) ]+ ;
GRQGHvalor = { literal | funciónSinArgum | NULL }
\funciónSinArgumHV USER yCURRENT_USER DXWKRUL]DWLRQ,'DFWXDO
SESSION_USER
DXWKRUL]DWLRQ,'GHODVHVLyQ64/DFWXDO
SYSTEM_USER
,'GHOXVXDULRGHO62DFWXDO
CURRENT_DATE
IHFKDGHOGtD
CURRENT_TIME
KRUDDFWXDO
CURRENT_TIMESTAMP PDUFDGHWLHPSRDFWXDO
,' LGHQWLILFDGRU
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH'20,1,2
‡ /D5,HVHVSHFLILFDGDFRPRSDUWHGHODVHQWHQFLDGHGHILQLFLyQGHOGRPLQLR
CREATE DOMAIN Color AS VARCHAR(10)
DEFAULT ‘no-se’
→ CONSTRAINT color_valido
CHECK ( VALUE IN ( ‘marron’,‘gris’,‘azul’,‘verde’,‘negro’,‘no-se’ ) );
CREATE DOMAIN SEXO CHAR(1)
CONSTRAINT valor_sex_ok CHECK ( VALUE IN (‘M’, ‘F’) ) ;
- CREATE DOMAIN SEXO AS CHAR(1) NOT NULL ££,1&255(&72
CREATE DOMAIN SEXO AS CHAR(1) CHECK (VALUE IS NOT NULL)££&255(&72
,(QODHMHFXFLyQORVGRPLQLRVQRFDPELDQVLQRORVDWULEXWRVVXVYDORUHV
ª XQD5,'RPLQLRµSHUVH¶QXQFDVHFKHTXHDVLQROD5,$WULEXWRFRUUHVSRQGLHQWH
‡ $/7(5$&,Ð1'(81'20,1,2
ALTER DOMAIN<nomdominio>
3HUPLWH$xDGLU(OLPLQDU5HVWULFFLRQHVGH'RPLQLR\9DORUSRU'HIHFWR
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH'20,1,2
‡ (/,0,1$&,Ð1'(81'20,1,2H[LVWHQWH
DROP DOMAIN <nomDominio> { RESTRICT | CASCADE } ;
RESTRICT
/DHOLPLQDFLyQIDOODVLHO GRPLQLRHVUHIHUHQFLDGRHQFXDOTXLHU
9GHILQLFLyQGHFROXPQDR
9GHILQLFLyQGHYLVWDR
95HVWULFFLyQGH,QWHJULGDG
(QRWURFDVRWHQGUip[LWRHOGHVFULSWRUGHOGRPLQLRHVHOLPLQDGRGHOFDWiORJR
CASCADE
7RGD5,\YLVWDFX\DGHILQLFLyQUHIHUHQFLHDOGRPLQLRHVHOLPLQDGDFRQpO
&DGDFROXPQDGHILQLGDVREUHWDOGRPLQLR12HVHOLPLQDGDVLQRDOWHUDGD
 'HILQLGDGLUHFWDPHQWHVREUHHO7LSRGH'DWRVVXE\DFHQWHDOGRPLQLR
 6LQRWLHQH'()$8/7H[SOtFLWRWRPDHOGHOGRPLQLRVLpVWHORWHQtD
 +HUHGDWRGD5HVWULFFLyQGH,QWHJULGDGDVRFLDGDDOGRPLQLR
FRQYHUWLGDHQXQD5HVWULFFLyQGH7DEOD%DVH
VXVWLWX\HQGR³VALUE´SRUHOQRPEUHGHODFROXPQD
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
‡ 5(675,&&,Ð1$62&,$'$$81$7$%/$%$6(HVSHFtILFD
(VWRQRVLJQLILFDTXHQR SXHGDDIHFWDUDPXFKDVWDEODVEDVHVLQRTXH
à OD5,HVWiGHILQLGDFRPRSDUWHGHODGHILQLFLyQGHODWDEOD
à OD5,QRH[LVWHVLODWDEODDVRFLDGDQRH[LVWH\
à HOLPLQDUODWDEODLPSOLFDHOLPLQDUOD5,
‡ 7RGD5,WDEODSXHGHH[SUHVDUVHFRPRXQD5,JHQHUDO
VDOYRODSDUWHGHXQD5,GHFODYHH[WHUQDTXHLQGLFDODDFFLyQUHIHUHQFLDO
‡ 5,(63(&,),&$'$'(1752'(CREATE TABLE
CREATE TABLE <nombreTabla> ( <lista de elementos de tabla base> ) ;
GRQGHORVelementosSXHGHQVHU
'HILQLFLyQGH&ROXPQD
'HILQLFLyQGH5HVWULFFLyQ7DEODSUHFHGLGDRQRSRUCONSTRAINT <nomRestric>
5HVWULFFLyQGH&ODYH&DQGLGDWD
5HVWULFFLyQGH&ODYH([WHUQD
5HVWULFFLyQCHECK
‡ 5,$f$','$(/,0,1$'$'(1752'(ALTER TABLE
<nombreTabla>
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
'HILQLFLyQGH&ROXPQD5,GH$WULEXWR
‡ (VSHFLILFDFLyQGHOGRPLQLRGHODWULEXWR\RWUDV5,GHDWULEXWR
‡ 1RQHFHVDULDVHQWHQFLDGHFUHDFLyQH[SOtFLWDIRUPDSDUWHGHODGHILQLFLyQGH
DWULEXWRGHQWURGHODVHQWHQFLDGHFUHDFLyQGHODWDEOD
CREATE TABLE ACTOR
VARCHAR(30) NOT NULL,
( nombre
INT(9)
NOT NULL DEFAULT 20000,
cache
NOT NULL,
colorOjos Color
CHAR(4), ...) ;
agencia
‡ 7RGD5,$WULEXWRHVFRPSUREDGDLQPHGLDWDPHQWH
WRGRLQWHQWRGHINSERTRUPDATEVREUHHODWULEXWRGHYDORU ∉GRPLQLR
HVUHFKD]DGRDOLQVWDQWH
ª /DUHVSXHVWDDXQLQWHQWRGHYLRODFLyQVLHPSUHHV REJECT (rechazo)
‡ /DFRPSUREDFLyQGHODUHVWULFFLyQHVGHULYDGDKDFLDODFRPSUREDFLyQ
GHODUHVWULFFLyQGHOGRPLQLRGHODWULEXWRVLH[LVWH
‡ 8QD5,$WULEXWRVHGHVWUX\HDOHOLPLQDUHODWULEXWRGHODUHODFLyQEDVH
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
'HILQLFLyQGH&ODYH&DQGLGDWD&.
‡ &ODYH3ULPDULD3.
‡ &ODYH$OWHUQDWLYD$.
PRIMARY KEY (<listaAtributos>)
LQFOX\H5,(QWLGDG
UNIQUE (<listaAtributos>)
'HILQLFLyQGH&ODYH([WHUQD).
‡ ,QWHUUHODFLyQHQWUHGRVUHODFLRQHVGLVWLQWDVGHOD%DVHGH'DWRV
FOREIGN KEY (<ListaAtributos>) REFERENCES <nomTabla> (<listaAtributos>)
[ ON DELETE { NO ACTION | CASCADE | SET DEFAULT | SET NULL } ]
[ ON UPDATE { NO ACTION | CASCADE | SET DEFAULT | SET NULL } ]
↑DFFLRQHVUHIHUHQFLDOHVSRVLEOHV
‡ &XDOTXLHUFROXPQDRFRPELQDFLyQGHFROXPQDV SXHGHVHUXQDFODYHH[WHUQD
‡ 64/SHUPLWHTXH ).UHIHUHQFLHDFXDOTXLHUFODYHFDQGLGDWDQRVyOR3.V
‡ 5HSUHVHQWDODUHIHUHQFLDGHVGHILODGH7DXQD&.GHRWUDILODHQRWUDWDEOD7
ª(OSUREOHPDGHDVHJXUDUTXHFDGD7).WLHQHXQYDORUGH7&.
H[LVWHQWHHVHOSUREOHPDGHODLQWHJULGDGUHIHUHQFLDO
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
‡ 3XHGHQH[LVWLUDXWRUHIHUHQFLDV\FLFORVUHIHUHQFLDOHV
‡ 64/SHUPLWH £SRUVXSXHVWRTXHXQD).SXHGDFRQWHQHUHOYDORU18//
± VDOYRVLVHLQGLFDNOT NULLH[SOtFLWDPHQWHSDUDOD).HQHO CREATE TABLE
‡ /D).\OD&.DODTXHUHIHUHQFLDGHEHQ
± FRQWHQHUHOPLVPRQžGHFRPSRQHQWHVFROXPQDVRDWULEXWRV\
± pVWRVHVWDUGHILQLGRVVREUHORV PLVPRVGRPLQLRV
‡ 6L).QRHVFRPSXHVWD~QLFRDWULEXWRQRHVQHFHVDULDFOiXVXOD)25(,*1.(<
CREATE TABLE ACTUA_EN
CREATE TABLE ACTUA_EN
( actor ... ,
( actor ... REFERENCES ACTOR(codA) ...,
film ... ,
film ... REFERENCES PELICULA(codP) ...,
papel ... ,
papel ... ,
PRIMARY KEY (actor, film),
PRIMARY KEY (actor, film)
FOREIGN KEY (actor)
... ) ;
REFERENCES ACTOR(codA) ...,
FOREIGN KEY (film) REFERENCES PELICULA(codP)
... ) ;
,12FRPSUREDGDLQPHGLDWDPHQWHGLIHULGDKDVWDILQGHWUDQVDFFLyQ COMMIT
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
$&&,21(65()(5(1&,$/(6UHFRUGDWRULR
² 0DQWHQLPLHQWRGH,QWHJULGDG5HIHUHQFLDOVLQUHFKD]DUDFWXDOL]DFLRQHVTXHODYLROHQ
± (MHFXFLyQGHRSHUDFLRQHVDGLFLRQDOHVTXHGHMDQOD %'HQHVWDGRFRQVLVWHQWH
ON DELETE...
± ,QGLFDOD 5HJODGH%RUUDGRSDUDILODVGH 7WDEODUHIHUHQFLDGD UHVSHFWRGH 7).
4XpRFXUUHVLVHLQWHQWDERUUDUXQDILODUGH7ILODREMHWLYRRWDUJHWURZ\H[LVWH
DOJXQDILODUHQ7TXHOHKDFHUHIHUHQFLDHGFRQW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DFRPSRQHQWHVPRGLILFDGRVHQ 7&.VHSRQHQD
QXORHQWRGDVODVILODVU
<VHDFWXDOL]DU
ª7DOHVFRPSRQHQWHVGHOD).GHEHWHQHU nulos permitidos
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
&203257$0,(172$17(23(5$&,21(6GH0$1,38/$&,Ð1GH'$726
6HDT2.FKXQDFODYHH[WHUQDKDFLDT1.CK
6LODGHILQLFLyQGHFK12LQFOX\HFOiXVXODON DELETEQLON UPDATE...
1.INSERTHQ7RUPDATEHQ7).WDOTXHHOYDORUGH 7).QRH[LVWHHQ7&.
⇒5HFKD]R12$&7,21
2.DELETEHQ7RUPDATEHQ7&.VLDOJXQDWXSODGH7OHKDFHUHIHUHQFLD
⇒5HFKD]R12$&7,21
6LODGHILQLFLyQGHFKLQFOX\HFOiXVXODON DELETERELHQON UPDATE...
1.INSERTHQ7RUPDATEHQ7).WDOTXHHOYDORUGH 7).QRH[LVWHHQ7&.
⇒5HFKD]R12$&7,21
2.DELETEHQ7RUPDATEHQ7&.VLDOJXQDWXSODGH7OHKDFHUHIHUHQFLD
⇒'HSHQGHGHODDFFLyQUHIHUHQFLDOHVSHFLILFDGDHQODVFOiXVXODV
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDGGH7$%/$%$6(
'HILQLFLyQGH5HVWULFFLyQ&+(&.
‡ 5HJODTXHVHUHILHUH~QLFDPHQWHDXQDUHODFLyQ
‡ 3XHGHHVSHFLILFDUUHVWULFFLRQHVDGLFLRQDOHVSDUDDOJ~QDWULEXWR
(OFDFKHGHXQDFWRUVLHPSUHHVWiHQWUH\SWDV
CREATE TABLE ACTOR ( ...,
CONSTRAINT actor_cache_ok
CHECK ( cache > 50000 AND cache < 200000 ), ... );
‡ 3XHGHHVSHFLILFDUUHVWULFFLRQHVTXHLQYROXFUDQYDULRVDWULEXWRVGHODUHODFLyQ
/DIHFKDGHHVWUHQRGHXQDSHOtFXODGHEHVHUSRVWHULRUDODIHFKDGH
ILQDOL]DFLyQGHOURGDMHGHODPLVPD
CREATE TABLE PELICULA ( ...,
CONSTRAINT pelicula_fechas_ok
CHECK ( fecha_fin_rodaje < fecha_estreno ), ... );
,7RGD5,7DEOD%DVHHVFRPSUREDGDLQPHGLDWDPHQWH
WRGDRSHUDFLyQGHPRGLILFDFLyQVREUHODUHODFLyQLQFOX\HFRQFHSWXDOPHQWH
HOFKHTXHRGHWRGDVVXV5, FRPRXQpaso final de dicha operaciónDFFLyQ
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDG*(1(5$/(6
‡ (OHPHQWRGHODEDVHGHGDWRVLQGHSHQGLHQWHGHWDEODV\DH[LVWHQWHV
‡ (VSHFLILFD5HVWULFFLRQHVGH,QWHJULGDGTXH12VRQ
9GHFODYHFDQGLGDWD(primaria o alternativa)
9GHLQWHJULGDGUHIHUHQFLDO(clave ajena -externa o foránea-)
‡ 5HVWULFFLyQGHFRPSOHMLGDGDUELWUDULDTXHLQFOX\HFXDOTXLHUQžGHFROXPQDV
GHFXDOTXLHUQžGHUHODFLRQHVEDVH
‡ &DGDDVHUWRWLHQHXQQRPEUH\FRQVWDGHXQDFRQGLFLyQFOiXVXODCHECK
‡ &5($&,Ð1'(81$5(675,&&,Ð1*(1(5$/$VHUFLyQR$VHUWR
CREATE ASSERTION <nomRestricción>
CHECK ( <condición> ) ;
QRPEUHGHUHVWULFFLyQ REOLJDWRULR
± 6LXQDWXSODGHOD%'KDFHTXHcondición )$/6(ODUHVWULFFLyQKDEUi
VLGRYLRODGD
² 8QHVWDGRGHOD%'VDWLVIDFHXQDUHVWULFFLyQVLQLQJXQDFRPELQDFLyQGH
WXSODVGHGLFKRHVWDGRYLRODODUHVWULFFLyQ
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDG*(1(5$/(6
• Normalmente, condición se expresa “en negativo”:
7RGR;VDWLVIDFH<≡1LQJ~Q;VDWLVIDFH12<
7RGRDFWRUUHSUHVHQWDGRSRUODDJHQFLDGHEHFREUDURPiV
CREATE ASSERTION age1_cache_ok
CHECK (NOT EXISTS (SELECT * FROM ACTOR
WHERE agencia=1 AND cache<50000)) ;
1LQJXQDDJHQFLDUHSUHVHQWDDPiVGHDFWRUHV
CREATE ASSERTION num_actores_age_ok
CHECK (NOT EXISTS (SELECT * FROM AGENCIA
WHERE 40 < (SELECT SUM(*) FROM ACTOR
WHERE codAge=agencia ) ) ) ;
/RVDFWRUHVQRSURWDJRQLVWDVGHXQDSHOtFXODQRSXHGHQFREUDUPiVTXHORV
SURWDJRQLVWDV
CREATE ASSERTION paga_actores_ok
CHECK (NOT EXISTS (SELECT * FROM ACTUA_EN ACT, ACTUA_EN PROTA
WHERE ACT.film=PROTA.film AND ACT.actor<>PROTA.actor
AND PROTA.papel=‘protagonista’ AND ACT.paga>PROTA.paga ));
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDG*(1(5$/(6
'HEHGHH[LVWLUDOPHQRVXQDGLVWULEXLGRUDGHSHOtFXODV
CREATE ASSERTION existencia_distribuidora
CHECK ( 0 < SELECT COUNT (*) FROM DISTRIBUIDORA ) ;
127$VREUHHVWHDVHUWR
'HEHFUHDUVHXQDYH]TXH\DH[LVWDDOJXQDWXSODHQ DISTRIBUIDORA
2SHUDFLyQDELETESXHGHRQRYLRODUODSHURQXQFDORKDUiXQINSERT
(OFyGLJRGHFDGDJXLyQHV~QLFR≡VLKD\GRVJXLRQHVFRQLJXDOFyGLJRVRQHOPLVPR
CREATE ASSERTION guion_codigo_unico
NOT EXISTS (SELECT * FROM GUION G1, GUION G2
WHERE G1.codG = G2. codG
AND NOT (G1.titulo=G2.titulo AND G1.descrip= G2.descrip));
127$VREUHHVWHDVHUWR
(TXLYDOHDHVSHFLILFDUUNIQUE( codG ) HQHO CREATE TABLE
,QWHJULGDGHQVLVWHPDVGH%'5
64/5HJODVGH,QWHJULGDG*(1(5$/(6
/RVDFWRUHV\SHOtFXODVDQRWDGRVHQODUHODFLyQACTUA_ENGHEHQH[LVWLU
CREATE ASERTION actor_actuaen_pelicula
CHECK (NOT EXISTS (SELECT *
FROM ACTUA_EN AE, ACTOR A, PELICULA P
WHERE NOT (AE.actor = A.codA AND AE.film = P.codP) ) ;
127$VREUHHVWHDVHUWR
(TXLYDOHDHVSHFLILFDU
FOREIGN KEY (actor) REFERENCES ACTOR(codA)...
FOREIGN KEY (film) REFERENCES PELICULA(codP)...
\
HQHO CREATE TABLE
‡ (/,0,1$&,Ð1'(81$5(675,&&,Ð1*(1(5$/
DROP ASSERTION <nomRestricción> ;
RMR6,1RSFLyQRESTRICTRCASCADE
,QWHJULGDGHQVLVWHPDVGH%'5
$QWLJXDVYHUVLRQHVGH64/'LVSDUDGRUHV
‡ (QPXFKRVFDVRVFRQYLHQHHVSHFLILFDUOD$&&,Ð1TXHHMHFXWDU
GLVSDUDUWUDVODYLRODFLyQGHXQDUHVWULFFLyQ
$ERUWDUODWUDQVDFFLyQTXHSURYRFDODYLRODFLyQR
,QIRUPDUGHHOORDOXVXDULRPHQVDMHR
(MHFXWDUFLHUWRSURFHGLPLHQWRR
'LVSDUDURWUDVDFWXDOL]DFLRQHV
ª (VWRVHFRQVLJXHPHGLDQWHORV 75,**(5V
75,**(5'LVSDUDGRU
DEFINE TRIGGER <nombre> ON <lista tablas> :
<condición> ACTION_PROCEDURE <llamada procedimiento>
‡ (VSHFLILFDXQDFRQGLFLyQ\XQDDFFLyQTXHUHDOL]DUVLODFRQGLFLyQVHFXPSOH
‡ /DHMHFXFLyQGHO7ULJJHUVHFRQVLGHUDSDUWHGHODRSHUDFLyQTXH
LQWHQWyYLRODUOD5,
,QWHJULGDGHQVLVWHPDVGH%'5
$QWLJXDVYHUVLRQHVGH64/'LVSDUDGRUHV
'LVSDUDGRUTXHVLDOJ~QDFWRUHQXQDSHOtFXODSHUFLEHXQDSDJDPD\RUTXHOD
GHXQSURWDJRQLVWDSLGDFRQILUPDFLyQSDUDSHUPLWLUGLFKRHVWDGRRQR
DEFINE TRIGGER Disp_Paga ON ACTUA_EN ACT, ACTUA_EN PROTA:
ACT.actor<>PROTA.actor AND ACT.film=PROTA.film
AND PROTA.papel=“protagonista” AND ACT.paga > PROTA.paga
ACTION_PROCEDURE SolicitarAprobacion(...) ;
‡ 'LIHUHQFLDHQWUHASSERTION y TRIGGER
ASSERTIONSURKLEHUHDOL]DUXQDDFWXDOL]DFLyQTXHYLRODHODVHUWR
HVGHFLUTXHKDFHFALSEODFRQGLFLyQ
TRIGGERSXHGHSHUPLWLUODDFWXDOL]DFLyQTXHFXPSOHODFRQGLFLyQ
HVGHFLUTXHYLRODXQD5, SHURHMHFXWDODDFFLyQTXHSXHGH
UHSDUDUODYLRODFLyQGHMDQGRFRQVLVWHQWHOD%'
ª/DVFRQGLFLRQHVHVSHFLILFDGDVHQXQD\RWUDVRQLQYHUVDV
‡ 'LVSDUDGRUHVFRPELQDQORVHQIRTXHVGHFODUDWLYR\SURFHGLPHQWDO
/DFRQGLFLyQGHOGLVSDUDGRUHV GHFODUDWLYD
6XDFFLyQRSHUDSRU SURFHGLPLHQWRV
,QWHJULGDGHQVLVWHPDVGH%'5
64/&20352%$&,Ð1'(5(675,&&,21(6
‡ 1RUPDOPHQWHHO VLVWHPDFRPSUXHEDXQD5,LQPHGLDWDPHQWHFRPR~OWLPRSDVR
GHODHMHFXFLyQGHXQDRSHUDFLyQRVHQWHQFLD64/
‡ 6LOD5,HVYLRODGDODVHQWHQFLDVHFDQFHOD\VXHIHFWRVREUHOD%'HVQXOR
‡ $YHFHVHVQHFHVDULRTXHFLHUWDVUHVWULFFLRQHVQRVHDQFKHTXHDGDVKDVWD
SDVDGRXQWLHPSRVLVHFRPSUREDUDQGHLQPHGLDWRVLHPSUHIDOODUtDQ
(MHPSORFLFORUHIHUHQFLDO T1
T2
CREATE TABLE T1
( atrib1 ... ,
FOREIGN KEY (atrib1)
REFERENCES T2 (...) ... ,
... ) ;
,QLFLDOPHQWHT1\T2HVWiQYDFtDV
CREATE TABLE T2
( atrib2 ... ,
FOREIGN KEY (atrib2)
REFERENCES T1 (...) ... ,
... ) ;
&RQ&KHTXHR,QPHGLDWRGHODV5,5HIHUHQFLDOWRGDLQVHUFLyQGHILODHQFXDOTXLHUWDEOD
IDOODUtDSXHVQXQFDHQFRQWUDUtDODILODWDUJHWHQODRWUDWDEOD
,QWHJULGDGHQVLVWHPDVGH%'5
64/&20352%$&,Ï1'(5(675,&&,21(6
‡ (QXQPRPHQWRGDGRGHQWURGHFLHUWD WUDQVDFFLyQ64/WRGD5,GHEHHVWDUHQPRGR
- INMEDIATEGHEHVHUFKHTXHDGDLQPHGLDWDPHQWH R
- DEFERREDVHUiFKHTXHDGDDOILQDOGHODWUDQVDFFLyQ
‡ 7RGDGHILQLFLyQGH5HVWULFFLyQGH,QWHJULGDGSXHGHLQFOXLUDOILQDOODFOiXVXOD
[ INITIALLY { DEFERRED | IMMEDIATE } ]
[ [ NOT ] DEFERRABLE ]
INITIALLY DEFERREDRINITIALLY IMMEDIATEHVSHFLILFDQHOPRGRLQLFLDOGHOD5,
GHVSXpVGHVHUGHILQLGDOD5,\DOFRPLHQ]RGHFDGDWUDQVDFFLyQ 64/
'()(55$%/(R127'()(55$%/(LQGLFDVLOD5,SXHGHSDVDUDPRGR'()(55('RQR
1RWD(QHOHVWiQGDU64/
 5HVWULFFLRQHVGH'RPLQLRQRWLHQHVHQWLGRXQFKHTXHRGLIHULGR
 5HVWULFFLRQHVNOT NULL\GH&ODYH&DQGLGDWDGHEHQVHU NOT DEFERRABLE
,QWHJULGDGHQVLVWHPDVGH%'5
64/&20352%$&,Ï1'(5(675,&&,21(6
‡ 6LQRVHLQGLFD,1,7,$//<'()(55('QL,1,7,$//<,00(',$7(VXSRQH ,1,7,$//<,00(',$7(
‡ 6LVHLQGLFD,1,7,$//<,00(',$7(RVHVXSRQH
6LQRVHLQGLFD'()(55$%/(QL127'()(55$%/(VHVXSRQH 127'()(55$%/(
6LVHLQGLFD,1,7,$//<'()(55(' QRSXHGHLQGLFDUVH127'()(55$%/(
3XHGHSRQHUVH'()(55$%/(DXQTXHVHVXSRQH
‡ 6HQWHQFLD SET CONSTRAINTS
SET CONSTRAINTS { <listaRestricciones> | ALL } { DEFERRED | IMMEDIATE }
(VWDEOHFHHOPRGRSDUDUHVWULFFLRQHVUHVSHFWRGHODWUDQVDFFLyQ64/DFWXDO
7RGD5, PHQFLRQDGD GHEHVHUDEFERRABLE ⇒ALLVLJQLILFD WRGDVODV5,GHIHUUDEOHV
· DEFERREDKDFHTXHWRGDVODV5,SDVHQDPRGRDEFERRED
· IMMEDIATEKDFHTXHSDVHQDPRGRIMMEDIATE\VRQFRPSUREDGDV
VLHOFKHTXHRGHDOJXQD5,IDOODSET CONSTRAINTSIDOOD\
QLQJXQD5,FDPELDGHPRGR
• COMMIT⇒SET CONSTRAINTS ALL IMMEDIATE
6LXQDFRPSUREDFLyQGH5,IDOOD⇒COMMITIDOOD⇒WUDQVDFFLyQFRPSOHWDIDOODrolledback
,QWHJULGDGHQVLVWHPDVGH%'5
Descargar