Forum Moderators: coopster

Message Too Old, No Replies

Inserting into multiple tables - views - mysql insert id

How to create relationships on the fly ,with views

         

bleak26

10:00 am on Jan 26, 2008 (gmt 0)

10+ Year Member



Hi,

I would like to insert data into a view which is 4 tables combined. The 4 tables have relationships, but nothing that is enforced by the database.

Normally when not using a view i would use the primary key from one table as the foreign key in another table, but when i am inserting into a view , i do not have any details of the primary key as i have not inserted the record yet. so i cannot create the relationship.

I am using myisam

What is the best way to create relationships between records in different tables, when you insert information using views?

and

is it possible to retrieve the insert id from all tables where data is being inserted?

coopster

3:17 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




29.6.6: Can you insert into views that are based on joins?

It is possible, provided that your

INSERT
statement has a column list that makes it clear there's only one table involved.

You cannot insert into multiple tables with a single insert on a view.


Some views are updatable. That is, you can use them in statements such as
UPDATE
,
DELETE
, or
INSERT
to update the contents of the underlying table. For a view to be updatable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table.

Resources:
MySQL 5.1 FAQ — Views [dev.mysql.com]
CREATE VIEW Syntax [dev.mysql.com]

You may consider a Stored Procedure [dev.mysql.com] instead.