PostgreSQL Fundamentals for Beginners
Introduction to Databases
What is a Database?
At its heart, a database is just an organized collection of information. Think of it as a digital filing cabinet. Instead of paper folders, you have a system designed for storing, managing, and retrieving electronic data quickly and reliably.
Every time you use an app to check your contacts, browse an online store's inventory, or look up your saved documents, you're interacting with a database. They are the invisible workhorses of the digital world, making sure information is available and in order.
Databases operate by organizing data into meaningful groupings called fields, records, tables, and finally databases.
There are many kinds of databases, but the most common type is the relational database. It’s the foundation for countless applications and systems.
Relational Databases
A relational database organizes data into tables, which look a lot like spreadsheets. Each table stores information about a specific type of thing, like customers, products, or orders. The real power comes from the relationships you can create between these tables. For example, you can link an order to the specific customer who made it and the products they bought.
This structure prevents you from having to duplicate information. Instead of typing a customer's full address into every single order they make, you just link the order to that customer's single entry in the
Customerstable.
Let's break down the three core components of a table:
Tables: A table holds a collection of related data. You might have one table for Employees and another for Departments.
Rows: Also called records, rows represent a single entry in a table. In a Customers table, each row would hold the information for one specific customer.
Columns: Also called fields or attributes, columns define the types of information stored in a table. For the Customers table, you might have columns for FirstName, LastName, and EmailAddress.
| CustomerID | FirstName | LastName | |
|---|---|---|---|
| 1 | Maria | Jones | m.jones@example.com |
| 2 | Chen | Li | chen.li@example.com |
| 3 | Sam | Miller | sam.miller@example.com |
Keeping Data Trustworthy with ACID
Imagine two people trying to book the last seat on an airplane at the exact same moment. How does the system make sure the seat isn't sold twice? Or what happens if a bank transfer is interrupted halfway through? The money can't just disappear.
To handle these challenges, relational databases follow a set of rules for any operation, or transaction, that changes data. These rules are known by the acronym ACID.
Utilizing ACID properties–Atomicity, Consistency, Isolation, Durability–ensures reliable processing of database transactions.
ACID properties guarantee that database transactions are processed reliably. Let's break them down.
Atomicity: This means a transaction is "all or nothing." If any part of the transaction fails, the entire transaction is rolled back as if it never happened. A bank transfer must debit one account and credit the other. If one step fails, the other is undone.
Consistency: This ensures that a transaction can only bring the database from one valid state to another. It prevents transactions that would violate the database's rules, like creating an order for a customer who doesn't exist.
Isolation: This property ensures that concurrent transactions don't interfere with each other. The result of running multiple transactions at once should be the same as if they were run one after another. This is what prevents two people from booking the same seat simultaneously.
Durability: Once a transaction has been successfully completed, it is permanently saved. Even in the event of a power outage or system crash, the change will persist.
These four properties are the bedrock of transaction processing in most relational databases, including PostgreSQL. They provide the guarantees needed to build reliable applications that manage critical data.
In a relational database, what is a column also known as?
A user attempts to create a new order in a database, but references a customer ID that does not exist. The database prevents this action. Which ACID property is being enforced?
