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;
  • 相关阅读:
    智慧城市建设中政府网站群建设起到了积极的作用
    SQLite 入门教程(四)增删改查,有讲究 (转)
    基于H.264的实时网络摄像——Android客户端
    中小型数据存储方案探讨
    SQL的多表操作
    lua中的时间函数
    C++ 输入输出文件流(ifstream&ofstream)
    linux系统下的shell脚本
    makefile的简单写法
    Linux-ubuntu
  • 原文地址:https://www.cnblogs.com/oural-yan/p/6953993.html
Copyright © 2011-2022 走看看