What is a Database?
INF2603 - Databases I · Introduction to Databases
What is a Database?
A database is an organised collection of data that can be easily accessed, managed, and updated. Databases are used to store information in a structured way, allowing for efficient retrieval and manipulation of data. They are essential for various applications, from small personal projects to large enterprise systems.
Characteristics of Databases
Databases have several key characteristics that distinguish them from simple data storage methods, such as spreadsheets or plain text files:
- Structured Data: Data in a database is organised into tables, which consist of rows and columns. Each table represents a specific entity, such as customers or products.
- Data Integrity: Databases enforce rules to maintain the accuracy and consistency of data. This includes constraints like primary keys and foreign keys.
- Data Manipulation: Databases allow users to perform various operations on data, including creating, reading, updating, and deleting (CRUD operations).
- Multi-user Access: Databases support multiple users accessing the data simultaneously, ensuring that data is managed effectively in a multi-user environment.
Components of a Database
A database consists of several components that work together to store and manage data:
- Tables: The primary structure in a database, tables hold data in rows and columns. Each row represents a record, and each column represents an attribute of the record.
- Relationships: Relationships define how tables are connected to each other. For example, a customer table may be related to an orders table through a customer ID.
- Queries: Queries are used to retrieve specific data from the database. They are written in a database query language, such as SQL (Structured Query Language).
- Indexes: Indexes improve the speed of data retrieval operations by allowing the database to find rows quickly without scanning the entire table.
Types of Databases
While this topic focuses on what a database is, it is important to mention that there are various types of databases, including:
- Relational Databases: These databases store data in tables with predefined relationships. Examples include MySQL, PostgreSQL, and Oracle Database.
- NoSQL Databases: These databases are designed for unstructured data and can store data in various formats, such as key-value pairs, documents, or graphs. Examples include MongoDB and Cassandra.
- Object-oriented Databases: These databases store data in the form of objects, similar to object-oriented programming. Examples include db4o and ObjectDB.
Remember: A database is not just a collection of data; it is a structured system that allows for efficient data management and retrieval.
Database Management Systems (DBMS)
A Database Management System (DBMS) is software that interacts with the database and users. It allows users to create, read, update, and delete data while ensuring data integrity and security. Common DBMS include:
- MySQL: An open-source relational database management system widely used for web applications.
- PostgreSQL: An advanced open-source relational database known for its robustness and support for complex queries.
- Microsoft SQL Server: A relational database management system developed by Microsoft, often used in enterprise environments.
Example: Creating a Simple Database
Let us consider an example of creating a simple database for a library system. We will create a table to store information about books.
Step 1: Define the Table Structure
We want to store the following information for each book:
- Book ID (primary key)
- Title
- Author
- Publication Year
Step 2: Create the Table
Using SQL, we can create the table with the following command:
CREATE TABLE Books ( BookID INT PRIMARY KEY, Title VARCHAR(100), Author VARCHAR(100), PublicationYear INT);Step 3: Insert Data into the Table
Now, we will insert some sample data into the Books table:
INSERT INTO Books (BookID, Title, Author, PublicationYear) VALUES (1, 'The Great Gatsby', 'F. Scott Fitzgerald', 1925), (2, '1984', 'George Orwell', 1949), (3, 'To Kill a Mockingbird', 'Harper Lee', 1960);Step 4: Query the Table
To retrieve all the books from the table, you can use the following SQL query:
SELECT * FROM Books;This query will return all records in the Books table.
Watch out: Ensure that you define the primary key correctly. A primary key must be unique for each record in the table.
Conclusion
A database is a structured collection of data that allows for efficient data management and retrieval. Understanding the components and characteristics of databases is essential for working with them effectively. Familiarity with SQL and DBMS will further enhance your ability to manage databases.
Check your understanding
- What is the primary purpose of a database?
- List three key characteristics of databases.
- What is a primary key, and why is it important?
- Explain the difference between relational and NoSQL databases.