What a DBMS gives you
TipA DBMS is the software that manages a database. Data independence means applications don't depend on how the data is stored.
order_item links them.TipA DBMS is the software that manages a database. Data independence means applications don't depend on how the data is stored.
CREATE, ALTER, DROP and TRUNCATE.SELECT, INSERT, UPDATE and DELETE.GRANT and REVOKE.COMMIT, ROLLBACK and SAVEPOINT.TrapTRUNCATE removes rows, but it is DDL, not DML.
Asked in interviews
AskedWhat is the difference between DDL, DML, DCL, and TCL?TipA schema defines tables, columns, relationships and constraints. Each ANSI-SPARC level can change without affecting the one above.
Asked in interviews
AskedWhat is a database schema, and what is the three-level architecture?orders.customer_id .manager_id pointing at employee_id.TipSplit a composite attribute, like an address, into columns. Give a multivalued one its own table.
TipWork top-down for a new system, and bottom-up from a legacy system's data. Complex systems mix both.
TrapIn the three-level architecture, conceptual and logical name one level. In data modeling, they are two models.
Asked in interviews
AskedWhat is data modeling and what are the different approaches?unit_price DECIMAL(10, 2) NOT NULLUse DECIMAL, not a float, which stores some values only approximately.CREATE INDEX idx_orders_customer ON orders (customer_id, created_at);TrapStore each item's unit price at the time of purchase. Otherwise a later price change rewrites past orders.
Asked in interviews
AskedHow would you design a database schema for an e-commerce application?NULL. A table has one.NULL unless you add NOT NULL.NULLA unique key. PostgreSQL allows many NULL values, and SQL Server allows one.TipA composite key is a primary key of several columns, like (order_id, product_id).
Asked in interviews
AskedExplain different types of keys in a database.TrapA surrogate key doesn't stop duplicate rows. Keep a UNIQUE constraint on the natural key too.
Asked in interviews
AskedWhat is the difference between a natural key and a surrogate key?ALTER TABLE products ADD CONSTRAINT chk_price CHECK (price > 0);ALTER TABLE users ADD CONSTRAINT uk_email UNIQUE (email);TipThe database rejects a bad write from every application, not only yours.
TrapA complex CHECK runs on every insert and update, so keep it cheap.
Asked in interviews
AskedWhat are database constraints and how do they ensure data integrity?NULL or matches an existing parent row, so no row is orphaned.ON DELETE CASCADEON DELETE SET NULLCASCADE deletes them. SET NULL keeps them with a NULL key.ON DELETE RESTRICTNO ACTION, PostgreSQL's default, also blocks it. A deferrable one checks at commit.TrapPostgreSQL doesn't index a foreign key for you. Without that index, each parent delete scans the child table.
Asked in interviews
AskedWhat is referential integrity and how is it maintained?product_name in order items keyed by order and productIt breaks 2NF, since it depends on only part of the key. Move it to products.dept_name stored in employeesIt breaks 3NF, since it depends on dept_id, not the key. Move it to departments.TipEach form needs the one before it. Only a table with a composite key can break 2NF.
TipNormalize first. Denormalize only where measurements show a need.
TrapEvery update must now keep each copy in sync, so writes get more complex.
Asked in interviews
AskedWhen would you consider denormalization?Asked in interviews
AskedWhat are database indexes and how do they improve performance?WHERE, JOIN and ORDER BY columns, foreign keys, and columns with many distinct values.email and statusIndex email. A three-value status has low selectivity and barely narrows the search.TipMeasure before you add an index, and drop the ones nothing uses.
Asked in interviews
AskedWhen should you create an index and when should you avoid it?(a, b, c) servesQueries that filter on its leading columns, the leftmost prefix rule.a = 5 AND b > 42Equality columns first, then the range or sort column.TipAn index on (a, b) also serves a alone, so a separate index on a is usually redundant.
Asked in interviews
AskedDoes column order matter in a composite index?EXPLAIN SELECT * FROM orders WHERE customer_id = 42;Look for a full table scan.WHERE OrderDate >= '2023-01-01' AND OrderDate < '2024-01-01'Sargable, meaning an index can serve it. YEAR(OrderDate) = 2023 can't.TrapDon't reorder joins or WHERE conditions by hand. The optimizer picks the order itself.
Asked in interviews
AskedExplain query optimization techniques.Asked in interviews
AskedExplain the ACID properties of a transaction and why they matter.READ UNCOMMITTEDRead changes nobody has committed yetREAD COMMITTEDThe default in most databases, PostgreSQL included.See only committed dataREPEATABLE READMySQL InnoDB's default. PostgreSQL also blocks phantoms here.Get the same row back when you read it againSERIALIZABLEAct as if transactions ran one at a timeTipA dirty read sees uncommitted data. A non-repeatable read gets a changed row. A phantom read finds new rows.
Asked in interviews
AskedWhat are database transactions and isolation levels?SELECT * FROM Account WHERE AccountID = 1 FOR UPDATE;Pessimistic. Other writers wait for your transaction.SET Version = Version + 1 WHERE AccountID = 1 AND Version = 2Optimistic. Each row carries a version number.TipUse pessimistic locking for high contention, and optimistic when conflicts are rare.
TrapAn update that matches zero rows raises no error. Check the row count, then read again and retry.
Asked in interviews
AskedWhat is the difference between optimistic and pessimistic locking?TrapThe victim gets an error. Your code must catch it and retry.
Asked in interviews
AskedExplain deadlocks and how to prevent them.A runs at Repeatable Read. Its reads keep seeing 1000, even after B commits 900, because B wrote a new row version. Neither waits for the other.
TipOld versions take storage until garbage collection removes them. PostgreSQL calls it VACUUM.
TrapWriters still lock the rows they change. A second writer to the same row waits.
Asked in interviews
AskedExplain MVCC (Multi-Version Concurrency Control) and its benefits.CREATE VIEW customer_contacts AS SELECT name, email FROM customers;REFRESH MATERIALIZED VIEW monthly_sales;A full rebuild. Oracle can also refresh incrementally.TrapA plain view stores no data, so it isn't faster. A materialized view is only as fresh as its last refresh.
WITH. The statement reads it like a table, as often as it needs.WITH RECURSIVEAn anchor picks the top rows, then a recursive part joins back with UNION ALL.TrapA CTE lasts for one statement. The next statement can't read it.
Asked in interviews
AskedWhat are Common Table Expressions (CTEs) and when are they useful?BEFORE, AFTER or INSTEAD OF the change. SQL Server has no BEFORE, and MySQL no INSTEAD OF.CREATE, ALTER or DROP, and a logon trigger at session start. MySQL has neither.TipUse triggers for audit logs and rules every application must follow.
TrapA trigger slows every write to its table. Keep heavy or risky work in the application.
Asked in interviews
AskedWhat are database triggers and when should you use them?TipOLTP is Online Transaction Processing. OLAP is Online Analytical Processing.
Asked in interviews
AskedExplain the difference between OLTP and OLAP systems.TrapA star repeats values like category names. Changing one means updating many rows.
Asked in interviews
AskedWhat is the difference between a star schema and a snowflake schema?TipPartition at millions of rows, or when a table outgrows memory.
TrapA query that skips the partition key reads every partition. Too many partitions also slow planning.
mongos sends each query to its shard.TipDirectory sharding maps keys with a lookup service, which can become a bottleneck.
TrapPick the shard key from your queries. Cross-shard joins and transactions are slow and complex.
TipOlder docs say master-slave. MySQL now says source and replica.
TrapAn asynchronous replica lags behind. A read from it right after a write may miss that write.
Asked in interviews
AskedExplain database replication strategies.majority writes and linearizable reads in MongoDB, QUORUM in Cassandra, and ConsistentRead in DynamoDB.TipPACELC adds that with no partition, systems trade latency against consistency.
TrapDon't say "pick two of three". No distributed system can rule out partitions, so you choose C or A during one.
Asked in interviews
AskedExplain CAP theorem and its implications for database design.TrapACID no longer settles the choice. MongoDB supports multi-document transactions across collections and shards.
Asked in interviews
AskedWhen would you choose NoSQL over relational databases?TipHikariCP suggests twice the server's CPU cores, plus the disks that serve reads. Count zero disks when the active data is cached.
TrapAlways return the connection to the pool, even when a query fails. Otherwise the pool runs out.
Asked in interviews
AskedExplain database connection pooling and its benefits.TipFollow the 3-2-1 rule. Keep 3 copies, on 2 kinds of media, with 1 offsite.
TrapA backup you have never restored may not work. Test restores on a schedule.
Asked in interviews
AskedExplain database backup and recovery strategies.GRANT SELECT, INSERT, UPDATE ON app_db.orders TO 'app_user'@'localhost'; Least privilege, often through roles. The example is MySQL syntax.TrapTest copies of production data leak too. Mask the data in non-production environments.
Asked in interviews
AskedExplain database security best practices.Nothing matches. Try a shorter word, or clear the search.
With Pro, you can:
Already have Pro? Sign in