I have 2 tables, as you will see in my postgresql code below. The first students in the table have 2 columns, one for the student’s name and the other student_id the primary key. In my second table, called tests, it has 4 columns, one for subject_id, one for subject_name, then one for the student with the highest score in the subject, which is the highest Student_id. I am trying to make highStudent_id a reference to student_id in my students table. This is the code I have below, not sure if the syntax is correct:
CREATE TABLE students ( student_id SERIAL PRIMARY KEY, player_name TEXT); CREATE TABLE tests ( subject_id SERIAL, subject_name, highestStudent_id SERIAL REFERENCES students);
is the highestStudent_id SERIAL REFERENCES students syntax correct? because I saw another one similar to highestStudent_id REFERENCES students(student_id))
What would be the correct way to create a foreign key in postgresql, please?
sql relational-database postgresql foreign-keys foreign-key-relationship
Hamza Feb 17 '15 at 9:44 2015-02-17 09:44
source share