Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a Teacher table and a Pupils table.
A teacher can have 5 to 30 students and a student can have only one teacher. With SQLite, how can I check this cardinality with an SQL query?

For example, how do you prevent a user from adding more than 30 students to the Teacher table or from assigning multiple teachers to a student by using C# and SQLite?

Thank you for your help.

What I have tried:

I tried with some SQL requests (SQLite), but it seems there is not a check of cardinality. What is the way to check cardinality in SQL ?
Posted
Updated 5-Jul-21 0:43am
Comments
FranzBe 5-Jul-21 6:15am    
This link might be helpful: https://stackoverflow.com/questions/56876547/sql-query-find-records-with-field-that-has-cardinality-greater-than-one

1 solution

All you need is the COUNT function see SQLite COUNT Function: Count Items In A Group[^]
Before linking a student to a teacher, query to see if the student already has a teacher - you will also need to know how to use SQLite WHERE - Filter Rows in a Result Set[^].
If teacher is already assigned, then do not add student to teacher otherwise query to see how many students the teacher already has.
If teacher already has 30 then do not add student to this teacher.
How you then handle the situation is up to you.
If you are still stuck then come back with the code you are trying to use and we will try to help further, but without seeing your code it is difficult to be more specific (I will not just give you the code as this looks like homework)
 
Share this answer
 
Comments
Maciej Los 5-Jul-21 8:58am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900