Friday, 15 June 2012

mysql - Unique values only when data in another column matches -



mysql - Unique values only when data in another column matches -

i have next table.

sql-fiddle: http://sqlfiddle.com/#!2/8d142/1

create table steps ( id int auto_increment primary key, step_day varchar(10), title varchar(32), description varchar(500), step_order tinyint unsigned not null ); -- test insert insert steps (step_day, title, description, step_order) values ('monday', 'step 1', 'do stuff.', '1'), ('saturday', 'step 1', 'do stuff.', '1'), ('monday', 'step 2', 'do stuff.', '2'), ('saturday', 'step 2', 'do more stuff.', '2');

the info in test insert ok, protect columns 'title' , 'step_order' duplicate entries when info in 'step_day' column same.

so...

('monday', 'step 1', 'do stuff.', '1'), ('monday', 'step 1', 'do stuff.', '1')

...is bad, but...

('monday', 'step 1', 'do stuff.', '1'), ('saturday', 'step 1', 'do stuff.', '1')

...is ok.

how do this? thanks.

create table steps ( id int auto_increment primary key, step_day varchar(10), title varchar(32)not null, description varchar(500), step_order tinyint unsigned not null, constraint uc_steps unique (step_day,title,step_order) );

this allow have unique info in step_day,title,step_order.

this allow add

insert steps (step_day, title, description, step_order) values ('monday', 'step 1', 'do stuff.', '1'), ('saturday', 'step 1', 'do stuff.', '1');

but not

insert steps (step_day, title, description, step_order) values ('monday', 'step 1', 'do stuff.', '1'), ('monday', 'step 1', 'do stuff.', '1');

mysql

No comments:

Post a Comment