zoukankan      html  css  js  c++  java
  • 命令行中执行批量SQL的方法

    基础信息介绍

      测试库:test;

      测试表:user;

      user表定义:

    CREATE TABLE `user` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` char(30) NOT NULL,
      `age` int(11) NOT NULL,
      `gender` tinyint(1) DEFAULT '1' COMMENT '性别:1男;2女',
      `addr` char(30) NOT NULL,
      `status` tinyint(1) DEFAULT '1',
      PRIMARY KEY (`id`),
      KEY `idx` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    

      希望一次性执行的命令如下:

    # 指定数据库
    use test;
    
    # 清空user表
    truncate table user;
    
    # 插入若干条数据
    insert into user
        (id, name, age, gender, addr, status)
    values
        (1, 'a', 1, 1, 'beijing', 1),
        (2, 'a', 1, 1, 'beijing', 1),
        (3, 'a', 1, 1, 'beijing', 1),
        (4, 'a', 1, 1, 'beijing', 1),
        (5, 'a', 1, 1, 'beijing', 1);
    
    # 删除一条数据
    delete from user where id=4;
    
    # 修改一条数据
    update user set status=0 where id=5;
    
    # 查看所有记录
    select * from user;
    

      接下来就用下面列举的3种方式,在命令行中一次性执行上面的sql。  

    方式1:登录mysql后source

      将要执行的sql保存到一个文件中,我这里将其命名为command.sql,命名随意,后缀.sql是为了便于识别是sql代码,也可以执行其他的比如.txt或者.cmd,与此同时我将command.sql保存到/tmp目录下。

      登录进入mysql,然后使用source命令,参数就是保存sql的文件绝对路径,sql文件的保存路径没有特殊要求,只要通过路径找到文件即可;

      下面是示例:

    # 登录到mysql
    $ mysql -uroot -p
    Enter password:
    ......
    
    # 使用source命令,指定sql文件所在的绝对路径
    mysql> source /tmp/command.sql
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 5 rows affected (0.00 sec)
    Records: 5  Duplicates: 0  Warnings: 0
    
    Query OK, 1 row affected (0.00 sec)
    
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    +----+------+-----+--------+---------+--------+
    | id | name | age | gender | addr    | status |
    +----+------+-----+--------+---------+--------+
    |  1 | a    |   1 |      1 | beijing |      1 |
    |  2 | a    |   1 |      1 | beijing |      1 |
    |  3 | a    |   1 |      1 | beijing |      1 |
    |  5 | a    |   1 |      1 | beijing |      0 |
    +----+------+-----+--------+---------+--------+
    4 rows in set (0.00 sec)
    
    mysql>

    方式2:mysql -u -p < command.sql

      先将要执行的sql保存到文件中,此处仍为/tmp/command.sql;

    $ mysql -uroot -p < /tmp/command.sql
    Enter password:
    id	name	age	gender	addr	status
    1	a	1	1	beijing	1
    2	a	1	1	beijing	1
    3	a	1	1	beijing	1
    5	a	1	1	beijing	0

      

    方式3:cat command.sql | mysql -u -p

      这种方式其实等价于方式二,先将要执行的sql保存到文件中,此处仍为/tmp/command.sql;

    $ cat /tmp/command.sql | mysql -uroot -p
    Enter password:
    id	name	age	gender	addr	status
    1	a	1	1	beijing	1
    2	a	1	1	beijing	1
    3	a	1	1	beijing	1
    5	a	1	1	beijing	0
    

      

    命令行显式指定操作的数据库

      对于方式2和方式3来说,需要在sql文件中指定操作的数据库,如果想要在命令行中指定数据库,可以在命令行中增加数据库名:

    # 指定对test数据库进行操作
    $ mysql -uroot -p test < /tmp/command.sql
    Enter password:
    id	name	age	gender	addr	status
    1	a	1	1	beijing	1
    2	a	1	1	beijing	1
    3	a	1	1	beijing	1
    5	a	1	1	beijing	0
    

      虽然命令行指定了数据库,但是sql文件中仍可以进行数据库的切换,所以不建议在命令行中指定操作的数据库。

  • 相关阅读:
    updatepanel中不能使用fileupload的弥补方法
    AJAXPro用法,关于JS同步和异步调用后台代码的学习
    How do I get data from a data table in javascript?
    记不住ASP.NET页面生命周期的苦恼
    浅谈ASP.NET中render方法
    解决AjaxPro2中core.ashx 407缺少对象的问题
    ServU 6.0出来了
    关于X Server/Client和RDP的畅想
    这个Blog好像没有分页功能嘛
    AOC的显示器太烂了
  • 原文地址:https://www.cnblogs.com/-beyond/p/11759689.html
Copyright © 2011-2022 走看看