views:

1589

answers:

5

I have been slowly learning SQL the last few weeks. I've picked up all of the relational algebra and the basics of how relational databases work. What I'm trying to do now is learn how it's implemented.

A stumbling block I've come across in this, is foreign keys in MySQL. I can't seem to find much about the other than that they exist in the InnoDB storage schema that MySQL has.

What is a simple example of foreign keys implemented in MySQL?

Here's part of a schema I wrote that doesn't seem to be working if you would rather point out my flaw than show me a working example.

CREATE TABLE `posts` (
`pID` bigint(20) NOT NULL auto_increment,
`content` text NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`uID` bigint(20) NOT NULL,
`wikiptr` bigint(20) default NULL,
`cID` bigint(20) NOT NULL,
PRIMARY KEY  (`pID`),
Foreign Key(`cID`) references categories,
Foreign Key(`uID`) references users
) ENGINE=InnoDB;
+2  A: 

This has some code showing how to create foreign keys by themselves, and in CREATE TABLE.

Here's one of the simpler examples from that:

CREATE TABLE parent (id INT NOT NULL,
                 PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
                    INDEX par_ind (parent_id),
                    FOREIGN KEY (parent_id) REFERENCES parent(id)
                      ON DELETE CASCADE
) ENGINE=INNODB;
DOK
+8  A: 

Assuming your categories and users table already exist and contain cID and uID respectively as primary keys, this should work:

CREATE TABLE `posts` (
`pID` bigint(20) NOT NULL auto_increment,
`content` text NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`uID` bigint(20) NOT NULL,
`wikiptr` bigint(20) default NULL,
`cID` bigint(20) NOT NULL,
PRIMARY KEY  (`pID`),
Foreign Key(`cID`) references categories(`cID`),
Foreign Key(`uID`) references users(`uID`)
) ENGINE=InnoDB;

The column name is required in the references clause.

Robert Gamble
Awesome, thanks.
icco
+2  A: 

I agree with Robert. You are missing the name of the column in the references clause (and you should be getting the error 150). I'll add that you can check how the tables got created in reality with:

SHOW CREATE TABLE posts;
Vinko Vrsalovic
+4  A: 

Edited: Robert and Vinko state that you need to declare the name of the referenced column in the foreign key constraint. This is necessary in InnoDB, although in standard SQL you're permitted to omit the referenced column name if it's the same name in the parent table.

One idiosyncrasy I've encountered in MySQL is that foreign key declaration will fail silently in several circumstances:

  • Your MySQL installation doesn't include the innodb engine
  • Your MySQL config file doesn't enable the innodb engine
  • You don't declare your table with the ENGINE=InnoDB table modifier
  • The foreign key column isn't exactly the same data type as the primary key column in the referenced table

Unfortunately, MySQL gives no message that it has failed to create the foreign key constraint. It simply ignores the request, and creates the table without the foreign key (if you SHOW CREATE TABLE posts, you may see no foreign key declaration). I've always thought this is a bad feature of MySQL!

Tip: the integer argument for integer data types (e.g. BIGINT(20)) is not necessary. It has nothing to do with the storage size or range of the column. BIGINT is always the same size regardless of the argument you give it. The number refers to how many digits MySQL will pad the column if you use the ZEROFILL column modifier.

Bill Karwin
Can you provide a reference to your claim regarding columns names in foreign key? According to the syntax at (http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html) this is not an optional parameter and it doesn't work for me unless I specify it.
Robert Gamble
Yes, you are right, now that I test it more carefully I do observe the same thing. Standard SQL should permit the referenced column name to be implicit if it's the same in the primary table, but InnoDB doesn't support this, unfortunately. Mea culpa!
Bill Karwin
I have edited my first paragraph to be more accurate.
Bill Karwin
It depends on your version of MySQL. In 5, it fails to create the table with the helpful message "Error 150" which means "failed to create table" or some other incredibly useful thing.
MBCook
+1  A: 

The previous answers deal with the foreign key constraint. While the foreign key constraint is definitely useful to maintain referential integrity, the concept of "foreign key" itself is fundamental to the relational model of data, regardless of whether you use the constraint or not.

Whenever you do an equijoin, you are equating a foreign key to something, usually the key it references. Example:

select *
from 
   Students
inner join
   StudentCourses
on Students.StudentId = StudentCourses.StudentId

StudentCourses.StudentId is a foreign key referencing Students.StudentId.

Walter Mitty
do you think you could explain what this is doing a little more? Is it just a join?
icco