Say there is a database for students and professors (a very simple one) and the relational database is the following:
GradStudent (_id_, name, gradStuff)
Und
If by "fetch the student id for the Teaches table", you mean you want Teaches.stud_id to be a FK that references "GradStudent or Undergradstudent as is the case", you can't. The target of a FK must be a key of a table that is not a view. You have no such table, ergo you have no such key either.
Only way I see is to code a trigger that does the check upon inserts/updates to Teaches.