Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Created January 6, 2011 17:43
Show Gist options
  • Save lukemorton/768248 to your computer and use it in GitHub Desktop.
Save lukemorton/768248 to your computer and use it in GitHub Desktop.
/*
Book Owners Table
*/
CREATE TABLE IF NOT EXISTS `book_owners` (
`book_id` int(10) NOT NULL,
`owner_id` int(10) NOT NULL,
KEY `book` (`book_id`),
KEY `owner` (`owner_id`),
CONSTRAINT `book` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `owner` FOREIGN KEY (`owner_id`) REFERENCES `owners` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `book_owners` (`book_id`, `owner_id`) VALUES
(1, 1);
/*
Books Table
*/
CREATE TABLE IF NOT EXISTS `books` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `books` (`id`, `name`) VALUES
(1, 'Harry Potter and the Philosophers Stone');
/*
Owners Table
*/
CREATE TABLE IF NOT EXISTS `owners` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `owners` (`id`, `name`) VALUES
(1, 'Luke');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment