2012-02-29 83 views
0

嗨,大家好,我需要在ORACLE 10g中創建一個觸發器,它確保在birthDate之前的deathDate無法輸入。我真的很苦惱,請你能提供支持,謝謝你仁慈的人。協助創建一個觸發器

我的方案是:

Actor (actorID, lastName, firstName, middleName, suffix, gender, birthDate, deathDate) 
Movie (movieID, title, year, company, totalNoms, awardsWon, DVDPrice, discountPrice) 
Quote (quoteID, quote) 
Role (roleID ,roleName ,gender ,actorID* ,movieID*) 
RoleQuote (roleID*, quoteID*) 

謝謝

如果有任何更多的信息,你需要請諮詢

我的關係約束是:

CONSTRAINT_NAME    C             
------------------------------ -             
QUOTE_FK      R             
ROLE_FK      R             
MOVIE_ROLE_FK     R             
ACTOR_ROLE_FK     R             
ACTORID      P             
MOVIEID      P             
QUOTEID      P             
ROLEID       P             
ROLEQUOTEID     P  

Relationships: 

ALTER TABLE Role ADD CONSTRAINT ACTOR_ROLE_FK FOREIGN KEY (actorID) REFERENCES Actor (actorID); 
ALTER TABLE Role ADD CONSTRAINT MOVIE_ROLE_FK FOREIGN KEY (movieID) REFERENCES Movie (movieID); 
ALTER TABLE RoleQuote ADD CONSTRAINT Role_FK FOREIGN KEY (roleID) REFERENCES Role (roleID); 
ALTER TABLE RoleQuote ADD CONSTRAINT Quote_FK FOREIGN KEY (quoteID) REFERENCES Quote (quoteID); 
+0

你嘗試過這麼遠嗎? [P.S ...](http://www.techonthenet.com/oracle/triggers/index.php) – Ben 2012-02-29 23:56:23

+0

我將發佈: CREATE TRIGGER death_date_not_valid BEFORE birthDate user1137472 2012-02-29 23:59:18

+0

我不知道這是正確的,但它是我試過的 – user1137472 2012-02-29 23:59:51

回答

5

那種的約束應該作爲一個CHECK con來實現straint

ALTER TABLE actor 
    ADD CONSTRAINT chk_birth_before_death CHECK(birthDate < deathDate); 

如果你真的需要約束與觸發器執行(這將是慢,需要更多的工作)

CREATE OR REPLACE TRIGGER check_death_date 
    AFTER INSERT OR UPDATE ON actor 
    FOR EACH ROW 
BEGIN 
    IF(:new.birthDate > :new.deathDate) 
    THEN 
    RAISE_APPLICATION_ERROR(-20001, 'Birth date cannot be later than death date.'); 
    END IF; 
END; 
+0

非常感謝你我會試試這個,讓你知道上帝保佑你 – user1137472 2012-03-01 00:11:22

+0

我需要添加alter table嗎?或者我可以運行觸發器,謝謝 – user1137472 2012-03-01 00:13:32

+0

@ user1137472 - 您可以創建約束,也可以創建觸發器。兩者都沒有意義。我強烈的偏好是使用約束而不是使用觸發器來強制執行此規則。 – 2012-03-01 00:15:27