![]() This post explained the FOREIGN KEY CONSTRAINT with a practical example.This is a sample that doesn’t work for me: CREATE TABLE public.dim_dateĬONSTRAINT dim_date_date_id_pkey PRIMARY KEY (date_id) - This worksĬOMMENT ON COLUMN public.dim_date.date_idĬOMMENT ON COLUMN public.dim_date.formatted_dateĬOMMENT ON COLUMN public.dim_date.day_nameĬOMMENT ON COLUMN public.dim_date.day_of_weekĬOMMENT ON COLUMN public.dim_date.month_nameĭROP SEQUENCE IF EXISTS sample_table_id_seq The table referencing the foreign key is named the child/referenced table, while the table referenced by the foreign key is known as the parent/referenced table. A table can have zero, one, or multiple foreign keys it depends on the table’s relation with other tables. ![]() In PostgreSQL, Foreign keys are a widely used concept that allows us to link the data of one table to others. That was all the basics regarding Postgres FOREIGN KEY CONSTRAINT. ![]() The output clarifies that the emp_id is a foreign key in the dept_info table. Let’s execute the below command to understand the table’s relation more clearly: \d+ dept_info The output shows that the emp_id column is referenced by the dept_info table. Let’s run the below command to get more clarity: \d+ emp_info The dept_info table has been created successfully. Let’s create another table named dept_info that contains a foreign key emp_id: CREATE TABLE dept_info( The following example shows how a foreign key constraint can be created at the time of table creation: CREATE TABLE emp_info(Įmp_info table with four columns has been created successfully.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |