Pergunta

I have a problem while designing my database and I don't know how to solve it:

i have following table (relevant columns):

CREATE TABLE IF NOT EXISTS `prmgmt_tasks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(300) NOT NULL,
  `project_id` int(11) NOT NULL,
);

What I want: every task has a unique id (autoincrement). The name of the task is not unique, but it should be unique for each project. For example "Design userinterface" can occur in project with id 1 and 2, but not twice in project with id 1. Something like 'unique for: group by project_id'.

Of course, I could check that in every query, but I am looking for a way to model this in the database, so it will allways be consistent, no matter what queries are executed.

Thanks for help!

Foi útil?

Solução

Create a unique composite index on the combined fields.

CREATE UNIQUE INDEX tasks_name_project
ON prmgmt_tasks (name, project_id);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top