码迷,mamicode.com
首页 > 数据库 > 详细

mysql-插入、更新、删除数据

时间:2017-06-08 23:41:48      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:sel   row   清空   now()   ace   数据   font   表示   from   

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;

 

mysql-插入、更新、删除数据

标签:sel   row   清空   now()   ace   数据   font   表示   from   

原文地址:http://www.cnblogs.com/oural-yan/p/6953993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!