Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Essentially:</p> <pre><code>publisher: publisher_id publisher_name &lt;other publisher info&gt; type: type_id type_name &lt;other type info&gt; movie: movie_id movie_name publisher_id references publisher(publisher_id) &lt;other movie info&gt; movie_type: movie_id references movie(movie_id) type_id references type(type_id) </code></pre> <p>with suitable primary keys for all those.</p> <p>Then assuming you have the publisher and type inserted, you can insert the movie thus:</p> <pre><code>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; </code></pre> <p>In other words, you use sub-selects to get the IDs from the relevant tables based on a <em>unique</em> 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).</p> <p>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:</p> <pre><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; </code></pre> <hr> <p>In response to your comment:</p> <blockquote> <p>hey, i didn't got the point that of type and movie relationship.. will you please elaborate this point .. regards Abid</p> </blockquote> <p>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.</p> <p>Many-to-many relationships are best represented with a separate table containing the cross matches between the two related tables.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload