Jump to content

MySQL unique index


centenial

Recommended Posts

Hi,

 

I have a table

 

CREATE TABLE processes
(
process_id int primary key,
process_name tinytext,
process_type varchar(10),
gallery_id int
);

 

The `process_type`field has two possible values:

  • Task1
  • Task2

 

Only one instance of "Task2" can run on a gallery at a time. Task1 cannot run on a gallery that is currently being used by Task2. Multiple instances of Task1 can run on the same gallery. I want to create an index that will handle this logic for me, so that if I try to insert into the table, it will return false should there be a conflict.

 

Is it possible to do this with unique indexes?

Link to comment
Share on other sites

Sure.

 

Valid example 1:

INSERT INTO processes (0, 'process1', 'Task1','10');
INSERT INTO processes (0, 'process2', 'Task1','10');
INSERT INTO processes (0, 'process3', 'Task1','10');

 

Valid example 2:

INSERT INTO processes (0, 'process1', 'Task1','10');
INSERT INTO processes (0, 'process2', 'Task2','20');

 

Invalid example 1:

// invalid because Task1 and Task2 cannot run on the same gallery.

INSERT INTO processes (0, 'process1', 'Task1','10');
INSERT INTO processes (0, 'process2', 'Task2','10');

 

Invalid example 2:

// invalid because only one instance of Task2 can run on the same gallery.

INSERT INTO processes (0, 'process1', 'Task2','10');
INSERT INTO processes (0, 'process2', 'Task2','10');

Link to comment
Share on other sites

What about

INSERT INTO processes (0, 'process1', 'Task1','10');
INSERT INTO processes (0, 'process2', 'Task1','10');

?

This is valid, because multiple instances of Task1 can run on the same gallery. I've been banging my head on this for quite some time. Appreciate the help. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.