<?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>ALOMOHORA &#187; SQL</title>
	<atom:link href="http://alomohora.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://alomohora.com</link>
	<description>All about programming languages</description>
	<lastBuildDate>Tue, 25 Oct 2011 04:27:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>SQL CREATE INDEX Statement</title>
		<link>http://alomohora.com/sql-create-index-statement/</link>
		<comments>http://alomohora.com/sql-create-index-statement/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 08:36:54 +0000</pubDate>
		<dc:creator>alomohora</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://alomohora.com/?p=2229</guid>
		<description><![CDATA[The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading the whole table. Indexes An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/queries. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The CREATE INDEX statement is used to create indexes in tables.</p>
<p style="text-align: justify;"><span id="more-2229"></span></p>
<p style="text-align: justify;">Indexes allow the database application to find data fast; without reading the whole table.</p>
<h2>Indexes</h2>
<p style="text-align: justify;">An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/queries.</p>
<p style="text-align: justify;"><b>Note:</b> Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So you should only create indexes on columns (and tables) that will be frequently searched against.</p>
<p style="text-align: justify;">SQL CREATE INDEX Syntax</p>
<p style="text-align: justify;">Creates an index on a table. Duplicate values are allowed:</p>
<p style="text-align: center;">CREATE INDEX index_name ON table_name (column_name)</p>
<p style="text-align: justify;">SQL CREATE UNIQUE INDEX Syntax</p>
<p style="text-align: justify;">Creates a unique index on a table. Duplicate values are not allowed:</p>
<p style="text-align: center;">CREATE UNIQUE INDEX index_name ON table_name (column_name)</p>
<p style="text-align: justify;"><b>Note:</b> The syntax for creating indexes varies amongst different databases. Therefore: Check the syntax for creating indexes in your database.</p>
<h2>CREATE INDEX Example</h2>
<p style="text-align: justify;">The SQL statement below creates an index named &#8220;PIndex&#8221; on the &#8220;LastName&#8221; column in the &#8220;Persons&#8221; table:</p>
<p style="text-align: center;">CREATE INDEX PIndex ON Persons (LastName)</p>
<p style="text-align: justify;">If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:</p>
<p style="text-align: center;">CREATE INDEX PIndex ON Persons (LastName, FirstName)</p>
<h4>Incoming search terms for the article:</h4><p style="text-align: justify;"><a href="http://alomohora.com/sql-create-index-statement/" title="sql example create table with index in Delphi">sql example create table with index in Delphi</a></p>]]></content:encoded>
			<wfw:commentRss>http://alomohora.com/sql-create-index-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL DEFAULT Constraint</title>
		<link>http://alomohora.com/sql-default-constraint/</link>
		<comments>http://alomohora.com/sql-default-constraint/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 08:36:10 +0000</pubDate>
		<dc:creator>alomohora</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://alomohora.com/?p=2227</guid>
		<description><![CDATA[The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified. SQL DEFAULT Constraint on CREATE TABLE The following SQL creates a DEFAULT constraint on the &#8220;City&#8221; column when the &#8220;Persons&#8221; table is created: My SQL / [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The DEFAULT constraint is used to insert a default value into a column.</p>
<p style="text-align: justify;"><span id="more-2227"></span></p>
<p style="text-align: justify;">The default value will be added to all new records, if no other value is specified.</p>
<h2>SQL DEFAULT Constraint on CREATE TABLE</h2>
<p style="text-align: justify;">The following SQL creates a DEFAULT constraint on the &#8220;City&#8221; column when the &#8220;Persons&#8221; table is created:</p>
<p style="text-align: justify;"><b>My SQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT &#8216;Sandnes&#8217; )</p>
<p style="text-align: justify;">The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():</p>
<p style="text-align: center;">CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, OrderDate date DEFAULT GETDATE() )</p>
<h2>SQL DEFAULT Constraint on ALTER TABLE</h2>
<p style="text-align: justify;">To create a DEFAULT constraint on the &#8220;City&#8221; column when the table is already created, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ALTER City SET DEFAULT &#8216;SANDNES&#8217;</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ALTER COLUMN City SET DEFAULT &#8216;SANDNES&#8217;</p>
<h2>To DROP a DEFAULT Constraint</h2>
<p style="text-align: justify;">To drop a DEFAULT constraint, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ALTER City DROP DEFAULT</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ALTER COLUMN City DROP DEFAULT</p>
]]></content:encoded>
			<wfw:commentRss>http://alomohora.com/sql-default-constraint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL CHECK Constraint</title>
		<link>http://alomohora.com/sql-check-constraint/</link>
		<comments>http://alomohora.com/sql-check-constraint/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 08:35:29 +0000</pubDate>
		<dc:creator>alomohora</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://alomohora.com/?p=2225</guid>
		<description><![CDATA[The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The CHECK constraint is used to limit the value range that can be placed in a column.</p>
<p style="text-align: justify;"><span id="more-2225"></span></p>
<p style="text-align: justify;">If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.</p>
<h2>SQL CHECK Constraint on CREATE TABLE</h2>
<p style="text-align: justify;">The following SQL creates a CHECK constraint on the &#8220;P_Id&#8221; column when the &#8220;Persons&#8221; table is created. The CHECK constraint specifies that the column &#8220;P_Id&#8221; must only include integers greater than 0.</p>
<p style="text-align: justify;"><b>My SQL:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (P_Id&gt;0) )</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL CHECK (P_Id&gt;0), LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )</p>
<p style="text-align: justify;">To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT chk_Person CHECK (P_Id&gt;0 AND City=&#8217;Sandnes&#8217;) )</p>
<h2>SQL CHECK Constraint on ALTER TABLE</h2>
<p style="text-align: justify;">To create a CHECK constraint on the &#8220;P_Id&#8221; column when the table is already created, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ADD CHECK (P_Id&gt;0)</p>
<p style="text-align: justify;">To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the following SQL syntax:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ADD CONSTRAINT chk_Person CHECK (P_Id&gt;0 AND City=&#8217;Sandnes&#8217;)</p>
<h2>To DROP a CHECK Constraint</h2>
<p style="text-align: justify;">To drop a CHECK constraint, use the following SQL:</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons DROP CONSTRAINT chk_Person</p>
<h4>Incoming search terms for the article:</h4><p style="text-align: justify;"><a href="http://alomohora.com/sql-check-constraint/" title="how to drop check constraint in mysql">how to drop check constraint in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql constraint column values check mysql">sql constraint column values check mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access 2010 check constraint">access 2010 check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access 2010 constrain check">access 2010 constrain check</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check constraint in mysql">drop check constraint in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql drop check constraint">mysql drop check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql check constraint">mysql check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql check constraint on multiple columns">mysql check constraint on multiple columns</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql check constraints syntax">mysql check constraints syntax</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql constraint check">mysql constraint check</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql add check multiple columns">sql add check multiple columns</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql drop check constraints">mysql drop check constraints</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql - drop check constraint">mysql - drop check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mssql check constraint int range">mssql check constraint int range</a>, <a href="http://alomohora.com/sql-check-constraint/" title="ms sql check constraint on multiple columns">ms sql check constraint on multiple columns</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how to use check constraints in access sql">how to use check constraints in access sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how to use check constraints within access 2010">how to use check constraints within access 2010</a>, <a href="http://alomohora.com/sql-check-constraint/" title="make check constraint on access">make check constraint on access</a>, <a href="http://alomohora.com/sql-check-constraint/" title="microsoft access 2010 create check constraint">microsoft access 2010 create check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="microsoft access check constraints sql">microsoft access check constraints sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="microsoft access constraint check">microsoft access constraint check</a>, <a href="http://alomohora.com/sql-check-constraint/" title="microsoft access sql check constraint">microsoft access sql check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="ms access 2010 check constraint">ms access 2010 check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="ms access sql check constraint">ms access sql check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="mysql int range constraint">mysql int range constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="not able to use check constraint in ms acess">not able to use check constraint in ms acess</a>, <a href="http://alomohora.com/sql-check-constraint/" title="oracle alter table add constraints multiple">oracle alter table add constraints multiple</a>, <a href="http://alomohora.com/sql-check-constraint/" title="SQL constraint check">SQL constraint check</a>, <a href="http://alomohora.com/sql-check-constraint/" title="visual studio 2010 drop check constraints">visual studio 2010 drop check constraints</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql server check constraint">sql server check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql server check constraint multiple columns">sql server check constraint multiple columns</a>, <a href="http://alomohora.com/sql-check-constraint/" title="syntax check constraint 1 or 0 in sql">syntax check constraint 1 or 0 in sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="syntax check constraint sql server">syntax check constraint sql server</a>, <a href="http://alomohora.com/sql-check-constraint/" title="syntax for checking multiple constraints in mysql">syntax for checking multiple constraints in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="syntax for value constraints in ms access sql">syntax for value constraints in ms access sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql check_constraint">sql check_constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql check range">sql check range</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql check constraint for adding specific cities">sql check constraint for adding specific cities</a>, <a href="http://alomohora.com/sql-check-constraint/" title="oracle drop multiple check constraints">oracle drop multiple check constraints</a>, <a href="http://alomohora.com/sql-check-constraint/" title="oracle sql check range constraint">oracle sql check range constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="oracle sql constraint not">oracle sql constraint not</a>, <a href="http://alomohora.com/sql-check-constraint/" title="range constraint sql">range constraint sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql access2010 constraint">sql access2010 constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql add comment to check constraint">sql add comment to check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="Sql Add greater than 0 constraint">Sql Add greater than 0 constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="sql check constraint">sql check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="to allow naming of a check constraint and for defining a check constraint on multiple columns use the following sql syntax:">to allow naming of a check constraint and for defining a check constraint on multiple columns use the following sql syntax:</a>, <a href="http://alomohora.com/sql-check-constraint/" title="a check constraint in VS 2010">a check constraint in VS 2010</a>, <a href="http://alomohora.com/sql-check-constraint/" title="ADD CONSTRAINT access how to">ADD CONSTRAINT access how to</a>, <a href="http://alomohora.com/sql-check-constraint/" title="alter table ADD CHECK ( access">alter table ADD CHECK ( access</a>, <a href="http://alomohora.com/sql-check-constraint/" title="alter table add constraints mysql">alter table add constraints mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="check constraint access 2010">check constraint access 2010</a>, <a href="http://alomohora.com/sql-check-constraint/" title="check constraint for multiple words in sql server">check constraint for multiple words in sql server</a>, <a href="http://alomohora.com/sql-check-constraint/" title="check constraint in mysql example">check constraint in mysql example</a>, <a href="http://alomohora.com/sql-check-constraint/" title="check constraint on multiple columns mysql">check constraint on multiple columns mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="check constraint varchar within a range sql">check constraint varchar within a range sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="add column constraint in access for a range?">add column constraint in access for a range?</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access sql server check constraint">access sql server check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access sql DROP CONSTRAINT multiple columns">access sql DROP CONSTRAINT multiple columns</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access 2010 column constraint">access 2010 column constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access 2010 create table check constraint">access 2010 create table check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access 2010 sql alter table check">access 2010 sql alter table check</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access 2010 sql constraints">access 2010 sql constraints</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access check constraint">access check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access check constraint sql">access check constraint sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access sql">access sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="access sql check constraint">access sql check constraint</a>, <a href="http://alomohora.com/sql-check-constraint/" title="check constraints in mysql syntax">check constraints in mysql syntax</a>, <a href="http://alomohora.com/sql-check-constraint/" title="constraint check in ms access">constraint check in ms access</a>, <a href="http://alomohora.com/sql-check-constraint/" title="constraint chk_person check (p_id&gt;0 and city=\sandnes\)">constraint chk_person check (p_id&gt;0 and city=\sandnes\)</a>, <a href="http://alomohora.com/sql-check-constraint/" title="dropping check constraint in mysql">dropping check constraint in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="find schema of oracle database">find schema of oracle database</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how drop check constraint from mysql">how drop check constraint from mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how drop check constraints">how drop check constraints</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how to check constraints before dropping it in sql server">how to check constraints before dropping it in sql server</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how to check constraints in sql server on column">how to check constraints in sql server on column</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how to drop a check constraint in mysql">how to drop a check constraint in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check using mysql">drop check using mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="dropping a check constraint in mysql">dropping a check constraint in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop default constraint in sql server">drop default constraint in sql server</a>, <a href="http://alomohora.com/sql-check-constraint/" title="DROP CONSTRAINT MS ACCESS">DROP CONSTRAINT MS ACCESS</a>, <a href="http://alomohora.com/sql-check-constraint/" title="constraints value check access 2010 sql">constraints value check access 2010 sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="default constraintd in sql using ms access">default constraintd in sql using ms access</a>, <a href="http://alomohora.com/sql-check-constraint/" title="does check constraint work on ms access sql">does check constraint work on ms access sql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check access">drop check access</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check constraint microsoft">drop check constraint microsoft</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check constraint mysql">drop check constraint mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check constraints in oracle">drop check constraints in oracle</a>, <a href="http://alomohora.com/sql-check-constraint/" title="drop check in mysql">drop check in mysql</a>, <a href="http://alomohora.com/sql-check-constraint/" title="how to make check constraint in access 2010">how to make check constraint in access 2010</a></p>]]></content:encoded>
			<wfw:commentRss>http://alomohora.com/sql-check-constraint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL FOREIGN KEY Constraint</title>
		<link>http://alomohora.com/sql-foreign-key-constraint/</link>
		<comments>http://alomohora.com/sql-foreign-key-constraint/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 08:34:56 +0000</pubDate>
		<dc:creator>alomohora</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://alomohora.com/?p=2223</guid>
		<description><![CDATA[A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Let&#8217;s illustrate the foreign key with an example. Look at the following two tables: The &#8220;Persons&#8221; table: P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger The [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">A FOREIGN KEY in one table points to a PRIMARY KEY in another table.</p>
<p style="text-align: justify;"><span id="more-2223"></span></p>
<p style="text-align: justify;">Let&#8217;s illustrate the foreign key with an example. Look at the following two tables:</p>
<p style="text-align: justify;">The &#8220;Persons&#8221; table:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="center">P_Id</th>
<th align="center">LastName</th>
<th align="center">FirstName</th>
<th align="center">Address</th>
<th align="center">City</th>
</tr>
<tr>
<td align="center">1</td>
<td>Hansen</td>
<td>Ola</td>
<td>Timoteivn 10</td>
<td>Sandnes</td>
</tr>
<tr>
<td align="center">2</td>
<td>Svendson</td>
<td>Tove</td>
<td>Borgvn 23</td>
<td>Sandnes</td>
</tr>
<tr>
<td align="center">3</td>
<td>Pettersen</td>
<td>Kari</td>
<td>Storgt 20</td>
<td>Stavanger</td>
</tr>
</tbody>
</table>
<p></p>
<p style="text-align: justify;">The &#8220;Orders&#8221; table:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="center">O_Id</th>
<th align="center">OrderNo</th>
<th align="center">P_Id</th>
</tr>
<tr>
<td align="center">1</td>
<td>77895</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">2</td>
<td>44678</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">3</td>
<td>22456</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4</td>
<td>24562</td>
<td align="center">1</td>
</tr>
</tbody>
</table>
<p></p>
<p style="text-align: justify;">Note that the &#8220;P_Id&#8221; column in the &#8220;Orders&#8221; table points to the &#8220;P_Id&#8221; column in the &#8220;Persons&#8221; table. The &#8220;P_Id&#8221; column in the &#8220;Persons&#8221; table is the PRIMARY KEY in the &#8220;Persons&#8221; table. The &#8220;P_Id&#8221; column in the &#8220;Orders&#8221; table is a FOREIGN KEY in the &#8220;Orders&#8221; table. The FOREIGN KEY constraint is used to prevent actions that would destroy link between tables. The FOREIGN KEY constraint also prevents that invalid data is inserted into the foreign key column, because it has to be one of the values contained in the table it points to.</p>
<h2>SQL FOREIGN KEY Constraint on CREATE TABLE</h2>
<p style="text-align: justify;">The following SQL creates a FOREIGN KEY on the &#8220;P_Id&#8221; column when the &#8220;Orders&#8221; table is created:</p>
<p style="text-align: justify;"><b>MySQL:</b></p>
<p style="text-align: center;">CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Orders ( O_Id int NOT NULL PRIMARY KEY, OrderNo int NOT NULL, P_Id int FOREIGN KEY REFERENCES Persons(P_Id) )</p>
<p style="text-align: justify;">To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )</p>
<h2>SQL FOREIGN KEY Constraint on ALTER TABLE</h2>
<p style="text-align: justify;">To create a FOREIGN KEY constraint on the &#8220;P_Id&#8221; column when the &#8220;Orders&#8221; table is already created, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)</p>
<p style="text-align: justify;">To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Orders ADD CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)</p>
<h2>To DROP a FOREIGN KEY Constraint</h2>
<p style="text-align: justify;">To drop a FOREIGN KEY constraint, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL:</b></p>
<p style="text-align: center;">ALTER TABLE Orders DROP FOREIGN KEY fk_PerOrders</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Orders DROP CONSTRAINT fk_PerOrders</p>
<h4>Incoming search terms for the article:</h4><p style="text-align: justify;"><a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql foreign key constraint">sql foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 sql foreign key">access 2010 sql foreign key</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="what is this CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES">what is this CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key sql access 2010">foreign key sql access 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="CONSTRAINT fk_PerOrders">CONSTRAINT fk_PerOrders</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="ms sql add foreign key constraint">ms sql add foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="ms access foreign key references">ms access foreign key references</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="ms access foreign key constraint">ms access foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="ms access alter table add fk not null">ms access alter table add fk not null</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="ms access 2010 - foreign key reference">ms access 2010 - foreign key reference</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="microsoft access alter table foreign key multiple">microsoft access alter table foreign key multiple</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how use fk in sql 2010">how use fk in sql 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how to create table foreign key sql server">how to create table foreign key sql server</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how to create multiple foreign keys in sql table">how to create multiple foreign keys in sql table</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how to create a table in access 2010 sql with primary key and foreign key together">how to create a table in access 2010 sql with primary key and foreign key together</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how to add foreign key in sql server 2010">how to add foreign key in sql server 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how to access foreign key sql with constraint">how to access foreign key sql with constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="how add constraint in Access 2010">how add constraint in Access 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="MS SQL constraint">MS SQL constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="ms sql foreign key on multiple columns">ms sql foreign key on multiple columns</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="multiple foreign key references in ms access 2010">multiple foreign key references in ms access 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="what is an invalid foreign key constraint in MSS">what is an invalid foreign key constraint in MSS</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql why use foreign key constraint">sql why use foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql server foreign key constraint on multi column primary key">sql server foreign key constraint on multi column primary key</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql server 2010 create table foreign key constraint">sql server 2010 create table foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql server 2010 create table foreign key">sql server 2010 create table foreign key</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql foreign key constraint with multipe columns">sql foreign key constraint with multipe columns</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql for access 2010">sql for access 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql add foreign key constraint">sql add foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="sql 2010 create foreign key">sql 2010 create foreign key</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="p_id int foreign key references persons(p_id)">p_id int foreign key references persons(p_id)</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="orders and persons tables in which sql?">orders and persons tables in which sql?</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="oracle create table with foreign key constraint on multiple columns">oracle create table with foreign key constraint on multiple columns</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="mysql one to one sql foreign key create table">mysql one to one sql foreign key create table</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="multiple foreign keys sql server">multiple foreign keys sql server</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="hora en sql">hora en sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key sql server">foreign key sql server</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="alter table add multiple foreign key in oracle sql">alter table add multiple foreign key in oracle sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="add foreign keys sql oracle not mandatory">add foreign keys sql oracle not mandatory</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="add foreign key sql">add foreign key sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="add constraint Ms sql">add constraint Ms sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access sql foreign key reference multiple">access sql foreign key reference multiple</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access sql foreign key constraint">access sql foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="Access SQL constraint foreign key multiple">Access SQL constraint foreign key multiple</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access FOREIGN KEY">access FOREIGN KEY</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access alter table FOREIGN KEY sql">access alter table FOREIGN KEY sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 sql type constraint foreign key">access 2010 sql type constraint foreign key</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 sql foreign and primary keys">access 2010 sql foreign and primary keys</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 sql create table fields references">access 2010 sql create table fields references</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 primary key sql alter">access 2010 primary key sql alter</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 foreign key constraint">access 2010 foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="alter table delete foreing key sql2010">alter table delete foreing key sql2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)">CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="CONSTRAINT fk_PerOrders sql">CONSTRAINT fk_PerOrders sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key sql in access 2010">foreign key sql in access 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key one to one ms access sql">foreign key one to one ms access sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key multiple columns mssql">foreign key multiple columns mssql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key ms sql">foreign key ms sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key constraint on multiple columns in sql server">foreign key constraint on multiple columns in sql server</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="FOREIGN KEY constraint ms sql server multiple column">FOREIGN KEY constraint ms sql server multiple column</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="foreign key constraint">foreign key constraint</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="fk_PerOrders">fk_PerOrders</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="defining a FOREIGN KEY constraint on multiple columns in Sql">defining a FOREIGN KEY constraint on multiple columns in Sql</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="CREATE TABLE Orders ( Of Id int NOT NULL Order No int NOT NULL P_Id int PRIMARY KEY (O_Id) CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )">CREATE TABLE Orders ( Of Id int NOT NULL Order No int NOT NULL P_Id int PRIMARY KEY (O_Id) CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) )</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="create foreign key field referenced to existing column access 2010">create foreign key field referenced to existing column access 2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="constraint sqlserver2010">constraint sqlserver2010</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="constraint foreign key microsoft access">constraint foreign key microsoft access</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="constraint foreign key access">constraint foreign key access</a>, <a href="http://alomohora.com/sql-foreign-key-constraint/" title="access 2010 ALTER TABLE INTEGER PRIMARY KEY">access 2010 ALTER TABLE INTEGER PRIMARY KEY</a></p>]]></content:encoded>
			<wfw:commentRss>http://alomohora.com/sql-foreign-key-constraint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL PRIMARY KEY Constraint</title>
		<link>http://alomohora.com/sql-primary-key-constraint/</link>
		<comments>http://alomohora.com/sql-primary-key-constraint/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 08:20:50 +0000</pubDate>
		<dc:creator>alomohora</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://alomohora.com/?p=2216</guid>
		<description><![CDATA[The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only one primary key. SQL PRIMARY KEY Constraint on CREATE TABLE The following SQL creates a PRIMARY [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The PRIMARY KEY constraint uniquely identifies each record in a database table.</p>
<p style="text-align: justify;"><span id="more-2216"></span></p>
<p style="text-align: justify;">Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only one primary key.</p>
<h2>SQL PRIMARY KEY Constraint on CREATE TABLE</h2>
<p style="text-align: justify;">The following SQL creates a PRIMARY KEY on the &#8220;P_Id&#8221; column when the &#8220;Persons&#8221; table is created:</p>
<p style="text-align: justify;"><b>MySQL:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255),<br />
Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) )</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )</p>
<p style="text-align: justify;">To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) )</p>
<h2>SQL PRIMARY KEY Constraint on ALTER TABLE</h2>
<p style="text-align: justify;">To create a PRIMARY KEY constraint on the &#8220;P_Id&#8221; column when the table is already created, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ADD PRIMARY KEY (P_Id)</p>
<p style="text-align: justify;">To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:</p>
<p style="text-align: justify;"><b>MySQL / SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)</p>
<p style="text-align: justify;"><b>Note:</b> If you use the ALTER TABLE statement to add a primary key, the primary key column(s) must already have been declared to not contain NULL values (when the table was first created).</p>
<h2>To DROP a PRIMARY KEY Constraint</h2>
<p style="text-align: justify;">To drop a PRIMARY KEY constraint, use the following SQL:</p>
<p style="text-align: justify;"><b>MySQL:</b></p>
<p style="text-align: center;">ALTER TABLE Persons DROP PRIMARY KEY</p>
<p style="text-align: justify;"><b>SQL Server / Oracle / MS Access:</b></p>
<p style="text-align: center;">ALTER TABLE Persons DROP CONSTRAINT pk_PersonID</p>
<h4>Incoming search terms for the article:</h4><p style="text-align: justify;"><a href="http://alomohora.com/sql-primary-key-constraint/" title="create table primary key sql oracle">create table primary key sql oracle</a>, <a href="http://alomohora.com/sql-primary-key-constraint/" title="primary key constraint syntax">primary key constraint syntax</a></p>]]></content:encoded>
			<wfw:commentRss>http://alomohora.com/sql-primary-key-constraint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

