I want to add a UNIQUE INDEX on 3 fields when one of the fields is True. ie equal to 1
UNIQUE INDEX pkg_box_delivered (package_id, box_id, delivered) WHERE delivered = 1;
I need a Alter statement and how add it to a Create Table statement.
I want to add a UNIQUE INDEX on 3 fields when one of the fields is True. ie equal to 1
UNIQUE INDEX pkg_box_delivered (package_id, box_id, delivered) WHERE delivered = 1;
I need a Alter statement and how add it to a Create Table statement.
You can't create an index on some of the rows in a table - only on all the rows in a table regardless of value.
I need a Alter statement and how add it to a Create Table statement
You use ALTER TABLE to change the definition of an existing table, while CREATE TABLE creates a table and it's fields, hence if the table exists already....
ALTER TABLE yourtablename
ADD UNIQUE INDEX pkg_box_delivered (package_id, box_id, delivered);
or if it does not already exist....
CREATE TABLE yourtablename (
package_id INT NOT NULL,
box_id INT NOT NULL,
delivered BOOLEAN,
...
UNIQUE INDEX pkg_box_delivered (package_id, box_id, delivered)
);
Just as you can't selectively apply an index to some rows, you can't selectively apply the unique constraint. You'd need to add a trigger to enforce this.