Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Only allow column to have true once for each id?

I have this table:

ID | genre_id | is_best_in_genre | movie_name
--------------------------------
1  | 3        | 0                | Hateful Eight
2  | 3        | 0                | Django Unchained
2  | 3        | 1                | Inglorious B
2  | 3        | 0                | Once Upon A Time in Hollywood

is_best_in_genre can only be true (1) once for every genre_id. There can only be 1 best movie in each genre. How would I make a constraint such as this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Alternative idea:

The more proper, normalized way to handle this is probably to make a separate 'best_in_genre' table with a unique constraint on genre_id.

This is also easier to update, because you're not required to make sure that everything gets 0'd when selecting a new 'best'.

over 4 years ago · Santiago Trujillo Report

0

A better approach might be to "move" the column is_best_in_genre to a separate (joined) table. A simple table with two columns could do the job:

CREATE TABLE best (
  idmovie int,
  idgenre int,
  PRIMARY KEY (idgenre))  

This would need to be joined with the original table like:

SELECT m.*, CASE b.idmovie>0 THEN 1 ELSE 0 END best_in_genre
FROM movietable m
LEFT JOIN best b ON idmovie=id AND idgenre=genre_id

The PRIMARY KEY constraint in the table best will make sure that each genre can only appear once.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!