首页  >  文章  >  数据库  >  如何在 MySQL INSERT INTO 语句中不指定列名的情况下在列中插入值?

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

WBOY
WBOY转载
2023-08-25 15:05:131582浏览

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

为了在 INSERT INTO 语句中不指定列名的情况下在列中插入值,我们必须给出与表中的列数匹配的值的数量,同时还要注意该列的数据类型.

示例

在下面的示例中,我们插入了值,但未指定列名称。

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)

如果我们不关心列的总数及其数据类型,MySQL 会抛出错误,如下所示 -

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

以上是如何在 MySQL INSERT INTO 语句中不指定列名的情况下在列中插入值?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除