- Connect to MySQL server:
mysql -u [username] -p
- Show databases:
SHOW DATABASES;
- Create database:
CREATE DATABASE [database_name];
- Use database:
USE [database_name];
- Show tables:
SHOW TABLES;
- Create table:
CREATE TABLE [table_name] ([column_definitions]);
- Insert data:
INSERT INTO [table_name] ([columns]) VALUES ([values]);
- Select data:
SELECT * FROM [table_name];
- Update data:
UPDATE [table_name] SET [column] = '[new_value]' WHERE [condition];
- Delete data:
DELETE FROM [table_name] WHERE [condition];
- Drop table:
DROP TABLE [table_name];
- Drop database:
DROP DATABASE [database_name];
- Join tables (Inner Join):
SELECT * FROM [table1] JOIN [table2] ON [table1].[column] = [table2].[column];
- Left Join:
SELECT * FROM [table1] LEFT JOIN [table2] ON [table1].[column] = [table2].[column];
- Right Join:
SELECT * FROM [table1] RIGHT JOIN [table2] ON [table1].[column] = [table2].[column];
- Full Outer Join (via Union):
SELECT * FROM [table1] LEFT JOIN [table2] ON [table1].[column] = [table2].[column] UNION SELECT * FROM [table1] RIGHT JOIN [table2] ON [table1].[column] = [table2].[column];
- Cross Join:
SELECT * FROM [table1] CROSS JOIN [table2];
- Group data:
SELECT [column], COUNT(*) FROM [table] GROUP BY [column];
- Order data:
SELECT * FROM [table] ORDER BY [column] ASC/DESC;
- Create index:
CREATE INDEX [index_name] ON [table_name]([column_name]);
- Add foreign key:
ALTER TABLE [table_name] ADD FOREIGN KEY ([column_name]) REFERENCES [other_table]([column_name]);
Note: Replace
[username]
,[database_name]
,[table_name]
,[column_definitions]
,[columns]
,[values]
,[new_value]
,[condition]
,[size]
,[column]
,[table1]
,[table2]
,[index_name]
, and[other_table]
with your specific details.