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..
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:
with suitable primary keys for all those.
Then assuming you have the publisher and type inserted, you can insert the movie thus:
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:
In response to your comment:
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.