zoukankan      html  css  js  c++  java
  • mysql-插入、更新、删除数据

    1、插入:
    
        ①  mysql中有三种插入:insert intoreplace intoinsert ignore 
                insert into:表示插入数据,数据库会检查主键,如果出现重复会报错; 
                replace into:表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样; 
                insert ignore:表示如果表中已经存在相同的记录,则忽略当前新数据,当主键重复时会忽略新数据。 
                    
                    
                    
                    > insert ignore into class values(49,'张全蛋',now());
                    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
                    > select * from class;
                    +----------+------------+---------------------+
                    | class_id | class_name | date                |
                    +----------+------------+---------------------+
                    |       46 | 小强       | 2017-06-06 22:04:46 |
                    |       47 | 小丽       | 2017-06-06 22:04:46 |
                    |       48 | 小芳       | 2017-06-06 22:04:46 |
                    |       49 | 小王       | 2017-06-06 22:04:46 |
                    +----------+------------+---------------------+
                    
                    
                    > replace into class values(49,'张全蛋',now());
                    Query OK, 2 rows affected (0.00 sec)
    
                    > select * from class;
                    +----------+------------+---------------------+
                    | class_id | class_name | date                |
                    +----------+------------+---------------------+
                    |       46 | 小强       | 2017-06-06 22:04:46 |
                    |       47 | 小丽       | 2017-06-06 22:04:46 |
                    |       48 | 小芳       | 2017-06-06 22:04:46 |
                    |       49 | 张全蛋     | 2017-06-06 22:25:55 |
                    +----------+------------+---------------------+
                    
    
        ②  将多行查询结果插入到表中:
            语法:select_statement语句中查询的字段数应该和前面要插入的字段一致。
                insert into table1_name(field_name ...) select field_name ... from table2_name where ...;
                    
                insert into class(class_name) select name from asd [where id between 5 and 10]; 
    
    
        ③  当要导入的数据中有重复值的时候,MYSQL会有三种方案:
    
            方案一:使用 ignore 关键字  ignore(忽视)
            方案二:使用 replace into   
            方案三:ON DUPLICATE KEY UPDATE
            
        
            
    2、更新:
          update tb_name set field_name=value where field1_name=value;
              
    3、删除:
          删除行:
              delete from tb_name where field_name=value;
                  
          删除一定范围内的数据:
              delete from tb_name where field_name between value1 and value2;
                  
          清空表:
              delete from tb_name;
  • 相关阅读:
    Postman-插入断言
    浅谈如何提高自动化测试的稳定性和可维护性 (pytest&allure)
    pytest+allure+jenkins-持续集成平台生成allure报告
    pytest简介及入门
    allure描述用例详细讲解
    allure标记用例级别severity
    allure-pytest 功能用例与自动化用例完美对接
    allure-pytest 环境准备和初步运行
    selenium常用操作之上传操作
    selenium常用操作之JS处理日历控件
  • 原文地址:https://www.cnblogs.com/oural-yan/p/6953993.html
Copyright © 2011-2022 走看看