18 Star 92 Fork 52

柯基与佩奇 / 数据库SQL实战

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
44.将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005.md 1.33 KB
Copy Edit Raw Blame History

将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005

题目描述

将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005, 其他数据保持不变,使用replace实现。

CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);

insert into titles_test values ('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');

答案

replace into titles_test values ('5', '10005', 'Senior Engineer', '1986-06-26', '9999-01-01')

题解

replace into检测主键或者唯一索引冲突,冲突则替换,否则退化成insert into操作

SQL
1
https://gitee.com/wp950820/database-sql-combat.git
git@gitee.com:wp950820/database-sql-combat.git
wp950820
database-sql-combat
数据库SQL实战
master

Search