<?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>Tadlock Enterprises &#187; sql server</title>
	<atom:link href="http://www.tadlockenterprises.com/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tadlockenterprises.com</link>
	<description>A specialized west coast firm focused on product and technology vision, planning and execution.</description>
	<lastBuildDate>Thu, 18 Mar 2010 07:45:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Auto Generate a Code Column Value in SQL</title>
		<link>http://www.tadlockenterprises.com/2010/02/auto-generate-a-code-column-value-in-sql/</link>
		<comments>http://www.tadlockenterprises.com/2010/02/auto-generate-a-code-column-value-in-sql/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 21:58:09 +0000</pubDate>
		<dc:creator>Craig Tadlock</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[tips & tricks]]></category>
		<category><![CDATA[data design]]></category>
		<category><![CDATA[data model]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>

		<guid isPermaLink="false">http://www.tadlockenterprises.com/?p=437</guid>
		<description><![CDATA[Often in database design a table has a secondary unique key which defines a random alpha-numeric value; lets call this Code. While the table&#8217;s primary key is used for foreign key references in the database, the Code column has several advantages and uses. Since it is not a sequential value it can be shown to [...]]]></description>
			<content:encoded><![CDATA[<p>Often in database design a table has a secondary unique key which defines a random alpha-numeric value; lets call this Code. While the table&#8217;s primary key is used for foreign key references in the database, the Code column has several advantages and uses. Since it is not a sequential value it can be shown to the user without giving away possible valuable information; for example <a href="http://www.techcrunch.com/2010/02/04/nexus-one-google-sales/" target="_blank">giving away your sales volume</a>. It can also be used as a URL query parameter to discourage one-up-attacks (note this should not be a replacement for proper data security).</p>
<p>So now that we are sold on having a Code column, what is the best way to implement it? Here is a method you can use to implement a proper Code column and have the database generate the value for you. This is a similar design pattern to using an <em>IDENTITY </em>column to generate the next value for a primary key. We would like for the column to have a default value of a new Code value, such that the application tier doesn&#8217;t have to worry about setting the value. An issue with this in SQL Server is that column default values do not allow for &#8216;dynamic&#8217; values; we can use a view to get around this. As for the Code generation algorithm I piggybacked on the <em>NEWID()</em> function which generates a new <em>uniqueidentifier</em>. There are countless other algorithms you could use for this.</p>
<p>Create a view to generate the new codes&#8230;</p>
<p><em>CREATE VIEW [dbo].[Codes]<br />
AS<br />
SELECT LOWER(SUBSTRING(REPLACE(NEWID(), &#8216;-&#8217;, &#8221;), 0, 16)) AS Code;</em></p>
<p>Create a function which returns a new code&#8230;</p>
<p><em>CREATE FUNCTION [dbo].[NewCode]<br />
( )<br />
RETURNS NVARCHAR (50)<br />
AS<br />
BEGIN<br />
RETURN (SELECT Code FROM Codes)<br />
END</em></p>
<p>Now we can use the <em>dbo.NewCode()</em> function as the default value for our table&#8217;s Code column..</p>
<p><em>ALTER TABLE [dbo].[XXX] ADD  CONSTRAINT [DF_XXX_Code]  DEFAULT ([dbo].[NewCode]()) FOR [Code]</em></p>
<p>That&#8217;s it! Now if you insert a row into the table the Code column will have a new value in it by default. Simple.</p>
<p><strong>UPDATE</strong></p>
<p>Here is a much better algorithm to generate codes. The previous algorithm has a &#8216;hidden&#8217; issue; the <em>NEWID()</em> function only generates the letters A-H, so you don&#8217;t get the security of the full 26 character alphabet. The algorithm below also lets you choose which characters you want. This allows you to prevent issues like 1 and I, and 0 and O confusion.</p>
<p><em>CREATE VIEW [dbo].[Codes]<br />
AS<br />
select<br />
[Code] =<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1) +<br />
substring(ch, convert(int, RAND() * (LEN(ch) &#8211; 1)) + 1, 1)<br />
from<br />
(select ch = &#8216;ABCDEFGHJKLMNPQURSUVWXYZ23456789&#8242;) a</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tadlockenterprises.com/2010/02/auto-generate-a-code-column-value-in-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
