Mysql Basics
Mysql Basics
and commands for beginners. You can use this as a reference to learn or prepare
for interviews. To create a PDF, simply copy this content into a Word or Google
Doc and export it as a PDF.
---
---
---
---
Example:
```sql
CREATE DATABASE my_database;
```
Example:
```sql
USE my_database;
```
Example:
```sql
SHOW DATABASES;
```
- To delete a database:
Example:
```sql
DROP DATABASE my_database;
```
Example:
```sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT
);
```
- **ALTER TABLE**: Modify an existing table (e.g., add columns, change data
types).
Example:
```sql
ALTER TABLE users ADD COLUMN address VARCHAR(255);
```
Example:
```sql
DROP TABLE users;
```
---
- **String Types**:
- `VARCHAR`: Variable-length string.
- `CHAR`: Fixed-length string.
- `TEXT`: Used for large text data.
---
Example:
```sql
SELECT * FROM users; -- Retrieves all columns from the 'users' table
SELECT name, email FROM users; -- Retrieves only 'name' and 'email' columns
```
Example:
```sql
SELECT * FROM users WHERE age > 25;
```
Example:
```sql
INSERT INTO users (name, email, age) VALUES ('John Doe', 'john.doe@example.com',
30);
```
Example:
```sql
UPDATE users SET age = 31 WHERE name = 'John Doe';
```
Example:
```sql
DELETE FROM users WHERE age < 20;
```
---
Example:
```sql
SELECT * FROM users WHERE age BETWEEN 18 AND 30;
```
Example:
```sql
SELECT * FROM users WHERE age > 20 AND name = 'John Doe';
```
Example:
```sql
SELECT * FROM users ORDER BY age DESC;
```
---
Example:
```sql
SELECT users.name, orders.amount
FROM users
INNER JOIN orders ON users.id = orders.user_id;
```
Example:
```sql
SELECT users.name, orders.amount
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
```
Example:
```sql
SELECT users.name, orders.amount
FROM users
RIGHT JOIN orders ON users.id = orders.user_id;
```
You can simulate it by combining **LEFT JOIN** and **RIGHT JOIN** using `UNION`.
---
Example:
```sql
SELECT COUNT(*) FROM users;
```
Example:
```sql
SELECT SUM(amount) FROM orders;
```
- **AVG()**: Calculates the average of a numeric column.
Example:
```sql
SELECT AVG(age) FROM users;
```
Example:
```sql
SELECT MIN(age), MAX(age) FROM users;
```
Example:
```sql
SELECT age, COUNT(*) FROM users GROUP BY age;
```
Example:
```sql
SELECT age, COUNT(*) FROM users GROUP BY age HAVING COUNT(*) > 1;
```
---
## **9. Indexing**
### 9.1. What are Indexes?
- An **index** is a data structure that improves the speed of data retrieval
operations on a table at the cost of additional space and reduced performance on
data modification operations (INSERT, UPDATE, DELETE).
Example:
```sql
CREATE INDEX idx_name ON users (name);
```
Example:
```sql
DROP INDEX idx_name ON users;
```
---
## **10. Normalization**
### 10.1. What is Normalization?
- **Normalization** is the process of organizing data to reduce redundancy and
improve data integrity. It involves dividing a database into tables and creating
relationships between them.
### 10.2. Normal Forms (NF)
- **1NF (First Normal Form)**: Ensures that the table has no repeating groups or
arrays.
- **2NF (Second Normal Form)**: Ensures that the table is in 1NF and that all
non-key attributes are fully dependent on the primary key.
- **3NF (Third Normal Form)**: Ensures that the table is in 2NF and that there
is no transitive dependency.
---
## **11. Conclusion**
- MySQL is a powerful, widely used relational database system. Mastering basic
SQL commands, data manipulation, and understanding how to organize and query
data is essential for database management and application development. This
guide covers foundational concepts that will help you get started with MySQL.
---
---
This guide provides a basic overview of MySQL concepts and commands. You can
format it into a PDF using any document editor or save it directly as a PDF. Let
me know if you need further details or more examples on any specific topic!