SQL查询获取数据并插入到db

时间:2018-03-18 08:44:31

标签: sql oracle sql-insert create-table

employeeAccount

CREATE TABLE employeeAccount
( 
    employAccID NUMBER(2),
    emplyUsername VARCHAR(20),
    emplyFirstName VARCHAR(20),
    emplyLastName VARCHAR(20),
)

INSERT INTO employeeAccount(employAccID ,emplyUsername ) VALUES (1,'TestAccount1')
INSERT INTO employeeAccount(employAccID ,emplyUsername ) VALUES (2,'TestAccount2')

JobRole

CREATE TABLE Jobrole
( 
    jobNo NUMBER(2),
    jobName VARCHAR(20),
)

INSERT INTO Jobrole(jobNo ,jobName) VALUES (001,'Admin')
INSERT INTO Jobrole(jobNo ,jobName) VALUES (002,'CEO')

这是我的employeeJob代码:

CREATE TABLE employeeJob
(
  empid NUMBER(2),
  empjob NUMBER(2),
  CONSTRAINT pk_employeeJob PRIMARY KEY(empid,empjob),
  CONSTRAINT fk_empassignjob1 FOREIGN KEY(empid) REFERENCES employeeAccount(employAccID),
 CONSTRAINT fk_empassignjob2 FOREIGN KEY(empjob) REFERENCES Jobrole(jobNo)
)

以下是插入查询:

INSERT INTO employeeJob(empid,empjob)
VALUES (1,'001')

任何想法如何让它自动从emplyFirstName表格和emplyLastName中获取employeeAccountemployeeJob等所有数据。插入employeeAccount。我该怎么做才能让它自动插入从employeeJob复制到.round { position: relative; } .round label { background-color: #fff; border: 1px solid #ccc; border-radius: 50%; cursor: pointer; height: 28px; left: 0; position: absolute; top: 0; width: 28px; } .round label:after { border: 2px solid #fff; border-top: none; border-right: none; content: ""; height: 6px; left: 7px; opacity: 0; position: absolute; top: 8px; transform: rotate(-45deg); width: 12px; } .round input[type="checkbox"] { visibility: hidden; } .round input[type="checkbox"]:checked+label { background-color: #66bb6a; border-color: #66bb6a; } .round input[type="checkbox"]:checked+label:after { opacity: 1; } html, body { height: 100%; margin: 0; } body { background-color: #f1f2f3; -webkit-box-align: center; -ms-flex-align: center; align-items: center; display: -webkit-box; display: -ms-flexbox; display: flex; } .container { margin: 0 auto; }表的其他数据?

2 个答案:

答案 0 :(得分:1)

是的,你可以,

INSERT INTO table2
SELECT * FROM table1
WHERE condition;

您可以自table2自动填充table1。如果您想要所有记录,请忽略WHERE条件。但是,如果您只想自动填充从table1table2的某些记录,请在condition语句中填写WHERE

答案 1 :(得分:0)

您可以在插入内的employeejob表格上创建触发器,如

Create trigger trgGetOtherDetails
On employeejob AFTER INSERT -- the AFTER key word is Necessary
AS
BEGIN
Declare @emplyFirstname   varchar(30), 
@emplyLastname   varchar(30),
@empId      varchar(5)

select @empId = empId from inserted

Select @emplyFirstname = emplyFirstname, @emplyLastname = emplyLastname from employeeAccount where employAccID = @empId

Update employeejob 
Set emplyFirstname = @emplyFirstname, 
emplyLastname = @emplyLastname 
Where empId = @empId

END