首页 > 编程笔记 > MySQL笔记 阅读:24

SQL INSERT语句的2种用法(附带实例)

创建完数据库和数据表后,接下来就该往表中插入数据了,在 MySQL/MariaDB 数据库中,可以通过 INSERT 语句向数据库已有的数据表中插入一行或者多行数据。

INSERT 语句有两种形式:INSERT...VALUES... 语句和 INSERT...SET... 语句。

1) INSERT...VALUES... 语句的语法格式为:
INSERT INTO 表名[(列名1, 列名2, 列名3, 列名n)]
VALUES(值1,值2,值3,值n),
(值1,值2,值3,值n);
各字段的含义如下:
2) INSERT...SET...语句的语法格式为:
INSERT INTO 表名 SET 列名1=值1, 列名2=值2, 列名n=值n;
这两种语法格式各具特点,INSERT...SET... 语句允许在插入数据时列名和列的值能够成双成对地出现,所呈现出来的效果就是插入的数据与对应的列一目了然;而 INSERT...VALUES... 语句允许一次性插入多条数据,这就省去了多次执行 INSERT 语句的时间,效率更高。

【实例 1】应用INSERT...SET...语句和INSERT...VALUES...语句。
##新创建一个数据库,在这个数据库中创建表,用来演示插入数据
MariaDB [(none)]> create database test01;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> use test01;
Database changed

##新创建一个表,注意:stu_id和english字段不能为空,其他字段可以
MariaDB [test01]> create table stu_score (
    -> stu_id int not null,
    -> name varchar(20) primary key,
    -> english char(3) not null,
    -> mathematics char(3),
    -> geography char(3)
    -> );
Query OK, 0 rows affected (0.006 sec)

##查看表结构
MariaDB [test01]> desc stu_score;
+-------------+-------------+------+------+---------+-------+
| Field       | Type        | Null | Key  | Default | Extra |
+-------------+-------------+------+------+---------+-------+
| stu_id      | int(11)     | NO   |      | NULL    |       |
| name        | varchar(20) | NO   | PRI  | NULL    |       |
| english     | char(3)     | NO   |      | NULL    |       |
| mathematics | char(3)     | YES  |      | NULL    |       |
| geography   | char(3)     | YES  |      | NULL    |       |
+-------------+-------------+------+------+---------+-------+
5 rows in set (0.001 sec)

##使用INSERT...VALUES...语句插入多条数据
MariaDB [test01]> insert into stu_score
    -> (stu_id,name,english,mathematics,geography)
    -> values
    -> (1,'小孙','56','44','96'),
    -> (2,'小刘','67','58','74');
Query OK, 2 rows affected (0.001 sec)
Records: 2  Duplicates: 0  Warnings: 0

##使用INSERT...SET...语句格式插入一条数据
MariaDB [test01]> insert into stu_score set
    -> stu_id = 3,
    -> name = '小崔',
    -> english = '78',
    -> mathematics = '76',
    -> geography = '48';
Query OK, 1 row affected (0.001 sec)

##此命令专门用来看表中所有的数据
MariaDB [test01]> select * from stu_score;
+--------+--------+---------+-------------+------------+
| stu_id | name   | english | mathematics | geography  |
+--------+--------+---------+-------------+------------+
| 2      | 小刘   | 67      | 58          | 74         |
| 1      | 小孙   | 56      | 44          | 96         |
| 3      | 小崔   | 78      | 76          | 48         |
+--------+--------+---------+-------------+------------+
3 rows in set (0.001 sec)

【实例 2】通过 INSERT...VALUES...语句对表中所有的字段都插入数据(插入完整的数据记录),可以省略字段部分内容。
##使用 INSERT...VALUES...语句时,省略字段部分内容,对表中所有的字段插入数据(插入完整的数据记录 )
MariaDB [test01]> insert into stu_score
    -> values
    -> (4,'小狗','76','39','69');
Query OK, 1 row affected (0.001 sec)

##针对某几个字段插入数据(插入一部分数据记录)
MariaDB [test01]> insert into stu_score (stu_id,name,english) values (5,'小猫','88');
Query OK, 1 row affected (0.001 sec)

##查看目前表中的数据
MariaDB [test01]> select * from stu_score;
+--------+--------+---------+-------------+------------+
| stu_id | name   | english | mathematics | geography  |
+--------+--------+---------+-------------+------------+
| 2      | 小刘   | 67      | 58          | 74         |
| 1      | 小孙   | 56      | 44          | 96         |
| 3      | 小崔   | 78      | 76          | 48         |
| 4      | 小狗   | 76      | 39          | 69         |
| 5      | 小猫   | 88      | NULL        | NULL       |
+--------+--------+---------+-------------+------------+
5 rows in set (0.000 sec)
以上就是通过 INSERT 语句向表中插入数据的具体用法,INSERT语句的两种语法格式精通一种即可,另一种作为备用,在有特殊需求的时候再使用。

相关文章