Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

sql - how to create inheritance table in oracle

there is table called event that act as parent the child inherit tables of event are special event and hotel event i have created the types as bellow but I'm contuse about how to create tables to these tables in oracle.I have referred most of the currently available solutions within Stack overflow, git hub etc. However, none of these solutions have worked out successfully.

Table types :

Event_t (
  EventID:char(5),
  EventType:varchar(20),
  VenueName:varchar(50),
  NoOfGuest:number(10)
) NOT FINAL

HotelEvent_t (
  Date:date,
  Price:numbr(8,2)
) UNDER Event_t

SpecialEvent_t (
  BookingDate:date,
  EndDate:date,
  MenuNumber:number(2), 
  Reservation ref Reservation_t
) UNDER event_t

Thank you very much and any suggestion will be greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create your types using the correct syntax:

CREATE TYPE Event_t AS OBJECT(
  EventID   char(5),
  EventType varchar(20),
  VenueName varchar(50),
  NoOfGuest number(10)
) NOT FINAL;

CREATE TYPE HotelEvent_t UNDER Event_t (
  datetime date,                -- Date is a keyword, try to use a different name.
  Price  number(8,2)
);

CREATE TYPE SpecialEvent_t UNDER event_t (
  BookingDate date,
  EndDate     date,
  MenuNumbers NUMBER(2),
  Reservation ref Reservation_t
);

Then you can create an object table:

CREATE TABLE Events OF Event_T(
  eventid CONSTRAINT Events__EventID__PK PRIMARY KEY
);

Then you can insert the different types into it:

INSERT INTO EVENTS VALUES(
  HotelEvent_T(
    'H1',
    'HOTEL',
    'Venue1',
    42,
    DATE '0001-02-03' + INTERVAL '04:05:06' HOUR TO SECOND,
    123456.78
  )
);

INSERT INTO EVENTS VALUES(
  SpecialEvent_T(
    'SE1',
    'SPECIAL',
    'Time Travel Convention',
    -1,
    SYSDATE,
    TRUNC(SYSDATE),
    0,
    NULL
  )
);

and get the data out of it:

SELECT e.*,
       TREAT( VALUE(e) AS HotelEvent_T ).datetime AS datetime,
       TREAT( VALUE(e) AS HotelEvent_T ).price AS price,
       TREAT( VALUE(e) AS SpecialEvent_T ).bookingdate AS bookingdate,
       TREAT( VALUE(e) AS SpecialEvent_T ).enddate AS enddate,
       TREAT( VALUE(e) AS SpecialEvent_T ).menunumbers AS menunumbers
FROM   Events e;

Which outputs:

EVENTID | EVENTTYPE | VENUENAME              | NOOFGUEST | DATETIME            |     PRICE | BOOKINGDATE         | ENDDATE             | MENUNUMBERS
:------ | :-------- | :--------------------- | --------: | :------------------ | --------: | :------------------ | :------------------ | ----------:
H1      | HOTEL     | Venue1                 |        42 | 0001-02-03 04:05:06 | 123456.78 | null                | null                |        null
SE1     | SPECIAL   | Time Travel Convention |        -1 | null                |      null | 2020-03-30 21:11:22 | 2020-03-30 00:00:00 |           0

db<>fiddle here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...