Varray compare item in the collections - sql

The a table of movies with attribute of directors and actors. The actor is a varray type of actor_type contain 5 actor. i want to retrieve the movie that actor is both director and actor.
i tried
select actors, title, director
from movie
where actors = director
it telling me inconsistent datatype

You didn’t provide the sample data so my understanding from your description is:
The director and actor attribute are in the movie table and the actor is displayed as Varray, which being called actor_type. Also, the actor Varray contains 5 actors in each movie.
The way to query Varray is different, you can use TABLE expression in FROM clause.
This website might help you
Database Object-Relational Developer's Guide:
https://docs.oracle.com/database/121/ADOBJ/adobjcol.htm#ADOBJ00204
Then, you have to loop through the actor list and use the EXISTS or IN operator to check if the director is inside the actor list instead of using actors=director

You didn't specify what datatype the director column is. You can do a convert on the join actor = convert(director).
But I highly recommend you don't do this.
The problem with joining on strings is it is very inconsistent and if this is your only option to join actor to director, you need to restructure your tables.
I advise you have a Person table which contains both actors and directors.
You then need a table for movies and have the director and actor as foreign keys.
If the movie can have more than one actor, for instance, you then need a link table between person and movie tables (Same with the director if you have more than one director)
Hope this helps.

Related

Storing list of strings values for each row in a table is bad practice and violates normal forms. What should I do?

I'm doing a hobby project of creating my own movie database with ratings, genres, release dates and what actors are linked to what movies (to in the future distinguise movies from actors search).
I have a table containing Movies with some attributes where one attribute at the moment should be actors included in that movie. I know I can't store a list of strings (names of actors) in a cell for each row (movie) in the table. But what is best practice here? Create another table with MovieActors and link what movies each are connected to? What if the actor is connected to more movies?
I'm running Microsoft SQL Server Management Studio in my own localhost database and have just set up the Movie table where I found out I might get this problem.
I expect to get a table containing movies, and in the future be able to search movies, find their attributes and find out what actors are connected to that movie. Also be able to filter movies depending on what actor I've chosen.
What you have is a many-to-many relationship. When building a relational database, this would consist of a Movie Table, an Actor Table. and then a "junction" or "join" table, which houses a foreign key to both Movie and Actor. You can then join using this "junction" table to get all actors who were in a specific movie, or vice versa, get all movies a specific actor was in, etc.
Example Diagram:
Table Movie Table ActorMovie Table Actor
MovieId | MovieName MovieId | ActorId ActorId | ActorName
1 jaws 1 1 1 Guy1
2 star wars 1 2 2 Guy2
2 1
As you suggested, the usual practice is to have an intermediate MovieActors table, which links what movies have which actors.
Like this:
Movie Actor
1 2
1 3
1 4
2 2
2 5
You'll link the IDs with their respective tables via foreign keys (FK).

Entity relationship model diagram

I need to design an entity relationship diagram for a movie database with the following requirements..
Each movie is identified by its title, year of release and length in minutes and can have 0 or more languages
Production companies are identified by name. They have an address and each production company can produce one or more movies
Actors are identified by name, date of birth and can act in one or more movies - and each has a role in movies
Directors are identified by name and date of birth, so each director can direct one or more movies, and each movie can be directed by one or more directors
Each movie belongs to any one category like horror, action, drama etc.

SQL: How do I find which movie genre a user watched the most? (IMDb personal project)

