tags:

views:

68

answers:

2

i have created a db names movielibrarysystem in which i have 3 tables..

that are type,publisher and movie... now 1 publisher could have many many movies and 1 movie is of many type.. in the movie table, the publisher id as well as the typeid are acting as a foreign keys..

my question is that how to insert a data into a movie table... i have already inserted data into publisher and type tables but i`m not able to insert into movie table..

A: 

If you constrain tables type and publisher to use foreign keys you will need to first insert both ids into your movies table. If possible I would let the movies table be the one that increments the foreign keys.

aefxx
i` have done that already.. i only want to insert the data into movies which i`m not getting
Abid
+1  A: 

Here's what you do. First, the relationship between publisher and movie is one to many - a publisher can publish many movies but each movie only has one publisher. However, type to movie is a many-to-many relationship (you state that one movie is of many types, but it's also the case that one type is of many movies), so you should have an extra table for that relationship.

Essentially:

publisher:
    publisher_id
    publisher_name
    <other publisher info>
type:
    type_id
    type_name
    <other type info>
movie:
    movie_id
    movie_name
    publisher_id references publisher(publisher_id)
    <other movie info>
movie_type:
    movie_id references movie(movie_id)
    type_id references type(type_id)

with suitable primary keys for all those.

Then assuming you have the publisher and type inserted, you can insert the movie thus:

begin transaction;
insert into movie (movie_name,publisher_id) values (
    'Avatar',
    (select publisher_id from publisher where publisher_name = 'Spielberg')
);
insert into movie_type (movie_id,type_id) values (
    (select movie_id from movie where movie_name = 'Avatar'),
    (select type_id from type where type_name = 'SciFi')
);
insert into movie_type (movie_id,type_id) values (
    (select movie_id from movie where movie_name = 'Avatar'),
    (select type_id from type where type_name = 'GrownUpSmurfs')
);
commit;

In other words, you use sub-selects to get the IDs from the relevant tables based on a unique set of properties (above example assumes movie names are unique, in reality you will need a more specific query, such as to handle the different films with the same name: The Omega Man, for example).

If you're not using a DBMS that supports selects in value sections, your best bet will be probably just to remember or extract the relevant values to a variable in whatever programming language you're using and construct a query to do it. In pseudo-code:

begin transaction;
insert into movie (movie_name,publisher_id) values (
    'Avatar',
    (select publisher_id from publisher where publisher_name = 'Spielberg')
);
select movie_id into :m_id from movie where movie_name = 'Avatar';
select type_id into :t_id1 from type where type_name = 'SciFi';
select type_id into :t_id2 from type where type_name = 'GrownUpSmurfs';
insert into movie_type (movie_id,type_id) values (:m_id, :t_id1);
insert into movie_type (movie_id,type_id) values (:m_id, :t_id2);
commit;

In response to your comment:

hey, i didn't got the point that of type and movie relationship.. will you please elaborate this point .. regards Abid

Both Avatar and Solaris can be considered of the type SciFi. So many movies to one genre. And Xmen:Wolverine can be considered both action and comic-remake. So many types to one movie.

Many-to-many relationships are best represented with a separate table containing the cross matches between the two related tables.

paxdiablo
hey, i didn`t got the point that of type and movie relationship.. will you please elaborate this point .. regards Abid
Abid
error : it gives that sub queries are not allowed in this context only scalarexpressions are allowed
Abid
+1: You put so much effort into this for someone who put so little effort into their question and with such a low accept rate. Nice dedication. :)
Mark Byers