Avoiding rewrite attributes with MySQL AUTO_INCREMENT

MySQLMySQLi Database

You can avoid with the help of providing the value (NULL,0,DEFAULT) with AUTO_INCREMENT. Let us first create a table −

mysql> create table DemoTable1350
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20)
   -> );
Query OK, 0 rows affected (0.44 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1350 values(NULL,'Chris');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1350 values(0,'Chris');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1350 values(DEFAULT,'Chris');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1350;

This will produce the following output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|         1 | Chris       |
|         2 | Chris       |
|         3 | Chris       |
+-----------+-------------+
3 rows in set (0.00 sec)
raja
Updated on 05-Nov-2019 09:29:46

Advertisements