SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2010
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    References to columns in same table

    I want a column to refer to the primary key of other entries in the same table. In essence like this:

    Code MySQL:
    CREATE TABLE entries
    (
    entry_id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR NOT NULL,
    entry_reference SMALLINT UNSIGNED, 
    FOREIGN KEY (entry_reference) REFERENCES entries (entry_id)
    ) ENGINE=InnoDB;

    What are the possibilities to achieve such functionality. It is a foreign key also, of course.

    EDIT: Nevermind, it seems to have merely been a syntax error with the varchar declaration.

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    I'm not sure what the purpose of a self-reference would be in your generic example.

    I can think of a different example...in users tables, I've seen a "created/added by" field that references the userID.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    parent/child, category/subcategory, productmaster/product, ponzi scheme downlines, ...

    there are lots of examples

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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