{"id":39823334,"url":"https://github.com/muhammadfarooq85/postgresql-relationships","last_synced_at":"2026-01-18T13:01:21.650Z","repository":{"id":282566220,"uuid":"870441718","full_name":"muhammadfarooq85/PostgreSQL-Relationships","owner":"muhammadfarooq85","description":"This repository is about the how to effectively use relationships in PostgreSQL .","archived":false,"fork":false,"pushed_at":"2024-10-14T05:06:22.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T13:37:17.646Z","etag":null,"topics":["postgresql","postgresql-database","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/muhammadfarooq85.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-10T03:42:58.000Z","updated_at":"2024-10-14T05:06:25.000Z","dependencies_parsed_at":"2025-03-15T13:37:22.071Z","dependency_job_id":"fad15562-1d71-45b0-a937-c34aca4f2d4b","html_url":"https://github.com/muhammadfarooq85/PostgreSQL-Relationships","commit_stats":null,"previous_names":["muhammadfarooq85/postgresql-relationships"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/muhammadfarooq85/PostgreSQL-Relationships","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FPostgreSQL-Relationships","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FPostgreSQL-Relationships/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FPostgreSQL-Relationships/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FPostgreSQL-Relationships/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/muhammadfarooq85","download_url":"https://codeload.github.com/muhammadfarooq85/PostgreSQL-Relationships/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muhammadfarooq85%2FPostgreSQL-Relationships/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28536686,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-18T10:13:46.436Z","status":"ssl_error","status_checked_at":"2026-01-18T10:13:11.045Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["postgresql","postgresql-database","sql"],"created_at":"2026-01-18T13:01:16.843Z","updated_at":"2026-01-18T13:01:21.631Z","avatar_url":"https://github.com/muhammadfarooq85.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL-Relationships\n\n## This repository is about the how to effectively use relationships in PostgreSQL .\n\n## What is Relationship?\n\n### In a relational database, relationships are used to connect data between two or more tables. They are used to enforce data integrity, ensure data consistency, and provide a flexible and scalable data model. PostgreSQL supports three types of relationships: one-to-one, one-to-many, and many-to-many.\n\n#### Note:- There are 3 types of table relationships in a relational database. The relationships can be enforced by defining the right foreign key constraints on the columns.\nhttps://medium.com/@patrikstrausz17/part3-postgresql-relationships-creating-and-managing-database-relationships-5e1eaea769d8#:~:text=In%20a%20relational%20database%2C%20relationships,One%2Dto%2DOne\n\n### 1) `One to One`\n\n#### A one-to-one relationship exists between two tables where each record in one table is linked to only one record in another table, and vice versa. In other words, there is a one-to-one mapping between the two tables.\n\n#### For example, let’s consider a database that stores information about countries and their capitals. Each capital is assigned only to one country, and each country can be assigned only one capital. Therefore, the capital and country tables have a one-to-one relationship.\n\n```\nCREATE TABLE capital (\n  id SERIAL PRIMARY KEY,\n  name VARCHAR(100)\n);\n\nCREATE TABLE country (\n  id SERIAL PRIMARY KEY,\n  name VARCHAR(100),\n  capital_id INTEGER,\n  CONSTRAINT fk_capital FOREIGN KEY(capital_id) REFERENCES capital(id)\n);\n```\n\n### 2) `One to Many`\n\n#### A one-to-many relationship exists between two tables where each record in one table is linked to one or more records in another table, but each record in the other table is linked to only one record in the first table.\n\n#### For example, let’s consider a database that stores information about customers and their orders. Each customer can have many orders, but each order is associated with only one customer. Therefore, the customer and order tables have a one-to-many relationship.\n\n```\nCREATE TABLE customer (\n  id SERIAL PRIMARY KEY,\n  name VARCHAR(100),\n  address VARCHAR(100),\n  email VARCHAR(100)\n  );\n\nCREATE TABLE \"order\" (\n  id SERIAL PRIMARY KEY,\n  order_date DATE,\n  order_amount INTEGER,\n  price DOUBLE PRECISION,\n  customer_id INTEGER,\n  CONSTRAINT fk_customer FOREIGN KEY(customer_id) REFERENCES customer(id)\n);\n```\n\n### 2) `One to Many`\n\n#### A many-to-many relationship exists between two tables where each record in one table is linked to one or more records in another table, and each record in the other table is linked to one or more records in the first table.\n\n#### For example, let’s consider a database that stores information about users and their roles. Each user can have multiple roles, and each role can have multiple users. Therefore, the users and roles tables have a many-to-many relationship.\n\n#### To implement a many-to-many relationship in PostgreSQL, you need to create a junction table that stores the relationships between the two tables. The junction table contains foreign keys from both tables and acts as a bridge between them.\n\n```\nCREATE TABLE users(\n  id SERIAL PRIMARY KEY,\n  firstname VARCHAR(100),\n  lastname VARCHAR(100)\n  );\nCREATE TABLE roles(\n  id SERIAL PRIMARY KEY,\n  role VARCHAR(45)\n);\nCREATE TABLE users_roles(\nid SERIAL PRIMARY KEY,\nuser_id INTEGER,\nrole_id INTEGER,\nCONSTRAINT fk_users FOREIGN KEY(user_id) REFERENCES users(id),\nCONSTRAINT fk_roles FOREIGN KEY(role_id) REFERENCES roles(id)\n);\n```\n\n### Conclusion\n\n#### Relationships are a fundamental aspect of relational databases and are essential for connecting data between two or more tables. PostgreSQL supports three types of relationships: one-to-one, one-to-many, and many-to-many. By understanding and implementing relationships correctly, you can create a robust and flexible database model that allows you to efficiently store and retrieve data.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuhammadfarooq85%2Fpostgresql-relationships","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuhammadfarooq85%2Fpostgresql-relationships","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuhammadfarooq85%2Fpostgresql-relationships/lists"}