Relational data modeling
44 questions in 11 subtopics.
Entity relationship modeling
What is the difference between an identifying and a non-identifying relationship in an ER model? Give a table example of each.Model a small library system: members borrow copies of books, a book has many authors, and a member can reserve a book (not a specific copy). Name the entities, their relationships and cardinalities, and the tables you would create.A product owner describes an 'account' that sometimes means a login, sometimes a billing customer and sometimes a company with several users. The current schema has one accounts table with 60 nullable columns. How do you untangle the model?Several teams each own a part of the customer data (identity, billing, support, marketing) in separate services and databases. How do you model 'customer' across the organization without one shared customer table?Your company's core data model was designed 15 years ago for one product and now constrains three new product lines. Leaders ask whether to remodel. How do you decide and run such a long-lived data model change?Primary, foreign, natural and surrogate keys
Explain natural keys and surrogate keys with an example of each. Why do many teams use a surrogate primary key even when a natural key exists, and what should still be enforced on the natural key?Write PostgreSQL DDL for orders and order_lines where an order line belongs to exactly one order, deleting an order deletes its lines, a product referenced by a line cannot be deleted, and a line number is unique within an order.Deleting a single row from customers takes 40 seconds and blocks other writes. The table has 2 million rows, andorders(customer_id) references customers(id). What is the likely cause and fix?Your platform team must set a standard for primary keys and foreign keys across 150 services, some of which share data through events and exports. What would the standard say?Normalization 1NF to BCNF
A table enrollments(student_id, course_id, student_name, course_title, instructor, grade) has primary key (student_id, course_id). Which normal forms does it violate and how would you decompose it?A users table has a column phone_numbers storing values like '555-1234,555-9876'. Support reports they cannot find users by a second phone number and updates sometimes drop a number. What is wrong with the model and what would you change?Explain the difference between 3NF and BCNF using a table where it matters, for examplebookings(student, subject, tutor) where each tutor teaches one subject and a student has one tutor per subject.Normalization theory dates from the 1970s, and today many teams use document databases and event logs. How do you explain to a modern engineering organization where normalization still matters and where it does not?Denormalization trade-offs
What is denormalization, and give two examples where it is a good idea and one where it causes bugs.An order list page joins orders, customers, addresses and order_lines to show the customer name and order total, and is slow at 5000 requests per second. A teammate proposes copying customer_name and total into orders. How do you evaluate this?Your product needs operational screens over the normalized OLTP schema and also heavy reporting. Teams keep adding denormalized columns and triggers to the OLTP database for reports. What architecture do you propose?Across your company, teams built dozens of denormalized copies of product and customer data in caches, search clusters and service databases. Incidents from stale data are rising. What principles and investments do you set?Constraints and referential integrity
Add constraints to a PostgreSQL tablebookings(id, room_id, starts_at, ends_at, status) so that ends_at is after starts_at, status is one of 'held', 'confirmed', 'cancelled', and two non-cancelled bookings of the same room cannot overlap.A team says 'we do not use foreign keys because they slow down writes and our ORM handles integrity'. A data audit finds 3 percent of order lines point to missing products. How do you respond?Your company has many databases where integrity is enforced only in application code, and data quality incidents keep reaching finance reports. How would you raise integrity standards without stopping delivery?Twenty years ago many systems kept business rules in stored procedures and triggers; then the industry moved rules into application code. Where should integrity rules live today, and how do you decide for a new platform?Modeling many-to-many and hierarchies
Model a category tree of unlimited depth in PostgreSQL with an adjacency list, and write a query that returns all descendants of category 10 with their depth.Users can follow other users, and the relationship needs a created_at, a notification setting and fast lookups of both 'who do I follow' and 'who follows me' for users with millions of followers. Design the table and indexes.An organisation chart service must answer 'all reports under this manager' and 'the management chain of this person' quickly for 300,000 employees, with reorganisations moving thousands of people at once. Which hierarchy model would you choose?Soft deletes and audit columns
A table uses a deleted_at column for soft deletes. What problems does this cause for unique constraints and queries, and how do you handle them?Implement soft delete for a JPA entity Customer in Hibernate 6 so thatrepository.delete(customer) sets deleted_at instead of deleting, and normal queries hide deleted rows. What are the gotchas?Which audit columns would you add to business tables (created_at, updated_at, created_by and so on), how do you populate them reliably, and when are they not enough?Legal asks engineering to guarantee that customer data is fully deleted within 30 days of a request across all systems, but most services use soft deletes, backups last 90 days and data is copied into analytics. How do you lead this?Temporal data and history tables
What is a history table and how does it differ from storing valid_from and valid_to on the main table? Give one way to populate a history table.Product prices change over time and invoices must show the price that applied on any past date. Design the schema and the query for 'price of product 42 on 2026-03-15'.Regulators require you to show what your system believed a customer's risk rating was on any past date, including later corrections to earlier data. Which temporal model do you need and how would you implement it?Leadership wants 'full history of everything' kept forever for analytics and audits, across all operational databases. What do you advise about cost, design and governance?UUID vs sequence keys
Compare bigint sequence keys and UUID keys for a PostgreSQL table. When would you choose each, and what changes with UUIDv7?In a Spring Boot 3 JPA entity using Hibernate 6, write the id mapping for (a) a PostgreSQL sequence with batched inserts and (b) a UUID generated by the application. Why doesGenerationType.IDENTITY hurt batching?After switching primary keys from bigint identity to random UUIDv4 stored as CHAR(36) in MySQL 8.4 InnoDB, insert throughput drops by half and the buffer pool hit rate falls. Explain why and fix it.Your organisation is debating a company-wide rule: 'all primary keys are UUIDv7'. Some teams have large MySQL tables with bigint keys. How would you decide and roll out such a rule?Modeling money, time zones and enums
How would you model an order status enum in the database: a native PostgreSQL ENUM type, a VARCHAR with a CHECK constraint, or a lookup table? What does JPA's@Enumerated(EnumType.ORDINAL) risk?A Java service stores prices as double and a PostgreSQL column as real. Customers see totals like 19.989999. What is wrong and how should money be modelled in Java and SQL?Your system stores appointments for clinics in many countries. Some columns are 'timestamp without time zone'. Users report appointments shifting by an hour after daylight saving changes. How should instants, local times and time zones be modelled?A global company has had repeated incidents from money rounding, currency conversion and time zone handling across 80 services in several languages. How would you prevent this class of problems at the organisation level?Multi-tenant schemas
Write PostgreSQL row-level security for a shared invoices table with a tenant_id column so that each connection only sees its tenant's rows, and show how a Spring service sets the tenant.Compare three multi-tenant database models for a B2B SaaS on PostgreSQL: shared tables with a tenant_id column, schema per tenant, and database per tenant. Which would you choose for 2000 small tenants and 5 very large ones?One tenant's bulk import in a shared-table SaaS database saturates I/O and slows every other tenant. How do you handle this now and design against it long term?Your SaaS started with schema-per-tenant and now has 15,000 schemas; migrations take a day and costs are rising. Leadership asks for a multi-year tenancy strategy. What do you propose?No question matches these choices.