2 ms·
Interesting read! I'm not very familiar with constraints. > We can set up these first two tables for users and rooms without any constraints Based on this sen
by prometheon1 5y ago
Interesting read! I'm not very familiar with constraints.
> We can set up these first two tables for users and rooms without any constraints
Based on this sentence I wondered what the difference is between a primary key and a constraint. I looked up primary key in the postgres docs, it seems that PRIMARY KEY is also a constraint?
> Adding a foreign key constraint here is a really easy way to tie our reservations table to the other data tables ensuring they are always tied together with primary keys.
This did not make me understand what the foreign key actually does. The first sentence of the pg docs on foreign keys helped a bit: A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table.
> You can also create the foreign key constraints after the fact on an existing table via ALTER TABLE like this:
Now I was wondering what happens when you use ALTER TABLE to add a foreign key to a table that already has data. Apparently in mysql it will only succeed if the existing data matches the references data[0], I assume this is the same in postgres.
Finally I'm wondering whether I could join tables automatically using the foreign keys in a select statement instead of typing all the column names, apparently not :(
[0] https://stackoverflow.com/a/19550474 https://stackoverflow.com/a/19550474