Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Unanswered: SqlScript-Primary and Foreign Keys?

    Im having some problems making an sql script that should implement my database... Im getting several errors when i try to run the script, the problem is the primary and foreign keys.... In some tabels i want to use a foreign key as primary key, how is this working? I have attached my sql script for you to see......

    Thank You
    Attached Files Attached Files

  2. #2
    Join Date
    Mar 2004
    Posts
    110
    taken a snapshot of your script:

    ......................
    Create TABLE Indspilning (
    Indspilningsaar integer PRIMARY KEY,
    Indspilningssted varchar(50) PRIMARY KEY,
    VaerkNavn varchar(50) PRIMARY KEY,
    KomponistNavn varchar(50) PRIMARY KEY,
    Varighed integer,
    FOREIGN KEY (GruppeNavn) REFERENCES Gruppe(GruppeNavn),
    FOREIGN KEY (VaerkNavn) REFERENCES Vaerk(VaerkNavn),
    FOREIGN KEY (KomponistNavn) REFERENCES Vaerk(KomponistNavn),
    FOREIGN KEY (SelskabsNavn) REFERENCES Koebtenhed(SelskabsNavn),
    FOREIGN KEY (Kode) REFERENCES Koebtenhed(kode),
    FOREIGN KEY (CD nummer) REFERENCES Sektion(Cd nummer),
    FOREIGN KEY (Startspor) REFERENCES Sektion(Startspor));
    ................

    Let me first state..im not a DBA, im just getting around learning this sql stuff but i think you can only have one primary key per table.

    What is the reason you want to have 4 primary keys in on table? Uniquenes of the data for those rows?
    Why are you using columns that already excist in another table? For joins, one foreign key is enough (to establish a relation between tables).

    Another thing that struck me, is that you are using VARCHAR as a primary key.
    From what i've 'heard', using varchar as a primary key is not such a good idea from 'index' pov. You'd better make a <columname>_ID integer and either use SERIAL or sequence (which should be created).

    I tried to make a sample of what would be in my mind better, but as i only write dutch and english, i cannot "visualize" what table- and columnnames we are talking about.

    Feel free to correct me on points that are not (entirely) correct.

    -Ed
    The Learning Noob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •