Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Basic pSQL problem

  • 09-04-2005 07:20PM
    #1
    Registered Users, Registered Users 2 Posts: 6,422 ✭✭✭


    I'm trying to populate the final 2 tables in a database of 8.

    The problem im running into happens with both the foreign keys and the primary keys.

    I believe its got something to do with not sequencing the numbers (3 options and selection by 1,2,3) but im having difficulty figuring it out.
    CREATE TABLE Stocks (
    branchnumber VARCHAR(255) REFERENCES Branch,
    productcode VARCHAR(255) REFERENCES Product,
    qtyonhand VARCHAR(255),
    shrinkage VARCHAR(255),

    PRIMARY KEY (branchnumber),

    FOREIGN KEY (productcode)
    REFERENCES Product (barcode)
    );

    Thats how th table is created. The error from the compiler is as follows
    jvaughan3_toystorey=> INSERT INTO Stocks VALUES ('x003', '1034567804003', '11',
    '0');
    INSERT 33718 1
    jvaughan3_toystorey=> INSERT INTO Stocks VALUES ('x003', '1034567804006', '21',
    '1');
    ERROR: duplicate key violates unique constraint "stocks_pkey"

    As you can see, it will accept the first entry but not the second or third or fourth etc. Having never done sequencing im at a loss.
    Any help would be appreciated.

    [Edit] the data types aren't too important. Its just required to display the items within the database when a Query is run[/Edit]


Comments

  • Moderators, Politics Moderators, Paid Member Posts: 44,042 Mod ✭✭✭✭Seth Brundle


    Your setting branchnumber up to be both primary & foreign key. As a PK it will be unique and your insertion of 'x003' violates the PK's unique property


Advertisement