Friday, January 29, 2016

AUTO INCREMENT IN SQL: How to Set Your Primary Key To Auto Increment

I often used an auto increment primary key in creating my table due to unavailable candidate for a primary key. With the use of the use of the auto increment property, you will avoid duplicate entries for your primary key.

So here is the easy way on how to do it.

First, you add new table and you write the table name, column name, data type, length. For your primary key, you must set the data type into integer since you cannot increment other data types.

Then, under the index column, select PRIMARY from the drop down menu.

Third, you need to check the checkbox under the A_I column. A_I stands for auto increment.

Click the save button and that's it! You're done!

Another way on how to set an ID to auto increment is to specify it when creating your table. I have here the SQL code for you to understand. This will create a STUDENT table with the following fields: stud_id, lastname and firstname. The properties for each columns are seen on the image below.

Use the DESCRIBE STUDENT; command to display the structure of a table.


ALTER TABLE table_name MODIFY COLUMN id INT auto_increment;


ALTER TABLE student MODIFY COLUMN stud_id INT auto_increment;

Make sure your column is set to PRIMARY KEY. Hope this helps!

AUTO INCREMENT IN SQL: How to Set Your Primary Key To Auto Increment

No comments:

Post a Comment