Subject Multiple Table INSERT and Foriegn Keys
Author Derek Basch
Thanks for the answer Andrey,
I see that you are doing an inner join of the two
tables in the view and then inserting to the view
using a trigger. My next question is, what if I want
to insert on 3 or more tables, not just 2?
For Example:

For example:
---t_emp_info---
pk_emp_id
fk_job_id
fk_car_id
fname
lname

---t_job_title---
pk_job_id
job_title

---t_car_title---
pk_car_id
car_title

Now say I have a new employee and want to insert new
values to:
t_emp_info
t_job_title
t_car_title

The value of the foriegn keys in t_emp_info will
depend upon the value of the primary keys in the child
tables. How can I?:

1) Insert a new car type (t_car_title).
2) Insert a new job title (t_job_title)
2) Set the foriegn keys in t_emp_info to the
corresponding primary keys of the child tables
(t_car_title/t_job_title).

I guess I am just confused on how to use foriegn keys.
Sorry for the newbie q's.
Thanks,
Derek Basch

--- In ib-support@y..., Derek Basch <dbasch@y...>
wrote:
> Hi,
> Im new to SQL and I have created several tables and
> would like to be able to INSERT to several of them
at
> the same time. I cant seem to find any
> examples/techniques for breaking apart records and
> inserting into multiple tables.
>
You can create View like this:
create view all_data_from_all_tables
(fields_from_table1,fields_from_tables2,...)
as
select t1.fields_from_table1,t2.fields_from_table2,...
from table1 t1
join table2 t2.on t2.table1_id=t1.id
...

and write before insert, before update and before
delete triggers for
View all_data_from_all_tables
for example:

create trigger all_data_from_all_tables_bi
for all_data_from_all_tables
before insert
as
begin
insert into table1(fields_from_table1)
values(new.fields_from_table1);

insert into table2(fields_from_table2)
values(new.fields_from_table2);
end

Also you can add into triggers the addition
funcionality you need.

Andrey Prokhorov


__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com