0% found this document useful (0 votes)
16 views6 pages

Mysql Basics

Uploaded by

raylangivens841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Mysql Basics

Uploaded by

raylangivens841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Here’s a comprehensive guide to **MySQL Basics**, covering essential concepts

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.

---

# **MySQL Basics - A Quick Guide**

---

## **1. Introduction to MySQL**


### 1.1. What is MySQL?
- **MySQL** is an open-source relational database management system (RDBMS)
based on SQL (Structured Query Language). It is used to store, manage, and
manipulate data in databases.
- MySQL is widely used in web applications, and it is a core component of the
**LAMP** stack (Linux, Apache, MySQL, PHP/Perl/Python).

### 1.2. Key Features of MySQL


- **Open-source**: Free to use and distribute.
- **Cross-platform**: Works on various platforms, including Windows, macOS, and
Linux.
- **ACID Compliant**: Ensures reliable transactions.
- **Scalability and Performance**: Efficient for both small and large-scale
applications.
- **Multi-User Access**: Supports concurrent access to databases by multiple
users.

---

## **2. MySQL Architecture**


- **MySQL Database**: Consists of a set of tables.
- **Tables**: Store data in rows and columns.
- **Schema**: A collection of database objects (tables, views, procedures,
etc.).
- **SQL Engine**: Handles the query processing.
- **Storage Engine**: Manages how data is stored and retrieved. Examples:
**InnoDB**, **MyISAM**.

---

## **3. Database Operations**


### 3.1. Creating and Managing Databases
- Use the **CREATE DATABASE** command to create a new database.

Example:
```sql
CREATE DATABASE my_database;
```

- Switch to the database using **USE**:

Example:
```sql
USE my_database;
```

- To list all databases:

Example:
```sql
SHOW DATABASES;
```
- To delete a database:

Example:
```sql
DROP DATABASE my_database;
```

### 3.2. Creating and Modifying Tables


- **CREATE TABLE**: Defines a new table within a 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);
```

- **DROP TABLE**: Deletes a table from the database.

Example:
```sql
DROP TABLE users;
```

---

## **4. Data Types in MySQL**


- **Numeric Types**:
- `INT`: Integer data type.
- `FLOAT`, `DOUBLE`: Floating-point numbers.
- `DECIMAL`: For exact precision (used for currency or financial data).

- **String Types**:
- `VARCHAR`: Variable-length string.
- `CHAR`: Fixed-length string.
- `TEXT`: Used for large text data.

- **Date and Time Types**:


- `DATE`: Stores date (YYYY-MM-DD).
- `TIME`: Stores time (HH:MM:SS).
- `DATETIME`: Stores both date and time (YYYY-MM-DD HH:MM:SS).
- `TIMESTAMP`: Stores date and time, typically used for tracking records.

---

## **5. Basic SQL Commands**


### 5.1. SELECT Query
- The **SELECT** statement is used to query data from one or more tables.

Example:
```sql
SELECT * FROM users; -- Retrieves all columns from the 'users' table
SELECT name, email FROM users; -- Retrieves only 'name' and 'email' columns
```

- Use **WHERE** to filter results.

Example:
```sql
SELECT * FROM users WHERE age > 25;
```

### 5.2. INSERT INTO Query


- The **INSERT INTO** statement is used to add new rows to a table.

Example:
```sql
INSERT INTO users (name, email, age) VALUES ('John Doe', 'john.doe@example.com',
30);
```

### 5.3. UPDATE Query


- The **UPDATE** statement is used to modify existing records in a table.

Example:
```sql
UPDATE users SET age = 31 WHERE name = 'John Doe';
```

### 5.4. DELETE Query


- The **DELETE** statement removes records from a table.

Example:
```sql
DELETE FROM users WHERE age < 20;
```

---

## **6. Filtering and Sorting Data**


### 6.1. WHERE Clause
- Use the **WHERE** clause to filter data based on conditions.

Example:
```sql
SELECT * FROM users WHERE age BETWEEN 18 AND 30;
```

### 6.2. AND, OR Operators


- Combine multiple conditions with **AND** and **OR**.

Example:
```sql
SELECT * FROM users WHERE age > 20 AND name = 'John Doe';
```

### 6.3. ORDER BY


- Use **ORDER BY** to sort the result set in ascending or descending order.

Example:
```sql
SELECT * FROM users ORDER BY age DESC;
```

### 6.4. LIMIT


- Use **LIMIT** to restrict the number of rows returned.
Example:
```sql
SELECT * FROM users LIMIT 5;
```

---

## **7. Joins in MySQL**


### 7.1. INNER JOIN
- Combines rows from two or more tables based on a related column.

Example:
```sql
SELECT users.name, orders.amount
FROM users
INNER JOIN orders ON users.id = orders.user_id;
```

### 7.2. LEFT JOIN


- Returns all records from the left table, and matching records from the right
table.

Example:
```sql
SELECT users.name, orders.amount
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
```

### 7.3. RIGHT JOIN


- Returns all records from the right table, and matching records from the left
table.

Example:
```sql
SELECT users.name, orders.amount
FROM users
RIGHT JOIN orders ON users.id = orders.user_id;
```

### 7.4. FULL OUTER JOIN


- Returns all records when there is a match in either left or right table (MySQL
doesn't support FULL OUTER JOIN natively).

You can simulate it by combining **LEFT JOIN** and **RIGHT JOIN** using `UNION`.

---

## **8. Aggregating Data**


### 8.1. Aggregate Functions
- **COUNT()**: Returns the number of rows.

Example:
```sql
SELECT COUNT(*) FROM users;
```

- **SUM()**: Calculates the sum of a numeric column.

Example:
```sql
SELECT SUM(amount) FROM orders;
```
- **AVG()**: Calculates the average of a numeric column.

Example:
```sql
SELECT AVG(age) FROM users;
```

- **MIN()** and **MAX()**: Find the minimum and maximum values.

Example:
```sql
SELECT MIN(age), MAX(age) FROM users;
```

### 8.2. GROUP BY


- **GROUP BY** is used to group rows that have the same values in specified
columns.

Example:
```sql
SELECT age, COUNT(*) FROM users GROUP BY age;
```

### 8.3. HAVING


- The **HAVING** clause is used to filter results after grouping, similar to the
**WHERE** clause.

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).

### 9.2. Creating an Index


- Use **CREATE INDEX** to create an index on one or more columns.

Example:
```sql
CREATE INDEX idx_name ON users (name);
```

### 9.3. Dropping an Index


- Use **DROP INDEX** to remove an index.

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.

---

**End of MySQL Basics Guide**

---

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!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy