Unix Technical Forum

Foreign key

This is a discussion on Foreign key within the MySQL forums, part of the Database Server Software category; --> Hi First, i assume i RTFM but am new to MySQL world so i got some problems. I got ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:17 AM
Bruno Guerpillon
 
Posts: n/a
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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:17 AM
Giuseppe Maxia
 
Posts: n/a
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
http://dev.mysql.com/doc/refman/5.0/...nstraints.html
http://dev.mysql.com/doc/refman/5.0/...eign-keys.html
http://dev.mysql.com/doc/refman/5.0/...eign-keys.html

ciao
gmax

--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
http://datacharmer.blogspot.com/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 07:17 AM
Jim Michaels
 
Posts: n/a
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
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:35 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com