Database Systems: Transactions, ACID, Security, psycopg2 & Distributed Systems

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/47

encourage image

There are no tags or description

Looks like no tags are added yet.

48 Terms

1
New cards

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.

2
New cards

What does ACID stand for?

Atomicity, Consistency, Isolation, and Durability.

3
New cards

What is atomicity?

All or nothing. Either all changes in a transaction are committed or none are.

4
New cards

What is consistency?

A successful transaction takes the database from one valid state to another, following rules such as constraints.

5
New cards

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.

6
New cards

What is durability?

Once a transaction is committed, its changes survive a crash and recovery.

7
New cards

What is TCL?

Transaction Control Language. It includes commands that manage transactions, such as BEGIN, COMMIT, ROLLBACK, and SAVEPOINT.

8
New cards

What does BEGIN do?

Starts a transaction so multiple statements can be handled as one unit.

9
New cards

What does COMMIT do?

Ends a successful transaction and saves its changes.

10
New cards

What does ROLLBACK do?

Ends the current transaction and undoes its uncommitted changes.

11
New cards

What does SAVEPOINT do?

Creates a named checkpoint inside a transaction.

12
New cards

What does ROLLBACK TO SAVEPOINT do?

Undoes changes made after the named savepoint and leaves the transaction open.

13
New cards

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.

14
New cards

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.

15
New cards

How do concurrency and parallelism differ?

Concurrency means tasks make progress during overlapping time periods. Parallelism means tasks execute at the same instant.

16
New cards

What is a dirty read?

Reading a change made by another transaction before it commits. The change could still be rolled back.

17
New cards

What is a lost update?

One transaction overwrites another transaction's update, often because both calculated their new value from the same old value.

18
New cards

What is locking?

A way to control concurrent access by giving transactions permission to read or modify a database resource.

19
New cards

What is a shared lock?

A read lock. Multiple transactions can hold shared locks on the same resource at once.

20
New cards

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.

21
New cards

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.

22
New cards

What extra rule does strict two-phase locking add?

A transaction holds its exclusive locks until it commits or aborts.

23
New cards

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.

24
New cards

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.

25
New cards

How does a database resolve a deadlock?

It aborts and rolls back one of the transactions, releasing its locks so the others can proceed.

26
New cards

What is DCL?

Data Control Language. It controls who has access and what they can do.

27
New cards

What does GRANT do?

Gives a user or role a permission, such as SELECT on a table.

28
New cards

What does REVOKE do?

Removes a previously granted permission.

29
New cards

What is the principle of least privilege?

Give each user or role only the permissions needed to perform its job.

30
New cards

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.

31
New cards

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.

32
New cards

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.

33
New cards

What are good database security practices?

Use individual logins, assign permissions through roles, follow least privilege, and review access regularly.

34
New cards

What does a database administrator do?

Manages security, backups and recovery, performance, schema changes, upgrades, monitoring, and documentation.

35
New cards

What is psycopg2?

A Python adapter for connecting to and working with PostgreSQL.

36
New cards

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.

37
New cards

What is the difference between a connection and a cursor?

A connection manages the session and transactions. A cursor executes SQL and fetches results.

38
New cards

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,)).

39
New cards

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.

40
New cards

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.

41
New cards

What exception can you catch for psycopg2 database errors?

psycopg2.Error. Roll back a failed transaction before using the same connection for more work.

42
New cards

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.

43
New cards

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.

44
New cards

What is a distributed system?

A system whose components run on multiple networked computers and coordinate to perform work.

45
New cards

What does CAP stand for?

Consistency, Availability, and Partition Tolerance.

46
New cards

What does the CAP theorem say?

During a network partition, a distributed system cannot guarantee both consistent reads and successful responses to every request.

47
New cards

How do vertical and horizontal scaling differ?

Vertical scaling gives an existing machine more resources. Horizontal scaling adds more machines.

48
New cards

What is MapReduce?

A model for processing data in parallel. Map produces intermediate key-value pairs, and Reduce combines the values for each key.