|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
rycamore, pabloj
I read some previous threads you guys have posted which were very informative! Wonder if you could help me with an issue I have? I have 3 tables: Employee, Phones, and Employee_Phones URL I want to be able to INSERT into these tables using a view such as the following: CREATE VIEW <VIEW> AS SELECT e.id, e.fname, e.lname, p.ph_num FROM employees e, phones p, employee_phones ep WHERE e.id=ep.e_id AND p.id=ep.ph_id; ID FNAME LNAME PH_NUM 01 ROB JONES 222-2600 INSERT INTO <VIEW> (id, fname, lname, ph_num) VALUES (seq.nexval, 'John', 'Smith', '911-9988'); BTW ryc I read somewhere that Oracle supports query rewrites... Any help you can would be great! Last edited by rsingleton : May 8th, 2003 at 04:10 PM. |
|
#2
|
|||
|
|||
|
I was under the impression that Oracle has automated support for simple view updating. Oracle should be able to INSERT, UPDATE or DELETE your view, because it doesn't contain any aggregates, subqueries or any other expressions that would make an update ambiguous.
But otherwise, you can make a view updateable in Oracle using an INSTEAD OF trigger, and I will post an example from the Oracle docs: Code:
CREATE TABLE Project_tab (
Prj_level NUMBER,
Projno NUMBER,
Resp_dept NUMBER);
CREATE TABLE Emp_tab (
Empno NUMBER NOT NULL,
Ename VARCHAR2(10),
Job VARCHAR2(9),
Mgr NUMBER(4),
Hiredate DATE,
Sal NUMBER(7,2),
Comm NUMBER(7,2),
Deptno NUMBER(2) NOT NULL);
CREATE TABLE Dept_tab (
Deptno NUMBER(2) NOT NULL,
Dname VARCHAR2(14),
Loc VARCHAR2(13),
Mgr_no NUMBER,
Dept_type NUMBER);
The following example shows an INSTEAD OF trigger for inserting rows into the MANAGER_INFO view.
CREATE OR REPLACE VIEW manager_info AS
SELECT e.ename, e.empno, d.dept_type, d.deptno, p.prj_level,
p.projno
FROM Emp_tab e, Dept_tab d, Project_tab p
WHERE e.empno = d.mgr_no
AND d.deptno = p.resp_dept;
CREATE OR REPLACE TRIGGER manager_info_insert
INSTEAD OF INSERT ON manager_info
REFERENCING NEW AS n -- new manager information
FOR EACH ROW
DECLARE
rowcnt number;
BEGIN
SELECT COUNT(*) INTO rowcnt FROM Emp_tab WHERE empno = :n.empno;
IF rowcnt = 0 THEN
INSERT INTO Emp_tab (empno,ename) VALUES (:n.empno, :n.ename);
ELSE
UPDATE Emp_tab SET Emp_tab.ename = :n.ename
WHERE Emp_tab.empno = :n.empno;
END IF;
SELECT COUNT(*) INTO rowcnt FROM Dept_tab WHERE deptno = :n.deptno;
IF rowcnt = 0 THEN
INSERT INTO Dept_tab (deptno, dept_type)
VALUES(:n.deptno, :n.dept_type);
ELSE
UPDATE Dept_tab SET Dept_tab.dept_type = :n.dept_type
WHERE Dept_tab.deptno = :n.deptno;
END IF;
SELECT COUNT(*) INTO rowcnt FROM Project_tab
WHERE Project_tab.projno = :n.projno;
IF rowcnt = 0 THEN
INSERT INTO Project_tab (projno, prj_level)
VALUES(:n.projno, :n.prj_level);
ELSE
UPDATE Project_tab SET Project_tab.prj_level = :n.prj_level
WHERE Project_tab.projno = :n.projno;
END IF;
END;
There should be plenty more info on these in the docs. I believe "query rewrite" in Oracle is a different thing than rewrite rules in PostgreSQL. As I understand it from brief reading, Oracle query rewrite is an optional automatic process that happens internally to allow Oracle to optimize queries, using materialized views, caching results, etc...
__________________
The real n-tier system: FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL Amazon wishlist -- rycamor (at) gmail.com |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Multi-table INSERT in Oracle |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|