• SQL语法基础之INSEART语句


                  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);

    6>.INSERT ... VALUES语句不光可以插入一条数据,也可以插入多条数据;

    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> 
    mysql> INSERT INTO student_primary(stu_name,gender) VALUES('柯南',12),('黑猫警长',5),('金刚葫芦娃',10);
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0        #注意,Records代表此语句操作了多少行数据,但不一定是多少行被插入但数据,因为如果存在相同但行数据且违反了某个唯一性,则Duplicates会新鲜事非0数值,Warning代表语句执行过程中但一些警告信息。
    
    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 |
    |     52 | 柯南            |     12 |
    |     53 | 黑猫警长        |      5 |
    |     54 | 金刚葫芦娃      |     10 |
    +--------+-----------------+--------+
    11 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO student_primary(stu_name,gender) VALUES('柯南',12),('黑猫警长',5),('金刚葫芦娃',10);

    7>.LOW_PRIORITY关键词代表如何有其他链接正在读取目标表数据,则此INSERT语句需要等待读取完成;

    8>.LOW_PRIORITY和HIGH_PRIORITY关键词仅在MyISAM,MEMORY,and MERGE三种存储引擎下才生效;

    9>.IGNORE关键词代表INSERT语句如果违反主键和唯一键但约束条件,则不报错而只产生警告信息,违反的行被丢弃,而不是整个语句回退;在数据类型转换有问题是如果有IGNORE则只产生告警信息,而不是语句回退; 

    mysql> DESC student_primary;
    +----------+-------------+------+-----+---------+----------------+
    | Field    | Type        | Null | Key | Default | Extra          |
    +----------+-------------+------+-----+---------+----------------+
    | stu_id   | int(11)     | NO   | PRI | NULL    | auto_increment |
    | stu_name | varchar(50) | YES  |     | NULL    |                |
    | gender   | int(11)     | YES  |     | NULL    |                |
    +----------+-------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
    
    mysql> 
    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 |
    |     52 | 柯南            |     12 |
    |     53 | 黑猫警长        |      5 |
    |     54 | 金刚葫芦娃      |     10 |
    +--------+-----------------+--------+
    11 rows in set (0.00 sec)
    
    mysql> 
    mysql> 
    mysql> INSERT INTO student_primary VALUES(1,'尹正杰',18);        #由于主键的特效,我对stu_id字段插入一条已经存在对id数据,发现报错了,说是主键重复!
    ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
    mysql> 
    mysql> 
    mysql> INSERT IGNORE INTO student_primary VALUES(1,'尹正杰',18);    #如果我们使用了IGNORE关键字后,发现上述对报错信息是不会提示出来的,而是出现了一个warning的警告信息
    Query OK, 0 rows affected, 1 warning (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 |
    |     52 | 柯南            |     12 |
    |     53 | 黑猫警长        |      5 |
    |     54 | 金刚葫芦娃      |     10 |
    +--------+-----------------+--------+
    11 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT IGNORE INTO student_primary VALUES(1,'尹正杰',18);

    三.INSERT进阶知识

    1>.INSERT ... SELECT语句详解

       该语句用于从另外的表中查询记录并插入到目标表中,当目标表和SELECT语句中的表相同时,则会现将SELECT语句的结果存放在临时表中,再插入到目标表中(注意执行顺序),下面的SQL语句就是一个案例。

    mysql> SELECT * FROM students;
    +--------+--------------+--------+
    | stu_id | stu_name     | gender |
    +--------+--------------+--------+
    |     10 | 漩涡鸣人     |    100 |
    |     11 | 佐助         |    100 |
    |     21 | 孙悟空       |    200 |
    |     22 | 猪八戒       |    250 |
    |     23 | 唐三藏       |    300 |
    |     24 | 沙和尚       |    350 |
    +--------+--------------+--------+
    6 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO student_primary SELECT * FROM students WHERE stu_id > 20;
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    
    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 |
    |     21 | 孙悟空          |    200 |
    |     22 | 猪八戒          |    250 |
    |     23 | 唐三藏          |    300 |
    |     24 | 沙和尚          |    350 |
    |     50 | NULL            |   NULL |
    |     51 | NULL            |     90 |
    |     52 | 柯南            |     12 |
    |     53 | 黑猫警长        |      5 |
    |     54 | 金刚葫芦娃      |     10 |
    +--------+-----------------+--------+
    15 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO student_primary SELECT * FROM students WHERE stu_id > 20;

    2>.INSERT DELAYED 语句详解

      在5.6.6版本之前,用来表示此插入语句当碰到其他链接正在使用目标表时就等待,知道目标表没有被用时在插入数据。

      在5.7版本时,DELAYED关键词就不再支持,但语句执行时不会报错,只会产生一个警告信息,后续版本可能会去掉此关键词。

      在8.0.14版本中DELEYED关键词依旧还没有被剔除。

    mysql> SELECT * FROM student_primary;
    +--------+-----------------+--------+
    | stu_id | stu_name        | gender |
    +--------+-----------------+--------+
    |      1 | json            |     10 |
    |      2 | Danny           |     20 |
    |      3 | 胡歌            |     30 |
    |     10 | 漩涡鸣人        |    100 |
    |     11 | 佐助            |    100 |
    |     12 | 马云            |     40 |
    |     21 | 孙悟空          |    200 |
    |     22 | 猪八戒          |    250 |
    |     23 | 唐三藏          |    300 |
    |     24 | 沙和尚          |    350 |
    |     50 | NULL            |   NULL |
    |     51 | NULL            |     90 |
    |     52 | 柯南            |     12 |
    |     53 | 黑猫警长        |      5 |
    |     54 | 金刚葫芦娃      |     10 |
    +--------+-----------------+--------+
    15 rows in set (0.00 sec)
    
    mysql> SELECT * FROM students;
    +--------+--------------+--------+
    | stu_id | stu_name     | gender |
    +--------+--------------+--------+
    |     10 | 漩涡鸣人     |    100 |
    |     11 | 佐助         |    100 |
    |     21 | 孙悟空       |    200 |
    |     22 | 猪八戒       |    250 |
    |     23 | 唐三藏       |    300 |
    |     24 | 沙和尚       |    350 |
    |     31 | 李白         |    400 |
    |     32 | 蛮王         |    500 |
    |     33 | 诡术妖姬     |    600 |
    |     34 | 小鱼人       |    700 |
    +--------+--------------+--------+
    10 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT DELAYED INTO student_primary SELECT * FROM students WHERE stu_id > 30;
    Query OK, 4 rows affected, 1 warning (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 1
    
    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 |
    |     21 | 孙悟空          |    200 |
    |     22 | 猪八戒          |    250 |
    |     23 | 唐三藏          |    300 |
    |     24 | 沙和尚          |    350 |
    |     31 | 李白            |    400 |
    |     32 | 蛮王            |    500 |
    |     33 | 诡术妖姬        |    600 |
    |     34 | 小鱼人          |    700 |
    |     50 | NULL            |   NULL |
    |     51 | NULL            |     90 |
    |     52 | 柯南            |     12 |
    |     53 | 黑猫警长        |      5 |
    |     54 | 金刚葫芦娃      |     10 |
    +--------+-----------------+--------+
    19 rows in set (0.00 sec)
    
    mysql> 
    mysql> SELECT * FROM students;
    +--------+--------------+--------+
    | stu_id | stu_name     | gender |
    +--------+--------------+--------+
    |     10 | 漩涡鸣人     |    100 |
    |     11 | 佐助         |    100 |
    |     21 | 孙悟空       |    200 |
    |     22 | 猪八戒       |    250 |
    |     23 | 唐三藏       |    300 |
    |     24 | 沙和尚       |    350 |
    |     31 | 李白         |    400 |
    |     32 | 蛮王         |    500 |
    |     33 | 诡术妖姬     |    600 |
    |     34 | 小鱼人       |    700 |
    +--------+--------------+--------+
    10 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT DELAYED INTO student_primary SELECT * FROM students WHERE stu_id > 30;

    3>.INSERT ON DUPLLICATE KEY UPDATE 语句详解

       当INSERT语句中使用ON DUPLICATE KEY UPDATE子句时,如果碰到当前插入的数据违反主键或者唯一性约束,则INSERT会转变成UPDATE语句修改对应依旧存在表中的这条数据。当然,ON DUPLICATE KEY UPDATE子句后面可以跟多个修改,用逗号隔开即可,但一般情况喜爱我们应该避免出现对应多条的情况。

    mysql> SELECT * FROM student_primary WHERE stu_id < 10;
    +--------+----------+--------+
    | stu_id | stu_name | gender |
    +--------+----------+--------+
    |      1 | json     |     10 |
    |      2 | Danny    |     20 |
    |      3 | 胡歌     |     30 |
    +--------+----------+--------+
    3 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO student_primary(stu_id,stu_name,gender) VALUES(1,'尹正杰',18) ON DUPLICATE KEY UPDATE gender=gender+2;    #注意,我们修改stu_id这个字段为1的这行数据已经存在,因此它只修改已经存在的字段对应的值!
    Query OK, 2 rows affected (0.00 sec)
    
    mysql> 
    mysql> SELECT * FROM student_primary WHERE stu_id < 10;
    +--------+----------+--------+
    | stu_id | stu_name | gender |
    +--------+----------+--------+
    |      1 | json     |     12 |
    |      2 | Danny    |     20 |
    |      3 | 胡歌     |     30 |
    +--------+----------+--------+
    3 rows in set (0.00 sec)
    
    mysql> 
    mysql> UPDATE student_primary SET gender=gender+2 WHERE stu_id = 1;            #上面的那条语句很明显只是修改已经存在表中的那条数据,它和咱们这条执行语句结果相同!
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> 
    mysql> SELECT * FROM student_primary WHERE stu_id < 10;
    +--------+----------+--------+
    | stu_id | stu_name | gender |
    +--------+----------+--------+
    |      1 | json     |     14 |
    |      2 | Danny    |     20 |
    |      3 | 胡歌     |     30 |
    +--------+----------+--------+
    3 rows in set (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO student_primary(stu_id,stu_name,gender) VALUES(1,'尹正杰',18) ON DUPLICATE KEY UPDATE gender=gender+2;

    4>.小试牛刀

    • 将如下数据插入到student表中
        1,’孙悟空’ ; 2,’猪八戒’; 4,’沙和尚’
    
    • 根据create table ... like语句创建teacher_backup表,并插入如下数 据:
        1,’Chinese’,1; 2,’English’,4; 3,’Physic’,4
    
    • 将teacher_backup表的数据通过insert...select语句插入到teacher表中
     

       我们把题读完后,发现需要三张表,第一张表是students,第二张表是teacher表,第三张表是teacher_backup表且该表的表结构和第二张表相同。结合题意的数据我们不难创建出表结构,具体SQL如下。

    mysql> CREATE TABLE student(stu_id INT(11) PRIMARY KEY AUTO_INCREMENT,stu_name VARCHAR(30));
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> 
    mysql> INSERT INTO student(stu_id,stu_name) VALUES(1,'孙悟空');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO student(stu_id,stu_name) VALUES(2,'猪八戒');
    Query OK, 1 row affected (0.01 sec)
    
    mysql> 
    mysql> INSERT INTO student(stu_id,stu_name) VALUES(4,'沙和尚');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> 
    mysql> SELECT * FROM student; 
    +--------+-----------+
    | stu_id | stu_name  |
    +--------+-----------+
    |      1 | 孙悟空    |
    |      2 | 猪八戒    |
    |      4 | 沙和尚    |
    +--------+-----------+
    3 rows in set (0.00 sec)
    
    mysql> 
    创建学生表(student)并插入数据
    mysql> CREATE TABLE teacher(t_id INT(11) PRIMARY KEY AUTO_INCREMENT,course_name VARCHAR(20),teacher_id INT(11));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> 
    mysql> CREATE TABLE teacher_backup LIKE teacher;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> 
    mysql> INSERT INTO teacher_backup(course_name,teacher_id) VALUES('Chinese',1),('English',4),('Physic',4);
    Query OK, 3 rows affected (0.01 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> 
    mysql> SELECT * FROM teacher_backup;
    +------+-------------+------------+
    | t_id | course_name | teacher_id |
    +------+-------------+------------+
    |    1 | Chinese     |          1 |
    |    2 | English     |          4 |
    |    3 | Physic      |          4 |
    +------+-------------+------------+
    3 rows in set (0.00 sec)
    
    mysql> 
    创建teacher_backup表并插入数据
    mysql> SELECT DATABASE();
    +-------------+
    | DATABASE()  |
    +-------------+
    | yinzhengjie |
    +-------------+
    1 row in set (0.00 sec)
    
    mysql> 
    mysql> SHOW TABLES;
    +-----------------------+
    | Tables_in_yinzhengjie |
    +-----------------------+
    | student               |
    | teacher               |
    | teacher_backup        |
    +-----------------------+
    3 rows in set (0.00 sec)
    
    mysql> 
    mysql> SELECT * FROM student;
    +--------+-----------+
    | stu_id | stu_name  |
    +--------+-----------+
    |      1 | 孙悟空    |
    |      2 | 猪八戒    |
    |      4 | 沙和尚    |
    +--------+-----------+
    3 rows in set (0.00 sec)
    
    mysql> 
    mysql> SELECT * FROM teacher_backup;
    +------+-------------+------------+
    | t_id | course_name | teacher_id |
    +------+-------------+------------+
    |    1 | Chinese     |          1 |
    |    2 | English     |          4 |
    |    3 | Physic      |          4 |
    +------+-------------+------------+
    3 rows in set (0.00 sec)
    
    mysql> 
    mysql> SELECT * FROM teacher;
    Empty set (0.00 sec)
    
    mysql> 
    mysql> INSERT INTO teacher SELECT * FROM teacher_backup;      #将teacher_backup的数据倒入到teacher表中。
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> 
    mysql> SELECT * FROM teacher;
    +------+-------------+------------+
    | t_id | course_name | teacher_id |
    +------+-------------+------------+
    |    1 | Chinese     |          1 |
    |    2 | English     |          4 |
    |    3 | Physic      |          4 |
    +------+-------------+------------+
    3 rows in set (0.00 sec)
    
    mysql> 
     
  • 相关阅读:
    调用API接口,查询手机号码归属地(2)
    调用API接口,查询手机号码归属地(1)
    F
    Icebound and Sequence(非互质逆元 快速乘法)or(矩阵快速幂)
    ProblemC、小花梨判连通(dfs)+想法stl
    cwb个人练习
    Fire Net (二分图匹配 匈牙利算法模板)
    Fire Net HDU
    Going Home POJ
    Two Sequences (二分+二进制) (好题)
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/10284741.html
Copyright © 2020-2023  润新知