Skip to content
Advertisement

Limit number of occurrences of value in a table oracle sql

I have oracle sql table of students, students are assigned to groups.

I have to set constraint so there will be no more than 10 students in a group (ideally no more than N students in a group).

Basically restrict table from having more than N rows with the same GROUP_ID value.

Here is table declaration.

I’ve tried to use trigger that throws error when there is more than 10 students in a group. It worked with inserts but it was useless for updates of table, as i was constantly getting error ORA-04091: table students is mutating

Here is shortened version of the trigger:

I’ve read that oracle forbids to read from table when it is updating. If that’s so then how can i ensure that there can’t be more than 10 students in a group?

Advertisement

Answer

The “shortened version” of the trigger doesn’t show what type of trigger it is – most probably a row level trigger. That won’t work because of the mutating table trigger error. A statement level trigger does work:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement