Database Connection and Basic Structure
You'll learn how programs connect to relational databases and the fundamental anatomy of SQL statements.
Connecting to Databases
Before you can ask a database anything, you need to establish a connection—think of it like dialing into a service. Your application (written in Python, Java, C++, etc.) uses a database driver or library to open a communication channel with the database server.
A typical connection requires:
- Hostname/address: Where the database server lives (e.g.,
localhostordb.example.com) - Port: The specific door to knock on (e.g.,
5432for PostgreSQL,3306for MySQL) - Database name: Which specific database you want to access
- Credentials: Username and password for authentication
Once connected, you hold a connection object that lets you send SQL statements and receive results back.
SQL Statement Structure
SQL statements follow predictable patterns. Every query is built from keywords (like SELECT, FROM, WHERE) combined with identifiers (table names, column names) and values.
Basic syntax conventions:
- Keywords are typically written in UPPERCASE (though case doesn't matter in most systems)
- Identifiers refer to your schema objects (tables, columns)
- Statements often end with a semicolon
; - Whitespace is flexible—SQL cares about order, not spacing
Example anatomy:
SELECT column_name FROM table_name WHERE condition;
Keyword → what to retrieve → FROM keyword → where it lives → optional filter
Key Takeaway: Database connections are your application's pipeline to the database server, and SQL statements follow a structured syntax built from keywords, identifiers, and values arranged in a logical order.