Can we insert values without mentioning the column name in MySQL?


Yes, we can insert values without mentioning the column name using the following syntax −

insert into yourTableName values(yourValue1,yourValue2,yourValue3,.....N);

Let us first create a table. Here, we have set Id as NOT NULL −

mysql> create table DemoTable862(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100) ,
   Age int
);
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable862 values(NULL,'Chris',23);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable862 values(NULL,'Robert',21);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable862 values(NULL,'Mike',24);
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable862 values(NULL,'Sam',25);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable862 values(NULL,'Bob',26);
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable862;

This will produce the following output. Above, we have set NULL while inserting the values. Since we have set Id as NOT NULL, therefore those NULL values won’t work for Id column and the auto_increment will automatically add values for Id −

+----+-----------+------+
| Id | FirstName | Age  |
+----+-----------+------+
| 1  | Chris     |   23 |
| 2  | Robert    |   21 |
| 3  | Mike      |   24 |
| 4  | Sam       |   25 |
| 5  | Bob       |   26 |
+----+-----------+------+
5 rows in set (0.00 sec)

Updated on: 03-Sep-2019

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements