我有一张小桌子来跟踪任务。如果我的桌子是这样的:
+----+---------+----------+-----------+
| id | project | task_num | task_name |
+----+---------+----------+-----------+
我可以使任务编号对于项目列是唯一的吗?这样我可以拥有:
+----+---------+----------+-----------+
| id | project | task_num | task_name |
+----+---------+----------+-----------+
| 1 | Proj_1 | 1 | firsttask |
| 2 | Proj_1 | 2 | sectask |
| 3 | Proj_2 | 1 | fdsfds |
| 4 | Proj_2 | 2 | gg |
| 5 | Proj_2 | 3 | gf |
| 6 | Proj_3 | 1 | xx |
| 7 | Proj_1 | 3 | xxx |
+----+---------+----------+-----------+
这是否有意义?
答案 0 :(得分:1)
您可以使用:
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n);
至于您
ALTER TABLE table_name ADD CONSTRAINT proj_task UNIQUE (project, task_num);