Posts

Showing posts with the label database design

Tuple comparison and Multi-column IN

Image
Introduction In PostgreSQL, Tuple comparison (row/tuple comparison) and Multi-column IN (IN condition on multiple columns) are two extremely powerful features that help write concise, clearer SQL queries and significantly optimize performance compared to manually chaining multiple AND/OR conditions. Tuple comparison Tuple comparison allows grouping multiple columns or values into a tuple (using parentheses (...)) and comparing these two tuples directly with each other using operators like =, <>, <, >, <=, >= Used for Keyset Pagination with more than 1 column and sorting applied Lexicographical order PostgreSQL compares elements from left to right, similar to dictionary sorting, starting by comparing the first pair of elements. If they differ, the result of the entire comparison is decided immediately without evaluating subsequent columns If they are equal, it proceeds to compare the next pair of elements, continuing this process until the end. General Example (a, ...

Table relationships

Image
Introduction First, let us look at the concept of the three basic relationships in a Relational Database defined based on the fundamental nature of Cardinality links between two tables: One-to-One Relationship (1-1) A record in Table A is linked to exactly one record in Table B and vice versa. Implemented by placing a Foreign Key in one of the two tables and assigning it a UNIQUE constraint. Examples: A User has only one UserProfile. A Product has only one ProductDetail. When to use: When splitting an oversized table containing rarely used columns to optimize data read performance. When security is required: Separating sensitive information such as credit cards or passwords into a dedicated table with stricter access controls. One-to-Many Relationship (1-N) A record in Table A can be linked to multiple records in Table B. Conversely, a record in Table B is linked to only one record in Table A. Implemented by placing a Foreign Key in the "Many" side table (Table B) pointing t...