databases

«< Back - Next »>

Challenge: build a table for GPA!

Now we have a student table that looks something like this:

id          student     id_program
----------  ----------  ----------
1           Josefina    3
2           Cecilia     2
3           Nico        2
4           Sarah       1

Let’s imagine that we want to connect each student to their GPA. How can we do this?

Things to consider:

Solution

Create a New Table For GPAs

CREATE TABLE gpas (
	id INTEGER PRIMARY KEY,
	gpa DOUBLE PRECISION,
	id_student INTEGER,
	FOREIGN KEY (id_student) REFERENCES students(id)
);

You may notice a new term in the above example. DOUBLE PRECISION is just a number that can have a decimal point.

Populate the GPA Table with GPA Score and Foreign Key

INSERT INTO gpas (gpa, id_student) VALUES
	(2.67, 2),
	(3.9, 1),
	(1.23, 3),
	(4.0, 4);

And thus our table for GPAS looks like:

id          gpa         id_student
----------  ----------  ----------
1           2.67        2
2           3.9         1
3           1.23        3
4           4.0         4

«< Back - Next »>