1/47
Looks like no tags are added yet.
What is a database transaction?
A group of database operations treated as one unit of work, like subtracting money from checking and adding it to savings in one transfer.
What does ACID stand for?
Atomicity, Consistency, Isolation, and Durability.
What is atomicity?
All or nothing. Either all changes in a transaction are committed or none are.
What is consistency?
A successful transaction takes the database from one valid state to another, following rules such as constraints.
What is isolation?
Controls how concurrent transactions see and affect each other. For example, one transaction should be protected from reading another transaction's uncommitted changes.
What is durability?
Once a transaction is committed, its changes survive a crash and recovery.
What is TCL?
Transaction Control Language. It includes commands that manage transactions, such as BEGIN, COMMIT, ROLLBACK, and SAVEPOINT.
What does BEGIN do?
Starts a transaction so multiple statements can be handled as one unit.
What does COMMIT do?
Ends a successful transaction and saves its changes.
What does ROLLBACK do?
Ends the current transaction and undoes its uncommitted changes.
What does SAVEPOINT do?
Creates a named checkpoint inside a transaction.
What does ROLLBACK TO SAVEPOINT do?
Undoes changes made after the named savepoint and leaves the transaction open.
A transfer subtracts $50 from checking, but adding $50 to savings fails. What happens after a full rollback?
The subtraction is undone, so both accounts keep their original balances. This illustrates atomicity.
What is interleaving?
Mixing operations from different transactions over time, such as running one step of T1, then one step of T2, then another step of T1.
How do concurrency and parallelism differ?
Concurrency means tasks make progress during overlapping time periods. Parallelism means tasks execute at the same instant.
What is a dirty read?
Reading a change made by another transaction before it commits. The change could still be rolled back.
What is a lost update?
One transaction overwrites another transaction's update, often because both calculated their new value from the same old value.
What is locking?
A way to control concurrent access by giving transactions permission to read or modify a database resource.
What is a shared lock?
A read lock. Multiple transactions can hold shared locks on the same resource at once.
What is an exclusive lock?
A lock used to modify a resource. It blocks other transactions from acquiring shared or exclusive locks on that resource.
What is the rule in basic two-phase locking?
A transaction first acquires locks. Once it starts releasing locks, it cannot acquire any new ones.
What extra rule does strict two-phase locking add?
A transaction holds its exclusive locks until it commits or aborts.
What is a deadlock?
A cycle of transactions waiting for one another. For example, T1 waits for a lock held by T2 while T2 waits for a lock held by T1.
How do you read an arrow in a waits-for graph?
T1 → T2 means T1 is waiting for a lock held by T2. A cycle indicates a deadlock.
How does a database resolve a deadlock?
It aborts and rolls back one of the transactions, releasing its locks so the others can proceed.
What is DCL?
Data Control Language. It controls who has access and what they can do.
What does GRANT do?
Gives a user or role a permission, such as SELECT on a table.
What does REVOKE do?
Removes a previously granted permission.
What is the principle of least privilege?
Give each user or role only the permissions needed to perform its job.
Why grant permissions to job roles?
People with the same duties can inherit the same permissions. When someone changes jobs, you can update their role membership.
What are the four layers of database access in the study guide?
Database access, schema or table access, record access for specific rows, and attribute access for specific columns.
An employee can see customer names but cannot see their email addresses. Which access layer is this?
Attribute-level access, because names and email addresses are separate columns.
What are good database security practices?
Use individual logins, assign permissions through roles, follow least privilege, and review access regularly.
What does a database administrator do?
Manages security, backups and recovery, performance, schema changes, upgrades, monitoring, and documentation.
What is psycopg2?
A Python adapter for connecting to and working with PostgreSQL.
How should Python code handle database credentials?
Load credentials from a protected source, such as environment variables or a secrets manager. Keep passwords out of source code and version control.
What is the difference between a connection and a cursor?
A connection manages the session and transactions. A cursor executes SQL and fetches results.
How do you pass user input to a psycopg2 query safely?
Use placeholders in the SQL and pass the values as a separate argument to execute, such as cur.execute("SELECT * FROM customer WHERE customer_id = %s", (customer_id,)).
What is SQL injection?
A vulnerability where untrusted input changes the structure or logic of a SQL query. Passing input as separate parameters keeps it as data.
How do you save or discard changes in psycopg2?
Use conn.commit() to save changes or conn.rollback() to discard uncommitted changes. By default, the first SQL command starts a transaction.
What exception can you catch for psycopg2 database errors?
psycopg2.Error. Roll back a failed transaction before using the same connection for more work.
How can you write query results to a CSV file in Python?
Fetch rows with the cursor, open an output file, and use Python's csv.writer to write the rows.
What does a psycopg2 connection context manager do?
A with conn: block commits on successful exit and rolls back if an exception escapes the block. The connection stays open.
What is a distributed system?
A system whose components run on multiple networked computers and coordinate to perform work.
What does CAP stand for?
Consistency, Availability, and Partition Tolerance.
What does the CAP theorem say?
During a network partition, a distributed system cannot guarantee both consistent reads and successful responses to every request.
How do vertical and horizontal scaling differ?
Vertical scaling gives an existing machine more resources. Horizontal scaling adds more machines.
What is MapReduce?
A model for processing data in parallel. Map produces intermediate key-value pairs, and Reduce combines the values for each key.