Database Basics
Before learning SQL, you need to understand basic concepts of databases. This chapter introduces core concepts, relational model, and basic terminology of databases, laying a solid foundation for subsequent learning.
What is a Database?
Definition
A database is an organized collection of data, usually stored in a computer system. It allows users to efficiently store, retrieve, and manage data.
Database Management System (DBMS)
A Database Management System (DBMS) is software used to create, manage, and operate databases. Common DBMS include:
- MySQL
- PostgreSQL
- SQL Server
- Oracle
- SQLite
Relationship
Database ← Place where data is stored DBMS ← Software that manages the database SQL ← Language for interacting with DBMS
Relational Databases
Core Concepts
A relational database is a database system based on the relational model, where data is stored in tabular format (tables).
Table Example
This is a typical database table containing rows and columns.
Basic Database Terminology
1. Table
A table is the basic unit for storing data in a database, similar to an Excel spreadsheet.
Example: Users Table
2. Column
A column defines the type and attributes of data in a table, also called a field.
Column Features
- Data Type: Defines the type of data in the column (integer, text, date, etc.)
- Constraint: Rules that limit data in the column (cannot be empty, must be unique, etc.)
- Default Value: Value used by default if no value is provided
Example: Column Definition
3. Row
A row, also called a record, is a complete piece of data in a table.
Example: Row Data
4. Primary Key
A primary key is a column in a table that uniquely identifies each row. Primary key values cannot be duplicated and cannot be null.
Primary Key Features
- Uniqueness: Each row's primary key value must be unique
- Non-null: Primary key values cannot be NULL
- Immutability: Primary key values typically do not change
Example: Defining Primary Key
5. Foreign Key
A foreign key is a column in one table that references the primary key of another table, used to establish relationships between tables.
Foreign Key Functions
- Ensure data consistency
- Establish relationships between tables
- Prevent insertion of invalid data
Example: Foreign Key Relationship
6. Index
An index is a data structure that improves query performance, similar to a table of contents in a book.
Index Functions
- Speed up data retrieval
- Optimize query performance
- But occupies additional storage space
Example: Creating Index
Database Structure
Database → Table → Rows/Columns
Hierarchy
- Database Server: Computer running DBMS
- Database: Collection of related tables
- Table: Structure storing related data
- Row/Column: Actual data units
Relational Model
One-to-One Relationship (1:1)
One record in a table corresponds to one record in another table.
Example: Users and User Details
One-to-Many Relationship (1:N)
One record in a table corresponds to multiple records in another table.
Example: Users and Orders
Many-to-Many Relationship (M:N)
Records in two tables can correspond to multiple records in each other.
Example: Students and Courses
Common Data Types
Numeric Types
String Types
Date/Time Types
Boolean Type
Database Design Principles
1. Data Normalization
Normalization is the process of organizing database tables to reduce data redundancy and improve data integrity.
2. Primary Key Design
- Use a single column as the primary key
- Use auto-incrementing integers
- Avoid using columns with business meaning
3. Naming Conventions
- Use meaningful names
- Use lowercase letters and underscores
- Avoid using reserved words
Real Example: E-commerce Platform
Database Structure
Data Relationships
Basic Database Operation Flow
1. Connect to Database
2. Select Database
3. Execute Query
4. Disconnect
Summary
This chapter introduced basic database concepts:
- Database Definition: Organized collection of data
- Relational Database: Database based on relational model
- Basic Terminology: Table, column, row, primary key, foreign key, index
- Data Types: Numeric, string, date/time, boolean
- Relationship Types: One-to-one, one-to-many, many-to-many
- Design Principles: Normalization, primary key design, naming conventions
Understanding these basic concepts is the foundation for learning SQL. In the following chapters, we will learn how to use SQL to actually operate databases.
Next Step: Learn SQL Basic Syntax and start writing your first SQL statement.