Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

MySQL 教程

MySQL 主页 MySQL 简介 MySQL RDBMS

MySQL SQL

MySQL SQL MySQL SELECT MySQL WHERE MySQL AND, OR, NOT MySQL ORDER BY MySQL INSERT INTO MySQL 空值 MySQL UPDATE MySQL DELETE MySQL LIMIT MySQL MIN 和 MAX MySQL COUNT, AVG, SUM MySQL LIKE MySQL 通配符 MySQL IN MySQL BETWEEN MySQL 别名 MySQL 连接 MySQL INNER JOIN MySQL LEFT JOIN MySQL RIGHT JOIN MySQL CROSS JOIN MySQL 自连接 MySQL UNION MySQL GROUP BY MySQL HAVING MySQL EXISTS MySQL ANY, ALL MySQL INSERT SELECT MySQL CASE MySQL 空值函数 MySQL 注释 MySQL 运算符

MySQL 数据库

MySQL 创建数据库 MySQL 删除数据库 MySQL 创建表 MySQL 删除表 MySQL 修改表 MySQL 约束 MySQL 非空 MySQL 唯一 MySQL 主键 MySQL 外键 MySQL 检查 MySQL 默认 MySQL 创建索引 MySQL 自动递增 MySQL 日期 MySQL 视图

MySQL 参考

MySQL 数据类型 MySQL 函数

MySQL 示例

MySQL 示例 MySQL 编辑器 MySQL 问答 MySQL 练习 MySQL 证书

MySQL INSERT INTO 语句


MySQL INSERT INTO 语句

MySQL INSERT INTO 语句用于在表中插入新记录。

INSERT INTO 语法

可以使用两种方式编写 INSERT INTO 语句

1. 指定要插入的列名和值

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

2. 如果要为表的所有列添加值,则无需在 SQL 查询中指定列名。但是,请确保值的顺序与表中的列顺序相同。在这种情况下, INSERT INTO 语法如下

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

演示数据库

以下是 Northwind 示例数据库中 "Customers" 表的一部分数据

CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90

Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91

Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland


INSERT INTO 示例

以下 SQL 语句在 "Customers" 表中插入一条新记录

示例

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');

现在从 "Customers" 表中选择的记录将如下所示

CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90

Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91

Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland
92 Cardinal Tom B. Erichsen Skagen 21 Stavanger 4006 Norway

您注意到我们没有在 CustomerID 字段中插入任何数字吗?
CustomerID 列是一个 自动递增 字段,将在向表中插入新记录时自动生成。


仅在指定列中插入数据

也可以只在特定列中插入数据。

以下 SQL 语句将插入一条新记录,但只在 "CustomerName"、"City" 和 "Country" 列中插入数据(CustomerID 将自动更新)

示例

INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

现在从 "Customers" 表中选择的记录将如下所示

CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90

Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91

Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland
92 Cardinal null null Stavanger null Norway

通过练习测试自己

练习

Customers 表中插入一条新记录。

 Customers 
CustomerName, 
Address, 
City, 
PostalCode,
Country
 
'Hekkan Burger',
'Gateveien 15',
'Sandnes',
'4306',
'Norway';

开始练习


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.