开发者

How can I limit number of applications per student to 3?

开发者 https://www.devze.com 2023-02-07 06:32 出处:网络
I have one table \'Students\' and one table \'Applications\'. Ea开发者_开发问答ch student can apply for max 3 courses. How can I ensure that in database? Trigger? Constraint?

I have one table 'Students' and one table 'Applications'.

Ea开发者_开发问答ch student can apply for max 3 courses. How can I ensure that in database? Trigger? Constraint?

Can you show me how to write it? I'm new to SQL


One way is to put a counter on students. When a row is inserted to Applications increment it, when a row is deleted, decrement it. Doing this with triggers is very simple.

Then put a constraint on Students so the counter must be <= 3.


SELECT COUNT(*) FROM Applications GROUP BY Student_ID
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号