MySQL表数据更新

时间:2021-07-13 07:25:21

1,INSERT 语句:

<1>,单行插入: INSERT INTO table(column1,column2...)

                            VALUES (value1,value2,...);

 简单例子: INSERT INTO tasks(subject,start_date,end_date,description)
                   VALUES('Learn MySQL INSERT','2018-05-24','2018-05-25','Start learning..');

<2>,多行插入: INSERT INTO table(column1,column2...)
                            VALUES (value1,value2,...),
                                          (value1,value2,...),

                            ...;

简单例子:INSERT INTO tasks(subject,start_date,end_date,description)
                 VALUES ('任务-1','2018-05-24','2018-05-25','Description 1'),
                 ('任务-2','2018-05-24','2018-05-25','Description 2'),
                 ('任务-3','2018-05-24','2018-05-25','Description 3');

<3>,带SELECT子句的插入:在MySQL中,可以使用SELECT语句返回的列和值来填充INSERT语句的值。 此功能非常方便,因为您可以使用INSERT和SELECT子句完全或部分复制表。

简单例子:假设要将tasks表复制到tasks_bak表。       

                 首先,通过复制tasks表的结构,创建一个名为tasks_bak的新表,如下所示:

                 CREATE TABLE tasks_bak LIKE tasks;

                 第二步,使用以下INSERT语句将tasks表中的数据插入tasks_bak表:

                 INSERT INTO tasks_bak SELECT * FROM tasks;

<4>,INSERT与ON DUPLICATE KEY UPDATE:

如果新行违反主键(PRIMARY KEY)或UNIQUE约束,MySQL会发生错误。 例如,如果执行以下语句:

INSERT INTO tasks(task_id,subject,start_date,end_date,description)
VALUES (4,'Test ON DUPLICATE KEY UPDATE','2018-05-24','2018-05-25','Next Priority');

错误消息:

Error Code: 1062. Duplicate entry '4' for key 'PRIMARY' 0.016 sec

因为表中的主键task_id列已经有一个值为 4 的行了,所以该语句违反了PRIMARY KEY约束。
但是,如果在INSERT语句中指定ON DUPLICATE KEY UPDATE选项,MySQL将插入新行或使用新值更新原行记录。

此时使用 ON DUPLICATE KEY UPDATE:

INSERT INTO tasks(task_id,subject,start_date,end_date,description)
VALUES (4,'Test ON DUPLICATE KEY UPDATE','2018-05-24','2018-05-25','Next Priority')
ON DUPLICATE KEY UPDATE 

   task_id = task_id + 1, 

  subject = 'Test ON DUPLICATE KEY UPDATE',

  start_date ='2018-05-24',

  end_date ='2018-05-25',

  description ='Next Priority';

2,UPDATE 语句:

<1>,单多列更新:

UPDATE [LOW_PRIORITY] [IGNORE] table_name 
SET 
    column_name1 = expr1,
    column_name2 = expr2,
    ...
WHERE
    condition;

LOW_PRIORITY修饰符指示UPDATE语句延迟更新,直到没有从表中读取数据的连接。 LOW_PRIORITY对仅使用表级锁定的存储引擎(例如MyISAM,MERGE,MEMORY)生效。即使发生错误,IGNORE修饰符也可以使UPDATE语句继续更新行。导致错误(如重复键冲突)的行不会更新。

简单例子:单列:
                UPDATE employees 
                SET 
                              email = 'mary.new@yiibai.com'
                WHERE
                              employeeNumber = 1056;
                多列:
                UPDATE employees 
                SET 
                              lastname = 'Hill',
                              email = 'mary.hill@yiibai.com'
                WHERE
                              employeeNumber = 1056;


<2>,使用SELECT语句的UPDATE:

要更新customers表中的销售代表员工编号(employeeNumber)列(为空值),从employees表中随机选择一个职位为Sales Rep的雇员,并将其更新到customers表中。

UPDATE customers 
SET 
    salesRepEmployeeNumber = (SELECT 
            employeeNumber
        FROM
            employees
        WHERE
            jobtitle = 'Sales Rep'
        LIMIT 1)
WHERE

    salesRepEmployeeNumber IS NULL;

3,DELETE 语句:DELETE FROM table_name
                            WHERE condition;

<1>,指定条件删除:DELETE FROM employees 
                                  WHERE
                                                        officeCode = 4;

<2>,删除所有行:DELETE FROM employees;

<3>,DELETE和LIMIT子句:限定删除的行数:DELETE FROM table

                                                                        LIMIT row_count;

按客户名称按字母排序客户,删除前十行:

                    DELETE FROM customers

                    ORDER BY customerName

                    LIMIT 10;