<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Internet Strategy Guide &#187; alter table add constraint</title>
	<atom:link href="http://phpprotip.com/tag/alter-table-add-constraint/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpprotip.com</link>
	<description>Together we can defeat the internet</description>
	<lastBuildDate>Sat, 11 Feb 2012 01:39:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>mysql alter table add foreign key</title>
		<link>http://phpprotip.com/2009/07/mysql-alter-table-add-foreign-key/</link>
		<comments>http://phpprotip.com/2009/07/mysql-alter-table-add-foreign-key/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 17:20:09 +0000</pubDate>
		<dc:creator>chance</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[alter table add constraint]]></category>
		<category><![CDATA[alter table add foreign key]]></category>
		<category><![CDATA[errno 150]]></category>
		<category><![CDATA[foreign key]]></category>
		<category><![CDATA[innodb]]></category>

		<guid isPermaLink="false">http://phpprotip.com/?p=159</guid>
		<description><![CDATA[I recently ran into a situation where I needed to add a foreign key to a table. This seems easy at first but was actually a 3 step process compared to the couple of lines it takes in a create table sytax. Short version: when creating your FK column, it helps to have the column [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a situation where I needed to add a foreign key to a table. This seems easy at first but was actually a 3 step process compared to the couple of lines it takes in a create table sytax.</p>
<p>Short version: when creating your FK column, it helps to have the column definition of the FK match the column definition. In retrospec, this seems like a no-brainer but was the heart of my problem.</p>
<p>So, you want create a FK to an existing table. Lets start by creating our table.</p>
<pre>
CREATE TABLE IF NOT EXISTS Role(
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
Name VARCHAR(255) NOT NULL UNIQUE,
Description TEXT DEFAULT NULL,
PRIMARY KEY(Id)
);
</pre>
<p>Pretend we pooped data into it and now it's a big pain in the ass to do a drop/create with how we want things.</p>
<p>First we add our column that will be the Foreign Key.</p>
<pre>
ALTER TABLE Role ADD COLUMN Parent int(11) UNSIGNED DEFAULT NULL;
</pre>
<p>Next we add an index. Why? <a href="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL2Rldi5teXNxbC5jb20vZG9jL3JlZm1hbi81LjEvZW4vaW5ub2RiLWZvcmVpZ24ta2V5LWNvbnN0cmFpbnRzLmh0bWw=">because mysql says so</a> and FKs rely on indexes</p>
<pre>
ALTER TABLE Role ADD INDEX Parent (Parent);
</pre>
<p>Now we add our constraint.</p>
<pre>
ALTER TABLE `Role` ADD CONSTRAINT `Parent` FOREIGN KEY(`Parent`) REFERENCES `Role`(`Id`) ON DELETE SET NULL ON UPDATE SET NULL;
</pre>
<p>For fun, do</p>
<pre>
ALTER TABLE Role ADD COLUMN Parent int(11) DEFAULT NULL;
</pre>
<p>for the first step. Everything will look awesome until you get to the the last step. You'll get a wonderful "Errno 150" error and spend a bunch of time googling various key words trying to figure out what went wrong. Well, at least I did.</p>
 <img src="http://phpprotip.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=159" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://phpprotip.com/2009/07/mysql-alter-table-add-foreign-key/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

