Sei sulla pagina 1di 10

CREATE TABLE Author ( AuthorID Name Class );

CHAR(18) NOT NULL , CHAR(18) NULL , CHAR(18) NULL

CREATE UNIQUE INDEX XPKAuthor ON Author (AuthorID ASC); ALTER TABLE Author ADD CONSTRAINT XPKAuthor PRIMARY KEY (AuthorID); CREATE TABLE Book ( BookID AuthorID PublicationID Description price );

CHAR(18) CHAR(18) CHAR(18) CHAR(18) CHAR(18)

NOT NULL , NOT NULL , NOT NULL , NULL , NULL

CREATE UNIQUE INDEX XPKBook ON Book (BookID ASC,AuthorID ASC,PublicationID

ASC);

ALTER TABLE Book ADD CONSTRAINT XPKBook PRIMARY KEY (BookID,AuthorID,PublicationID); CREATE TABLE Customer ( Customer_ID Name Age Gender Address );

INT NOT NULL , CHAR(18) NULL , INT NULL , BINARY_DOUBLE NULL , CHAR(18) NULL

CREATE UNIQUE INDEX XPKCustomer ON Customer (Customer_ID ASC); ALTER TABLE Customer ADD CONSTRAINT XPKCustomer PRIMARY KEY (Customer_ID); CREATE TABLE Order ( OrderrID BookID Customer_ID AuthorID PublicationID );

CHAR(18) NOT CHAR(18) NOT INT NOT NULL CHAR(18) NOT CHAR(18) NOT

NULL , NULL , , NULL , NULL

CREATE UNIQUE INDEX XPKOrder ON Order (OrderrID ASC,Customer_ID ASC,BookID C);

ASC,AuthorID

ASC,PublicationID

AS

ALTER TABLE Order ADD CONSTRAINT XPKOrder PRIMARY KEY (OrderrID,Customer_ID,BookID,Author ID,PublicationID);

CREATE TABLE Publication ( PublicationID Name Location );

CHAR(18) NOT NULL , CHAR(18) NULL , CHAR(18) NULL

CREATE UNIQUE INDEX XPKPublication ON Publication (PublicationID ASC); ALTER TABLE Publication ADD CONSTRAINT XPKPublication PRIMARY KEY (PublicationID); ALTER TABLE Book ADD (CONSTRAINT Author_writes_books FOREIGN KEY (AuthorID) REFERENCES Au thor (AuthorID)); ALTER TABLE Book ADD (CONSTRAINT Publishes_books FOREIGN KEY (PublicationID) REFERENCES P ublication (PublicationID)); ALTER TABLE Order ADD (CONSTRAINT Customer_orders FOREIGN KEY (Customer_ID) REFERENCES Cus tomer (Customer_ID)); ALTER TABLE Order ADD (CONSTRAINT Book_ordered FOREIGN KEY (BookID, AuthorID, PublicationI D) REFERENCES Book (BookID, AuthorID, PublicationID)); CREATE TRIGGER tD_Author AFTER DELETE ON Author for each row -- ERwin Builtin Trigger -- DELETE trigger on Author DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Author Book on parent delete restrict */ /* ERWIN_RELATION:CHECKSUM="0000d3c1", PARENT_OWNER="", PARENT_TABLE="Author " CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Author_writes_books", FK_COLUMNS="AuthorID" */ SELECT count(*) INTO NUMROWS FROM Book WHERE /* %JoinFKPK(Book,:%Old," = "," AND") */ Book.AuthorID = :old.AuthorID; IF (NUMROWS > 0) THEN raise_application_error( -20001, 'Cannot delete Author because Book exists.' ); END IF; -- ERwin Builtin Trigger END; /

CREATE TRIGGER tU_Author AFTER UPDATE ON Author for each row -- ERwin Builtin Trigger -- UPDATE trigger on Author DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Author Book on parent update restrict */ /* ERWIN_RELATION:CHECKSUM="0000ffde", PARENT_OWNER="", PARENT_TABLE="Author" CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Author_writes_books", FK_COLUMNS="AuthorID" */ IF /* %JoinPKPK(:%Old,:%New," <> "," OR ") */ :old.AuthorID <> :new.AuthorID THEN SELECT count(*) INTO NUMROWS FROM Book WHERE /* %JoinFKPK(Book,:%Old," = "," AND") */ Book.AuthorID = :old.AuthorID; IF (NUMROWS > 0) THEN raise_application_error( -20005, 'Cannot update Author because Book exists.' ); END IF; END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tD_Book AFTER DELETE ON Book for each row -- ERwin Builtin Trigger -- DELETE trigger on Book DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Book Order on parent delete restrict */ /* ERWIN_RELATION:CHECKSUM="000106a3", PARENT_OWNER="", PARENT_TABLE="Book" CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Book_ordered", FK_COLUMNS="BookID""AuthorID""PublicationID" * / SELECT count(*) INTO NUMROWS FROM Order WHERE /* %JoinFKPK(Order,:%Old," = "," AND") */ Order.BookID = :old.BookID AND Order.AuthorID = :old.AuthorID AND Order.PublicationID = :old.PublicationID; IF (NUMROWS > 0) THEN raise_application_error( -20001, 'Cannot delete Book because Order exists.' );

END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tI_Book BEFORE INSERT ON Book for each row -- ERwin Builtin Trigger -- INSERT trigger on Book DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Publication Book on child insert restrict */ /* ERWIN_RELATION:CHECKSUM="0001f842", PARENT_OWNER="", PARENT_TABLE="Public ation" CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Publishes_books", FK_COLUMNS="PublicationID" */ SELECT count(*) INTO NUMROWS FROM Publication WHERE /* %JoinFKPK(:%New,Publication," = "," AND") */ :new.PublicationID = Publication.PublicationID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */ NUMROWS = 0 ) THEN raise_application_error( -20002, 'Cannot insert Book because Publication does not exist.' ); END IF; /* ERwin Builtin Trigger */ /* Author Book on child insert restrict */ /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="Author " CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Author_writes_books", FK_COLUMNS="AuthorID" */ SELECT count(*) INTO NUMROWS FROM Author WHERE /* %JoinFKPK(:%New,Author," = "," AND") */ :new.AuthorID = Author.AuthorID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */ NUMROWS = 0 ) THEN raise_application_error( -20002, 'Cannot insert Book because Author does not exist.' ); END IF;

-- ERwin Builtin Trigger END; / CREATE TRIGGER tU_Book AFTER UPDATE ON Book for each row -- ERwin Builtin Trigger -- UPDATE trigger on Book DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Book Order on parent update restrict */ /* ERWIN_RELATION:CHECKSUM="000357de", PARENT_OWNER="", PARENT_TABLE="Book" CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Book_ordered", FK_COLUMNS="BookID""AuthorID""PublicationID" * / IF /* %JoinPKPK(:%Old,:%New," <> "," OR ") */ :old.BookID <> :new.BookID OR :old.AuthorID <> :new.AuthorID OR :old.PublicationID <> :new.PublicationID THEN SELECT count(*) INTO NUMROWS FROM Order WHERE /* %JoinFKPK(Order,:%Old," = "," AND") */ Order.BookID = :old.BookID AND Order.AuthorID = :old.AuthorID AND Order.PublicationID = :old.PublicationID; IF (NUMROWS > 0) THEN raise_application_error( -20005, 'Cannot update Book because Order exists.' ); END IF; END IF; /* ERwin Builtin Trigger */ /* Publication Book on child update restrict */ /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="Publicat ion" CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Publishes_books", FK_COLUMNS="PublicationID" */ SELECT count(*) INTO NUMROWS FROM Publication WHERE /* %JoinFKPK(:%New,Publication," = "," AND") */ :new.PublicationID = Publication.PublicationID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */ NUMROWS = 0 ) THEN raise_application_error( -20007, 'Cannot update Book because Publication does not exist.'

); END IF; /* ERwin Builtin Trigger */ /* Author Book on child update restrict */ /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="Author" CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Author_writes_books", FK_COLUMNS="AuthorID" */ SELECT count(*) INTO NUMROWS FROM Author WHERE /* %JoinFKPK(:%New,Author," = "," AND") */ :new.AuthorID = Author.AuthorID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */ NUMROWS = 0 ) THEN raise_application_error( -20007, 'Cannot update Book because Author does not exist.' ); END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tD_Customer AFTER DELETE ON Customer for each row -- ERwin Builtin Trigger -- DELETE trigger on Customer DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Customer Order on parent delete restrict */ /* ERWIN_RELATION:CHECKSUM="0000de81", PARENT_OWNER="", PARENT_TABLE="Custom er" CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Customer_orders", FK_COLUMNS="Customer_ID" */ SELECT count(*) INTO NUMROWS FROM Order WHERE /* %JoinFKPK(Order,:%Old," = "," AND") */ Order.Customer_ID = :old.Customer_ID; IF (NUMROWS > 0) THEN raise_application_error( -20001, 'Cannot delete Customer because Order exists.' ); END IF; -- ERwin Builtin Trigger END;

