


Id | integer | | not null | nextval('table_1_id_seq'::regclass) | plain | |ĭescribing both two tables above has a certain purpose. Id | integer | | not null | nextval('table_0_id_seq'::regclass) | plain | | Test=# alter table table_1 add constraint fk_table_0 foreign key(parent_id) references table_0(id) on update cascade on delete cascade Ĭolumn | Type | Collation | Nullable | Default | Storage | Stats target | Description Last but not least, try to describe both the two tables as follows :
Postgresql add foreign key to existing table serial#
Test=# create table table_1(id serial primary key, name text, parent_id int) ĥ. Test=# create table table_0(id serial primary key, name text) The following are the process for creating those tables : The first table name is ‘table_0’ and the second one is ‘table_1’. One of the table will have a foreign key referencing to the column inside the other table. The two tables will show the relationship between them. In order to create the simulation, create two new tables. You are now connected to database "test" as user "postgres". The following is the step for connecting to the database, just by typing ‘\c’ in PostgreSQL Command Console : After successfully creating the new database, connect to the database. The following is the command or query for creating a new database named ‘test’ :ģ. Below is the command for connecting to PostgreSQL Database psql -Upostgres postgresĢ. In other words, it is the task to connect to the PostgreSQL Database in this article. The execution to perform the task is done in a command line interface.
