Schema Design & DDL Flashcards

CREATE, ALTER, DROP (tables, columns) Data types (PostgreSQL vs Oracle differences) Index creation syntax Unique constraints and composite keys Views and materialized views Sequences and identity columns Referential integrity (38 cards)

1
Q

What does the CREATE statement do in SQL?

A

CREATE is used to define new database objects like tables, indexes, or views.

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

What does the ALTER statement do in SQL?

A

ALTER modifies the structure of an existing database object, such as adding a column to a table.

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

What does the DROP statement do in SQL?

A

DROP permanently deletes database objects like tables or indexes.

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

What are common data types in PostgreSQL?

A

INTEGER, SERIAL, TEXT, VARCHAR, BOOLEAN, TIMESTAMP, JSONB, UUID.

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

What are common data types in Oracle?

A

NUMBER, VARCHAR2, CLOB, BLOB, DATE, TIMESTAMP, RAW.

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

What is a key difference between PostgreSQL and Oracle data types?

A

PostgreSQL supports JSON/JSONB natively; Oracle relies on CLOB for similar use.

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

What is the syntax to create an index in SQL?

A

CREATE INDEX index_name ON table_name (column1, column2);

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

What is a unique constraint?

A

A constraint that ensures all values in a column or group of columns are unique.

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

What is a composite key?

A

A primary key made up of more than one column to uniquely identify a record.

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

What is a SQL view?

A

A virtual table representing the result of a stored query, not storing data physically.

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

What is a materialized view?

A

A view that stores the result set physically and can be refreshed periodically.

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

What is the difference between a view and a materialized view?

A

Views are virtual and always reflect current data; materialized views store data and can improve performance but may become stale.

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

What is a sequence in SQL?

A

A database object that generates a sequence of unique numeric values, often for primary keys.

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

What is an identity column in SQL?

A

A column that auto-increments its value using a built-in mechanism, replacing explicit sequences in some RDBMS.

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

What is referential integrity in databases?

A

Ensures relationships between tables remain consistent using foreign keys.

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

What is the advantage of using views?

A

Views provide abstraction, security, and simplified query logic.

17
Q

What is a disadvantage of using views?

A

Views can become complex, hard to debug, and may impact performance if nested or improperly indexed.

18
Q

What are best practices for schema design?

A

Use meaningful names, normalize where needed, enforce constraints, and plan indexing based on query patterns.

19
Q

What is a use case for materialized views?

A

Caching aggregated reports or slow-running joins for faster access.

20
Q

How do schema changes impact system design?

A

They affect API behavior, data migration, and require version control in CI/CD.

21
Q

What is the impact of constraints on performance?

A

Constraints enforce data quality but add overhead during writes and updates.

22
Q

What is an architectural implication of indexes?

A

Indexes improve read performance but can slow down inserts and updates, affecting write-heavy systems.

23
Q

How do you monitor index usage?

A

Use database query plans, statistics views, and APM tools to identify index efficiency.

24
Q

What is a real-world tradeoff in schema design?

A

Normalized schemas reduce redundancy but may require complex joins; denormalization simplifies reads but can cause anomalies.

25
What is a common interview question on DDL?
What is the difference between DELETE, DROP, and TRUNCATE?
26
What’s a potential gotcha with ALTER TABLE?
Altering large tables can lock the table and impact availability during production.
27
What’s a potential gotcha with DROP TABLE?
DROP removes all data and structure permanently; it's not reversible without backups.
28
What’s a best practice when creating indexes?
Index only frequently queried columns and avoid redundant indexes.
29
What’s a common use case for composite keys?
Linking association tables in many-to-many relationships.
30
What is a tradeoff of using materialized views?
They improve read speed but require scheduled refreshes and can go stale.
31
What is a challenge when working with sequences in distributed systems?
Ensuring global uniqueness and avoiding gaps or collisions when scaling horizontally.
32
What happens if referential integrity is not enforced?
Orphaned records and inconsistent data may occur, leading to data quality issues.
33
What’s the difference between SERIAL and IDENTITY in PostgreSQL?
SERIAL is legacy using sequences; IDENTITY is ANSI-standard and preferred in newer versions.
34
What is a best practice when managing schema versions?
Use migrations tracked in version control and avoid direct DDL in production environments.
35
How does schema design affect fault tolerance?
Proper use of constraints and types reduces data corruption and supports recovery.
36
What tools help debug schema issues?
ER diagrams, pgAdmin, SQL Developer, dbSchema, and logging slow queries involving structure.
37
What are examples of schema-related performance issues?
Unindexed foreign keys, over-indexing, missing constraints causing full scans.
38
What is a composite unique constraint?
Ensures a combination of values in multiple columns is unique together.