CREATE TABLE IF NOT EXISTS tasks (
task_id INT(11) AUTO_INCREMENT,
subject VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO tasks(subject,start_date,end_date,description)--输入全部的字段名
VALUES ('任务-1','2017-01-01','2017-01-02','Description 1'),
('任务-2','2017-01-01','2017-01-02','Description 2'),
('任务-3','2017-01-01','2017-01-02','Description 3');
代码样式
INSERT INTO table1
SELECT c1,c2,FROM table2;
主键task_id列已经有一个值为4的行,如果插入以下,将违法primary KEY约束
INSERT INTO tasks(task_id,subject,start_date,end_date,description)
VALUES (4,'Test ON DUPLICATE KEY UPDATE','2017-01-01','2017-01-02','Next Priority');
INSERT INTO tasks(task_id,subject,start_date,end_date,description)
VALUES (5,'Test ON DUPLICATE KEY UPDATE','2017-01-01','2017-01-02','Next Priority')
ON DUPLICATE KEY UPDATE
task_id = task_id + 3,
subject = 'Test ON DUPLICATE KEY UPDATE';
等效于
UPDATE tasks
SET
task_id = task_id + 1,
subject = 'Test ON DUPLICATE KEY UPDATE'
WHERE
task_id = 7;
首先查询原来的数据:
SELECT
lastName,firstName,email,employeeNumber
FROM
employees
WHERE
employeeNumber=1056;
然后,使用UPDATE语句将Mary的电子邮件更新为新的电子邮件:[email protected],如下查询所示:
UPDATE
employees
SET
email='[email protected]'
WHERE
employeeNumber=1056;
最后,查询更新后的数据是否生效
SELECT
lastName,firstName,email
FROM
employees
WHERE
employeeNumber=1056;
UPDATE
employees
SET
email='[email protected]'
lastName = 'NIKO'
WHERE
employeeNumber=1056;
执行选中代码:CTRL+SHIFT+R
执行窗口代码:CTRL+R