所谓 CRUD => Create , Read , Update , Delete 指的就是增删改查
,为资料库一个很重要的地方。
前面我们做过C CREATE,R SELECT了,接下来要来操辍关于UPDATE、DELETE的部分。
UPDATE table_name set ...:UPDATE能够修改我们现有的资料。
透过,可以一次改变多个column。
UPDATE table_name set salary=8700,notes=updated where title="Software Engineer"+----+------------+-----------+-------------------+--------+------------+-------+| id | first_name | last_name | title | salary | hire_date | notes |+----+------------+-----------+-------------------+--------+------------+-------+| 1 | Robin | Jackman | Software Engineer | 8700 | 2001-10-12 | updated || 5 | Eliza | Clifford | Software Engineer | 8700 | 1998-10-19 | updated || 6 | Nancy | Newman | Software Engineer | 8700 | 2007-01-23 | updated |+----+------------+-----------+-------------------+--------+------------+-------+
DELETE from table_name ...:删除特定资料。
DELETE from employee where title="Software Engineer";uery OK, 3 rows affected (0.01 sec)可发现其资料已经消失。mysql> SELECT * from employee;+----+------------+-----------+------------------------+--------+------------+-------+| id | first_name | last_name | title | salary | hire_date | notes |+----+------------+-----------+------------------------+--------+------------+-------+| 2 | Taylor | Edward | Software Architect | 7200 | 2002-09-21 | NULL || 3 | Vivian | Dickens | Database Administrator | 6000 | 2012-08-29 | NULL || 4 | Harry | Clifford | Database Administrator | 6800 | 2015-12-10 | NULL || 7 | Melinda | Clifford | Project Manager | 8500 | 2013-10-29 | NULL || 8 | Jack | Chan | Test Engineer | 6500 | 2018-09-07 | NULL || 9 | Harley | Gilbert | Software Architect | 8000 | 2000-07-17 | NULL |+----+------------+-----------+------------------------+--------+------------+-------+6 rows in set (0.00 sec)
DELETE from table_name
可以删除指定之table所有的资料。
CREATE TABLE IF NOT EXISTS table_name:IF NOT EXISTS代表如果这个table不存在,我们就执行CREATE TABLE,反之。
原本我们不加上IF NOT EXISTS虽然还是有执行INSERT的部分,但还是会报错,而加上以后,就没有报错INSERT的部分也有执行,整个代码会更加严谨。