Databases / Data Flashcards
Schema
Blueprint / structure of DB.
Key Components
- Tables
- Columns/fields
- Views
- Indexes
Benefits of having multiple DB Schemas
- Allows for better organization
- Separation of DB objects into logical groupings
- Easier to manage
- Security and access control allowing for fine-grained control of who can access the data
- Modularity: Schemas can represent diff modules / components of an application allowing for modular design and development
Flyway
Open source DB migration tool that helps manage changes to a DB. (Similar to GIT)
Helps keep DB in sync, organized and consistent.
Flyway Versioned Migrations
Uses versioning mechanism to apply migrations in a specific order. Each is given a unique version number
Benefits of Flyway
- Automates the process of applying DB migrations
- Ensures same migrations are applied in same order across all env
- Version control making it easy to track changes over time
DB Object
Any defined obj in a DB that can store or reference data.
Created using Data Definition Language (DDL) like CREATE, ALTER, DROP
DB Object Examples
- Tables: rows and columns
- Views: virtual tables representing the result of a stored query
…
Views
Virtual tables defined by a query
- simplify complex’s queries
- encapsulate complex logic
- present data in a specific format without altering underlying tables
View Details
Views do not store data but dynamically generate results based on the query when accessed
What 4 properties a transaction must exhibit
ACID
- Atomicity: ensures all operations are completed successfully. If any operations fail the transaction is rolled back and DB is left unchanged
- Consistency: guarantees a transaction will bring the DB from one valid state to another
- Isolation: ensures that all operations of a transaction are isolated from others. Intermediate states within a transaction are not visible to other transactions
- Durability: ensures changes by a committed transaction are permanent even in the case of system failure
DB Transactions
A sequence of operations performed as a single logical unit of work
Example Transaction Scenario
- Begin Transaction
BEGIN;
- Debit amount from account A
UPDATE accounts SET balance = 200;
- Update amount from account B
UPDATE accounts SET balance = 1;
- Commit Transaction
COMMIT;
** If the debit operation fails the transaction is rolled back
ROLLBACK
unnest
used to expand an array into a set of rows
“unnests” the array elements and returns them as individual rows
Used in replacement of writing multiple queries. Basically a bulk query.