Home > Database > Mysql Tutorial > How to insert a value into a column without specifying the column name in MySQL INSERT INTO statement?

How to insert a value into a column without specifying the column name in MySQL INSERT INTO statement?

WBOY
Release: 2023-08-25 15:05:13
forward
1596 people have browsed it

如何在 MySQL INSERT INTO 语句中不指定列名的情况下在列中插入值?

In order to insert a value in a column without specifying a column name in the INSERT INTO statement, we must give the number of values ​​that matches the number of columns in the table, and also Note the data type of the column.

Example

In the example below, we inserted the value without specifying the column name.

mysql> Insert into student values(100,'Gaurav','Ph.D');
Query OK, 1 row affected (0.08 sec)

mysql> Select * from student;
+--------+--------+--------+
| RollNO | Name   | Class  |
+--------+--------+--------+
| 100    | Gaurav | Ph.D   |
+--------+--------+--------+

1 row in set (0.00 sec)

mysql> Insert into student values(200,'Rahul','Ph.D'),(300,'Aarav','B.tech');
Query OK, 2 rows affected (0.12 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> Select * from student;

+--------+--------+--------+
| RollNO | Name   | Class  |
+--------+--------+--------+
| 100    | Gaurav | Ph.D   |
| 200    | Rahul  | Ph.D   |
| 300    | Aarav  | B.tech |
+--------+--------+--------+

3 rows in set (0.00 sec)
Copy after login

If we don’t care about the total number of columns and their data types, MySQL will throw an error as shown below -

mysql> Insert into student values(400,'Raman',M.Tech);
ERROR 1054 (42S22): Unknown column 'M.Tech' in 'field list'

mysql> Insert into student values(400,'Raman');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
Copy after login

The above is the detailed content of How to insert a value into a column without specifying the column name in MySQL INSERT INTO statement?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template