← Back to Chapters

MySQL Introduction

? MySQL Introduction

⚡ Quick Overview

MySQL is an open-source relational database management system (RDBMS) based on Structured Query Language (SQL). It is widely used to store and manage data for various applications, from small websites to large-scale enterprise applications.

? Open Source ? Relational ? Scalable ⚡ High Performance ? Secure

? Key Concepts

  • Database: A collection of related data stored in tables.
  • Table: Organizes data in rows and columns, similar to a spreadsheet.
  • Row (Record): A single, complete entry of data in a table.
  • Column (Field): A specific attribute of the data (such as name or email).
  • SQL: The language used to interact with the database (create, insert, update, delete, query).

? MySQL Features

  • Open-Source: MySQL is free to use and available for various platforms.
  • Relational Database: It organizes data in tables with rows and columns, maintaining relationships between them.
  • Scalable: MySQL supports large amounts of data and is used by millions of applications worldwide.
  • High Performance: Known for its fast data retrieval and optimized query performance.
  • Secure: Supports authentication, access control, and SSL encryption.

? Syntax / Theory

Common SQL statements you will use with MySQL:

  • CREATE DATABASE – Defines a new database.
  • USE – Switches to the specified database.
  • CREATE TABLE – Creates a new table with columns and data types.
  • INSERT INTO – Adds new records to a table.
  • SELECT – Retrieves data from one or more tables.
? View Code Example (Basic Operations)
-- Creating a database
CREATE DATABASE my_database;

-- Selecting a database
USE my_database;

-- Creating a table named 'users'
CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100),
  email VARCHAR(100)
);

-- Inserting data into the 'users' table
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');

-- Querying all rows from the 'users' table
SELECT * FROM users;

? Example: Company Database

This example demonstrates how to create a simple company database to store employee information.

? View Code Example (Company Database)
-- Creating a database named 'company'
CREATE DATABASE company;

-- Using the 'company' database
USE company;

-- Creating an 'employees' table
CREATE TABLE employees (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100),
  position VARCHAR(100),
  salary DECIMAL(10, 2)
);

-- Inserting data into 'employees' table
INSERT INTO employees (name, position, salary)
VALUES ('Alice Johnson', 'Software Engineer', 70000.00);

-- Query to get all employees
SELECT * FROM employees;

? Live Output / Explanation

  • CREATE DATABASE: Defines a new database (for example, company).
  • USE: Switches to the specified database so that subsequent commands run in that context.
  • CREATE TABLE: Defines a new table with specific columns and data types.
  • INSERT INTO: Adds new records (rows) to a table.
  • SELECT: Retrieves data from one or more tables, based on the query.

When you run the final SELECT * FROM employees; query in a MySQL client, you will see a result table showing all employees, including the record for Alice Johnson that you inserted.

? Tips & Best Practices

  • Always back up your database before making changes or updates.
  • Ensure your table columns are properly indexed for faster data retrieval.
  • Normalize your database to minimize redundancy and improve data integrity.
  • Use meaningful names for databases, tables, and columns.
  • Limit direct access to production databases and use strong authentication.

? Try It Yourself

  • Try creating your own database and table (for example, a library database with a books table).
  • Insert multiple records and use different SQL queries to retrieve the data (filter by author, price, etc.).
  • Experiment with adding constraints like NOT NULL or DEFAULT values on different columns.
  • Update an existing record using UPDATE and then verify the change with SELECT.
  • Delete a specific record using DELETE and confirm it is removed.