Announcement

Collapse
No announcement yet.

PHPMyadmin/mysql and linking one primary key to another table?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHPMyadmin/mysql and linking one primary key to another table?

    Hi,

    Tried to cover all the aspects of my question within the subject. I am trying to create an intranet based on mysql/php. I have created a table with names and each name has a primary key. I have then created a second table and am trying to use the primary key from the other table in this one. Could anyone point me in the right direction or give me some tips how I go about it please?

    Thanks

  • #2
    Re: PHPMyadmin/mysql and linking one primary key to another table?

    Google "mysql foreign key"

    Both tables must use the InnoDB engine (to do it this way at least). Create them as follows:

    Code:
    CREATE TABLE `cafeworld_db`.`stove` (
    `stove_id` TINYINT NOT NULL AUTO_INCREMENT ,
    `stove_name` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
    PRIMARY KEY ( `stove_id` ) ,
    UNIQUE (
    `stove_name`
    )
    ) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_bin;
    
    CREATE TABLE `cafeworld_db`.`cuisine` (
    `cuisine_id` TINYINT NOT NULL AUTO_INCREMENT ,
    `cuisine_name` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
    `stove_id` TINYINT NOT NULL ,
    INDEX stove_index (stove_id),
    FOREIGN KEY (stove_id) REFERENCES `cafeworld_db`.`stove`(stove_id) ON DELETE RESTRICT,
    PRIMARY KEY ( `cuisine_id` ) ,
    UNIQUE (
    `cuisine_name`
    )
    ) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_bin;
    This is one example - there are other options you can choose, such as other actions to perform on deletion of a row from the parent table.

    (Yes, I realise how uncool a Cafe World database is, I write software that there is a demand for ).

    Also, don't forget that there is a dedicated section of these forums for database queries </groan>.
    Last edited by gforceindustries; 27th July 2011, 12:01.
    Gareth Howells

    BSc (Hons), MBCS, MCP, MCDST, ICCE

    Any advice is given in good faith and without warranty.

    Please give reputation points if somebody has helped you.

    "For by now I could have stretched out my hand and struck you and your people with a plague that would have wiped you off the Earth." (Exodus 9:15) - I could kill you with my thumb.

    "Everything that lives and moves will be food for you." (Genesis 9:3) - For every animal you don't eat, I'm going to eat three.

    Comment


    • #3
      Re: PHPMyadmin/mysql and linking one primary key to another table?

      Thanks.... Totally forgot about this post but I actually sorted my "mysql" connection out. Appreciate the reply though

      Comment

      Working...
      X