SQL Query to auto increment primary key

In this article, we will find out about the auto increase function of SQL and afterward the procedure to include it our primary key.

Primary keys are significant in light of the fact that they have one of a kind records which at that point used to distinguish the records in the table. Be that as it may, how would we know which the one of a kind value is and which isn’t?

There is no simple way we can foresee the following exceptional value without glancing in the database. Along these lines, this brings an extraordinary idea which let the database handles the value of a primary key.

The database monitors the greatest value our key has reached previously and it generally increases that value by 1. At the point when we let the database handles the key, we should give any value for that field when we embed a record.

Presently to make a primary key AUTO_INCREMENT, we must have a field which has the datatype with number juggling abilities. Without a doubt, varchar isn’t that datatype.

On the off chance that we got this thing all together, at that point simply ALTER the table and MODIFY the section.

Table (worker) having three fields and four information in it,

    Id		Name		Address

    100		Aman		Mumbai
    200		Arun		Pune
    300		Karan		Delhi
    400		Aman		Mumbai

Above table got three sections and one of them (id) is our primary key and now we simply need to make an adjust table inquiry with alter segment and afterward give the segment name on which we have to include auto increase after that simply include AUTO_INCREMENT in it.

Query:

ALTER TABLE employee 
MODIFY COLUMN 'id' INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

Output:

SQL Query to auto increment primary key

Leave a Comment

error: Alert: Content is protected!!