Auto-increment is a very useful feature that allows a unique number to be generated when a new record is inserted into a table. The DBMS will automatically increment the last number of the table and add it to the auto-incremented column. Unfortunately, different DBMSs use different ways or syntax to do this. In this post, I will show you how to include the auto-increment feature in MySQL, Microsoft SQL Server, and Oracle.

Auto-Increment in MySQL

SQL:
  1. CREATE TABLE Students (
  2. id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  3. fullname VARCHAR(80) NOT NULL
  4. );

As you can see from the above code, the auto-increment feature is very simple when it's applied in MySQL. You only need to add auto_increment as shown in line 2. By default, the auto_increment value starts at 1. If you want the auto_increment to start with value other than 1, you have to set that value using ALTER TABLE:

SQL:
  1. ALTER TABLE Students AUTO_INCREMENT=2000

Google Maps

08Apr07

My Map

I finally had a time trying Google Maps today! It's quite exciting (well, at least for me! :D ). Check out my Google Maps:

SQL: if exists

19Mar07

I collected these SQL scripts so that whenever I want to check the existency of a table, view, stored procedure, or function in a database, all I have to do is to copy and paste them to the Query Analyzer. FYI, These scripts are only used for SQL Server :) . Hope it's useful!

Check if the table exists in a database:

SQL:
  1. IF EXISTS
  2. (
  3.   SELECT * FROM dbo.sysobjects
  4.   WHERE id = object_id(N'[dbo].[enterTableNameHere]')
  5.          AND OBJECTPROPERTY(id, N'IsUserTable') = 1
  6. )
  7.  
  8. DROP TABLE [dbo].[enterTableNameHere]
  9.  
  10. GO