Showing posts with label SQL Links. Show all posts
Showing posts with label SQL Links. Show all posts

Tuesday, September 20, 2011

Reset Identity column in SQL Server and Mysql

This is one of those simple tip posts that may seem obvious and taken for granted by those of us who have been working with SQL Server for a while now but maybe a newbie or two out there will find this helpful.
Every so often (just this morning!) I find myself resetting an identity column value back to 0after I've deleted all the existing records so the table gets a fresh start at primary key 1. Yes, I know all about primary keys not changing and how the value in the primary key doesn't matter and so on. Sometimes I just like the primary keys starting at 1.
The following line resets the Identity value for the Customer table to 0 so that the next record added starts at 1.
DBCC CHECKIDENT('Customer', RESEED, 0)
This is how we can reset the identity column in SQL Server....
For Mysql, we have a bit different command for resetting identity column
ALTER TABLE tablename AUTO_INCREMENT=0
However, if you have existing rows, the AUTO_INCREMENT value will only reset to one higher than the maximum current ID. For example, if you have a table with the following entries…
ID
First name
3
Peter
6
Paul
7
Mary
… and the AUTO_INCREMENT count is set at 60, the statement to reset the AUTO_INCREMENT count to 0 will only reset it to 8. Only if there are no rows remaining will it reset to 0.

Thursday, March 5, 2009

Difference b/w union and union all

Often people forget or misunderstand the difference between UNION and the UNION ALL keywords in a query.
UNION ALL
SELECT NOW = GETDATE()
UNION ALL
SELECT NOW = GETDATE()
This gives us the entire 'set' of data, both queries are executed, note, they are executed serially and not in parallel, the order the queries are executed is not determined; so, you never get a situation where the top query is parallelised with the bottom query; the query components themselves may be executed in parallel and combined as the last step.
Output (notice there are two rows that are identical, this is because GETDATE() gives a consistent value across the entire query rather than consistency at each query within the UNION construct.

NOW-----------------------2006-06-29 08:01:48.937
-----------------------2006-06-29 08:01:48.937
For Detail View : http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/06/29/849.aspx

Saturday, January 31, 2009

sql links

http://www.sqlservercentral.com/
Earn Money ! Affiliate Program
Open Directory Project at dmoz.org