469,612 Members | 1,940 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Foreign keys in mysql

I am working on my first database and I think I want to build a table with
a foreign key in it. The O'Reilly book on MySQL says that MySQL does not
support foreign keys, but it still talks about them in the section on
database design.

So, if if I want to use a foreign key, how do I do it?
Many thanks,
Jeff

Jul 20 '05 #1
2 1567

"Jeff Silverman" <je***@NOPAM.commercialventvacNOSPAM.com> wrote in message
news:cb**********@216.39.145.57...
I am working on my first database and I think I want to build a table with
a foreign key in it. The O'Reilly book on MySQL says that MySQL does not
support foreign keys, but it still talks about them in the section on
database design.

So, if if I want to use a foreign key, how do I do it?

Create InnoDB type tables.

CREATE TABLE agents (
akey INTEGER NOT NULL auto_increment PRIMARY KEY,
ln varchar(30),
fn varchar(25),
phone varchar(20),
timeEnter timestamp(14))
ENGINE = InnoDB;

CREATE TABLE clients (
ckey INTEGER NOT NULL auto_increment PRIMARY KEY,
f_akey INTEGER NOT NULL,
ln varchar(30),
fn varchar(25),
phone varchar(20),
FOREIGN KEY (f_akey) REFERENCES agents(akey))
ENGINE = InnoDB;

See (TIP 29 and TIP 9) at the following link for more detail and better
formatting.
http://prdownloads.sourceforge.net/s...l.txt?download

Hope this helps.

Mike Chirico
Jul 20 '05 #2
Jeff Silverman wrote:
I am working on my first database and I think I want to build a table with
a foreign key in it. The O'Reilly book on MySQL says that MySQL does not
support foreign keys, but it still talks about them in the section on
database design.

So, if if I want to use a foreign key, how do I do it?


In current versions of MySQL, you must use the InnoDB table type for
foreign key constraints to be recorded and enforced.

Read this:
http://dev.mysql.com/doc/mysql/en/AN...eign_Keys.html

Regards,
Bill K.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Olivier Crèvecoeur | last post: by
reply views Thread by Andrew Kuebler | last post: by
reply views Thread by Kevin Holmes | last post: by
2 posts views Thread by Ian Davies | last post: by
2 posts views Thread by kal stevens | last post: by
5 posts views Thread by coosa | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.