sql >> Databasteknik >  >> RDS >> Oracle

hur man skapar arvstabell i oracle

Skapa dina typer med rätt 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
);

Sedan kan du skapa en objekttabell:

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

Sedan kan du infoga de olika typerna i den:

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
  )
);

och få ut data från det:

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;

Vilka utgångar:

db<>fiol här



  1. sökkriterieskillnad mellan Like vs Contains() i oracle

  2. Hur lägger man till kolumn med alter i mysql?

  3. Hur man får de översta N-värdena för varje kolumn i MySql

  4. SQL SELECT med m:n relation