BD (ORACLE Y POSTGRESQL) Flashcards

(61 cards)

1
Q

What is a Primary Key (PK)?

A

A column (or set of columns) that uniquely identifies each row in a table. It must be unique and not null. PKs are commonly indexed automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Foreign Key (FK)?

A

A constraint that enforces referential integrity: values in the child table must reference existing values in the parent table (or be null, depending on the FK definition).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

PK vs UNIQUE constraint — what’s the difference?

A

Both enforce uniqueness. A table can have only one PK but multiple UNIQUE constraints. PK columns are not nullable; UNIQUE columns may allow nulls (behavior varies by DB, but generally null is allowed).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a CHECK constraint?

A

A rule enforced by the database that restricts allowed values (e.g., status IN (‘NEW’,’PAID’)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why rely on DB constraints instead of only application validation?

A

Constraints protect data integrity regardless of which app/client writes to the DB, preventing invalid data from ever being stored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does ACID stand for?

A

Atomicity, Consistency, Isolation, Durability — key guarantees of database transactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a transaction?

A

A group of operations executed as a single unit of work: either all changes commit or none (rollback).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Atomicity — definition

A

All operations in a transaction succeed together, or the transaction is rolled back so no partial changes remain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Consistency — definition

A

A transaction moves the DB from one valid state to another, preserving constraints and invariants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Isolation — definition

A

Concurrent transactions should not interfere in a way that causes incorrect results (depends on isolation level).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Durability — definition

A

Once a transaction commits, its changes persist even after crashes (assuming proper DB configuration).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the most common isolation level in OLTP systems?

A

Read Committed — good balance between correctness and concurrency for many workloads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a dirty read?

A

Reading data written by another transaction that has not committed yet. Read Committed prevents this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a non-repeatable read?

A

You read the same row twice in one transaction and get different results because another committed transaction changed it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a phantom read?

A

A repeated query returns a different set of rows because another transaction inserted/deleted rows that match the query condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

High isolation levels vs throughput — tradeoff?

A

Higher isolation reduces anomalies but can increase blocking/conflicts and reduce throughput.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a lock (in basic terms)?

A

A mechanism that controls concurrent access to rows/tables to maintain correctness; writes typically require locks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a deadlock?

A

Two (or more) transactions each hold locks the other needs, causing circular waiting. The DB detects this and aborts one transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you reduce deadlocks?

A

Keep transactions short, acquire locks in a consistent order, avoid holding locks while doing slow work, and ensure proper indexing to avoid wide locking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Why should transactions be kept short?

A

Long transactions hold locks longer, increasing contention, deadlock risk, and latency for other operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is an index?

A

A data structure (often a B-tree) that speeds up lookups and range queries by allowing the DB to locate rows without scanning the whole table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Main tradeoff of adding indexes?

A

Faster reads, but slower writes (INSERT/UPDATE/DELETE must maintain indexes) and more storage usage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

When does an index typically help most?

A

High-selectivity filters (WHERE), join keys, range scans, and ordered queries (ORDER BY) when the index matches the access pattern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

When might an index not help?

A

Low-selectivity columns (e.g., boolean), very small tables, or queries that must read most rows anyway.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why can functions on indexed columns hurt performance?
Expressions like WHERE LOWER(email)=... may prevent using a normal index; consider functional indexes or store normalized values.
26
Why does LIKE '%abc' often avoid index usage?
Leading wildcard prevents efficient prefix search in many B-tree indexes; consider full-text search or trigram indexes (Postgres) if needed.
27
What is an execution plan (query plan)?
The strategy the optimizer chooses to run a query (scan type, join order, index usage, estimated costs).
28
PostgreSQL: how do you inspect query performance?
Use EXPLAIN to see the plan and EXPLAIN ANALYZE to execute and show actual timing/rows.
29
Oracle: how do you inspect query plans?
Use EXPLAIN PLAN and view it (commonly via DBMS_XPLAN) or use tools that show the actual execution plan.
30
What is an N+1 query problem?
An ORM pattern where fetching N parent records triggers N additional queries for children, causing many DB round trips and slow performance.
31
How do you reduce N+1 queries?
Use joins/fetch joins, batch fetching, or write endpoint-specific queries; also monitor query counts in tests.
32
What is pagination and why do we need it?
Returning results in chunks to avoid huge responses and heavy DB load; also improves client performance and UX.
33
Oracle: older pagination approach (historically)?
ROWNUM-based pagination was common in older Oracle versions.
34
Oracle: modern pagination syntax?
OFFSET ... FETCH is supported in modern Oracle versions for pagination.
35
PostgreSQL pagination basics
Use LIMIT/OFFSET for simple paging; for high-scale paging consider keyset pagination (WHERE id > lastId ORDER BY id LIMIT n).
36
What is an UPSERT?
An operation that inserts a row if it doesn’t exist, or updates it if it conflicts with an existing unique key.
37
PostgreSQL UPSERT syntax
INSERT ... ON CONFLICT (key) DO UPDATE ...
38
Oracle common UPSERT pattern
MERGE statement is commonly used to implement upsert-like behavior.
39
Oracle: what is a SEQUENCE used for?
Generating unique numeric values, commonly for primary keys (especially in older Oracle designs).
40
Oracle: identity columns — what are they?
A column where the DB automatically generates values (similar to auto-increment). Supported in modern Oracle versions.
41
PostgreSQL: SERIAL vs IDENTITY
SERIAL is older (uses a sequence). GENERATED AS IDENTITY is the modern, standards-based approach.
42
Oracle DATE vs timestamp types (basic)
Oracle DATE includes time-of-day but no timezone. Use TIMESTAMP WITH TIME ZONE when timezone awareness is required.
43
Why are time zones a common DB bug source?
Mixing local time and UTC can cause incorrect comparisons, reporting errors, and DST-related issues; prefer storing timestamps in UTC with clear types.
44
What is MVCC (PostgreSQL) in one sentence?
Multi-Version Concurrency Control: reads see a consistent snapshot while writes create new row versions, reducing read/write blocking in many cases.
45
Does MVCC mean ‘no locks’?
No. Updates/deletes still require locks and can conflict; some DDL and certain queries can block.
46
What is a full table scan and when is it bad?
Reading the entire table to satisfy a query; bad when the table is large and the query could be selective with an index.
47
What’s a common reason an index isn’t used?
Bad selectivity, outdated stats, mismatched data types, functions on the column, or the optimizer estimates a scan is cheaper.
48
Schema design: why use proper data types?
Correct types enforce validity, reduce storage, and improve performance (e.g., using numeric/time types vs strings).
49
Normalization: what’s the point (basic)?
Reduce duplication and update anomalies by modeling entities and relationships cleanly; denormalize only when justified by performance needs.
50
What are safe migration steps for adding a new column?
Add nullable column → backfill data → deploy app using it → add NOT NULL/constraints later (backward-compatible changes).
51
Why is it risky to add NOT NULL immediately on a large table?
It can lock the table or require expensive validation; safer to backfill first then enforce.
52
What is a unique constraint conflict and how should APIs handle it?
When an insert violates uniqueness (e.g., email already exists). Map it to a 409 Conflict (or 400) with a clear, non-sensitive error message.
53
How do you generally debug a slow query (quick checklist)?
Reproduce with real params → inspect plan (EXPLAIN/ANALYZE) → look for scans/joins/row estimates → add indexes or rewrite → validate with timing.
54
What is a composite index and when is it useful?
An index over multiple columns; useful when queries filter/order by the leftmost prefix of those columns.
55
Composite index: why does column order matter?
B-tree composite indexes are most effective for queries using the leading column(s); wrong order can make the index unusable.
56
What is a covering index (conceptually)?
An index that contains all columns needed by a query, allowing the DB to answer without fetching the full row (depends on DB capabilities).
57
Why should foreign key columns often be indexed?
It can speed up joins and reduce locking/scan overhead during parent updates/deletes and child lookups.
58
What is the difference between OLTP and OLAP (one-liner)?
OLTP: many small concurrent transactions. OLAP: fewer, heavy analytical queries scanning lots of data.
59
When would you consider keyset pagination over OFFSET?
When OFFSET becomes slow at high page numbers; keyset uses an indexed cursor (e.g., id > lastId) and scales better.
60
What’s the risk of SELECT * in APIs?
Unnecessary data transfer, serialization cost, and accidental coupling to schema changes; prefer selecting needed columns and using DTOs.
61
How can logging impact DB performance?
Over-logging can increase latency and storage; also avoid logging sensitive data or huge payloads (e.g., large SQL results).