FOREIGN KEY

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zeljko.prince@gmail.com

    #1

    FOREIGN KEY

    Given the following table:
    CREATE table1 (a INTEGER PRIMARY KEY) TYPE=INNODB;

    It is possible to create a relation between table1 i table2 using the
    following syntax:
    CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER, FOREIGN
    KEY(b) REFERENCES table1(a)) TYPE=INNODB;

    How can I make that relation within the column definition? This won't
    work:
    CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER REFERENCES table1(a), c
    INTEGER) TYPE=INNODB;


    Sincerly,
    Zeljko

  • noone

    #2
    Re: FOREIGN KEY

    zeljko.prince@g mail.com wrote:
    [color=blue]
    > Given the following table:
    > CREATE table1 (a INTEGER PRIMARY KEY) TYPE=INNODB;[/color]
    [color=blue]
    > It is possible to create a relation between table1 i table2 using the
    > following syntax:
    > CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER, FOREIGN
    > KEY(b) REFERENCES table1(a)) TYPE=INNODB;[/color]
    [color=blue]
    > How can I make that relation within the column definition? This won't
    > work:
    > CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER REFERENCES table1(a), c
    > INTEGER) TYPE=INNODB;[/color]


    http://dev.mysql.com/doc/refman/4.1/en/alter-table.html

    CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) TYPE=INNODB;
    alter table table2 add foreign key fk_table1_key (b) references table1(a);

    [color=blue]
    > Sincerly,
    > Zeljko[/color]


    Comment

    • zeljko.prince@gmail.com

      #3
      Re: FOREIGN KEY


      noone wrote:[color=blue]
      > http://dev.mysql.com/doc/refman/4.1/en/alter-table.html
      >
      > CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) TYPE=INNODB;
      > alter table table2 add foreign key fk_table1_key (b) references table1(a);[/color]

      Thank you for fast reply.
      I am currently using the ALTER TABLE statement, but I wanted somehow to
      eliminate it and make reference within table definition.

      According to http://dev.mysql.com/doc/refman/5.0/...ate-table.html I
      shoud acomplish that with the following query:

      CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER REFERENCES
      table1(a), c INTEGER) TYPE=INNODB;

      The previous query executes, but no relations are created.

      (CREATE TABLE tbl_name (col_name type reference_defin ition))


      Zeljko

      Comment

      • zeljko.prince@gmail.com

        #4
        Re: FOREIGN KEY

        It seems there is a bug in MySQL: http://bugs.mysql.com/bug.php?id=13301

        Comment

        • noone

          #5
          Re: FOREIGN KEY

          zeljko.prince@g mail.com wrote:

          [color=blue]
          > noone wrote:[color=green]
          >> http://dev.mysql.com/doc/refman/4.1/en/alter-table.html
          >>
          >> CREATE table2 (a INTEGER PRIMARY KEY, b INTEGER, c INTEGER) TYPE=INNODB;
          >> alter table table2 add foreign key fk_table1_key (b) references table1(a);[/color][/color]
          [color=blue]
          > Thank you for fast reply.
          > I am currently using the ALTER TABLE statement, but I wanted somehow to
          > eliminate it and make reference within table definition.[/color]
          [color=blue]
          > According to http://dev.mysql.com/doc/refman/5.0/...ate-table.html I
          > shoud acomplish that with the following query:[/color]

          According the docs the syntax is (not tested):

          CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER constraint
          [constr-name] foreign key REFERENCES table1(a), c INTEGER) TYPE=INNODB;



          Comment

          • zeljko.prince@gmail.com

            #6
            Re: FOREIGN KEY

            noone wrote:[color=blue]
            > According the docs the syntax is (not tested):
            >
            > CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER constraint
            > [constr-name] foreign key REFERENCES table1(a), c INTEGER) TYPE=INNODB;[/color]

            It breaks on both of the following queries (with or without CONSTRAINT
            keyword):

            CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER constraint
            whatever foreign key REFERENCES table1(a), c INTEGER) TYPE=INNODB;

            CREATE TABLE table2 (a INTEGER PRIMARY KEY, b INTEGER foreign key
            REFERENCES table1(a), c INTEGER) TYPE=INNODB;


            Never mind, I'll put FOREIGN KEY contstraints in table definition,
            although I wanted them in the column definition. As I understand, there
            is a bug in MySQL, as stated in my previous post. Thanks anyway.


            Zeljko

            Comment

            Working...