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

Tuesday, May 1, 2012

Resetting an Auto Incremental Primary Key on SQL Server

Almost all of us create tables in SQL Server with auto incremental primary keys. After creating these tables we write programs which use these tables to place data in them. These programs under go a  lot of testing and thus place a lot of test data in these tables and in turn increment the value of such auto increment fields.

Once all the testing is said and done and you are all set to launch your program/application/software you empty the database/tables so that there is no dummy/test data. but the simple delete statement:

DELETE FROM [tableName];

only clears the data but does not reset the values of any auto increment  fields, this gives a sense of uncleanliness when you are delivering the product/project.

So to reset any auto incremental primary key in MS SQL Server 2008 user defined database tables use the following command after the DELETE statement above.

DBCC CHECKIDENT ("dbo.[tableName]", RESEED, 0);

After the command has executed the next value generated for the auto incremental primary key would be 1, hence the count has been reset, giving a sense of a fresh start or a new beginning. Now you can test a fresh or deliver it a fresh. As pizza companies say Oven Hot we developers can say Database Fresh :).

Happy resetting. :)

Tuesday, June 29, 2010

Installing MongoDB Service on Windows7

The time when SQL was considered to be the Lingua Francia for data storage and retrieval will soon be coming to an end. No SQL databases have shown that they are way faster when it comes to data storage and retrieval. Among the many new Object or Document Databases (for simplicity we will refer to them as No SQL Databases) MongoDB is one which has shown and is still showing a lot of promise. In order to make it easier for people to you can find a comparison fo SQL Server 2008 vs. MongoDB here.

Now lets have a look at how to install MongoDB as a service on Windows7, (below are actual extracts from http://www.michaelckennedy.net/blog/2010/04/29/MongoDBVsSQLServer2008PerformanceShowdown.aspx)

  • Download and extract the MongoDB archive appropriate for your system. http://www.mongodb.org/display/DOCS/Downloads
  • Copy the extracted folder to C:\ and rename it mongo.
  • Create a directory called 'data' inside C:\mongo
  • Click on the Start Menu and enter "cmd" in the search box. Right-click on cmd.exe and select "Run as administrator." This is VERY important because you won't be able to register MongoDB as a service if you run cmd.exe with normal privileges.
  • Type "C:\mongo\bin\mongod --install" to register MongoDB as a Windows service. This will probably output what may look like an error message, but don't worry ... as long as you really did run cmd.exe with administrator privileges, everything is fine.
  • Click on the start menu and enter "regedit" in the search box. Click on regedit.exe, and then browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services. There should be child folder there named MongoDB that has several keys inside it. If not, go back to step 4 and try again.
  • The ImagePath key should read "c:\mongo\bin\mongod --service" right now; change it to "c:\mongo\bin\mongod --service --dbpath c:\mongo\data". This will allow MongoDB to find its data files.
  • Click on the Start Menu and enter "services" in the search box. Click on the "Component Services" option.
  • In the pane on the far left, double-click on "Services (Local)." Locate MongoDB in the list in the center pane, and double click on it to bring up the properties inspector window.
  • Make sure "Startup type" is set to automatic. Then click on the "Start" button.
  • Browse to http://localhost:28017/ to verify that MongoDB is really running.
The purpose sharing the actual extracts from the above mentioned article was to increase the availability and have more and more people switch to MongoDB and to keep it for my own reference as well.