DEV Community

Cover image for MySQL Basic Knowledge Points
Lanqi Gao
Lanqi Gao

Posted on

MySQL Basic Knowledge Points

What is MySQL?

MySQL is a widely used, open-source relational database management system (RDBMS). Besides relational database there are non relational database the differences are listed below.

The difference between relational database ot non-relational databases

Feature Relational (RDBMS) Non-Relational (NoSQL)
Data Model Tables (rows + columns) Documents, key-value, graphs
Schema Rigid (must define in advance) Flexible / dynamic
Joins Supported Not typical (uses nesting or refs)
Scalability Vertical Horizontal
Transactions Strong (ACID) Weaker (BASE/Eventual Consistency)
Best for Structured, consistent data Unstructured, large-scale data
Examples MySQL, PostgreSQL MongoDB, Redis, Cassandra

Basic usage of MySQL

1: Connect to MySQL

From the command line:

mysql -u root -p 
Enter fullscreen mode Exit fullscreen mode

-u root: login with the root user

-p: it will prompt you to enter the password

From Python (optional):

import mysql.connector conn = mysql.connector.connect( host="localhost", user="root", password="your_password", database="school_db" ) 
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation: Before doing anything with MySQL, you need to connect. The CLI is the most direct way for local development.

Database Operations

Create a new database:

CREATE DATABASE school_db; 
Enter fullscreen mode Exit fullscreen mode

View all databases:

SHOW DATABASES; 
Enter fullscreen mode Exit fullscreen mode

Delete a database:

DROP DATABASE test_db; 
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation: These commands let you manage the top-level containers of your data.

Table Operations

Create a table:

USE school_db; CREATE TABLE students ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), age INT, email VARCHAR(100) ); 
Enter fullscreen mode Exit fullscreen mode

View table structure:

DESCRIBE students; 
Enter fullscreen mode Exit fullscreen mode

Modify table (add a column):

ALTER TABLE students ADD gender VARCHAR(10); 
Enter fullscreen mode Exit fullscreen mode

Delete table:

DROP TABLE students; 
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation: This is about creating and modifying the structure inside your database.

Data Operations (CRUD)

Insert data:

INSERT INTO students (name, age, email) VALUES ('Alice', 20, 'alice@example.com'); 
Enter fullscreen mode Exit fullscreen mode

Query data:

SELECT * FROM students; 
Enter fullscreen mode Exit fullscreen mode

Update data:

UPDATE students SET age = 21 WHERE name = 'Alice'; 
Enter fullscreen mode Exit fullscreen mode

Delete data:

DELETE FROM students WHERE name = 'Alice'; 
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation: These are the most commonly used commands—managing the actual content of your tables.

Conditional Queries, Sorting, Pagination, Aggregation

Conditional query:

SELECT * FROM students WHERE age > 18; 
Enter fullscreen mode Exit fullscreen mode

Sorting:

SELECT * FROM students ORDER BY age DESC; 
Enter fullscreen mode Exit fullscreen mode

Pagination (show 10 results starting from the 21st):

SELECT * FROM students LIMIT 10 OFFSET 20; 
Enter fullscreen mode Exit fullscreen mode

Aggregation (average age):

SELECT AVG(age) AS avg_age FROM students; 
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation: These help you refine and summarize your results, which is crucial for reports, analytics, or APIs.

Image description

Image description

Image description

Image description

Image description

Top comments (0)