Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Databases

What is a Database?

A database is an organized collection of data, generally stored and accessed electronically from a computer system. Databases are structured to facilitate the storage, retrieval, modification, and deletion of data in conjunction with various data-processing operations.

Types of Databases

There are several types of databases, each serving different needs and purposes:

  • Relational Databases (RDBMS): Uses tables to store data. Examples include MySQL, PostgreSQL, SQLite.
  • NoSQL Databases: Designed for large-scale data storage and for massively-parallel, high-performance processing. Examples include MongoDB, Cassandra, Redis.
  • In-Memory Databases: Store data in a computer's main memory (RAM) for faster access. Example: Redis.
  • NewSQL Databases: Modern SQL-based databases that aim to provide the scalability of NoSQL without sacrificing ACID (Atomicity, Consistency, Isolation, Durability) properties. Example: Google Spanner.

Relational Database Concepts

Relational databases are one of the most common types of databases. Here are some key concepts:

  • Tables: A table is a collection of related data entries. It consists of columns and rows.
  • Rows: Each row in a table represents a single record. A row is also called a tuple.
  • Columns: Each column in a table represents a specific field within the record. Columns have a datatype and define what kind of data can be stored in that column.
  • Primary Key: A primary key is a unique identifier for a record in a table. It ensures that each record can be uniquely identified.
  • Foreign Key: A foreign key is a field (or collection of fields) in one table, that uniquely identifies a row of another table. It creates a relationship between the two tables.

Setting Up a Database in Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Let's see how to set up a database in Django.

Step 1: Install Django

pip install django

Step 2: Create a Django Project

django-admin startproject myproject

Step 3: Configure the Database

By default, Django uses SQLite as the database. You can configure other databases like PostgreSQL, MySQL, etc., in the settings.py file.

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'mydatabase',
    'USER': 'mydatabaseuser',
    'PASSWORD': 'mypassword',
    'HOST': 'localhost',
    'PORT': '5432',
  }
}

Step 4: Create Models

Models are the single, definitive source of information about your data. They contain the essential fields and behaviors of the data you’re storing. Here's an example:

from django.db import models

class Author(models.Model):
  name = models.CharField(max_length=100)

class Book(models.Model):
  title = models.CharField(max_length=200)
  author = models.ForeignKey(Author, on_delete=models.CASCADE)

Step 5: Create the Database Tables

After defining your models, run the following commands to create the necessary database tables:

python manage.py makemigrations
python manage.py migrate

Step 6: Interact with the Database

You can now interact with the database using Django's ORM (Object-Relational Mapping). Here are some examples:

# Create a new author
author = Author(name='J.K. Rowling')
author.save()

# Create a new book
book = Book(title='Harry Potter', author=author)
book.save()

# Query the database
books = Book.objects.all()
for book in books:
  print(book.title, book.author.name)

Conclusion

Databases are a fundamental component of modern software applications. They provide a systematic way to store, manage, and retrieve data efficiently. Understanding the basics of databases and how to interact with them through frameworks like Django is crucial for any developer.