In mysql can I set a condition that a combination of two fields can never be the same???
Example:
id-1
name-abc
grade-A
Now, another entry with name='abc' and grade='A' can not be added into that table ????
views:
17answers:
2
+1
A:
You can create a composite primary key from those two columns, but that makes little sense as the table already has an ID field, and surely two people of the same name can have the same grade? e.g.:
CREATE TABLE student_grades (
id int unsigned not null,
name varchar not null,
grade varchar not null,
PRIMARY KEY(name, grade));
karim79
2009-08-22 22:32:41
@atul - see my edit
karim79
2009-08-22 22:37:33
Thanks a lot....
halocursed
2009-08-22 22:38:37
A:
You can also add a secondary UNIQUE
constraint:
CREATE TABLE student_grades (
id int unsigned not null,
name varchar(10) not null,
grade varchar(10) not null,
PRIMARY KEY(id),
UNIQUE KEY(name, grade)
);
Bill Karwin
2009-08-22 23:09:01