0

Im creating a MySQL Schema with Two Tables (Student and Instructor).

1. Students are given a student number when they join the school.
   This is stored along with their name, date of birth, and the date they joined
   the school.

2. All instructors are also students, but clearly, not all students are
   instructors. In addition to the normal student information, for all 
   instructors, the date that they start working as an instructor must be
   recorded, along with their instructor status (compensated or volunteer). 

My Schema:

STUDENT

+---------------+
| ID            | PK
+---------------+
| LNAME         |
+---------------+
| FNAME         |
+---------------+
| MNAME         |
+---------------+
| BDAY          |
+---------------+
| JOIN_DATE     |
+---------------+
| IS_INSTRUCTOR |
+---------------+

INSTRUCTOR

+---------------+
| ID            | PK
+---------------+
| STUDENT_ID    | FK
+---------------+
| INSTR_STATUS  | (compensated or volunteer)
+---------------+
| DATE_WORKED   |
+---------------+

How can when I insert into STUDENT TABLE and if IS_INSTRUCTOR = 1 (meaning true) INSTRUCTOR TABLE (STUDENT_ID) will have the STUDENT.ID?

Or you guy have a better Table Design? TIA

  • 1
    I am not sure why you need `is_instructor` field in the `student` table. You will need to update it each time you add or remove a record from `instructor` table. If you need to see if particular student is an instructor a simple hash join on `student_id` would suffice. But the options I see: first is using a stored procedure, and the second option is just to wrap separate inserts into one transaction. – Alex P Mar 14 '14 at 20:52
  • Thanks @AlexP. Do you have a Diagram? – Just A Noob Mar 14 '14 at 21:00
  • The diagram almost remains the same. I would remove calculated field `is_instructor` unless there are specific application requirements which I don't know. And also I would remove surrogate primary key `id` from `instructor` table and make `student_id` a primary key which is also a foreign key to `student` table. – Alex P Mar 14 '14 at 21:14
  • @AlexP instead of comment why not answer so I can vote or accept it? – Just A Noob Mar 14 '14 at 21:32

4 Answers4

1

I am not sure why you need is_instructor field in the student table. You will need to update it each time you add or remove a record from instructor table. If you need to see if particular student is an instructor a simple hash join on student_id would suffice. But the options I see: first is using a stored procedure, and the second option is just to wrap separate inserts into one transaction.

I would remove calculated field is_instructor unless there are specific application requirements which I don't know. Though surrogate (or artificial) keys can be useful, for current design I would remove primary key id from instructor table and make student_id a primary key which is also a foreign key to student table.

Alex P
  • 1,721
  • 1
  • 17
  • 18
0

How about something in the lines of...

INSERT INTO INSTRUCTOR (STUDENT_ID, INSTR_STATUS, DATE_WORKED)
SELECT ID, 'volunteer', NOW() 
FROM STUDENT
WHERE IS_INSTRUCTOR = 1

You might want to rethink your design.

majidarif
  • 18,694
  • 16
  • 88
  • 133
0

I think a trigger is the best think you can do at this moment. But the model is a little bit confuse. Ins't better to create just one table for everything?

+---------------+  
| ID            | PK  
+---------------+  
| LNAME         |  
+---------------+  
| FNAME         |  
+---------------+  
| MNAME         |  
+---------------+  
| BDAY          |  
+---------------+  
| JOIN_DATE     |  
+---------------+  
| IS_INSTRUCTOR |  
+---------------+  
| INSTR_STATUS  | (compensated or volunteer)  
+---------------+  
| DATE_WORKED   |  
+---------------+  
majidarif
  • 18,694
  • 16
  • 88
  • 133
Sornii
  • 421
  • 3
  • 11
0

This link may help you

You would use a WHERE clause like:

WHERE IS_INSTRUCTOR = 1
Community
  • 1
  • 1
Sohail xIN3N
  • 2,951
  • 2
  • 30
  • 29