<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Journey to SQL Authority with Pinal Dave &#187; SQL Constraint and Keys</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-constraint-and-keys/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 01:30:19 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Journey to SQL Authority with Pinal Dave &#187; SQL Constraint and Keys</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
			<item>
		<title>SQL SERVER &#8211; Disable CHECK Constraint &#8211; Enable CHECK Constraint</title>
		<link>http://blog.sqlauthority.com/2009/11/12/sql-server-disable-check-constraint-enable-check-constraint/</link>
		<comments>http://blog.sqlauthority.com/2009/11/12/sql-server-disable-check-constraint-enable-check-constraint/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 01:30:14 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7198</guid>
		<description><![CDATA[Foreign Key and Check Constraints are two types of constraints that can be disabled or enabled when required. This type of operation is needed when bulk loading operations are required or when there is no need to validate the constraint. The T-SQL Script that does the same is very simple.
USE AdventureWorks
GO
-- Disable the constraint
ALTER TABLE HumanResources.Employee
NOCHECK CONSTRAINT CK_Employee_BirthDate
GO
-- Enable the constraint
ALTER TABLE HumanResources.Employee
WITH CHECK CHECK CONSTRAINT CK_Employee_BirthDate
GO
It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7198&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Foreign Key and Check Constraints are two types of constraints that can be disabled or enabled when required. This type of operation is needed when bulk loading operations are required or when there is no need to validate the constraint. The T-SQL Script that does the same is very simple.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- Disable the constraint<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">NOCHECK CONSTRAINT </span><span style="color:black;">CK_Employee_BirthDate<br />
GO<br />
</span><span style="color:green;">-- Enable the constraint<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">HumanResources.Employee<br />
</span><span style="color:blue;">WITH CHECK CHECK CONSTRAINT </span><span style="color:black;">CK_Employee_BirthDate<br />
GO</span></code></p>
<p style="text-align:justify;">It is very interesting that when the constraint is enabled, the world CHECK is used twice – WITH CHECK CHECK CONSTRAINT. I often ask those to find the mistake in this script when they claim to know the syntax very well.</p>
<p style="text-align:justify;">Have you ever disabled and enabled constraints in your production environment? I would like to know why you did so.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/">http://blog.sqlauthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7198/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7198/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7198/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7198&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/12/sql-server-disable-check-constraint-enable-check-constraint/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between Candidate Keys and Primary Key</title>
		<link>http://blog.sqlauthority.com/2009/10/22/sql-server-difference-between-candidate-keys-and-primary-key-2/</link>
		<comments>http://blog.sqlauthority.com/2009/10/22/sql-server-difference-between-candidate-keys-and-primary-key-2/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 01:30:18 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7111</guid>
		<description><![CDATA[Introduction
Not long ago, I had an interesting and extended debate with one of my friends regarding which column should be primary key in a table. The debate instigated an in-depth discussion about candidate keys and primary keys. My present article revolves around the two types of keys.
Let us first try to grasp the definition of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7111&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h3 style="text-align:justify;">Introduction</h3>
<p style="text-align:justify;">Not long ago, I had an interesting and extended debate with one of my friends regarding which column should be primary key in a table. The debate instigated an in-depth discussion about candidate keys and primary keys. My present article revolves around the two types of keys.</p>
<p style="text-align:justify;">Let us first try to grasp the definition of the two keys.</p>
<p style="text-align:justify;"><strong>Candidate Key </strong> &#8211; A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.</p>
<p style="text-align:justify;"><strong>Primary Key </strong> &#8211; A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key.</p>
<p style="text-align:justify;">One needs to be very careful in selecting the Primary Key as an incorrect selection can adversely impact the database architect and future normalization. For a Candidate Key to qualify as a Primary Key, it should be Non-NULL and unique in any domain. I have observed quite often that Primary Keys are seldom changed. I would like to have your feedback on not changing a Primary Key.</p>
<h3 style="text-align:justify;">An Example to Understand Keys</h3>
<p style="text-align:justify;">Let us look at an example where we have multiple Candidate Keys, from which we will select an appropriate Primary Key.</p>
<p style="text-align:justify;">Given below is an example of a table having three columns that can qualify as single column Candidate Key, and on combining more than one column the number of possible Candidate Keys touches seven. A point to remember here is that only one column can be selected as Primary Key. The decision of Primary Key selection from possible combinations of Candidate Key is often very perplexing but very imperative!</p>
<p style="text-align:justify;"><img src="http://www.pinaldave.com/bimg/sql-primary-candidate-key.gif" alt="" width="400" height="690" /></p>
<p style="text-align:justify;">On running the following script it will always give 504 rows in all the options. This proves that they are all unique in database and meet the criteria of a Primary Key.</p>
<p style="text-align:justify;">Run the following script to verify if all the tables have unique values or not.</p>
<div id="highlighter_210459" style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product<br />
GO<br />
</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">ProductID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product<br />
GO<br />
</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">Name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product<br />
GO<br />
</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">ProductNumber<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.Product<br />
GO </span></code></div>
<p style="text-align:justify;">All of the above queries will return the same number of records; hence, they all qualify as Candidate Keys. In other words, they are the candidates for Primary Key. There are few points to consider while turning any Candidate Key into a Primary Key.</p>
<h4 style="text-align:justify;">Select a key that does not contain NULL</h4>
<p style="text-align:justify;">It may be possible that there are Candidate Keys that presently do not contain value (not null) but technically they can contain null. In this case, they will not qualify for Primary Key. In the following table structure, we can see that even though column <code>[name] </code> does not have any NULL value it does not qualify as it has the potential to contain NULL value in future.</p>
<div id="highlighter_429139" style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[Production].[Product]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ProductID] [int] </span><span style="color:#434343;">IDENTITY</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:black;">1</span><span style="color:gray;">) NOT NULL,<br />
</span><span style="color:black;">[Name] [dbo].[Name] </span><span style="color:gray;">NULL,<br />
</span><span style="color:black;">[ProductNumber] [nvarchar]</span><span style="color:gray;">(</span><span style="color:black;">25</span><span style="color:gray;">) NOT NULL,<br />
</span><span style="color:black;">[Manufacturer] [nvarchar]</span><span style="color:gray;">(</span><span style="color:black;">25</span><span style="color:gray;">) NOT NULL<br />
)</span></code></div>
<h4 style="text-align:justify;">Select a key that is unique and does not repeat</h4>
<p style="text-align:justify;">It may be possible that Candidate Keys that are unique at this moment may contain duplicate value. These kinds of Candidate Keys do not qualify for Primary Key. Let us understand this scenario by looking into the example given above. It is absolutely possible that two Manufacturers can create products with the same name; the resulting name will be a duplicate and only the name of the Manufacturer will differ in the table. This disqualifies Name in the table to be a Primary Key.</p>
<h4 style="text-align:justify;">Make sure that Primary Key does not keep changing</h4>
<p style="text-align:justify;">This is not a hard and fast rule but rather a general recommendation: Primary Key values should not keep changing. It is quite convenient for a database if Primary Key is static. Primary Keys are referenced in numerous places in the database, from Index to Foreign Keys.  If they keep changing then they can adversely affect database integrity, data statistics as well as internal of Indexes.</p>
<h3 style="text-align:justify;">Selection of Primary Key</h3>
<p style="text-align:justify;">Let us examine our case by applying the above three rules to the table and decide on the appropriate candidate for Primary Key. Name can contain NULL so it disqualifies as per Rule 1 and Rule 2. Product Number can be duplicated for different Manufacturers so it disqualifies as per Rule 2. ProductID is Identity and Identity column cannot be modified. So, in this case ProductID qualifies as Primary Key.</p>
<p style="text-align:justify;">Please note that many database experts suggest that it is not a good practice to make Identity Column as Primary Key. The reason behind this suggestion is that many times Identity Column that has been assigned as Primary Key does not play any role in database. There is no use of this Primary Key in both application and in T-SQL. Besides, this Primary Key may not be used in Joins. It is a known fact that when there is JOIN on Primary Key or when Primary Key is used in the WHERE condition it usually gives better performance than non primary key columns. This argument is absolutely valid and one must make sure not to use such Identity Column. However, our example presents a different case. Here, although ProductID is Identity Column it uniquely defines the row and the same column will be used as foreign key in other tables. If a key is used in any other table as foreign key it is likely that it will be used in joins.</p>
<h3 style="text-align:justify;">Quick Note on Other Kinds of Keys</h3>
<p style="text-align:justify;">The above paragraph evokes another question &#8211; what is a foreign key? A foreign key in a database table is a key from another table that refers to the primary key in the table being used. A primary key can be referred by multiple foreign keys from other tables. It is not required for a primary key to be the reference of any foreign keys. The interesting part is that a foreign key can refer back to the same table but to a different column. This kind of foreign key is known as &#8220;self-referencing foreign key&#8221;.</p>
<h3 style="text-align:justify;">Summary</h3>
<p style="text-align:justify;">A table can have multiple Candidate Keys that are unique as single column or combined multiple columns to the table. They are all candidates for Primary Key. Candidate keys that follow all the three rules &#8211; 1) Not Null, 2) Unique Value in Table and 3) Static &#8211; are the best candidates for Primary Key. If there are multiple candidate keys that are satisfying the criteria for Primary Key, the decision should be made by experienced DBAs who should keep performance in mind.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/">http://blog.sqlauthority.com</a>)</strong>, <a href="http://dotnetslackers.com/articles/sql/Difference-Between-Candidate-Keys-and-Primary-Key.aspx" target="_blank">DNS</a></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7111&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/22/sql-server-difference-between-candidate-keys-and-primary-key-2/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/sql-primary-candidate-key.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Discussion &#8211; Effect of Missing Identity on System &#8211; Real World Scenario</title>
		<link>http://blog.sqlauthority.com/2009/08/11/sql-server-discussion-effect-of-missing-identity-on-system-real-world-scenario/</link>
		<comments>http://blog.sqlauthority.com/2009/08/11/sql-server-discussion-effect-of-missing-identity-on-system-real-world-scenario/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6524</guid>
		<description><![CDATA[About a week ago, SQL Server Expert, Imran Mohammed, provided a script, which will list all the missing identity values of a table in a database. In this post, I asked my readers if any could write a similar or better script. The results were interesting. While no one provided a new script, my question [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6524&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">About a week ago, SQL Server Expert, <strong>Imran Mohammed,</strong> provided a script, which will list all the missing identity values of a table in a database. In this <a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/">post</a>, I asked my readers if any could write a similar or better script. The results were interesting. While no one provided a new script, my question sparked a very active <a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/">discussion</a> that is still ongoing.</p>
<p style="text-align:justify;">When providing the script, Imran asked me if I knew of any specific circumstances in which this kind of query could be useful, as he could not think of an instance where it would be necessary to find a missing identity. I was unable to think of a single reason for listing missing identities in a table. I posted Imran&#8217;s script on the assumption that someone would come up with an improved script, but as mentioned earlier, nobody did. Instead, we have been able to follow a very interesting discussion on subject of the need, if any, for listing Missing Identity values.</p>
<p style="text-align:justify;">So, the question is this: &#8220;<strong><em>Do you know a real-world scenario where a Missing Identity value in any table can create problems?</em></strong>&#8220;</p>
<p style="text-align:justify;">I have already received some extremely interesting comments from many experts, and all have posed the above question in one form or another. At this moment, I am still trying to think of an example from my own experience, but have yet to find one. Imran has since come up with one good example. Here is what he and other experts have suggested so far.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54117" target="_blank"><strong>Jacob Sebastian</strong></a> &#8211; IDENTITY values are not expected to be sequential and there are all chances of having missing identity values, the most common cause is transaction rollbacks.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54139" target="_blank"><strong>Simon Worth</strong></a> &#8211; The identity column is basically just a random number – even though they come sequentially. A developer making an assumption that the next record inserted will have an identity that is 1 more than the last inserted record. And if this is the case – then there are flaws in the logic of the developer.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54158" target="_blank"><strong>Jacob Sebastian</strong></a> &#8211; What if the value in my table is 1, 2, 3, 5, 6 etc where “4″ is missing from the sequence. So what is the importance of knowing whether a table has missing identity values or not?</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comment-54184" target="_blank"><strong>Imran Mohammed</strong></a> &#8211; If you use Identity property as your most unique column and Transaction Identifier, then definitely you would want to know why few transaction did not completely, Is there any specific fashion these transaction fails (Can be found out looking at missing values of identity)&#8230; Could be helpful to debug.</p>
<p style="text-align:justify;">Now it is your turn. Let us have your thoughts.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Database, DBA, Pinal Dave, Readers Contribution, Readers Question, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Puzzle, SQL Query, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6524/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6524/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6524/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6524&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/08/11/sql-server-discussion-effect-of-missing-identity-on-system-real-world-scenario/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Puzzle &#8211; Write Script to Generate Primary Key and Foreign Key</title>
		<link>http://blog.sqlauthority.com/2009/07/23/sql-server-puzzle-write-script-to-generate-primary-key-and-foreign-key/</link>
		<comments>http://blog.sqlauthority.com/2009/07/23/sql-server-puzzle-write-script-to-generate-primary-key-and-foreign-key/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Challenge]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6032</guid>
		<description><![CDATA[In one of my recent projects, a large database migration project, I confronted a peculiar situation. SQL Server tables were already moved from Database_Old to Database_New. However, all the Primary Key and Foreign Keys were yet to be moved from the old server to the new server.
Please note that this puzzle is to be solved [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6032&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">In one of my recent projects, a large database migration project, I confronted a peculiar situation. SQL Server tables were already moved from Database_Old to Database_New. However, all the Primary Key and Foreign Keys were yet to be moved from the old server to the new server.</p>
<p style="text-align:justify;">Please note that this puzzle is to be solved for <strong>SQL Server 2005</strong> or <strong>SQL Server 2008</strong>. As noted by Kuldip it is possible to do this in SQL Server 2000.</p>
<p style="text-align:justify;">In SQL Server Management Studio (SSMS), there is no option to script all the keys. If one is required to script keys they will have to manually script each key one at a time. If database has many tables, generating one key at a time can be a very intricate task. I want to throw a question to all of you if any of you have script for the same purpose.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/pkgen.jpg" alt="" width="500" height="364" /></p>
<p style="text-align:justify;">As per my opinion, I think the challenge is to get orders of the column included in Primary Key as well on the filegroup they exist.</p>
<p style="text-align:justify;">Please note here that I am not looking for names of Primary Key or Foreign Key of database. I have already written an article for the same here : <a href="http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/" target="_blank">SQL SERVER – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database</a> . I am looking for T-SQL script that generates Primary Key from the existing table for all tables in database. There are already a couple of answers on my post here from two SQL Experts; you can refer to those for example <a href="http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/" target="_blank">here</a>.</p>
<p style="text-align:justify;">Following is an example of T-SQL that we need to generate. I<strong> am looking for script that will generate T-SQL script for all the Primary Key and Foreign Key for the entire database.</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">ALTER TABLE </span><span style="color:black;">[Person].[Address] </span><span style="color:blue;">ADD  CONSTRAINT </span><span style="color:black;">[PK_Address_AddressID] </span><span style="color:blue;">PRIMARY KEY CLUSTERED<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[AddressID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">If you have a solution for the same, please post here or email me at pinal &#8216;at&#8217; sqlauthority.com and I will post on this blog with due credit. Again, please spread the word and help community become stronger by your active participation.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<p style="text-align:justify;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology Tagged: SQL Challenge <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6032/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6032/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6032/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6032&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/07/23/sql-server-puzzle-write-script-to-generate-primary-key-and-foreign-key/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/pkgen.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database</title>
		<link>http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/</link>
		<comments>http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 01:30:18 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5900</guid>
		<description><![CDATA[There are two different methods of retrieving the list of Primary Keys and Foreign Keys from database.
Method 1: INFORMATION_SCHEMA
SELECT
DISTINCT
Constraint_Name AS [Constraint],
Table_Schema AS [Schema],
Table_Name AS [TableName]
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
GO 

Method 2: sys.objects
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects 
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT','PRIMARY_KEY_CONSTRAINT')
GO


I am often asked about my preferred method of retrieving list of Primary [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5900&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">There are two different methods of retrieving the list of Primary Keys and Foreign Keys from database.</p>
<p style="text-align:justify;"><strong>Method 1: INFORMATION_SCHEMA</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
DISTINCT<br />
</span><span style="color:black;">Constraint_Name </span><span style="color:blue;">AS </span><span style="color:black;">[Constraint]</span><span style="color:gray;">,<br />
</span><span style="color:black;">Table_Schema </span><span style="color:blue;">AS </span><span style="color:black;">[Schema]</span><span style="color:gray;">,<br />
</span><span style="color:black;">Table_Name </span><span style="color:blue;">AS </span><span style="color:black;">[TableName]<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">INFORMATION_SCHEMA.KEY_COLUMN_USAGE<br />
GO </span></code>
</p>
<p style="text-align:justify;"><strong>Method 2: sys.objects</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">NameofConstraint</span><span style="color:gray;">,<br />
</span><span style="color:black;">SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:black;">schema_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">SchemaName</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">parent_object_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">TableName</span><span style="color:gray;">,<br />
</span><span style="color:black;">type_desc </span><span style="color:blue;">AS </span><span style="color:black;">ConstraintType<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.objects </span><span style="color:green;"><br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">type_desc </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:red;">'FOREIGN_KEY_CONSTRAINT'</span><span style="color:gray;">,</span><span style="color:red;">'PRIMARY_KEY_CONSTRAINT'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code>
</p>
<p style="text-align:justify;">I am often asked about my preferred method of retrieving list of Primary Keys and Foreign Keys from database. I have a standard answer. I prefer method 3, which is querying sys database. The reason is very simple. sys. schema always provides more information and all the data can be retrieved in our preferred fashion with the preferred filter.</p>
<p style="text-align:justify;">Let us look at the example we have on our hand. When Information Schema is used, we will not be able to discern between primary key and foreign key; we will have both the keys together. In the case of sys schema, we can query the data in our preferred way and can join this table to another table, which can retrieve additional data from the same.</p>
<p style="text-align:justify;">Let us play a small puzzle here. Try to modify both the scripts in such a way that we are able to see the original definition of the key, that is, create a statement for this primary key and foreign key.</p>
<p style="text-align:justify;">If I get an appropriate answer from my readers, I will publish the solution on this blog with due credit.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5900/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5900/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5900/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5900&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/07/17/sql-server-two-methods-to-retrieve-list-of-primary-keys-and-foreign-keys-of-database/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Clustered Index on Separate Drive From Table Location</title>
		<link>http://blog.sqlauthority.com/2009/06/18/sql-server-clustered-index-on-separate-drive-from-table-location/</link>
		<comments>http://blog.sqlauthority.com/2009/06/18/sql-server-clustered-index-on-separate-drive-from-table-location/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5491</guid>
		<description><![CDATA[How to improve performance of SQL Server Queries is a common topic of discussion among many of us. Much has been said, much has been discussed. Few days back, I had an interesting discussion with one of the Junior developers regarding performance improvement of SQL Server Queries. We discussed on how by using a separate [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5491&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><strong>How to improve performance of SQL Server Queries</strong> is a common topic of discussion among many of us. Much has been said, much has been discussed. Few days back, I had an interesting discussion with one of the Junior developers regarding performance improvement of SQL Server Queries. We discussed on how by using a separate hard drive for several database objects can right away improve performance. I suggested him that non clustered index and tempdb can be created on a separate disk to improve performance.</p>
<p style="text-align:justify;">No sooner had I given my suggestion than I received a question &#8211; What will happen if we can create clustered index on a separate drive from the table on which it is built.</p>
<p style="text-align:justify;">My answer is : <strong>No! </strong>It is not possible at all.</p>
<p style="text-align:justify;">Let us first be clear about the difference between a clustered and a non clustered index.</p>
<p style="text-align:justify;"><strong>Clustered Index</strong></p>
<ul>
<li>Only 1 allowed per table</li>
<li>Physically rearranges data in the table to conform to the index constraints</li>
<li>For use on columns that are frequently searched for ranges of data</li>
<li>For use on columns with low selectivity</li>
</ul>
<p style="text-align:justify;"><strong>Non-Clustered Index</strong></p>
<ul>
<li>Up to 249 (for SQL Server 2005) and 999 (for SQL Server 2008) allowed per table</li>
<li>Creates a separate list of key values with pointers to the location of the data in the data pages</li>
<li>For use on columns that are searched for single values</li>
<li>For use on columns with high selectivity</li>
</ul>
<p style="text-align:justify;">A table devoid of primary key index is called heap, and here data is not arranged in a particular order, which gives rise to issues that adversely affect performance. Data must be stored in some kind of order. If we put clustered index on it then the order will be forced by that index and the data will be stored in that particular order.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5491/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5491&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/18/sql-server-clustered-index-on-separate-drive-from-table-location/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between Candidate Keys and Primary Key</title>
		<link>http://blog.sqlauthority.com/2009/05/30/sql-server-difference-between-candidate-keys-and-primary-key/</link>
		<comments>http://blog.sqlauthority.com/2009/05/30/sql-server-difference-between-candidate-keys-and-primary-key/#comments</comments>
		<pubDate>Sat, 30 May 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5208</guid>
		<description><![CDATA[Let us first try to grasp the definition of the two keys.
Candidate Key  &#8211; A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.
Primary Key  &#8211; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5208&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Let us first try to grasp the definition of the two keys.</p>
<p style="text-align:justify;"><strong>Candidate Key </strong> &#8211; A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.</p>
<p style="text-align:justify;"><strong>Primary Key </strong> &#8211; A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key.</p>
<p style="text-align:justify;">One needs to be very careful in selecting the Primary Key as an incorrect selection can adversely impact the database architect and future normalization. For a Candidate Key to qualify as a Primary Key, it should be Non-NULL and unique in any domain. I have observed quite often that Primary Keys are seldom changed. I would like to have your feedback on not changing a Primary Key.</p>
<p style="text-align:justify;">I have <span>illustrates the difference between a candidate key and a primary key in SQL Server.</span></p>
<p style="text-align:justify;">1 Introduction<br />
2 An Example to Understand Keys<br />
2.1 Select a key that does not contain NULL<br />
2.2 Select a key that is unique and does not repeat<br />
2.3 Make sure that Primary Key does not keep changing<br />
3 Selection of Primary Key<br />
4 Quick Note on Other Kinds of Keys<br />
5 Summary</p>
<h3 style="text-align:justify;"><strong><a href="http://dotnetslackers.com/articles/sql/Difference-Between-Candidate-Keys-and-Primary-Key.aspx" target="_blank">Read Complete Article Here</a></strong></h3>
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5208/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5208&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/05/30/sql-server-difference-between-candidate-keys-and-primary-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; How to Drop Primary Key Contraint</title>
		<link>http://blog.sqlauthority.com/2009/05/12/sql-server-how-to-drop-primary-key-contraint/</link>
		<comments>http://blog.sqlauthority.com/2009/05/12/sql-server-how-to-drop-primary-key-contraint/#comments</comments>
		<pubDate>Tue, 12 May 2009 01:30:15 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4555</guid>
		<description><![CDATA[One area that always, unfailingly pulls my interest is SQL Server Errors and their solution. I enjoy the challenging task of passing through the maze of error to find a way out with a perfect solution. However, when I received the following error from one of my regular readers, I was a little stumped at [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4555&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">One area that always, unfailingly pulls my interest is SQL Server Errors and their solution. I enjoy the challenging task of passing through the maze of error to find a way out with a perfect solution. However, when I received the following error from one of my regular readers, I was a little stumped at first! After some online probing, I figured out that it was actually syntax from MySql and not SQL Server. The reader encountered error when he ran the following query.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">ALTER TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">DROP PRIMARY KEY<br />
</span><span style="color:black;">GO </span></code></p>
<p style="text-align:justify;"><span style="color:#ff0000;">Msg 156, Level 15, State 1, Line 3<br />
Incorrect syntax near the keyword &#8216;PRIMARY&#8217;.</span>
</p>
<p style="text-align:justify;">As mentioned earlier, this syntax is for MySql, not SQL Server. If you want to drop primary key constraint in SQL Server, run the following query.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">ALTER TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">DROP CONSTRAINT </span><span style="color:black;">PK_Table1_Col1<br />
GO </span></code>
</p>
<p style="text-align:justify;">Let us now pursue the complete example. First, we will create a table that has primary key. Next, we will drop the primary key successfully using the correct syntax of SQL Server.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">Table1</span><span style="color:gray;">(<br />
</span><span style="color:black;">Col1 </span><span style="color:blue;">INT </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">Col2 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">)<br />
</span><span style="color:blue;">CONSTRAINT </span><span style="color:black;">PK_Table1_Col1 </span><span style="color:blue;">PRIMARY KEY CLUSTERED </span><span style="color:gray;">(<br />
</span><span style="color:black;">Col1 </span><span style="color:blue;">ASC</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;"><br />
/* For SQL Server/Oracle/MS ACCESS */<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">DROP CONSTRAINT </span><span style="color:black;">PK_Table1_Col1<br />
GO</span></code></p>
<p><code style="font-size:12px;"><span style="color:green;">/* For MySql */<br />
</span><span style="color:blue;">ALTER TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">DROP PRIMARY KEY<br />
</span><span style="color:black;">GO </span></code> <span style="color:black;"></span>
</p>
<p style="text-align:justify;"><span style="color:black;">I hope this example lucidly explains how to drop primary key. This, no doubt, is a very simple and basic explanation, but when I chanced upon the error message it aroused curiosity in me.  As you all know by now I love sharing new issues and ideas with my readers. So I have included this interesting error in my blog. </span></p>
<p style="text-align:justify;"><span style="color:black;">Let me have your feedback on this post and also, do feel free to share with me your ideas as well!</span></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4555/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4555/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4555/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4555&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/05/12/sql-server-how-to-drop-primary-key-contraint/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Find Relationship of Foreign Key and Primary Key using T-SQL &#8211; Find Tables With Foreign Key Constraint in Database</title>
		<link>http://blog.sqlauthority.com/2009/02/26/sql-server-2008-find-relationship-of-foreign-key-and-primary-key-using-t-sql-find-tables-with-foreign-key-constraint-in-database/</link>
		<comments>http://blog.sqlauthority.com/2009/02/26/sql-server-2008-find-relationship-of-foreign-key-and-primary-key-using-t-sql-find-tables-with-foreign-key-constraint-in-database/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 01:30:16 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2799</guid>
		<description><![CDATA[While searching for how to find Primary Key and Foreign Key relationship using T-SQL, I came across my own blog article written earlier SQL SERVER &#8211; 2005 &#8211; Find Tables With Foreign Key Constraint in Database. It is really handy script and not found written on line anywhere. This is one really unique script and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2799&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">While searching for how to find Primary Key and Foreign Key relationship using T-SQL, I came across my own blog article written earlier <strong><a href="http://blog.sqlauthority.com/2007/09/04/sql-server-2005-find-tables-with-foreign-key-constraint-in-database/" target="_blank">SQL SERVER &#8211; 2005 &#8211; Find Tables With Foreign Key Constraint in Database</a></strong>. It is really handy script and not found written on line anywhere. This is one really unique script and must be bookmarked. There may be situations when there is need to find out on relationship between Primary Key and Foreign Key.</p>
<p style="text-align:justify;">I have modified my previous script to add schema name along with table name. It would be really great if any of you can improve on this script.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">f.name </span><span style="color:blue;">AS </span><span style="color:black;">ForeignKey</span><span style="color:gray;">,<br />
</span><span style="color:black;">SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:black;">f.SCHEMA_ID</span><span style="color:gray;">) </span><span style="color:black;">SchemaName</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">f.parent_object_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">TableName</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">COL_NAME</span><span style="color:gray;">(</span><span style="color:black;">fc.parent_object_id</span><span style="color:gray;">,</span><span style="color:black;">fc.parent_column_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ColumnName</span><span style="color:gray;">,<br />
</span><span style="color:black;">SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:black;">o.SCHEMA_ID</span><span style="color:gray;">) </span><span style="color:black;">ReferenceSchemaName</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME </span><span style="color:gray;">(</span><span style="color:black;">f.referenced_object_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ReferenceTableName</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">COL_NAME</span><span style="color:gray;">(</span><span style="color:black;">fc.referenced_object_id</span><span style="color:gray;">,</span><span style="color:black;">fc.referenced_column_id</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ReferenceColumnName<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.foreign_keys </span><span style="color:blue;">AS </span><span style="color:black;">f<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.foreign_key_columns </span><span style="color:blue;">AS </span><span style="color:black;">fc<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">sys.objects </span><span style="color:blue;">AS </span><span style="color:black;">o </span><span style="color:blue;">ON </span><span style="color:black;">o.</span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:black;">fc.referenced_object_id<br />
</span><span style="color:blue;">ON </span><span style="color:black;">f.</span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:black;">fc.constraint_object_id<br />
GO </span></code>
</p>
<p style="text-align:justify;">
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/keylist.jpg" alt="" width="499" height="496" /></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2799/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2799/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2799/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2799/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2799/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2799/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2799/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2799/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2799/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2799/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2799&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/26/sql-server-2008-find-relationship-of-foreign-key-and-primary-key-using-t-sql-find-tables-with-foreign-key-constraint-in-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/keylist.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Observation &#8211; Effect of Clustered Index over Nonclustered Index</title>
		<link>http://blog.sqlauthority.com/2009/02/21/sql-server-observation-effect-of-clustered-index-over-nonclustered-index-2/</link>
		<comments>http://blog.sqlauthority.com/2009/02/21/sql-server-observation-effect-of-clustered-index-over-nonclustered-index-2/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 01:30:48 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2754</guid>
		<description><![CDATA[Note: This article is re-write of my previous article SQL SERVER &#8211; Observation &#8211; Effect of Clustered Index over Nonclustered Index. I have received so many request that re-write it as it is little confusing. I am going to re-write this with simpler words.
Query optimization is one art which is difficult to master. Just like [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2754&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><em><strong>Note: This article is re-write of my previous article <a href="http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/" target="_blank">SQL SERVER &#8211; Observation &#8211; Effect of Clustered Index over Nonclustered Index</a>. I have received so many request that re-write it as it is little confusing. I am going to re-write this with simpler words.</strong></em></p>
<p style="text-align:justify;">Query optimization is one art which is difficult to master. Just like any other art this requires creativity and imagination as well understanding of subject matter. Let us look at interesting observation which I came across.</p>
<p style="text-align:justify;">First of all<strong> </strong><a href="http://www.pinaldave.com/bimg/IndexObservation.zip" target="_blank"><strong>download</strong> </a>the script from here and run it in SSMS.</p>
<p style="text-align:justify;">Now enable Execution Plan (Using CTRL + M) in SSMS before running the script.</p>
<p style="text-align:justify;">The simple objective of this whole exercise is to understand how clustered index and nonclustered indexes are associated with each other.</p>
<p style="text-align:justify;"><em><strong>In our example we have one query which is not using any index. On the same table there is already nonclustered index created, which is also not being used. Now when we created clustered index on the same table, our query suddenly started to use nonclustered index which was so far it is not using. The interesting part of this is that query is using nonclustered index when clustered index is created on the same.</strong></em></p>
<p style="text-align:justify;">Now let us test the same thing with example.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">[AdventureWorks]<br />
GO<br />
</span><span style="color:green;">/* */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[dbo].[MyTable]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[First] [nchar]</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">) NULL,<br />
</span><span style="color:black;">[Second] [nchar]</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">) NULL<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">/* Create Sample Table */<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">[AdventureWorks].[dbo].[MyTable]<br />
</span><span style="color:gray;">(</span><span style="color:black;">[ID]</span><span style="color:gray;">,</span><span style="color:black;">[First]</span><span style="color:gray;">,</span><span style="color:black;">[Second]</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First1'</span><span style="color:gray;">,</span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'First2'</span><span style="color:gray;">,</span><span style="color:red;">'Second2'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'First3'</span><span style="color:gray;">,</span><span style="color:red;">'Second3'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">'First4'</span><span style="color:gray;">,</span><span style="color:red;">'Second4'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:red;">'First5'</span><span style="color:gray;">,</span><span style="color:red;">'Second5'<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">Now let us create nonclustered index over this table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Nonclustered Index over Table */<br />
</span><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_MyTable_NonClustered]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MyTable]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[First] </span><span style="color:blue;">ASC</span><span style="color:gray;">,<br />
</span><span style="color:black;">[Second] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Run following two queries together.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run following two queries together and observe the<br />
result in by Enabling Actual Execution Plan (CTRL + M)<br />
1st Query will use Table Scan<br />
2nd Query will use Index Seek<br />
*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Second<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/nic.gif" alt="" width="500" height="502" /></p>
<p style="text-align:justify;">It is clear from query that index applies to columns on which it is created. In our case as in WHERE condition we have same columns which are used in Index.<br />
<strong>Query 1 &#8211; Does not use any index<br />
Query 2 &#8211; Does nonclustered index seek</strong>
</p>
<p style="text-align:justify;">Now create Clustered Index over the same table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Clustered Index over Table */<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_MyTable_Clustered]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MyTable]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Once again run above two same query and see the execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run following two queries together and observe the<br />
result in 1st Query will use Index Seek<br />
2nd Query will use Index Seek<br />
*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Second<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/nic1.gif" alt="" width="500" height="522" /></p>
<p style="text-align:justify;"><strong>Query 1 &#8211; </strong><strong>Does nonclustered index seek</strong><br />
<strong>Query 2 &#8211; Does nonclustered index seek</strong>
</p>
<p style="text-align:justify;">Clean up the database by running following script.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Clean up */<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">[dbo].[MyTable]<br />
GO</span></code></p>
<p style="text-align:justify;">Let us go over our steps of whole exercise.<br />
Step 1: We have one table and one nonclustered index.<br />
Step 2: We ran Query 1 which does not use nonclustered index.<br />
Step 3: We created clustered index over table.<br />
Step 4: We ran Query 1 which now use nonclustered index.
</p>
<p style="text-align:justify;"><em><strong>What is puzzling and interesting is how come query suddenly started to use nonclustered query when clustered index is created on table?</strong></em></p>
<p style="text-align:justify;">Query 1 should not have used index which is for second query as there is no change in that index or query1. Additionally, Query 1 is not even retrieving the column which is in nonclustered index. We created clustered index on column used in Query 1, so it should make Query 1 to use that clustered index but instead it is using nonclustered index which was already created and available to use earlier.</p>
<p style="text-align:justify;">The question is : <strong>The question is why this has happened? If Query can use nonclustered index why did it has to wait for clustered index to be created?<br />
</strong></p>
<p style="text-align:justify;"><strong>Answer:</strong><br />
The reason for this is that every nonclustered index refers to clustered index internally. When clustered index is created on table it reorganizes the table in the physical order of the clustered index. When there is no clustered index created on table at that time all nonclustered index points to data in the table to retrieve the data, however once clustered index is created all the nonclustered indexes are reorganized and they point to clustered index. This effect is creating index seek operation on nonclustered index. In our example column on which clustered index is created is in SELECT clause and WHERE clause contains columns which are used in nonclustered index, which is creating the effect which we have observed.
</p>
<p style="text-align:justify;">Let me know what do you think about this re-written article, it should be clear now what I am trying to suggest. Again, I am looking forward to your feedback about this subject. I will be discussing this in next UG meeting which is today. Please leave a not to me and we can go over this article on goto meeting or live meeting.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2754/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2754/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2754/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2754&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/21/sql-server-observation-effect-of-clustered-index-over-nonclustered-index-2/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/nic.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/nic1.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to Force Index Query Hints &#8211; Index Hint &#8211; Part2</title>
		<link>http://blog.sqlauthority.com/2009/02/08/sql-server-introduction-to-force-index-query-hints-index-hint-part2/</link>
		<comments>http://blog.sqlauthority.com/2009/02/08/sql-server-introduction-to-force-index-query-hints-index-hint-part2/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 01:30:10 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2281</guid>
		<description><![CDATA[In my previous article SQL SERVER &#8211; Introduction to Force Index Query Hints &#8211; Index Hint I have discussed regarding how we can use Index Hints with any query. I just received email from one of my regular reader that are there any another methods for the same as it will be difficult to read [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2281&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">In my previous article <strong><a href="http://blog.sqlauthority.com/2009/02/07/sql-server-introduction-to-force-index-query-hints-index-hint/" target="_blank">SQL SERVER &#8211; Introduction to Force Index Query Hints &#8211; Index Hint</a></strong> I have discussed regarding how we can use Index Hints with any query. I just received email from one of my regular reader that are there any another methods for the same as it will be difficult to read the syntax of join.Yes, there is alternate way to do the same using OPTION clause however, as OPTION clause is specified at the end of the query we have to specify which table the index hint is put on.</p>
<p style="text-align:justify;"><strong>Example 1: Using Inline Query Hint</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">c.ContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact c<br />
</span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">AK_Contact_rowguid</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Person.Contact pc<br />
</span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">PK_Contact_ContactID</span><span style="color:gray;">))<br />
</span><span style="color:blue;">ON </span><span style="color:black;">c.ContactID </span><span style="color:blue;">= </span><span style="color:black;">pc.ContactID<br />
GO</span></code>
</p>
<p style="text-align:justify;"><strong>Example 2: Using OPTION clause</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">c.ContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact c<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Person.Contact pc<br />
</span><span style="color:blue;">ON </span><span style="color:black;">c.ContactID </span><span style="color:blue;">= </span><span style="color:black;">pc.ContactID<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:blue;">TABLE </span><span style="color:black;">HINT</span><span style="color:gray;">(</span><span style="color:black;">c</span><span style="color:gray;">, </span><span style="color:blue;">INDEX </span><span style="color:gray;">(</span><span style="color:black;">AK_Contact_rowguid</span><span style="color:gray;">)),<br />
</span><span style="color:blue;">TABLE </span><span style="color:black;">HINT</span><span style="color:gray;">(</span><span style="color:black;">pc</span><span style="color:gray;">, </span><span style="color:blue;">INDEX </span><span style="color:gray;">(</span><span style="color:black;">PK_Contact_ContactID</span><span style="color:gray;">)))<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2281&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/08/sql-server-introduction-to-force-index-query-hints-index-hint-part2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Introduction to Force Index Query Hints &#8211; Index Hint</title>
		<link>http://blog.sqlauthority.com/2009/02/07/sql-server-introduction-to-force-index-query-hints-index-hint/</link>
		<comments>http://blog.sqlauthority.com/2009/02/07/sql-server-introduction-to-force-index-query-hints-index-hint/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 01:30:03 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2267</guid>
		<description><![CDATA[This article, I will start with disclaimer instead of having it at the end of article.
&#8220;SQL Server query optimizer selects the best execution plan for a query, it is recommended to use query hints by experienced developers and database administrators in case of special circumstances.&#8221;
When any query is ran SQL Server Engine determines which index [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2267&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">This article, I will start with disclaimer instead of having it at the end of article.</p>
<p style="text-align:justify;"><em>&#8220;SQL Server query optimizer selects the best execution plan for a query, it is recommended to use query hints by experienced developers and database administrators in case of special circumstances.&#8221;</em></p>
<p style="text-align:justify;">When any query is ran SQL Server Engine determines which index has to be used. SQL Server makes uses Index which has lowest cost based on performance. Index which is the best for performance is automatically used. There are some instances when Database Developer is best judge of the index used. DBA can direct SQL Server which index to be used to execute query.</p>
<p style="text-align:justify;"><strong>Example 1 : SQL Server using default index</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexhint1.gif" alt="" width="426" height="274" /><br />
</span>
</p>
<p style="text-align:justify;"><strong>Example 2: SQL Server using forced index</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact </span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">AK_Contact_rowguid</span><span style="color:gray;">))<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexhint2.gif" alt="" width="500" height="282" /><br />
</span>
</p>
<p style="text-align:justify;"><strong>Example 3: SQL Server using different index for different/same tables</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">c.ContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact c<br />
</span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">AK_Contact_rowguid</span><span style="color:gray;">))<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Person.Contact pc<br />
</span><span style="color:blue;">WITH </span><span style="color:gray;">(</span><span style="color:blue;">INDEX</span><span style="color:gray;">(</span><span style="color:black;">PK_Contact_ContactID</span><span style="color:gray;">))<br />
</span><span style="color:blue;">ON </span><span style="color:black;">c.ContactID </span><span style="color:blue;">= </span><span style="color:black;">pc.ContactID<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexhint3.gif" alt="" width="500" height="403" /><br />
</span>
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2267/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2267/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2267/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2267/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2267/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2267/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2267/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2267/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2267/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2267/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2267&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/07/sql-server-introduction-to-force-index-query-hints-index-hint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/indexhint1.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexhint2.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexhint3.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Observation &#8211; Effect of Clustered Index over Nonclustered Index</title>
		<link>http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/</link>
		<comments>http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 01:30:51 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2255</guid>
		<description><![CDATA[Today I came across very interesting observation while I was working on query optimization. Let us run the example first. Make sure to to enable Execution Plan (Using CTRL + M) before running comparison queries.
USE [AdventureWorks]
GO
/* */
CREATE TABLE [dbo].[MyTable](
[ID] [int] NOT NULL,
[First] [nchar](10) NULL,
[Second] [nchar](10) NULL
) ON [PRIMARY]
GO
/* Create Sample Table */
INSERT INTO [AdventureWorks].[dbo].[MyTable]
([ID],[First],[Second])
SELECT 1,'First1','Second1'
UNION ALL
SELECT 2,'First2','Second2'
UNION ALL
SELECT 3,'First3','Second3'
UNION ALL
SELECT 4,'First4','Second4'
UNION ALL
SELECT 5,'First5','Second5'
GO 

Now let us create nonclustered [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2255&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Today I came across very interesting observation while I was working on query optimization. Let us run the example first. Make sure to to enable Execution Plan (Using CTRL + M) before running comparison queries.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">[AdventureWorks]<br />
GO<br />
</span><span style="color:green;">/* */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">[dbo].[MyTable]</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] [int] </span><span style="color:gray;">NOT NULL,<br />
</span><span style="color:black;">[First] [nchar]</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">) NULL,<br />
</span><span style="color:black;">[Second] [nchar]</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">) NULL<br />
) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO<br />
</span><span style="color:green;">/* Create Sample Table */<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">[AdventureWorks].[dbo].[MyTable]<br />
</span><span style="color:gray;">(</span><span style="color:black;">[ID]</span><span style="color:gray;">,</span><span style="color:black;">[First]</span><span style="color:gray;">,</span><span style="color:black;">[Second]</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">,</span><span style="color:red;">'First1'</span><span style="color:gray;">,</span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">2</span><span style="color:gray;">,</span><span style="color:red;">'First2'</span><span style="color:gray;">,</span><span style="color:red;">'Second2'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">3</span><span style="color:gray;">,</span><span style="color:red;">'First3'</span><span style="color:gray;">,</span><span style="color:red;">'Second3'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">4</span><span style="color:gray;">,</span><span style="color:red;">'First4'</span><span style="color:gray;">,</span><span style="color:red;">'Second4'<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">5</span><span style="color:gray;">,</span><span style="color:red;">'First5'</span><span style="color:gray;">,</span><span style="color:red;">'Second5'<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">Now let us create nonclustered index over this table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Nonclustered Index over Table */<br />
</span><span style="color:blue;">CREATE NONCLUSTERED INDEX </span><span style="color:black;">[IX_MyTable_NonClustered]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MyTable]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[First] </span><span style="color:blue;">ASC</span><span style="color:gray;">,<br />
</span><span style="color:black;">[Second] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Run following two queries together.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run following two queries together and observe the<br />
result in by Enabling Actual Execution Plan (CTRL + M)<br />
1st Query will use Table Scan<br />
2nd Query will use Index Seek<br />
*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Second<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/nic.gif" alt="" width="500" height="502" /></p>
<p style="text-align:justify;">It is clear from query that index applies to columns on which it is created. In our case as in WHERE condition we have same columns which are used in Index.</p>
<p style="text-align:justify;">Now create Clustered Index over the same table.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Clustered Index over Table */<br />
</span><span style="color:blue;">CREATE CLUSTERED INDEX </span><span style="color:black;">[IX_MyTable_Clustered]<br />
</span><span style="color:blue;">ON </span><span style="color:black;">[dbo].[MyTable]<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">[ID] </span><span style="color:blue;">ASC<br />
</span><span style="color:gray;">) </span><span style="color:blue;">ON </span><span style="color:black;">[PRIMARY]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Once again run above two same query and see the execution plan.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run following two queries together and observe the<br />
result in 1st Query will use Index Seek<br />
2nd Query will use Index Seek<br />
*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Second<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[MyTable]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">First </span><span style="color:blue;">= </span><span style="color:red;">'First1' </span><span style="color:gray;">AND </span><span style="color:black;">Second </span><span style="color:blue;">= </span><span style="color:red;">'Second1'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/nic1.gif" alt="" width="500" height="522" /></p>
<p style="text-align:justify;">Clean up the database by running following script.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Clean up */<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">[dbo].[MyTable]<br />
GO</span></code>
</p>
<p style="text-align:justify;">Interesting part of above execution plan is now both queries are using nonclustered index scan. Logically first query should have not used index which is for second query as it was retrieving the column which was not in the nonclustered index. However, it did used the nonclustered index and only difference between our first execution and second execution is that we have created clustered index over the column which is retrieved in the first query.</p>
<p style="text-align:justify;">The question is : <strong>The question is why this has happened?</strong></p>
<p style="text-align:justify;">In summary : A query which is not using nonclustered index to retrieve results used nonclustered index when clustered index created on the column which is retrieved.</p>
<p style="text-align:justify;">The reason for this happening is that every nonclustered index refers to clustered index internally. When clustered index is created on table it reorganizes the table in the physical order of the clustered index. When there is no clustered index created on table at that time all nonclustered index points to data in the table to retrieve the data, however once clustered index is created all the nonclustered indexes are reorganized and they point to clustered index. This effect is creating index seek operation on nonclustered index in our case as column on which clustered index is created is in SELECT clause and WHERE clause contains columns which are used in nonclustered index.</p>
<p style="text-align:justify;">Let me know what do you think about this article. <em>It may be possible I have not explained this problem properly so I suggest if my readers can rewrite this part of problem and send it to me and I can include their documentation here.</em></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong><em><br />
</em></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2255/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2255&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/04/sql-server-observation-effect-of-clustered-index-over-nonclustered-index/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/nic.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/nic1.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Interesting Observation about Order of Resultset without ORDER BY</title>
		<link>http://blog.sqlauthority.com/2008/11/24/sql-server-interesting-observation-about-order-of-resultset-without-order-by/</link>
		<comments>http://blog.sqlauthority.com/2008/11/24/sql-server-interesting-observation-about-order-of-resultset-without-order-by/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 01:30:23 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1561</guid>
		<description><![CDATA[Today I observed very interesting little thing about SQL Server and I felt that I should share this with my readers.
I ran following two queries and found that I am getting different result-set. When I carefully observed I found that actually the result was same but order of the records returned is different.
USE AdventureWorks
GO
SELECT ContactID
FROM [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1561&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Today I observed very interesting little thing about SQL Server and I felt that I should share this with my readers.</p>
<p style="text-align:justify;">I ran following two queries and found that I am getting different result-set. When I carefully observed I found that actually the result was same but order of the records returned is different.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">ContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact<br />
GO<br />
</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/orderof1.jpg" alt="" width="301" height="716" /></p>
<p style="text-align:justify;">This particular thing interested me. I knew that when &#8220;ORDER BY&#8221; is not used order of the table is not guaranteed but I was not able to reproduce simple example for the same. Every time when I countered example of different order without using ORDER BY it was very complex and not easy to explain.</p>
<p style="text-align:justify;">Now let us discuss why the order is different even though ORDER BY clause is not used. It is common belief that when ORDER BY clause is not used it gives result following primary key index. However, it is not true always. The sentenced to remember is:</p>
<h3 style="text-align:justify;"><strong>There is no order unless ORDER BY is used.</strong></h3>
<p style="text-align:justify;">If ORDER BY clause is not used what is the SQL Server&#8217;s logic of returning the result-set. From example above it is clear that SQL Server for sure does not use Index always. In fact SQL Server uses index which gives fastest result. SQL Server Query optimizer is built with keeping performance in focus. Query optimizer always returns results using any method which is optimized for performance.</p>
<p style="text-align:justify;">Let us observe following execution plan for the same example. This really helps us to understand what is going on behind the scene.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/orderof.jpg" alt="" width="418" height="512" /></p>
<p style="text-align:justify;">When SELECT ContactID is used, it uses non-clustered index to return the results, where as for SELECT * it uses clustered index to return the results. Even though clustered index is used to return result in second statement, results returned using non-clustered is faster and its costs of query execution is lesser than clustered index scan.</p>
<p style="text-align:justify;"><em><strong>Summary of our experiment suggests that clustered index is not always faster and efficient than non-clustered index. When ORDER BY clause is not used similar query can return different result-set.</strong></em></p>
<p style="text-align:justify;">I would like to know your opinion on this subject.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1561/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1561/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1561/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1561/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1561/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1561/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1561/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1561/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1561/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1561/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1561&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/11/24/sql-server-interesting-observation-about-order-of-resultset-without-order-by/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/orderof1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/orderof.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Guidelines and Coding Standards Complete List Download</title>
		<link>http://blog.sqlauthority.com/2008/09/25/sql-server-guidelines-and-coding-standards/</link>
		<comments>http://blog.sqlauthority.com/2008/09/25/sql-server-guidelines-and-coding-standards/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 01:30:54 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Coding Standards]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Trigger]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1143</guid>
		<description><![CDATA[SQL SERVER &#8211; Guidelines and Coding Standards complete List Download
Coding standards and guidelines are very important for any developer on the path of successful career. A coding standard is a set of guidelines, rules and regulations on how to write code. Coding standards should be flexible enough or should take care of the situation where [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1143&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-Database-Coding-Standards-and-Guidelines-Complete-List-Download"><strong>SQL SERVER &#8211; Guidelines and Coding Standards complete List Download</strong></a></h2>
<p style="text-align:justify;">Coding standards and guidelines are very important for any developer on the path of successful career. A coding standard is a set of guidelines, rules and regulations on how to write code. Coding standards should be flexible enough or should take care of the situation where they should not prevent best practices for coding. They are basically the guidelines that one should follow for better understanding.</p>
<p style="text-align:justify;">The concept behind implementing coding standards and guidelines, is that the consistency and uniformity in programming so that if multiple people are working on the same code, it becomes easier to communicate, share with or understand each other’s work.</p>
<p style="text-align:justify;">With the goal of promoting good coding standards and guidelines I have created document which can guide developers.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/" target="_blank"><strong>SQL SERVER &#8211; Guidelines and Coding Standards Part &#8211; 1</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/24/sql-server-coding-standards-guidelines-part-2/" target="_blank"><strong>SQL SERVER &#8211; Guidelines and Coding Standards Part &#8211; 2</strong></a></p>
<h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-Database-Coding-Standards-and-Guidelines-Complete-List-Download"><strong>SQL SERVER &#8211; Guidelines and Coding Standards complete List Download</strong></a></h2>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Data Warehousing, Database, DBA, Pinal Dave, SQL, SQL Authority, SQL Coding Standards, SQL Constraint and Keys, SQL Cursor, SQL Data Storage, SQL Documentation, SQL Download, SQL Function, SQL Index, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQL Trigger, SQL Utility, SQLAuthority, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1143/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1143&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/25/sql-server-guidelines-and-coding-standards/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Guidelines and Coding Standards Part &#8211; 1</title>
		<link>http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/</link>
		<comments>http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 01:30:30 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Coding Standards]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1107</guid>
		<description><![CDATA[
 Use &#8220;Pascal&#8221; notation for SQL server Objects Like Tables, Views, Stored Procedures. Also tables and views should have ending &#8220;s&#8221;.
Example:
UserDetails
Emails

 If you have big subset of table group than it makes sense to give prefix for this table group. Prefix should be separated by _. 
Example:
Page_ UserDetails
Page_ Emails

 Use following naming convention for Stored [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1107&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Use &#8220;Pascal&#8221; notation for SQL server Objects Like Tables, Views, Stored Procedures. Also tables and views should have ending &#8220;s&#8221;.</strong></li>
<p>Example:</p>
<p>UserDetails</p>
<p>Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>If you have big subset of table group than it makes sense to give prefix for this table group. Prefix should be separated by _. </strong></li>
<p>Example:</p>
<p>Page_ UserDetails</p>
<p>Page_ Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Use following naming convention for Stored Procedure. sp&lt;Application Name&gt;_[&lt;group name &gt;_]&lt;action type&gt;&lt;table name or logical instance&gt; Where action is: Get, Delete, Update, Write, Archive, Insert&#8230; i.e. verb </strong></li>
<p>Example:</p>
<p>spApplicationName_GetUserDetails</p>
<p>spApplicationName_UpdateEmails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Use following Naming pattern for triggers: TR_&lt;TableName&gt;_&lt;action&gt;&lt;description&gt; </strong></li>
<p>Example:</p>
<p>TR_Emails_LogEmailChanges</p>
<p>TR_UserDetails_UpdateUserName</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Indexes : IX_&lt;tablename&gt;_&lt;columns separated by_&gt; </strong></li>
<p>Example:</p>
<p>IX_UserDetails_UserID</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Primary Key : PK_&lt;tablename&gt; </strong></li>
<p>Example:</p>
<p>PK_UserDetails</p>
<p>PK_ Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Foreign Key : FK_&lt;tablename_1&gt;_&lt;tablename_2&gt; </strong></li>
<p>Example:</p>
<p>FK_UserDetails_Emails</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Default: DF_&lt;table name&gt;_&lt;column name&gt; </strong></li>
<p>Example:</p>
<p>DF_ UserDetails _UserName</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Normalize Database structure based on <strong>3<sup>rd</sup> Normalization Form</strong>. Normalization is the process of designing a data model to efficiently store data in a database. (<a href="http://blog.sqlauthority.com/2007/11/26/sql-server-rules-of-third-normal-form-and-normalization-advantage-3nf/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Avoid use of <strong>SELECT *</strong> in SQL queries. Instead practice writing required <strong>column</strong> names after <strong>SELECT</strong> statement.</li>
<p>Example:</p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">Username</span><span style="color:gray;">, </span><span style="color:black;">Password<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">UserDetails</span></code></ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Avoid using temporary tables and derived tables as it uses more disks I/O. Instead use <strong>CTE (Common Table Expression); </strong>its scope is limited to the next statement in SQL query. (<a href="http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Use <strong>SET NOCOUNT ON</strong> at the beginning of SQL Batches, Stored Procedures and Triggers. This improves the performance of Stored Procedure. (<a href="http://blog.sqlauthority.com/2006/11/30/sql-server-cursor-to-process-tables-in-database-with-static-prefix-and-date-created/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Properly <strong>format</strong> SQL queries using indents.</li>
<p>Example: <em><strong>Wrong</strong> Format</em></p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">Username</span><span style="color:gray;">, </span><span style="color:black;">Password </span><span style="color:blue;">FROM </span><span style="color:black;">UserDetails ud </span><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e </span><span style="color:blue;">ON </span><span style="color:black;">e.EmpID </span><span style="color:blue;">= </span><span style="color:black;">ud.UserID</span></code></p>
<p>Example: <em><strong>Correct</strong> Format</em></p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">Username</span><span style="color:gray;">, </span><span style="color:black;">Password</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">FROM </span><span style="color:black;">UserDetails ud</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">INNER JOIN </span><span style="color:black;">Employee e </span><span style="color:blue;">ON </span><span style="color:black;">e.EmpID </span><span style="color:blue;">= </span><span style="color:black;">ud.UserID</span></code></ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Practice writing Upper Case for all SQL keywords.</li>
<p>Example:</p>
<p>SELECT, UPDATE, INSERT, WHERE, INNER JOIN, AND, OR, LIKE.</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> There must be <strong>PRIMARY KEY</strong> in all the tables of database with column name ID. It is common practice to use Primary Key as <strong>IDENTITY</strong> column.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> If &#8220;One Table&#8221; references &#8220;Another Table&#8221; than the column name used in reference should use the following rule :</li>
<p><strong>Column of Another Table : &lt;OneTableName&gt; ID</strong></p>
<p>Example:</p>
<p>If User table references Employee table than the column name used in reference should be <strong>UserID</strong> where User is table name and ID primary column of User table and UserID is reference column of Employee table.</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Columns with <strong>Default value</strong> constraint should not allow NULLs.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Practice using <strong>PRIMARY</strong> key in <strong>WHERE</strong> condition of <strong>UPDATE</strong> or <strong>DELETE</strong> statements as this will avoid error possibilities.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Always create stored procedure in <strong>same database</strong> where its relevant table exists otherwise it will reduce network performance.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Avoid server-side Cursors</strong> as much as possible, instead use SELECT statement. If you need to use cursor then replace it with WHILE loop (or read next suggestion).</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Instead of using <strong>LOOP</strong> to insert data from Table B to Table A, try to use <strong>SELECT</strong> statement with <strong>INSERT</strong> statement. (<a href="http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/">Read More Here</a>)</li>
<p><code style="font-size:12px;"><span style="color:blue;">INSERT INTO TABLE </span><span style="color:black;">A </span><span style="color:gray;">(</span><span style="color:black;">column1</span><span style="color:gray;">, </span><span style="color:black;">column2</span><span style="color:gray;">)</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">column1</span><span style="color:gray;">, </span><span style="color:black;">column2</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">FROM TABLE </span><span style="color:black;">B</span></code><br />
<code style="font-size:12px;"><span style="color:blue;">WHERE </span><span style="color:black;">.... </span></code></ul>
<ul class="unIndentedList" style="text-align:justify;">
<li><strong>Avoid using spaces within the name of database objects</strong>; this may create issues with front-end data access tools and applications. If you need spaces in your database object name then will accessing it surround the database object name with square brackets.</li>
<p>Example:</p>
<p>[Order Details]</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Do not use <strong>reserved words</strong> for naming database objects, as that can lead to some unpredictable situations. (<a href="http://blog.sqlauthority.com/2007/04/09/sql-server-2005-reserved-keywords/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Practice writing comments</strong> in stored procedures, triggers and SQL batches, whenever something is not very obvious, as it won&#8217;t impact the performance.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Do not use <strong>wild card characters</strong> at the beginning of word while search using LIKE keyword as it results in Index scan.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Indent code</strong> for better readability. (<a href="http://blog.sqlauthority.com/2008/02/26/sql-server-select-1-vs-select-an-interesting-observation/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> While using <strong>JOINs</strong> in your SQL query always <strong>prefix column name</strong> with the table name. (<a href="http://blog.sqlauthority.com/2008/08/02/sql-server-effect-of-order-of-join-in-query/">Example</a>). If additionally require then prefix Table name with ServerName, DatabaseName, DatabaseOwner. (<a href="http://blog.sqlauthority.com/2007/06/26/sql-server-explanation-and-example-four-part-name/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Default constraint must be defined at the <strong>column level</strong>. All other constraints must be defined at the <strong>table level</strong>. (<a href="http://blog.sqlauthority.com/2008/09/08/sql-server-%e2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/">Read More Here</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Avoid using rules of database objects instead use <strong>constraints</strong>.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Do not use the <strong>RECOMPILE</strong> option for Stored Procedure as it reduces the performance.</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Always put the <strong>DECLARE</strong> statements at the starting of the code in the stored procedure. This will make the query optimizer to reuse query plans. (<a href="http://blog.sqlauthority.com/2007/04/11/sql-server-udf-user-defined-function-to-extract-only-numbers-from-string/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Put the <strong>SET</strong> statements in beginning (after DECLARE) before executing code in the stored procedure. (<a href="http://blog.sqlauthority.com/2007/04/11/sql-server-udf-user-defined-function-to-extract-only-numbers-from-string/">Example</a>)</li>
</ul>
<ul class="unIndentedList" style="text-align:justify;">
<li> Use <strong>BEGIN&#8230;END</strong> blocks only when multiple statements are present within a conditional code segment. (<a href="http://blog.sqlauthority.com/2007/11/28/sql-server-correct-syntax-for-stored-procedure-sp/">Read More Here</a>)</li>
</ul>
<p style="text-align:justify;">© Copyright 2000-2008<a title="Pinal Dave" href="http://www.pinaldave.com/"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/">SQLAuthority.com</a></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Database, Pinal Dave, Software Development, SQL, SQL Authority, SQL Coding Standards, SQL Constraint and Keys, SQL Documentation, SQL Index, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1107/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1107/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1107/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1107&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/23/sql-server-coding-standards-guidelines-part-1/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</title>
		<link>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/</link>
		<comments>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Download]]></category>
		<category><![CDATA[SQL Error Messages]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Trigger]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL XML]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1134</guid>
		<description><![CDATA[Download SQL Server 2008 Interview Questions and Answers Complete List
Interview is very important event for any person. A good interview leads to good career if candidate is willing to learn. I always enjoy interview questions and answers series. This is my very humble attempt to write SQL Server 2008 interview questions and answers. SQL Server [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1134&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-2008-Download-Interview-Questions-and-Answers"><strong>Download SQL Server 2008 Interview Questions and Answers Complete List</strong></a></h2>
<p style="text-align:justify;">Interview is very important event for any person. A good interview leads to good career if candidate is willing to learn. I always enjoy interview questions and answers series. This is my very humble attempt to write SQL Server 2008 interview questions and answers. SQL Server is very large subject and not everything is usually asked in interview. In interview what matters the most is <strong>conceptual knowledge</strong> and <strong>learning attitude</strong>.</p>
<p style="text-align:justify;">I have listed all the series in this post so that it can be easily downloaded and used. All the questions are collected and listed in one PDF which is here to download. If you have any question or if you want to add to any of the question please send me mail or write a comment.</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/12/sql-server-2008-interview-questions-and-answers-part-1/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 1</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 2</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/14/sql-server-2008-interview-questions-and-answers-part-3/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 3</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/15/sql-server-2008-interview-questions-and-answers-part-4/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 4</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/16/sql-server-2008-interview-questions-and-answers-part-5/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 5</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/17/sql-server-2008-interview-questions-and-answers-part-6/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 6</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/18/sql-server-2008-interview-questions-and-answers-part-7/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 7</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/09/19/sql-server-2008-interview-questions-and-answers-part-8/"><strong>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 8</strong></a></p>
<h2 style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=SQL-SERVER-2008-Download-Interview-Questions-and-Answers"><strong>Download SQL Server 2008 Interview Questions and Answers Complete List</strong></a></h2>
<p style="text-align:justify;">
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Database, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Cursor, SQL Data Storage, SQL DateTime, SQL Documentation, SQL Download, SQL Error Messages, SQL Function, SQL Index, SQL Interview Questions and Answers, SQL Joins, SQL Performance, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Server DBCC, SQL Stored Procedure, SQL Tips and Tricks, SQL Trigger, SQL Utility, SQLAuthority, T SQL, Technology Tagged: SQL XML <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1134&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 4</title>
		<link>http://blog.sqlauthority.com/2008/09/15/sql-server-2008-interview-questions-and-answers-part-4/</link>
		<comments>http://blog.sqlauthority.com/2008/09/15/sql-server-2008-interview-questions-and-answers-part-4/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 01:30:15 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=995</guid>
		<description><![CDATA[SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download
1) General Questions of SQL SERVER 
Which command using Query Analyzer will give you the version of SQL server and operating system?

SELECT SERVERPROPERTY ('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
What is SQL Server Agent?
SQL Server agent plays an important role in the day-to-day tasks of a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=995&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</a></strong></h2>
<p style="text-align:justify;"><strong>1) </strong><strong>General Questions of SQL SERVER </strong><br />
<strong>Which command using Query Analyzer will give you the version of SQL server and operating system?</strong>
</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">SERVERPROPERTY </span><span style="color:gray;">(</span><span style="color:red;">'productversion'</span><span style="color:gray;">), </span><span style="color:magenta;">SERVERPROPERTY </span><span style="color:gray;">(</span><span style="color:red;">'productlevel'</span><span style="color:magenta;">), SERVERPROPERTY </span><span style="color:gray;">(</span><span style="color:red;">'edition'</span><span style="color:gray;">)</span></code></p>
<p style="text-align:justify;"><strong>What is SQL Server Agent?</strong></p>
<p style="text-align:justify;">SQL Server agent plays an important role in the day-to-day tasks of a database administrator (DBA). It is often overlooked as one of the main tools for SQL Server management. Its purpose is to ease the implementation of tasks for the DBA, with its full-function scheduling engine, which allows you to schedule your own jobs and scripts. (<a href="http://blog.sqlauthority.com/2007/02/26/sql-server-whats-new-in-sql-server-agent-for-microsoft-sql-server-2005/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>Can a stored procedure call itself or recursive stored procedure? How much level SP nesting is possible?</strong></p>
<p style="text-align:justify;">Yes. Because Transact-SQL supports recursion, you can write stored procedures that call themselves. Recursion can be defined as a method of problem solving wherein the solution is arrived at by repetitively applying it to subsets of the problem. A common application of recursive logic is to perform numeric computations that lend themselves to repetitive evaluation by the same processing steps. Stored procedures are nested when one stored procedure calls another or executes managed code by referencing a CLR routine, type, or aggregate. You can nest stored procedures and managed code references up to 32 levels.</p>
<p style="text-align:justify;"><strong>What is Log Shipping?</strong></p>
<p style="text-align:justify;">Log shipping is the process of automating the backup of database and transaction log files on a production SQL server, and then restoring them onto a standby server. Enterprise Editions only supports log shipping. In log shipping the transactional log file from one server is automatically updated into the backup database on the other server. If one server fails, the other server will have the same db and can be used this as the Disaster Recovery plan. The key feature of log shipping is that it will automatically backup transaction logs throughout the day and automatically restore them on the standby server at defined interval.</p>
<p style="text-align:justify;"><strong>Name 3 ways to get an accurate count of the number of records in a table?</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">table1</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">COUNT</span><span style="color:gray;">(*) </span><span style="color:blue;">FROM </span><span style="color:black;">table1</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">rows </span><span style="color:blue;">FROM </span><span style="color:black;">sysindexes </span><span style="color:blue;">WHERE </span><span style="color:black;">id </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:black;">table1</span><span style="color:gray;">) AND </span><span style="color:black;">indid </span><span style="color:gray;">&lt; </span><span style="color:black;">2</span></code></p>
<p style="text-align:justify;"><strong>What does it mean to have QUOTED_IDENTIFIER ON? What are the implications of having it OFF?</strong></p>
<p style="text-align:justify;">When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. (<a href="http://blog.sqlauthority.com/2007/03/05/sql-server-quoted_identifier-onoff-and-ansi_null-onoff-explanation/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is the difference between a Local and a Global temporary table?</strong></p>
<p style="text-align:justify;"><em>A local</em> temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.</p>
<p style="text-align:justify;"><em>A global </em>temporary table remains in the database permanently, but the rows exist only within a given connection. When connection is closed, the data in the global temporary table disappears. However, the table definition remains with the database for access when database is opened next time.</p>
<p style="text-align:justify;"><strong>What is the STUFF function and how does it differ from the REPLACE function?</strong></p>
<p style="text-align:justify;">STUFF function is used to overwrite existing characters. Using this syntax, STUFF (string_expression, start, length, replacement_characters), string_expression is the string that will have characters substituted, start is the starting position, length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string. REPLACE function to replace existing characters of all occurrences. Using the syntax REPLACE (string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string.</p>
<p style="text-align:justify;"><strong>What is PRIMARY KEY?</strong></p>
<p style="text-align:justify;">A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.</p>
<p style="text-align:justify;"><strong>What is UNIQUE KEY constraint?</strong></p>
<p style="text-align:justify;">A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.</p>
<p style="text-align:justify;"><strong>What is FOREIGN KEY?</strong></p>
<p style="text-align:justify;">A FOREIGN KEY constraint prevents any actions that would destroy links between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.</p>
<p style="text-align:justify;"><strong>What is CHECK Constraint?</strong></p>
<p style="text-align:justify;">A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity. (<a href="http://blog.sqlauthority.com/2008/06/22/sql-server-create-check-constraint-on-column/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is NOT NULL Constraint?</strong></p>
<p style="text-align:justify;">A NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.</p>
<p style="text-align:justify;">(<a href="http://blog.sqlauthority.com/2007/10/15/sql-server-explanation-and-understanding-not-null-constraint/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>How to get @@ERROR and @@ROWCOUNT at the same time?</strong></p>
<p style="text-align:justify;">If @@Rowcount is checked after Error checking statement then it will have 0 as the value of @@Recordcount as it would have been reset. And if @@Recordcount is checked before the error-checking statement then @@Error would get reset. To get @@error and @@rowcount at the same time do both in same statement and store them in local variable. SELECT @RC = @@ROWCOUNT, @ER = @@ERROR</p>
<p style="text-align:justify;"><strong>What is a Scheduled Jobs or What is a Scheduled Tasks?</strong></p>
<p style="text-align:justify;">Scheduled tasks let user automate processes that run on regular or predictable cycles. User can schedule administrative tasks, such as cube processing, to run during times of slow business activity. User can also determine the order in which tasks run by creating job steps within a SQL Server Agent job. E.g. back up database, Update Stats of Tables. Job steps give user control over flow of execution. If one job fails, user can configure SQL Server Agent to continue to run the remaining tasks or to stop execution.</p>
<p style="text-align:justify;"><strong>What are the advantages of using Stored Procedures?</strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Stored procedure can reduced network traffic and latency, boosting application performance.</li>
<li> Stored procedure execution plans can be reused, staying cached in SQL Server&#8217;s memory, reducing server overhead.</li>
<li> Stored procedures help promote code reuse.</li>
<li> Stored procedures can encapsulate logic. You can change stored procedure code without affecting clients.</li>
<li> Stored procedures provide better security to your data.</li>
</ul>
<p style="text-align:justify;"><strong>What is a table called, if it has neither Cluster nor Non-cluster Index? What is it used for?</strong></p>
<p style="text-align:justify;">Unindexed table or Heap. Microsoft Press Books and Book on Line (BOL) refers it as Heap. A heap is a table that does not have a clustered index and, therefore, the pages are not linked by pointers. The IAM pages are the only structures that link the pages in a table together. Unindexed tables are good for fast storing of data. Many times it is better to drop all indexes from table and then do bulk of inserts and to restore those indexes after that.</p>
<p style="text-align:justify;"><strong>Can SQL Servers linked to other servers like Oracle?</strong></p>
<p style="text-align:justify;">SQL Server can be linked to any server provided it has OLE-DB provider from Microsoft to allow a link. E.g. Oracle has an OLE-DB provider for oracle that Microsoft provides to add it as linked server to SQL Server group</p>
<p style="text-align:justify;"><strong>What is BCP? When does it used?</strong></p>
<p style="text-align:justify;">BulkCopy is a tool used to copy huge amount of data from tables and views. BCP does not copy the structures same as source to destination. BULK INSERT command helps to import a data file into a database table or view in a user-specified format.</p>
<p style="text-align:justify;">© Copyright 2000-2009<a title="Pinal Dave" href="http://www.pinaldave.com/" target="_blank"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/" target="_blank">SQLAuthority.com</a></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/995/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/995/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/995/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/995/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/995/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/995/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/995/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/995/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/995/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/995/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/995/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/995/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=995&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/15/sql-server-2008-interview-questions-and-answers-part-4/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 3</title>
		<link>http://blog.sqlauthority.com/2008/09/14/sql-server-2008-interview-questions-and-answers-part-3/</link>
		<comments>http://blog.sqlauthority.com/2008/09/14/sql-server-2008-interview-questions-and-answers-part-3/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 01:30:14 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=993</guid>
		<description><![CDATA[SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download
1) General Questions of SQL SERVER 
2) Common Questions Asked 
Which TCP/IP port does SQL Server run on? How can it be changed?
SQL Server runs on port 1433. It can be changed from the Network Utility TCP/IP properties -&#62; Port number, both on client [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=993&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</a></strong></h2>
<p style="text-align:justify;"><strong>1) </strong><strong>General Questions of SQL SERVER </strong></p>
<p style="text-align:justify;"><strong>2) </strong><strong>Common Questions Asked </strong></p>
<p style="text-align:justify;"><strong>Which TCP/IP port does SQL Server run on? How can it be changed?</strong></p>
<p style="text-align:justify;">SQL Server runs on port 1433. It can be changed from the Network Utility TCP/IP properties -&gt; Port number, both on client and the server.</p>
<p style="text-align:justify;"><strong>What are the difference between clustered and a non-clustered index? </strong>(<a href="http://blog.sqlauthority.com/2007/04/23/sql-server-understanding-new-index-type-of-sql-server-2005-included-column-index-along-with-clustered-index-and-non-clustered-index/" target="_blank">Read More Here</a>)<strong></strong></p>
<p style="text-align:justify;"><strong><em>A clustered index</em></strong> is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.</p>
<p style="text-align:justify;"><strong><em>A non clustered index</em></strong> is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non clustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.</p>
<p style="text-align:justify;"><strong>What are the different index configurations a table can have?</strong></p>
<p style="text-align:justify;">A table can have one of the following index configurations:</p>
<ul class="unIndentedList" style="text-align:justify;">
<li> No indexes</li>
<li> A clustered index</li>
<li> A clustered index and many nonclustered indexes</li>
<li> A nonclustered index</li>
<li> Many nonclustered indexes</li>
</ul>
<p style="text-align:justify;"><strong>What are different types of Collation Sensitivity?</strong></p>
<p style="text-align:justify;"><em>Case sensitivity</em> &#8211; A and a, B and b, etc.</p>
<p style="text-align:justify;"><em>Accent sensitiv</em>ity &#8211; a and á, o and ó, etc.</p>
<p style="text-align:justify;"><em>Kana Sensitivity</em> &#8211; When Japanese kana characters Hiragana and Katakana are treated differently, it is called Kana sensitive.</p>
<p style="text-align:justify;"><em>Width sensitivity</em> &#8211; A single-byte character (half-width) and the same character represented as a double-byte character (full-width) are treated differently than it is width sensitive. (<a href="http://blog.sqlauthority.com/2007/04/30/case-sensitive-sql-query-search/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is OLTP (Online Transaction Processing)?</strong></p>
<p style="text-align:justify;">In OLTP &#8211; online transaction processing systems relational database design use the discipline of data modeling and generally follow the Codd rules of data normalization in order to ensure absolute data integrity. Using these rules complex information is broken down into its most simple structures (a table) where all of the individual atomic level elements relate to each other and satisfy the normalization rules.</p>
<p style="text-align:justify;"><strong>What&#8217;s the difference between a primary key and a unique key?</strong></p>
<p style="text-align:justify;">Both primary key and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn&#8217;t allow NULLs, but unique key allows one NULL only. (<a href="http://blog.sqlauthority.com/2007/02/05/sql-server-primary-key-constraints-and-unique-key-constraints/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is difference between DELETE &amp; TRUNCATE commands?</strong></p>
<p style="text-align:justify;">Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.</p>
<p style="text-align:justify;"><strong><em>TRUNCATE</em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.</li>
<li> TRUNCATE removes the data by deallocating the data pages used to store the table&#8217;s data, and only the page deallocations are recorded in the transaction log.</li>
<li> TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. The counter used by an identity for new rows is reset to the seed for the column.</li>
<li> You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.</li>
<li> TRUNCATE cannot be rolled back.</li>
<li> TRUNCATE is DDL Command.</li>
<li> TRUNCATE Resets identity of the table</li>
</ul>
<p style="text-align:justify;"><strong><em>DELETE</em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.</li>
<li> If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.</li>
<li> DELETE Can be used with or without a WHERE clause</li>
<li> DELETE Activates Triggers.</li>
<li> DELETE can be rolled back.</li>
<li> DELETE is DML Command.</li>
<li> DELETE does not reset identity of the table.</li>
</ul>
<p style="text-align:justify;">(<a href="http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolled-back-using-log-files-after-transaction-session-is-closed/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>When is the use of UPDATE_STATISTICS command?</strong></p>
<p style="text-align:justify;">This command is basically used when a large processing of data has occurred. If a large amount of deletions any modification or Bulk Copy into the tables has occurred, it has to update the indexes to take these changes into account. UPDATE_STATISTICS updates the indexes on these tables accordingly.</p>
<p style="text-align:justify;"><strong>What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?</strong></p>
<p style="text-align:justify;">They specify a search condition for a group or an aggregate. But the difference is that HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used only with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they are part of the GROUP BY function in a query. (<a href="http://blog.sqlauthority.com/2007/07/04/sql-server-definition-comparison-and-difference-between-having-and-where-clause/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What are the properties and different Types of Sub-Queries?</strong></p>
<p style="text-align:justify;"><strong><em>Properties of Sub-Query</em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> A sub-query must be enclosed in the parenthesis.</li>
<li> A sub-query must be put in the right hand of the comparison operator, and</li>
<li> A sub-query cannot contain an ORDER-BY clause.</li>
<li> A query can contain more than one sub-query.</li>
</ul>
<p style="text-align:justify;"><strong><em>Types of Sub-query</em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Single-row sub-query, where the sub-query returns only one row.</li>
<li> Multiple-row sub-query, where the sub-query returns multiple rows,. and</li>
<li> Multiple column sub-query, where the sub-query returns multiple columns</li>
</ul>
<p style="text-align:justify;"><strong>What is SQL Profiler?</strong></p>
<p style="text-align:justify;">SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of Microsoft SQL Server. You can capture and save data about each event to a file or SQL Server table to analyze later. For example, you can monitor a production environment to see which stored procedures are hampering performances by executing too slowly.</p>
<p style="text-align:justify;">Use SQL Profiler to monitor only the events in which you are interested. If traces are becoming too large, you can filter them based on the information you want, so that only a subset of the event data is collected. Monitoring too many events adds overhead to the server and the monitoring process and can cause the trace file or trace table to grow very large, especially when the monitoring process takes place over a long period of time.</p>
<p style="text-align:justify;"><strong>What are the authentication modes in SQL Server? How can it be changed?</strong></p>
<p style="text-align:justify;">Windows mode and Mixed Mode &#8211; SQL &amp; Windows.</p>
<p style="text-align:justify;">To change authentication mode in SQL Server click Start, Programs, Microsoft SQL Server and click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group. Select the server then from the Tools menu select SQL Server Configuration Properties, and choose the Security page.</p>
<p style="text-align:justify;">© Copyright 2000-2009<a title="Pinal Dave" href="http://www.pinaldave.com/" target="_blank"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/" target="_blank">SQLAuthority.com</a></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/993/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/993/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/993/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/993/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/993/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=993&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/14/sql-server-2008-interview-questions-and-answers-part-3/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/</link>
		<comments>http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 01:30:09 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Data Warehousing]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Cursor]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Joins]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLAuthority]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=991</guid>
		<description><![CDATA[SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download
1) General Questions of SQL SERVER 
What is Cursor?
Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.
In [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=991&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2 style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2008/09/20/sql-server-2008-interview-questions-and-answers-complete-list-download/" target="_blank">SQL SERVER &#8211; 2008 &#8211; Interview Questions and Answers Complete List Download</a></strong></h2>
<p style="text-align:justify;"><strong>1) </strong><strong>General Questions of SQL SERVER </strong></p>
<p style="text-align:justify;"><strong>What is Cursor?</strong></p>
<p style="text-align:justify;">Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.</p>
<p style="text-align:justify;">In order to work with a cursor we need to perform some steps in the following order:</p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Declare cursor</li>
<li> Open cursor</li>
<li> Fetch row from the cursor</li>
<li> Process fetched row</li>
<li> Close cursor</li>
<li> Deallocate cursor (<a href="http://blog.sqlauthority.com/2008/04/19/sql-server-is-cursor-database-object-or-datatype/" target="_blank">Read More Here</a>)</li>
</ul>
<p style="text-align:justify;"><strong>What is Collation?</strong></p>
<p style="text-align:justify;">Collation refers to a set of rules that determine how data is sorted and compared. Character data is sorted using rules that define the correct character sequence, with options for specifying case sensitivity, accent marks, kana character types and character width. (<a href="http://blog.sqlauthority.com/2007/04/24/sql-server-2005-collation-explanation-and-translation/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What is Difference between Function and Stored Procedure?</strong></p>
<p style="text-align:justify;">UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be. UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables. Inline UDF&#8217;s can be thought of as views that take parameters and can be used in JOINs and other Rowset operations.</p>
<p style="text-align:justify;"><strong>What is sub-query? Explain properties of sub-query?</strong></p>
<p style="text-align:justify;">Sub-queries are often referred to as sub-selects, as they allow a SELECT statement to be executed arbitrarily within the body of another SQL statement. A sub-query is executed by enclosing it in a set of parentheses. Sub-queries are generally used to return a single row as an atomic value, though they may be used to compare values against multiple rows with the IN keyword.</p>
<p style="text-align:justify;">A subquery is a SELECT statement that is nested within another T-SQL statement. A subquery SELECT statement if executed independently of the T-SQL statement, in which it is nested, will return a resultset. Meaning a subquery SELECT statement can standalone and is not depended on the statement in which it is nested. A subquery SELECT statement can return any number of values, and can be found in, the column list of a SELECT statement, a FROM, GROUP BY, HAVING, and/or ORDER BY clauses of a T-SQL statement. A Subquery can also be used as a parameter to a function call. Basically a subquery can be used anywhere an expression can be used. (<a href="http://blog.sqlauthority.com/2007/09/07/sql-server-correlated-and-noncorrelated-subquery-introduction-explanation-and-example/" target="_blank">Read More Here</a>)</p>
<p style="text-align:justify;"><strong>What are different Types of Join?</strong></p>
<p style="text-align:justify;"><strong><em>Cross Join</em></strong></p>
<p style="text-align:justify;">A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The common example is when company wants to combine each product with a pricing table to analyze each product at each price.</p>
<p style="text-align:justify;"><strong><em>Inner Join</em></strong></p>
<p style="text-align:justify;">A join that displays only the rows that have a match in both joined tables is known as inner Join.  This is the default type of join in the Query and View Designer.</p>
<p style="text-align:justify;"><strong><em>Outer Join</em></strong></p>
<p style="text-align:justify;">A join that includes rows even if they do not have related rows in the joined table is an Outer Join.  You can create three different outer join to specify the unmatched rows to be included:</p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Left Outer Join:</strong> In Left Outer Join all rows in the first-named table i.e. &#8220;left&#8221; table, which appears leftmost in the JOIN clause are included. Unmatched rows in the right table do not appear.<strong> </strong><strong><em> </em></strong></li>
</ul>
<p style="text-align:justify;"><strong><em> </em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Right Outer Join:</strong> In Right Outer Join all rows in the second-named table i.e. &#8220;right&#8221; table, which appears rightmost in the JOIN clause are included. Unmatched rows in the left table are not included.<strong><em></em></strong></li>
</ul>
<p style="text-align:justify;"><strong><em> </em></strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> <strong>Full Outer Join:</strong> In Full Outer Join all rows in all joined tables are included, whether they are matched or not.<strong></strong></li>
</ul>
<p style="text-align:justify;"><strong><em>Self Join</em></strong></p>
<p style="text-align:justify;">This is a particular case when one table joins to itself, with one or two aliases to avoid confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather unique in that it involves a relationship with only one table. The common example is when company has a hierarchal reporting structure whereby one member of staff reports to another. Self Join can be Outer Join or Inner Join. (<a href="http://blog.sqlauthority.com/2007/06/03/sql-server-2005-explanation-and-example-self-join/" target="_blank">Read More Here</a>)<strong></strong></p>
<p style="text-align:justify;"><strong>What are primary keys and foreign keys?</strong></p>
<p style="text-align:justify;">Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental of all keys and constraints. A table can have only one Primary key.</p>
<p style="text-align:justify;">Foreign keys are both a method of ensuring data integrity and a manifestation of the relationship between tables.</p>
<p style="text-align:justify;"><strong>What is User Defined Functions? What kind of User-Defined Functions can be created?</strong></p>
<p style="text-align:justify;">User-Defined Functions allow defining its own T-SQL functions that can accept 0 or more parameters and return a single scalar data value or a table data type.</p>
<p style="text-align:justify;"><em>Different Kinds of User-Defined Functions created are: </em></p>
<p style="text-align:justify;"><strong><em>Scalar User-Defined Function</em></strong></p>
<p style="text-align:justify;">A Scalar user-defined function returns one of the scalar data types. Text, ntext, image and timestamp data types are not supported. These are the type of user-defined functions that most developers are used to in other programming languages. You pass in 0 to many parameters and you get a return value.</p>
<p style="text-align:justify;"><strong><em>Inline Table-Value User-Defined Function</em></strong></p>
<p style="text-align:justify;">An Inline Table-Value user-defined function returns a table data type and is an exceptional alternative to a view as the user-defined function can pass parameters into a T-SQL select command and in essence provide us with a parameterized, non-updateable view of the underlying tables.<strong><em></em></strong></p>
<p style="text-align:justify;"><strong><em>Multi-statement Table-Value User-Defined Function</em></strong></p>
<p style="text-align:justify;">A Multi-Statement Table-Value user-defined function returns a table and is also an exceptional alternative to a view as the function can support multiple T-SQL statements to build the final result where the view is limited to a single SELECT statement. Also, the ability to pass parameters into a TSQL select command or a group of them gives us the capability to in essence create a parameterized, non-updateable view of the data in the underlying tables. Within the create function command you must define the table structure that is being returned. After creating this type of user-defined function, It can be used in the FROM clause of a T-SQL command unlike the behavior found when using a stored procedure which can also return record sets. (<a href="http://blog.sqlauthority.com/2007/09/08/sql-server-udf-user-defined-function-get-number-of-days-in-month/" target="_blank">Read Here For Example</a>)</p>
<p style="text-align:justify;"><strong>What is Identity?</strong></p>
<p style="text-align:justify;">Identity (or AutoNumber) is a column that automatically generates numeric values. A start and increment value can be set, but most DBA leave these at 1. A GUID column also generates numbers; the value of this cannot be controlled. Identity/GUID columns do not need to be indexed.</p>
<p style="text-align:justify;"><strong>What is DataWarehousing?</strong></p>
<ul class="unIndentedList" style="text-align:justify;">
<li> Subject-oriented, meaning that the data in the database is organized so that all the data elements relating to the same real-world event or object are linked together;</li>
<li> Time-variant, meaning that the changes to the data in the database are tracked and recorded so that reports can be produced showing changes over time;</li>
<li> Non-volatile, meaning that data in the database is never over-written or deleted, once committed, the data is static, read-only, but retained for future reporting.</li>
<li> Integrated, meaning that the database contains data from most or all of an organization&#8217;s operational applications, and that this data is made consistent.</li>
</ul>
<p style="text-align:justify;">© Copyright 2000-2009<a title="Pinal Dave" href="http://www.pinaldave.com/" target="_blank"> Pinal Dave.</a> All Rights Reserved. <a href="http://blog.sqlauthority.com/" target="_blank">SQLAuthority.com</a></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/sqlauthority.wordpress.com/991/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/sqlauthority.wordpress.com/991/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/991/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/991/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/991/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=991&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/13/sql-server-2008-interview-questions-and-answers-part-2/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
	</channel>
</rss>