I'm trying to write a 1-many schema with simple example. I have one table books
mapping to many tags
. (see fiddle links below)
What I want is for the tags table to have a foreign key to books table. I think I have managed this:
CREATE TABLE IF NOT EXISTS `books` (
`id` int(6) unsigned NOT NULL,
`book_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `tags` (
`tag_id` int(6) unsigned NOT NULL,
`tag` varchar(100) NOT NULL,
`book_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`tag_id`)
);
I don't understand the difference between:
`book_id` int(6) unsigned NOT NULL,
and
`book_id` int(6) unsigned NOT NULL REFERENCES `books`(`id`),
Both compile fine and work with inner joins
http://sqlfiddle.com/#!9/612337/1
http://sqlfiddle.com/#!9/2043b8/3
So what's the difference between the snippets?