7.5. Writing Joins

According to the relational concept, data is stored in various tables. To combine this data, so-called joins have to be performed. Joining means that tables are combined using common attributes.

The next scenario shows a relation between students and training courses. Every student can attend multiple training courses, so you need two tables. One table stores information about students and the second table contains the list of training courses a student attends. Here is the SQL code needed for generating the two tables:

phpbook=# CREATE TABLE student (id int4, name text);
CREATE
phpbook=# CREATE TABLE course(student_id int4, course text);
CREATE

Let's insert some data into the tables:

phpbook=# INSERT INTO student VALUES ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.