/ CREATE TRIGGER tU_Customer AFTER UPDATE ON Customer for each row -- ERwin Builtin Trigger -- UPDATE trigger on Customer DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Customer Order on parent update restrict */ /* ERWIN_RELATION:CHECKSUM="00010534", PARENT_OWNER="", PARENT_TABLE="Customer " CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Customer_orders", FK_COLUMNS="Customer_ID" */ IF /* %JoinPKPK(:%Old,:%New," <> "," OR ") */ :old.Customer_ID <> :new.Customer_ID THEN SELECT count(*) INTO NUMROWS FROM Order WHERE /* %JoinFKPK(Order,:%Old," = "," AND") */ Order.Customer_ID = :old.Customer_ID; IF (NUMROWS > 0) THEN raise_application_error( -20005, 'Cannot update Customer because Order exists.' ); END IF; END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tI_Order BEFORE INSERT ON Order for each row -- ERwin Builtin Trigger -- INSERT trigger on Order DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Book Order on child insert restrict */ /* ERWIN_RELATION:CHECKSUM="00021331", PARENT_OWNER="", PARENT_TABLE="Book" CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Book_ordered", FK_COLUMNS="BookID""AuthorID""PublicationID" * / SELECT count(*) INTO NUMROWS FROM Book WHERE /* %JoinFKPK(:%New,Book," = "," AND") */ :new.BookID = Book.BookID AND :new.AuthorID = Book.AuthorID AND :new.PublicationID = Book.PublicationID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */

NUMROWS = 0 ) THEN raise_application_error( -20002, 'Cannot insert Order because Book does not exist.' ); END IF; /* ERwin Builtin Trigger */ /* Customer Order on child insert restrict */ /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="Custom er" CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Customer_orders", FK_COLUMNS="Customer_ID" */ SELECT count(*) INTO NUMROWS FROM Customer WHERE /* %JoinFKPK(:%New,Customer," = "," AND") */ :new.Customer_ID = Customer.Customer_ID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */ NUMROWS = 0 ) THEN raise_application_error( -20002, 'Cannot insert Order because Customer does not exist.' ); END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tU_Order AFTER UPDATE ON Order for each row -- ERwin Builtin Trigger -- UPDATE trigger on Order DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Book Order on child update restrict */ /* ERWIN_RELATION:CHECKSUM="000212e3", PARENT_OWNER="", PARENT_TABLE="Book" CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Book_ordered", FK_COLUMNS="BookID""AuthorID""PublicationID" * / SELECT count(*) INTO NUMROWS FROM Book WHERE /* %JoinFKPK(:%New,Book," = "," AND") */ :new.BookID = Book.BookID AND :new.AuthorID = Book.AuthorID AND :new.PublicationID = Book.PublicationID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */

NUMROWS = 0 ) THEN raise_application_error( -20007, 'Cannot update Order because Book does not exist.' ); END IF; /* ERwin Builtin Trigger */ /* Customer Order on child update restrict */ /* ERWIN_RELATION:CHECKSUM="00000000", PARENT_OWNER="", PARENT_TABLE="Customer " CHILD_OWNER="", CHILD_TABLE="Order" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Customer_orders", FK_COLUMNS="Customer_ID" */ SELECT count(*) INTO NUMROWS FROM Customer WHERE /* %JoinFKPK(:%New,Customer," = "," AND") */ :new.Customer_ID = Customer.Customer_ID; IF ( /* %NotnullFK(:%New," IS NOT NULL AND") */ NUMROWS = 0 ) THEN raise_application_error( -20007, 'Cannot update Order because Customer does not exist.' ); END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tD_Publication AFTER DELETE ON Publication for each row -- ERwin Builtin Trigger -- DELETE trigger on Publication DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Publication Book on parent delete restrict */ /* ERWIN_RELATION:CHECKSUM="0000d169", PARENT_OWNER="", PARENT_TABLE="Public ation" CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Publishes_books", FK_COLUMNS="PublicationID" */ SELECT count(*) INTO NUMROWS FROM Book WHERE /* %JoinFKPK(Book,:%Old," = "," AND") */ Book.PublicationID = :old.PublicationID; IF (NUMROWS > 0) THEN raise_application_error( -20001,

'Cannot delete Publication because Book exists.' ); END IF; -- ERwin Builtin Trigger END; / CREATE TRIGGER tU_Publication AFTER UPDATE ON Publication for each row -- ERwin Builtin Trigger -- UPDATE trigger on Publication DECLARE NUMROWS INTEGER; BEGIN /* ERwin Builtin Trigger */ /* Publication Book on parent update restrict */ /* ERWIN_RELATION:CHECKSUM="000101a0", PARENT_OWNER="", PARENT_TABLE="Publicat ion" CHILD_OWNER="", CHILD_TABLE="Book" P2C_VERB_PHRASE="", C2P_VERB_PHRASE="", FK_CONSTRAINT="Publishes_books", FK_COLUMNS="PublicationID" */ IF /* %JoinPKPK(:%Old,:%New," <> "," OR ") */ :old.PublicationID <> :new.PublicationID THEN SELECT count(*) INTO NUMROWS FROM Book WHERE /* %JoinFKPK(Book,:%Old," = "," AND") */ Book.PublicationID = :old.PublicationID; IF (NUMROWS > 0) THEN raise_application_error( -20005, 'Cannot update Publication because Book exists.' ); END IF; END IF; -- ERwin Builtin Trigger END; /