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)