Search code examples
sqloracle-databaseoracle12c

How to create table using oracle database


Good evening I am a beginner in the Oracle database I wanted to create 3 tables as follows if you can explain to me please.

Architecture of my database:

architecture of my database


Solution

  • Use an Object Table:

    CREATE TYPE country IS OBJECT (
      country_name VARCHAR2(100)
    ) NOT FINAL;
    
    CREATE TYPE city UNDER country (
      city_name VARCHAR2(100)
    ) NOT FINAL;
    
    CREATE TYPE capitol UNDER city (
      area VARCHAR2(100)
    );
    
    CREATE TABLE countries OF country;
    CREATE TABLE cities OF city;
    CREATE TABLE capitols OF capitol;
    

    Then you can insert like this:

    INSERT INTO capitols ( country_name, city_name, area )
    VALUES ( 'Country', 'City', 'Area' );
    

    and the capitols table has inherited the columns from its ancestor type.

    db<>fiddle