I'm currently working on a personal project and I could use a little help. Here's the scenario:
I'm creating a database (MS Access) for all of the movies myself and some friends have ever watched. We rated all of our movies on IMDb and used the export feature to get all of the movie data and our movie ratings. I plan on doing some summary analysis on Excel. One thing I am interested in is the most common movie genre that each person watched. Below is my current scenario. Note that the column "const" is the movies' unique IDs. I also have individual tables for each person's ratings and the following tables are the summary tables that make up the combination of all the movies we have watched.
Here's the table I had: http://imgur.com/v5x9Dhg
I assigned each genre an ID, like this: http://imgur.com/aXdr9XI
And here is a table where I have separate instances for each movie ID and a unique genre: http://imgur.com/N0wULo8
I want to find a way to count up all of the genres that each person watches. Any advice? I would love to provide any additional information that you need!
Thank you!
You need to have at least one table which has one row per user and const (movie watched). In the 3 example tables you posted nothing shows who watched which movies, which is information you need to solve your problem. You mention having "individual tables for each person's ratings," so I assume you have that information. You will want to combine all of them though, into a table called PERSON_MOVIE or something of the like.
So let's say your second table is called GENRE and its columns are ID, Genre.
Let's say your third table is called GENRE_MOVIE and its columns are Const and ID (ID corresponds to ID on the GENRE table)
Let's say the fourth table, which you did not post, but which is required, is called PERSON_MOVIE and its columns are person, Const, rating.
You could then write a query like this:
select vw1.*, ge.genre
from (select um.person, gm.id as genre_id, count(*) as num_of_genre
from user_movie um
inner join genre_movie gm
on um.const = gm.const
group by um.person, gm.id) vw1
inner join (select person, max(num_of_genre) as high_count
from (select um.person, gm.id, count(*) as num_of_genre
from user_movie um
inner join genre_movie gm
on um.const = gm.const
group by um.person, gm.id) x
group by person) vw2
on vw1.person = vw2.person
and vw1.num_of_genre = vw2.high_count
inner join genre ge
on vw1.genre_id = ge.id
Edit re: your comment:
So right now you have multiple tables reflecting people's ratings of movies. You need to combine those into a table called PERSON_MOVIE or something similar (as in example above).
There will be 3 columns on the table: person, const, rating
I'm not sure if access supports the traditional create table as select query but ordinarily you would be able to construct such a table in the following way:
create table person_movie as
select 'Bob', const, [You rated]
from ratings_by_bob
union all
select 'Sally', const, [You rated]
from ratings_by_sally
union all
select 'Jack', const, [You rated]
from ratings_by_jack
....
If not, just combine the tables manually and add a third column as shown indicating what users are reflected by each row. Then you can run my initial query.

SQL movie database diagram

Im working on a small school project where im creating a movie database in SQL. I've created the tables and was wondering if I will encounter any problems with the model i created.
Thanks in advance.
Current diagram
Edit:
Here is the new diagram
MovieDetails is bad design. You need one row in MovieDetails per actor, while the director will be the same, which is data duplication. Instead, the Movie table should have a foreign key referencing director, then a MovieActor table should represent the many to many relationship between movies and actors.
Technically, there's also no reason to have different tables for Directors and Actors since you have the same data in the tables. You could just as well have a Person table with both.
How about (I am only showing the relevant columns)
movie table
-----------
id
title
person
------
id
name
cast table
----------
movie_id
person_id
movie_role_id (actor, director, ...)
role_type table
----------------
id
name (actor, director, ...)
genres table
------------
id
name
movie_genres table
------------------
movie_id
genre_id
I would consider the following structure:
movie(id, title, year, rating, plot, length)
actor(id, first name, last name, nationality, birth_date)
director(id, first name, last name, nationality, birth_date)
movie_x_actor(movie_id, actor_id)
movie_x_director(movie_id, director_id)
This is, of course, just the simplest example.
You can, for instance add a movie_series table like:
movie_series(id, title)
movie_x_movie_series(movie_id, series_id, plot_order_number)
and, like #Juergen described, a movie usually belongs to more than one genre so:
genres(id, genre_name, genre_description)
movie_x_genres(movie_id, genre_id)
You should definitely take a look at one-to-many and many-to-many relationships between rows in tables.
Cheers

Ms Access | Issues with INNER JOIN query

So, I'm doing a movie database and I got one table for actors and one table for movie titles.
In the Movies table I got Three columns for Actors. Actor_1, Actor_2, Actor_3. In these fields, I only write numbers which corresponds to a row in the Actors table.
Each actor have these columns:
Actor_ID, Firstname, Surname
Now if I do this query:
SELECT movie.titel, firstname + ' ' + surname AS name
FROM Movie
INNER JOIN Actors ON movie.actor_1=actor.actor_id
Then I get almost what I want. I get the movie titles but only one actor per movie. I don't know how I am supposed to do for the movies where I got two or three actors.
I had to translate to code so it would be more understandable. The code in its original shape works so don't mind if it's not 100% here. Just would appreciate some pointers on how I could do.
You need to change your table design to include a junction table. So you will have
Movies
ID
Etc
Actors
ID
Etc
MoviesActors
MovieID
ActorID
Etc
Your query might be:
SELECT m.MovieTitle, a.ActorName
FROM Actors a
INNER JOIN (Movies
INNER JOIN MoviesActors ma
ON m.ID = ma.MovieID)
ON a.ID = ma.ActorID
Relational database design
Junction tables