Ask a Question related to MySQL, Design and Development.

  1. #1

    Default Foreign key

    Hi

    First, i assume i RTFM but am new to MySQL world so i got some problems.
    I got 2 tables

    TABLE1
    T1Col1 Varchar(30) PRIMARY KEY

    TABLE2
    T2col1
    T2col2



    What i'd like to do is to set the T2col2 as foreign key for the T1Col1.
    Any1 got the synthaxe to do it ?

    Regards
    Bruno


    Bruno Guerpillon Guest

  2. Similar Questions and Discussions

    1. Self Referencing foreign key
      I am trying to create an employee/supervisor relationship. Since both are essentially employees, I created a foreign key relationship which...
    2. Find out if a key is foreign or not?
      I'm trying to find out if a key in the database is a foreign key and what it is linked to. This way I could automate lookups instead of coding them...
    3. Foreign char - ASP to DB
      Hi, I have a problem on storing foreign char to SQL DB. If user inputs a foreign char from asp page, for example, "ROSÉ", it will be stored in...
    4. foreign key is blank
      I have two forms in a one-to-many relationship. I've also created a data entry form for the two of them using a main form and a subform. The...
    5. SQL DMO + Foreign Keys
      I have table1 that has a Foriegn Key to table2, using the sql dmo i am able to get the script from Table1 to create the Foreign key with Table2 no...
  3. #2

    Default Re: Foreign key

    Bruno Guerpillon wrote:
    > Hi
    >
    > First, i assume i RTFM but am new to MySQL world so i got some problems.
    > I got 2 tables
    >
    > TABLE1
    > T1Col1 Varchar(30) PRIMARY KEY
    >
    > TABLE2
    > T2col1
    > T2col2
    >
    >
    >
    > What i'd like to do is to set the T2col2 as foreign key for the T1Col1.
    > Any1 got the synthaxe to do it ?
    >
    > Regards
    > Bruno
    >
    >
    To use foreign keys, you have to:
    - use InnoDB tables;
    - have a primary key in the parent table
    - have a key for the foreign key candidate column in the child table
    - use the constraint syntax:
    CONSTRAINT `constraint_name` FOREIGN KEY `key_name` (T2col2) references TABLE1 (T1Col1)

    This is the RTFM part :)
    [url]http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html[/url]
    [url]http://dev.mysql.com/doc/refman/5.0/en/example-foreign-keys.html[/url]
    [url]http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-foreign-keys.html[/url]

    ciao
    gmax

    --
    _ _ _ _
    (_|| | |(_|>< The Data Charmer
    _|
    [url]http://datacharmer.blogspot.com/[/url]
    Giuseppe Maxia Guest

  4. #3

    Default Re: Foreign key

    If you've installed MySQL locally, it comes with a pretty decent manual,
    especially if it's on Windows. you just go into the index tab and type in
    alter table. you can also learn about alter table by looking at the SQL
    generated when you modify a table in the query browser.
    I think the syntax would look something like
    ALTER TABLE TABLE2 ADD FOREIGN KEY ix_t2t2col2t1col1 (T2col2) REFERENCES
    TABLE1(T1Col1);

    "Bruno Guerpillon" <toto@toto.fr> wrote in message
    news:43d60d41$0$375$636a55ce@news.free.fr...
    > Hi
    >
    > First, i assume i RTFM but am new to MySQL world so i got some problems.
    > I got 2 tables
    >
    > TABLE1
    > T1Col1 Varchar(30) PRIMARY KEY
    >
    > TABLE2
    > T2col1
    > T2col2
    >
    >
    >
    > What i'd like to do is to set the T2col2 as foreign key for the T1Col1.
    > Any1 got the synthaxe to do it ?
    >
    > Regards
    > Bruno
    >

    Jim Michaels Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139