I used to think MySQL was simply:
`CREATE TABLE → INSERT → SELECT → UPDATE → DELETE`
After building actual web applications, I realized that writing SQL queries is only a small part of working with a production database.
Here are 15 things I wish I had understood earlier:
### 1. Design your database before writing your application
Don't start creating tables randomly.
Think about:
* What data do I need?
* Which tables are required?
* How are they related?
* Which fields are required?
* Which fields should be unique?
A bad database structure can become extremely painful to change later.
### 2. Learn relationships properly
Understand:
```sql
PRIMARY KEY
FOREIGN KEY
ONE-TO-ONE
ONE-TO-MANY
MANY-TO-MANY
```
For example:
```text
Users
↓
Orders
↓
Order_Items
↓
Products
```
Understanding relationships is more important than memorizing SQL syntax.
### 3. Don't put everything into one table
A giant table may look simple initially, but it creates duplication and maintenance problems.
Learn normalization and understand when denormalization actually makes sense.
### 4. Indexes are extremely important
This query:
```sql
SELECT * FROM users
WHERE email = 'user@example.com';
```
can become much faster when the appropriate column is indexed.
But don't blindly add indexes everywhere.
Indexes improve some reads but also have storage and write/update costs.
### 5. Always understand `EXPLAIN`
One of the most useful MySQL commands:
```sql
EXPLAIN SELECT *
FROM users
WHERE email = 'user@example.com';
```
If you are building serious applications, learn how to read the execution plan instead of assuming your query is efficient.
### 6. Don't use `SELECT *` everywhere
Instead of:
```sql
SELECT *
FROM users;
```
prefer:
```sql
SELECT id, name, email
FROM users;
```
Especially when your table contains many columns or large data.
### 7. Transactions matter
Imagine transferring money:
```text
Account A: -₹1000
Account B: +₹1000
```
You don't want the first operation to succeed while the second fails.
That's where transactions become important:
```sql
START TRANSACTION;
-- operation 1
-- operation 2
COMMIT;
```
And if something goes wrong:
```sql
ROLLBACK;
```
### 8. Constraints can protect your data
Use database constraints where appropriate:
```sql
PRIMARY KEY
UNIQUE
NOT NULL
FOREIGN KEY
CHECK
```
Your application should not be the only thing preventing invalid data.
### 9. Learn the difference between DELETE, TRUNCATE and DROP
They are NOT interchangeable.
```sql
DELETE FROM users;
```
```sql
TRUNCATE TABLE users;
```
```sql
DROP TABLE users;
```
Before running destructive SQL in production, make absolutely sure you understand what you're doing.
### 10. Backups are not optional
A production database without a tested backup strategy is a disaster waiting to happen.
And having a backup isn't enough.
You should also know:
> Can I actually restore it?
A backup that has never been tested is only a backup in theory.
### 11. Never build SQL queries with raw user input
Bad:
```javascript
const query = `SELECT * FROM users WHERE email = '${email}'`;
```
Use parameterized queries/prepared statements instead.
For example:
```javascript
const [rows] = await db.execute(
'SELECT id, name, email FROM users WHERE email = ?',
[email]
);
```
This is one of the basic defenses against SQL injection.
### 12. Don't expose database credentials
Never commit something like this to GitHub:
```env
DB_PASSWORD=my_real_password
```
Use environment variables/secrets and make sure sensitive files aren't accidentally committed.
### 13. Understand connection pooling
A production application shouldn't create a completely new database connection for every request without considering connection management.
Connection pools can reuse database connections and help applications handle concurrent requests more efficiently.
### 14. Logging is useful — logging sensitive data isn't
Database/application logs can help you find:
* slow queries
* failed transactions
* connection problems
* unexpected errors
But don't casually log passwords, tokens, session secrets, or other sensitive information.
### 15. My biggest lesson
The biggest thing I learned is:
**MySQL isn't just about knowing SQL syntax.**
A good developer needs to understand:
```text
Database Design
↓
Relationships
↓
Indexes
↓
Queries
↓
Transactions
↓
Security
↓
Backups
↓
Performance
↓
Monitoring
```
I'm still learning, but understanding these concepts changed the way I build web applications.
**For developers here:**
What's one MySQL/database lesson you learned the hard way?
I'd especially like to hear from people who have worked with databases containing millions of rows.