Introduction to Databases and MySQL
1. Introduction to Databases
A database is an organized collection of data that can be accessed, managed, and updated. It
helps in storing and retrieving data efficiently, reducing redundancy, and improving data
security.
Examples:
- Library database for books, borrowers, and transactions.
- Online shopping websites storing customer and order details.
2. Types of Databases
Relational Databases:
- Organize data into tables with rows and columns.
- Examples: MySQL, PostgreSQL.
NoSQL Databases:
- Flexible structure for semi-structured or unstructured data.
- Examples: MongoDB, Firebase.
Relational databases are ideal for structured data (e.g., a table of customers and orders),
while NoSQL works well for dynamic, large-scale applications like social media feeds.
3. Introduction to MySQL
MySQL is an open-source relational database management system (RDBMS) widely used for
web applications, data analysis, and e-commerce. It uses SQL (Structured Query Language)
to manage databases and allows features like:
- Scalability.
- Security.
- Data integrity.
4. Key Database Concepts
Table: A container storing data in rows and columns (like a spreadsheet).
Columns: Define the structure of the table (e.g., cust_name in the Customer table).
Rows: Represent individual entries or records.
Primary Key: A unique identifier for records in a table (e.g., cust_id).
Foreign Key: A reference to a primary key in another table, enabling relationships between
tables (e.g., cust_id in Order table referencing Customer table).
5. SQL Basics
Types of SQL Commands:
1. DDL (Data Definition Language):
- Used to define, modify, or delete table structures.
- Commands: CREATE, ALTER, DROP.
2. DML (Data Manipulation Language):
- Used to insert, update, delete, or retrieve data.
- Commands: INSERT, UPDATE, DELETE, SELECT.
3. DCL (Data Control Language):
- Used to manage access permissions.
- Commands: GRANT, REVOKE.
6. MySQL Installation and Tools
Install MySQL on their systems (or use an online MySQL compiler).
Tools: MySQL Workbench, phpMyAdmin, or CLI.
7. Practical Example: Pizza Ordering System
Below are three tables to demonstrate the pizza ordering system:
Customer Table
cust_id (PK) cust_name cust_address
1 Alice Smith 123 Baker St
2 John Doe 456 Elm St
Pizza Table
pizza_id (PK) pizza_flavour pizza_price
1 Pepperoni $12.99
2 Margherita $10.99
Order Table
order_id (PK) cust_id (FK) pizza_id cust_address total_price
1 1 1 123 Baker St $12.99
2 2 2 456 Elm St $10.99
7. SQL Commands for Pizza Ordering System
Here are some SQL commands to manipulate the above database:
Create Customer Table
CREATE TABLE Customer (
cust_id INT NOT NULL PRIMARY KEY,
cust_name VARCHAR(100),
cust_address VARCHAR(255)
);
Insert Customer Data
INSERT INTO Customer (cust_id, cust_name, cust_address)
VALUES (1, 'Alice Smith', '123 Baker St');
Retrieve Data from Customer Table
SELECT * FROM Customer;
9. Applications of MySQL
E-commerce websites.
Inventory management.
School management systems.