relation vs table...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terrence Brannon

    relation vs table...

    I don't know what Postgres considers a relation and had no intention of
    creating one when piping my schema to it... I always DROP TABLE before
    CREATE TABLE, so here are the ERRORS emitted when building the database:

    3:ERROR: table "country" does not exist
    6:ERROR: table "customer" does not exist
    11:ERROR: table "product" does not exist
    15:ERROR: table "license" does not exist
    19:ERROR: table "enduser" does not exist
    24:ERROR: table "orders" does not exist
    29:ERROR: table "enduser_licens e" does not exist
    33:ERROR: table "item" does not exist
    37:ERROR: Relation "product_versio n" does not exist
    38:ERROR: table "product_versio n" does not exist
    43:ERROR: table "ordered_it em" does not exist

    but each and every one of these was created via CREATE TABLE

    I don't understand why Postgres thinks I am creating a relation _and_ I
    don't know what it considers a relation to be. Create stmts follow:



    /*============== =============== =============== =============== =============== */
    /*
    Tables */
    /*============== =============== =============== =============== =============== */

    DROP TABLE country;
    CREATE TABLE country (
    country_id VARCHAR(3) PRIMARY KEY,

    country VARCHAR(80)
    );

    DROP TABLE customer;
    CREATE TABLE customer (
    customer_id SERIAL PRIMARY KEY,
    country_id VARCHAR(3) REFERENCES country(country _id),

    name VARCHAR(100) NOT NULL,
    companyname VARCHAR(100) NOT NULL,
    address1 VARCHAR(100) NOT NULL,
    address2 VARCHAR(100) NOT NULL,
    city VARCHAR(100) NOT NULL,
    zipcode VARCHAR(10) NOT NULL,
    state VARCHAR(10) NOT NULL,

    phone VARCHAR(20),
    fax VARCHAR(20),
    email VARCHAR(100),
    vatid VARCHAR(20)
    );

    DROP TABLE product;
    CREATE TABLE product (
    product_id SERIAL PRIMARY KEY,

    productdesc VARCHAR(100) NOT NULL,
    productname VARCHAR(100) NOT NULL
    );

    DROP TABLE license;
    CREATE TABLE license (
    license_id SERIAL PRIMARY KEY,

    lickey VARCHAR(80) NOT NULL,
    liccode VARCHAR(80) NOT NULL
    );

    DROP TABLE enduser;
    CREATE TABLE enduser (
    enduser_id SERIAL PRIMARY KEY,
    customer_id INTEGER REFERENCES customer(custom er_id),

    name VARCHAR(40),
    companyname VARCHAR(40)
    );

    DROP TABLE orders;
    CREATE TABLE orders (
    orders_id SERIAL PRIMARY KEY,
    country_id VARCHAR(3) REFERENCES country(country _id),
    customer_id INTEGER REFERENCES customer(custom er_id),

    name VARCHAR(100) NOT NULL,
    companyname VARCHAR(100) NOT NULL,
    address1 VARCHAR(100) NOT NULL,
    address2 VARCHAR(100) NOT NULL,
    city VARCHAR(100) NOT NULL,
    state VARCHAR(10) NOT NULL,
    zipcode VARCHAR(10) NOT NULL,

    orderdate DATE,
    phone VARCHAR(20),
    fax VARCHAR(20),
    email VARCHAR(100),
    vatid VARCHAR(20),
    refno VARCHAR(100),
    promotion VARCHAR(100)
    );

    DROP TABLE enduser_license ;
    CREATE TABLE enduser_license (
    license_id INTEGER REFERENCES license(license _id),
    enduser_id INTEGER REFERENCES enduser(enduser _id),

    PRIMARY KEY (license_id, enduser_id)
    );

    DROP TABLE item;
    CREATE TABLE item (
    item_id SERIAL,
    product_version _id INTEGER REFERENCES
    product_version (product_versio n_id),

    active INTEGER NOT NULL,
    itemname VARCHAR(100) NOT NULL,
    unitprice DECIMAL,
    numberoflic INTEGER,

    PRIMARY KEY (item_id, product_version _id)
    );

    DROP TABLE product_version ;
    CREATE TABLE product_version (
    product_version _id SERIAL PRIMARY KEY,
    product_id INTEGER REFERENCES product(product _id)
    );

    DROP TABLE ordered_item;
    CREATE TABLE ordered_item (
    orders_id INTEGER REFERENCES orders(orders_i d),
    item_id INTEGER NOT NULL,
    product_id INTEGER NOT NULL,

    quantity INTEGER NOT NULL,
    product_version _id VARCHAR(10),
    unitprice DECIMAL,

    PRIMARY KEY (orders_id, item_id, product_id)
    );


    /*============== =============== =============== =============== =============== */
    /*
    Indexes */
    /*============== =============== =============== =============== =============== */


    /*============== =============== =============== =============== =============== */
    /*
    Procedures */
    /*============== =============== =============== =============== =============== */

    /*============== =============== =============== =============== =============== */
    /*
    Triggers */
    /*============== =============== =============== =============== =============== */





    ---------------------------(end of broadcast)---------------------------
    TIP 4: Don't 'kill -9' the postmaster

  • Alvaro Herrera

    #2
    Re: relation vs table...

    On Thu, Oct 09, 2003 at 05:50:48AM -0700, Terrence Brannon wrote:[color=blue]
    > I don't know what Postgres considers a relation and had no intention of
    > creating one when piping my schema to it... I always DROP TABLE before
    > CREATE TABLE, so here are the ERRORS emitted when building the database:[/color]

    Well, when you do a DROP TABLE and there's no table with the name you
    give, the system is right in giving you a "table foo does not exist".
    However, when you create a foreign key to a table that doesn't exist
    (because you are creating it further down in your script), the system
    doesn't know it is a table (it could be a view, for example). So it
    falls back to using the more general term, "relation".

    "Relation" means, in Postgres terms, "anything that can have a pg_class
    entry". This means system tables and views, regular tables and views,
    indexes, sequences, TOAST tables and special relations like pg_xactlock
    if I'm not mistaken.

    --
    Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
    "Estoy de acuerdo contigo en que la verdad absoluta no existe...
    El problema es que la mentira sí existe y tu estás mintiendo" (G. Lama)

    ---------------------------(end of broadcast)---------------------------
    TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddres sHere" to majordomo@postg resql.org)

    Comment

    • Peter Eisentraut

      #3
      Re: relation vs table...

      Terrence Brannon writes:
      [color=blue]
      > I don't understand why Postgres thinks I am creating a relation _and_ I
      > don't know what it considers a relation to be.[/color]

      In the context of relational databases, relation means the same thing as
      table. Because of some implementation artifacts, PostgreSQL internally
      treats tables, views, indexes, and sequences alike to some extent and
      refers to all of them together as relations. So when you see an error
      message telling you that a relation was not found, that means PostgreSQL
      was looking for a table, a view, an index, or a sequence. This artificial
      terminology isn't ideal, but it creates few problems in practice.

      --
      Peter Eisentraut peter_e@gmx.net


      ---------------------------(end of broadcast)---------------------------
      TIP 1: subscribe and unsubscribe commands go to majordomo@postg resql.org

      Comment

      • Oliver Elphick

        #4
        Re: relation vs table...

        On Thu, 2003-10-09 at 13:50, Terrence Brannon wrote:[color=blue]
        > I don't know what Postgres considers a relation and had no intention of
        > creating one when piping my schema to it... I always DROP TABLE before
        > CREATE TABLE, so here are the ERRORS emitted when building the database:
        >
        > 3:ERROR: table "country" does not exist
        > 6:ERROR: table "customer" does not exist
        > 11:ERROR: table "product" does not exist
        > 15:ERROR: table "license" does not exist
        > 19:ERROR: table "enduser" does not exist
        > 24:ERROR: table "orders" does not exist
        > 29:ERROR: table "enduser_licens e" does not exist
        > 33:ERROR: table "item" does not exist[/color]

        All these are errors when you DROP a table that does not yet exist.
        [color=blue]
        > 37:ERROR: Relation "product_versio n" does not exist[/color]

        This is your attempt to have a foreign key reference to product_version
        from table "item", when product_version does not yet exist.
        [color=blue]
        > 38:ERROR: table "product_versio n" does not exist
        > 43:ERROR: table "ordered_it em" does not exist[/color]

        and again, you are dropping these tables before they exist.
        [color=blue]
        > but each and every one of these was created via CREATE TABLE[/color]

        but only _after_ you try to drop them (and your attempt to create "item"
        will have failed because of the bad foreign key reference).
        [color=blue]
        > I don't understand why Postgres thinks I am creating a relation _and_ I
        > don't know what it considers a relation to be. Create stmts follow:[/color]

        A table is a relation but a relation is not necessarily a table, A view
        is a relation. However, the error message for your bad foreign key is
        misleading because a foreign key must reference a table, not a relation:

        junk=# create view aaa as select * from xxx union select * from zzz;
        CREATE VIEW
        junk=# create table bbb (id serial, aid integer references aaa(id));
        NOTICE: CREATE TABLE will create implicit sequence "bbb_id_seq " for
        "serial" column "bbb.id"
        ERROR: referenced relation "aaa" is not a table

        --
        Oliver Elphick Oliver.Elphick@ lfix.co.uk
        Isle of Wight, UK http://www.lfix.co.uk/oliver
        GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C
        =============== =============== ==========
        "Every good gift and every perfect gift is from above,
        and cometh down from the Father of lights, with whom
        is no variableness, neither shadow of turning."
        James 1:17


        ---------------------------(end of broadcast)---------------------------
        TIP 4: Don't 'kill -9' the postmaster

        Comment

        Working...