Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 1.72 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Oracle Sql Check Constraint
  2. Create Table Event(
  3. IDEvent number (8) primary key,
  4. StartDate date not null,
  5. EndDate date not null
  6. );
  7.        
  8. Create Table Game(
  9. IDGame number (8) primary key,
  10. GameDate date not null,
  11. constraint checkDate
  12. check (GameDate >= to_date(StartDate references from Event(StartDate)))
  13. );
  14.        
  15. CREATE TABLE Event
  16. (
  17.   IDEvent     NUMBER(8) PRIMARY KEY,
  18.   StartDate   DATE NOT NULL,
  19.   EndDate     DATE NOT NULL
  20. );
  21.  
  22. CREATE TABLE Game
  23. (
  24.   IDGame     NUMBER(8) PRIMARY KEY,
  25.   GameDate   DATE NOT NULL,
  26.   eventid    NUMBER(8),   -- this is different to your table definition
  27.   CONSTRAINT fk_game_event FOREIGN KEY (eventid) REFERENCES event (idevent)
  28. );
  29.  
  30. CREATE INDEX game_eventid ON game (eventid);
  31.  
  32. CREATE MATERIALIZED VIEW LOG ON event
  33.    WITH ROWID, SEQUENCE (idevent, startdate) INCLUDING NEW VALUES;
  34.  
  35. CREATE MATERIALIZED VIEW LOG ON game
  36.    WITH ROWID, SEQUENCE (idgame, eventid, gamedate) INCLUDING NEW VALUES;
  37.  
  38. CREATE MATERIALIZED VIEW mv_event_game
  39. REFRESH FAST ON COMMIT WITH ROWID
  40. AS
  41. SELECT ev.idevent,
  42.        ev.startdate,
  43.        g.gamedate
  44. FROM event ev, game g
  45. WHERE g.eventid = ev.idevent;  
  46.  
  47. ALTER TABLE mv_event_game
  48.   ADD CONSTRAINT check_game_start check (gamedate >= startdate);
  49.        
  50. Connected to:
  51. Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
  52. With the Partitioning and OLAP options
  53.  
  54. SQL> INSERT INTO event
  55.   2  (idevent, startdate, enddate)
  56.   3  values
  57.   4  (1, date '2012-01-22', date '2012-01-24');
  58.  
  59. 1 row created.
  60.  
  61. SQL>
  62. SQL> INSERT INTO game
  63.   2  (idgame, eventid, gamedate)
  64.   3  VALUES
  65.   4  (1, 1, date '2012-01-01');
  66.  
  67. 1 row created.
  68.  
  69. SQL> commit;
  70. commit
  71. *
  72. ERROR at line 1:
  73. ORA-12008: error in materialized view refresh path
  74. ORA-02290: check constraint (FOOBAR.CHECK_GAME_START) violated