SQL语法基础之INSEART语句

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。 

 

 

一.查看帮助信息

1>.查看INSERT方法的帮助信息

mysql> ? INSERT
Name: 'INSERT'
Description:
Syntax:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    {VALUES | VALUE} (value_list) [, (value_list)] ...
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    SET assignment_list
    [ON DUPLICATE KEY UPDATE assignment_list]

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    SELECT ...
    [ON DUPLICATE KEY UPDATE assignment_list]

value:
    {expr | DEFAULT}

value_list:
    value [, value] ...

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...

INSERT inserts new rows into an existing table. The INSERT ... VALUES
and INSERT ... SET forms of the statement insert rows based on
explicitly specified values. The INSERT ... SELECT form inserts rows
selected from another table or tables. INSERT with an ON DUPLICATE KEY
UPDATE clause enables existing rows to be updated if a row to be
inserted would cause a duplicate value in a UNIQUE index or PRIMARY
KEY.

For additional information about INSERT ... SELECT and INSERT ... ON
DUPLICATE KEY UPDATE, see [HELP INSERT SELECT], and
http://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html.

In MySQL 8.0, the DELAYED keyword is accepted but ignored by the
server. For the reasons for this, see [HELP INSERT DELAYED],

Inserting into a table requires the INSERT privilege for the table. If
the ON DUPLICATE KEY UPDATE clause is used and a duplicate key causes
an UPDATE to be performed instead, the statement requires the UPDATE
privilege for the columns to be updated. For columns that are read but
not modified you need only the SELECT privilege (such as for a column
referenced only on the right hand side of an col_name=expr assignment
in an ON DUPLICATE KEY UPDATE clause).

When inserting into a partitioned table, you can control which
partitions and subpartitions accept new rows. The PARTITION option
takes a list of the comma-separated names of one or more partitions or
subpartitions (or both) of the table. If any of the rows to be inserted
by a given INSERT statement do not match one of the partitions listed,
the INSERT statement fails with the error Found a row not matching the
given partition set. For more information and examples, see
http://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html.

URL: http://dev.mysql.com/doc/refman/8.0/en/insert.html


mysql> 

2>.INSERT三种基本语法

  INSERT语句用于插入数据到表中,其基本语法有以下三种。

mysql> SELECT * FROM student_primary;                        #查看该表的数据,只有一条记录
+--------+----------+--------+
| stu_id | stu_name | gender |
+--------+----------+--------+
|      1 | json     |     10 |
+--------+----------+--------+
1 row in set (0.00 sec)

mysql> 
mysql> INSERT INTO student_primary VALUES(2,'Danny',20);                    #第一种基本语法插入一条数据
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> INSERT INTO student_primary SET stu_id = 3 ,stu_name = '胡歌',gender = 30;      #第二种基本语法插入一条数据,这种插入方式,需要关键字传参,即每个字段都得手动传参。
Query OK, 1 row affected (0.01 sec)

mysql> 
mysql> SELECT * FROM student_primary;                                #插入2条数据后,我们查看一下该表的内容信息
+--------+----------+--------+
| stu_id | stu_name | gender |
+--------+----------+--------+
|      1 | json     |     10 |
|      2 | Danny    |     20 |
|      3 | 胡歌     |     30 |
+--------+----------+--------+
3 rows in set (0.00 sec)

mysql> 
mysql> SELECT * FROM students;                                        #我们查看一张表结构和我们上面插入的表结构相同的表的数据
+--------+--------------+--------+
| stu_id | stu_name     | gender |
+--------+--------------+--------+
|     10 | 漩涡鸣人     |    100 |
|     11 | 佐助         |    100 |
+--------+--------------+--------+
2 rows in set (0.00 sec)

mysql> 
mysql> INSERT INTO student_primary SELECT * FROM students;                      #第三种基本语法插入数据,这种放啊是将一个表的查询结果插入到另一张表中。
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> 
mysql> SELECT * FROM students;                            
+--------+--------------+--------+
| stu_id | stu_name     | gender |
+--------+--------------+--------+
|     10 | 漩涡鸣人     |    100 |
|     11 | 佐助         |    100 |
+--------+--------------+--------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM student_primary;                     
+--------+--------------+--------+
| stu_id | stu_name     | gender |
+--------+--------------+--------+
|      1 | json         |     10 |
|      2 | Danny        |     20 |
|      3 | 胡歌         |     30 |
|     10 | 漩涡鸣人     |    100 |
|     11 | 佐助         |    100 |
+--------+--------------+--------+
5 rows in set (0.00 sec)

mysql> 

 

二.INSERT关键点剖析

1>.上面我们介绍了INSERT三种基本语法,前两种“INSERT ... VALUES” 和 “INSERT ... SET”两种语句都是将指定的数据插入到现成的表中,而 “INSERT ... SELECT”语句是将另外表中数据查出来并插入到现成的表中;

2>.Pritition子句代表可以将数据插入到指定到表分区中;

3>.Tab_name代表将数据插入到目标表;

4>.Col_name代表要插入指定数据到目标表列,如果是多列则用逗号隔开,如果目标表中到某些列没有在INSERT语句中指定,则这些列会插入默认值,当然可以使用DELFAULT显示指定查默认值;

5>.VALUES中除了可以指定到数值之外,还可以使用表达式(EXPR);

mysql> SELECT * FROM student_primary;
+--------+--------------+--------+
| stu_id | stu_name     | gender |
+--------+--------------+--------+
|      1 | json         |     10 |
|      2 | Danny        |     20 |
|      3 | 胡歌         |     30 |
|     10 | 漩涡鸣人     |    100 |
|     11 | 佐助         |    100 |
+--------+--------------+--------+
5 rows in set (0.00 sec)

mysql> 
mysql> INSERT INTO student_primary(stu_name,gender) VALUES('马云',40);
Query OK, 1 row affected (0.01 sec)

mysql> 
mysql> INSERT INTO student_primary(stu_id) VALUES(50);
Query OK, 1 row affected (0.01 sec)

mysql> 
mysql> INSERT INTO student_primary(gender) VALUES(30*3);
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> SELECT * FROM student_primary;
+--------+--------------+--------+
| stu_id | stu_name     | gender |
+--------+--------------+--------+
|      1 | json         |     10 |
|      2 | Danny        |     20 |
|      3 | 胡歌         |     30 |
|     10 | 漩涡鸣人     |    100 |
|     11 | 佐助         |    100 |
|     12 | 马云         |     40 |
|     50 | NULL         |   NULL |
|     51 | NULL         |     90 |
+--------+--------------+--------+
8 rows in set (0.00 sec)

mysql> 
mysql> INSERT INTO student_primary(gender) VALUES(30*3);

相关文章:

  • 2021-05-28
  • 2021-09-16
  • 2021-09-23
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-09-03
猜你喜欢
  • 2021-11-14
  • 2021-11-14
  • 2021-11-07
  • 2021-07-10
  • 2022-01-28
  • 2022-12-23
相关资源
相似解决方案