将指定列的值从一个表更新到另一个表

时间:2016-09-06 06:20:09

标签: sql oracle11g sql-update

查询:

select * from emp;

输出:

ID NAME                        AGE     SALARY ADDRESS
------ -------------------- ---------- ---------- --------------------
101 Naveen                       28      25000 charminar
102 Vishal                       32      28000 kothapet
180 Manish                       25      18000 Begumpet
120 Sravan                       29      20000 Vidyanagar
106 Mukesh                       26      21000 Vanastalipuram
115 Sankar                       23      15000 Kairtabad
130 Asif                         24      16000 Sultan nagar
142 Deeraj                       27      21500 Lalbazar

查询:

select * from dept;

输出:

JOB_ID JOB_TITLE            HIRE_DATE     EMP_ID
------ -------------------- --------- ----------
12254 Software Testing     16-APR-14
13696 Developer            25-MAR-13
13589 Manager              21-JUN-10
13696 developer            26-DEC-13
15395 DBA                  15-APR-12
12254 Software testing     14-NOV-14
13696 Developer            17-FEB-11
15395 DBA                  23-JAN-13

我必须将表EMP中的所有(ID)值复制到表DEPTEMP_ID)中。

2 个答案:

答案 0 :(得分:0)

emp和dept表之间没有关系。因此无法自动填充EMP_ID列。你应该一个接一个地做。

另一个提示是关于你的设计。您可以使用以下设计来存储员工和部门数据。

New design for storing department and employees

答案 1 :(得分:0)

与其他提到的评论者一样,您必须手动为每个EMP_ID建立链接:

UPDATE dept SET EMP_ID = 101 WHERE JOB_ID = 12254 AND HIRE_DATE = TO_DATE('16-APR-14', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 102 WHERE JOB_ID = 13696 AND HIRE_DATE = TO_DATE('25-MAR-13', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 180 WHERE JOB_ID = 13589 AND HIRE_DATE = TO_DATE('21-JUN-10', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 120 WHERE JOB_ID = 13696 AND HIRE_DATE = TO_DATE('26-DEC-13', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 106 WHERE JOB_ID = 15395 AND HIRE_DATE = TO_DATE('15-APR-12', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 115 WHERE JOB_ID = 12254 AND HIRE_DATE = TO_DATE('14-NOV-14', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 130 WHERE JOB_ID = 13696 AND HIRE_DATE = TO_DATE('17-FEB-11', 'DD-MON-YY');
UPDATE dept SET EMP_ID = 142 WHERE JOB_ID = 15395 AND HIRE_DATE = TO_DATE('23-JAN-13', 'DD-MON-YY');

确保每个语句中的EMP_ID都是所需的。