You can see from the above SQL statement that the line to establish this relationship isįOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE Published_date timestamp DEFAULT CURRENT_TIMESTAMP,įOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE,įOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE Foreign keys are how RDBMS sets up relationships between rows of data, either in the same table or across tables. For instance, a table might have a column called user_id as a foreign key column, which corresponds to the id column of the users table. Therefore, foreign key columns contain the value of the referenced row's primary key. In order to reference another row, the database needs a unique identifier for that row. It is not required to manually specify the primary key value each time you insert data, PostgreSQL will do it for you if you specify serial as the type for id.įoreign key columns are used to reference another row of data, perhaps in another table. With PostgreSQL, we use serial to auto increment our ids and set their type as integer. With many RDBMS, the AUTO_INCREMENT keyword is used.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |