CodingNic

Databases

MySQL Introduction

Databases 38 min read

MySQL Introduction

MySQL Introduction

Another very popular relational database system is MySQL.

MySQL has been widely used for websites, business software, and backend systems for many years.

If you understand SQL and databases, learning MySQL becomes much easier.


What Is MySQL?

MySQL is an open-source relational database management system (RDBMS).

It stores data in tables and uses SQL to manage data.

Like PostgreSQL, MySQL usually runs as a database server that applications connect to.


Why Learn MySQL?

MySQL is important because it is:

  • widely used
  • beginner-friendly
  • fast for many workloads
  • common in web development
  • supported by many hosting providers
  • used in real businesses

Where MySQL Is Used

MySQL is used in:

  • websites
  • e-commerce platforms
  • content systems
  • dashboards
  • internal business tools
  • backend APIs

MySQL vs SQLite

SQLite

  • file-based
  • no server
  • great for local apps

MySQL

  • server-based
  • supports many users
  • production deployments
  • strong ecosystem

MySQL vs PostgreSQL

MySQL

  • very common in web hosting
  • popular beginner choice
  • strong performance for common apps

PostgreSQL

  • known for advanced features
  • powerful complex queries
  • strong standards support

Both are excellent choices.


Basic SQL in MySQL

Create table:

sql
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    active BOOLEAN
);

What Is AUTO_INCREMENT?

Automatically increases the id value for each new row.

Example ids:

text
1
2
3

Insert Data

sql
INSERT INTO users (name, active)
VALUES ('Tom', TRUE);

Read Data

sql
SELECT * FROM users;

Update Data

sql
UPDATE users
SET active = FALSE
WHERE id = 1;

Delete Data

sql
DELETE FROM users
WHERE id = 1;

Connecting from Python

A common library is:

text
mysql-connector-python

Install:

bash
pip install mysql-connector-python

Example:

python
import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user="root",
    password="secret",
    database="mydb"
)

Parameterized Query Example

python
cursor.execute(
    "SELECT * FROM users WHERE id = %s",
    (1,)
)

Use parameters instead of building SQL strings manually.


Important Concepts

Host

The server address.

Example:

text
localhost

User

Database account name.

Password

Account password.

Database

The selected database to use.


Common Beginner Errors

Server Not Running

MySQL service must be running.

Wrong Password

Check credentials carefully.

Wrong Database Name

Use an existing database.

Missing Driver

Install the connector package.


Code Along

Design two tables for an online store in MySQL.

Example:

  • products
  • orders

Mini Challenge

Design a movie app.

Tasks:

  1. Create table name
  2. Choose 4 columns
  3. Pick a primary key
  4. Write one INSERT query
  5. Write one SELECT query

Real World Use Case

MySQL powers websites, e-commerce stores, CMS systems, dashboards, and backend business apps.


Quiz

  1. What is MySQL?
  2. How is MySQL different from SQLite?
  3. What does AUTO_INCREMENT do?
  4. What Python package connects to MySQL?
  5. Why is MySQL popular?

Assignment

Write a schema for a library system with books, members, and loans tables.


Summary

You learned what MySQL is, how it compares with SQLite and PostgreSQL, and how Python connects to it.