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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 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/ |
| ||||
| 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 > |