SQL
1. Connect to MySQL
To connect to a MySQL database from the command line:
bash
Copy code
mysql -h <RDS-endpoint> -u <username> -p
<RDS-endpoint>: Replace with your RDS instance endpoint (e.g.,
mydbinstance.xxxxxxxxxxx.us-west-2.rds.amazonaws.com).
<username>: Your MySQL username (e.g., admin).
After typing the command, it will ask for your password.
2. List Databases
To see all the databases on the MySQL server:
SHOW DATABASES;
3. Create a Database
To create a new database:
CREATE DATABASE mydatabase;
4. Select a Database
To use a specific database:
USE mydatabase;
5. Create a Table
To create a table within a selected database:
CREATE TABLE customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
6. Show Tables
To list all tables in the current database:
SHOW TABLES;
7. Describe a Table
To see the structure (schema) of a specific table:
DESCRIBE customers;
8. Insert Data into a Table
To insert data into a table:
INSERT INTO customers (name, email)
9. Select Data from a Table
To select and view data from a table:
SELECT * FROM customers;
10. Update Data in a Table
To update existing records in a table:
UPDATE customers
WHERE name = 'John Doe';
11. Delete Data from a Table
To delete records from a table:
DELETE FROM customers
WHERE name = 'John Doe';
12. Drop a Table
To delete a table and its data permanently:
DROP TABLE customers;
13. Drop a Database
To delete a database and all of its contents:
DROP DATABASE mydatabase;
14. Alter Table (Modify Table Structure)
To add a new column to an existing table:
ALTER TABLE customers
ADD phone_number VARCHAR(20);
To modify an existing column's data type:
ALTER TABLE customers
MODIFY email VARCHAR(255);
To delete a column:
ALTER TABLE customers
DROP COLUMN phone_number;