INDEX

Updating Data

UPDATE customers
SET cust_name = 'The Fudds',
cust_email = 'elmer@fudd.com'
WHERE cust_id = 10005;

To delete a column's value, you can set it to NULL (assuming the table is defined to allow NULL values). You can do this as follows:

UPDATE customers
SET cust_email = NULL
WHERE cust_id = 10005;

Here the NULL keyword is used to save no value to the cust_email column.

The IGNORE Keyword

If your UPDATE statement updates multiple rows and an error occurs while updating one or more of those rows, the entire UPDATE operation is cancelled (and any rows updated before the error occurred are restored to their original values). To continue processing updates, even if an error occurs, use the IGNORE keyword, like this:

UPDATE IGNORE customers ...

Deleting Data

DELETE FROM customers
WHERE cust_id = 10006;

DELETE takes no column names or wildcard characters. DELETE deletes entire rows, not columns. To delete specific columns use an UPDATE statement (as seen earlier in this chapter).

Faster Deletes

If you really do want to delete all rows from a table, don't use DELETE. Instead, use the trUNCATE TABLE statement that accomplished the same thing but does it much quicker (trUNCATE actually drops and recreates the table, instead of deleting each row individually).

Guidelines for Updating and Deleting Data

Here are some best practices that many SQL programmers follow:

  • Never execute an UPDATE or a DELETE without a WHERE clause unless you really do intend to update and delete every row.

  • Make sure every table has a primary key (refer to Chapter 15, "Joining Tables," if you have forgotten what this is), and use it as the WHERE clause whenever possible. (You may specify individual primary keys, multiple values, or value ranges.)

  • Before you use a WHERE clause with an UPDATE or a DELETE, first test it with a SELECT to make sure it is filtering the right recordsit is far too easy to write incorrect WHERE clauses.

  • Use database enforced referential integrity (refer to Chapter 15 for this one, too) so MySQL will not allow the deletion of rows that have data in other tables related to them.

The bottom line is that MySQL has no Undo button. Be very careful using UPDATE and DELETE, or you'll find yourself updating and deleting the wrong data.

MySQL Crash Course #11# Chapter 20. Updating and Deleting Data的更多相关文章

  1. MySQL Crash Course #05# Chapter 9. 10. 11. 12 正则.函数. API

    索引 正则表达式:MySQL only supports a small subset of what is supported in most regular expression implemen ...

  2. MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables

    之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...

  3. MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE

    索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...

  4. MySQL Crash Course #12# Chapter 18. Full-Text Searching

    INDEX 由于性能.智能结果等多方面原因,在搜索文本时,全文搜索一般要优于通配符和正则表达式,前者为指定列建立索引,以便快速找到对应行,并且将结果集智能排序.启用查询扩展可以让我们得到未必包含关键字 ...

  5. MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...

  6. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  7. MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance

    终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...

  8. MySQL Crash Course #18# Chapter 26. Managing Transaction Processing

    InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...

  9. MySQL Crash Course #17# Chapter 25. 触发器(Trigger)

    推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...

随机推荐

  1. HTML表单的运用

    没学习HTML表单之前,觉得文本框.密码框.隐藏域.单选按钮.复选框等这些在平常页面常见到的表单不起眼,挺简单,到自己用代码区实现将它们灵活串联运用起来,才发现一点都不简单.看着容易,自己操作还是出现 ...

  2. python----题库(一)

    1.执行 Python 脚本的两种方式 答:1.>>python ../pyhton.py 2. >>python.py #必须在首行有 #!/usr/bin/env pyth ...

  3. NET技术公众号已上线

    各位兄弟姐妹,本人构建技术微信号已正式上线,后续的技术分享主要以微信公众号为主,博客为铺,请各位有兴趣的同学关注. 微信公众号(wx51dotnet):

  4. iOS电话等中断事件的开始和结束通知

    #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...

  5. 关于static、内部类

    1.static不能修饰外部类的原因 static修饰的成员是属于某个类的.而外部类的上一级程序单元是包,所以static不能修饰外部类. 2.外部类,内部类有不同访问权限的原因 外部类的上一级程序单 ...

  6. SpringBoot @Transactional声明事务无效问题

    查看系统支持的存储引擎:show engines; 查看表使用的引擎:show table status from db_name where name='table_name'; 修改表引擎方法:  ...

  7. dedecms后台每页文章条数如何修改(“文档列表”每一页显示的文档条数)

    小明在学习采集,弄了个dedecms作为发布平台,几个小时后跑来报喜说好简单,但又不想制造那么多spam,每个分类只保留几条就好.在后台删除这些文章,每页只显示30个,看了下有100多页,立马沮丧了, ...

  8. Mysql中Left Join 与Right Join 与 Inner Join 与 Full Join的区别

    看看Left Join 与Right Join 与 Inner Join 与 Full Join对表进行操作后得到的结果. 在数据库中新建两张表,并插入要测试的数据. 新建表: USE [Test] ...

  9. 用Python实现的数据结构与算法:开篇

    一.概述 用Python实现的数据结构与算法 涵盖了常用的数据结构与算法(全部由Python语言实现),是 Problem Solving with Algorithms and Data Struc ...

  10. (转)Springboot 中filter 注入对象

    问题:我建立一个全局拦截器,当然,这是测试的时候建立的,我把它命名为LogFilter,它继承了Filter,web应用启动的顺序是:listener->filter->servlet,而 ...