Saturday, March 7, 2026

Database Fundamental 1

 

Database Fundamentals

Database Fundamentals is the basic knowledge of how data is stored, organized, and managed in a computer system. A database helps applications store information so it can be easily accessed, updated, and managed. One common database system used in web development is MySQL.


1. Database

A database is a collection of organized data stored in a computer system so that it can be easily accessed and managed.

Example

A school database may store:

  • Student names

  • Student ID numbers

  • Courses

  • Results


2. Table

A table is where data is stored inside a database. It organizes data into rows and columns.

Example Table (Students)

IDNameCourse
1JohnComputer Science
2MaryMathematics

3. Row (Record)

A row represents a single record in a table. Each row contains information about one item.

Example

In a student table, one row represents one student.


4. Column (Field)

A column represents a category of data in a table.

Example

In a student table, columns may include:

  • ID

  • Name

  • Course


5. Primary Key

A primary key is a unique identifier used to identify each record in a table. No two rows can have the same primary key.

Example

Student ID can be used as a primary key.


6. Relationship

A relationship connects tables in a database so they can share information.

Example

  • A Students table

  • A Courses table

These tables can be connected so each student is linked to a course.


7. Database Management System (DBMS)

A DBMS is software used to create, manage, and control databases.

Examples

  • MySQL

  • Oracle Database

  • Microsoft SQL Server


Simple Summary

TermMeaning
DatabaseCollection of organized data
TableStructure where data is stored
RowOne record of data
ColumnCategory of data
Primary KeyUnique identifier
RelationshipConnection between tables
DBMSSoftware used to manage databases

MySQL Workbench** to set up a database for Django:


## **1. Install MySQL Workbench**

* Download from [MySQL Workbench Downloads](https://dev.mysql.com/downloads/workbench/).
* Install it alongside MySQL Server.
* Open Workbench after installation.



## **2. Connect to MySQL Server**

1. Click **`+`** to create a new MySQL connection.
2. Fill in the details:

   * **Connection Name:** Any name (e.g., DjangoDB)
   * **Hostname:** `localhost` (if local)
   * **Username:** `root` or the MySQL user you created
   * **Password:** Click **Store in Vault** and enter the password
3. Click **Test Connection** → it should succeed.
4. Click **OK** to save the connection.


## **3. Create a New Database (Schema)**

1. Open the connection you just created.
2. On the **Navigator panel**, right-click **Schemas** → **Create Schema**.
3. Enter a database name (e.g., `school`)
4. Click **Apply**, review the SQL command, and click **Apply** again.

> This database will store all your Django tables.

## **4. Create a MySQL User (Optional but Recommended)**

1. Go to **Server → Users and Privileges**
2. Click **Add Account**
3. Fill in:

   * **Login Name:** `django_user`
   * **Authentication Type:** Standard
   * **Password:** `password123` (secure password)
4. Under **Schema Privileges**, select your database (`school`) → click **Apply**

---

## **5. Manage Tables Using Workbench**

1. Select your database under **Schemas**
2. Right-click **Tables → Create Table**
3. Define table name and columns (fields)
4. Set **Primary Key** for unique identification
5. Click **Apply** → Review SQL → Apply

> You can create tables here for testing, but Django will usually handle table creation via migrations.

---

## **6. Test the Connection**

* Open **SQL Editor**
* Run simple SQL queries to check the database:

```sql id="9vqldq"
USE school;
SHOW TABLES;
```

* This ensures your database is ready for Django.

---

## **7. Connect Django to Workbench Database**

In your Django `settings.py`:

```python id="1mhpu0"
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'school', # Your Workbench database
        'USER': 'django_user', # User you created
        'PASSWORD': 'password123', # Password for the user
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
```

✅ **Summary of Using MySQL Workbench for Django**

| Step | Action |
| ---- | --------------------------------- |
| 1 | Install Workbench and open it |
| 2 | Connect to MySQL Server |
| 3 | Create a new database (schema) |
| 4 | Create a database user (optional) |
| 5 | Optionally create tables |
| 6 | Test database with SQL queries |
| 7 | Connect Django in `settings.py` |

SQL with MySQL

SQL (Structured Query Language) is the language used to communicate with databases like MySQL. It lets you store, retrieve, update, and delete data in a database. Learning SQL is essential before working with Django, because Django uses SQL behind the scenes to interact with the database.


1. Creating a Database

You can create a database to store your tables.

CREATE DATABASE school;

2. Creating Tables

Tables store data in rows and columns.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    course VARCHAR(50)
);

3. Inserting Data

Add records to your table using INSERT.

INSERT INTO students (id, name, course)
VALUES (1, 'John', 'Computer Science');

4. Reading Data (SELECT)

Retrieve data from the table using SELECT.

SELECT * FROM students;
SELECT name, course FROM students;

5. Updating Data

Change existing data using UPDATE.

UPDATE students
SET course = 'Mathematics'
WHERE id = 1;

6. Deleting Data

Remove records using DELETE.

DELETE FROM students
WHERE id = 1;

7. Filtering Data

Use WHERE to filter records based on conditions.

SELECT * FROM students
WHERE course = 'Computer Science';

8. Sorting Data

Use ORDER BY to sort results.

SELECT * FROM students
ORDER BY name ASC;

9. Joining Tables

Combine data from multiple tables using JOIN.

SELECT students.name, courses.course_name
FROM students
JOIN courses ON students.id = courses.student_id;

10. Summary of Key SQL Commands

CommandPurpose
CREATE DATABASEMake a new database
CREATE TABLEMake a new table
INSERT INTOAdd data to a table
SELECTRead data
UPDATEChange data
DELETERemove data
WHEREFilter records
ORDER BYSort results
JOINCombine multiple tables


0 comments:

Post a Comment

 

BEST COMPUTER GUIDE Written by Abigail Odenigbo, Published @ 2014 by NOBIGDEAL(Ipietoon)