Databases are the backbone of modern applications, from simple to complex. They store, manage, and retrieve data efficiently, making them a critical component of the tech world. In this beginner-friendly guide, we'll introduce you to the world of databases, explore different types, and provide practical examples to get you started.
At its core, a database is an organized collection of data. It's like a digital filing cabinet where you can store, manage, and retrieve information. Databases come in various types, each designed for specific use cases.
There are two primary types of databases:
Relational databases are like well-structured spreadsheets. They consist of tables with rows and columns. For example, consider a simple database to store information about books:
ID | Title | Author | Year |
---|---|---|---|
1 | "To Kill a Mockingbird" | "Harper Lee" | 1960 |
2 | "1984" | "George Orwell" | 1949 |
NoSQL databases, on the other hand, are more flexible. They don't rely on fixed schemas and can handle unstructured data. A NoSQL database might be used for social media posts:
{
"username": "john_doe",
"post": "Having a great day!",
"likes": 25
}
Some widely used database management systems include:
Let's create a simple SQLite database in Node.js. First, you'll need to install the sqlite3
package:
Create a new project folder and initialize it with "npm":
mkdir my-node-database-app
cd my-node-database-app
npm init -y
Install the sqlite3
package, which provides SQLite support in Node.js:
npm install sqlite3
Let's create a SQLite database and a table to store user information. Create a file named app.js
:
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('mydatabase.db');
// Create a table to store user data
db.serialize(() => {
db.run('CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)');
});
// Close the database connection
db.close();
This code creates a database file called mydatabase.db
and defines a table named users.
Now, let's insert a user into the database:
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('mydatabase.db');
db.serialize(() => {
const stmt = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
stmt.run('John Doe', 'john@example.com');
stmt.finalize(); // Finalize the statement
});
db.close();
Retrieve and display the user from the database:
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('mydatabase.db');
db.serialize(() => {
db.each('SELECT id, name, email FROM users', (err, row) => {
console.log(`User ID: ${row.id}, Name: ${row.name}, Email: ${row.email}`);
});
});
db.close();
This code selects all users from the users
table and logs their information.
You can also update and delete data in the database. For example, to update a user's email:
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('mydatabase.db');
db.serialize(() => {
db.run('UPDATE users SET email = ? WHERE name = ?', ['new_email@example.com', 'John Doe']);
});
db.close();
Or to delete a user:
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('mydatabase.db');
db.serialize(() => {
db.run('DELETE FROM users WHERE name = ?', 'John Doe');
});
db.close();
You've now learned how to create, insert, query, update, and delete data in a SQLite database using Node.js. Databases are essential for building applications that require data persistence. To continue your journey, you can explore more advanced concepts, such as working with multiple tables, using ORMs (Object-Relational Mapping), and integrating databases into your web applications.