如何在表格中强制执行唯一性? [英] How do I enforce uniqueness in a table?

查看:95
本文介绍了如何在表格中强制执行唯一性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我必须编写一个具有以下内容的注册表:

For example, I have to program an enrolment table which has:

  1. enrollment_id
  2. academic_period
  3. 结果
  4. student_id
  5. 主题

我想强制执行一项操作,以确保学生在特定学期内不能多次注册该科目. 因此,约束条件应包括学生ID",学科"和学期"列.

I want to enforce to make sure that a student can't enroll in a subject more than once in a specific academic period. So, the constraint shall involve student_id, subject, and academic period columns.

我仍然不确定如何做到这一点.有帮助吗?

I'm still unsure how do I make this happen. Any help?

推荐答案

向表中添加唯一约束:

ALTER TABLE yourtable ADD CONSTRAINT constraintname UNIQUE (student_id, subject, academic_period)

这篇关于如何在表格中强制执行唯一性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