Step-by-Step Guide for MySQL Command Line
Step-by-Step Guide for Creating a Database and Working with MySQL Command Line
1. Install MySQL:
Windows: Download from the official MySQL website.
Linux: sudo apt update && sudo apt install mysql-server
MacOS: brew install mysql
2. Start MySQL:
Linux/MacOS: sudo service mysql start
Windows: MySQL service should start automatically.
3. Log into MySQL:
mysql -u root -p
4. Create a New Database:
CREATE DATABASE database_name;
5. Show Existing Databases:
SHOW DATABASES;
6. Select a Database:
USE database_name;
7. Create a Table:
CREATE TABLE table_name (
column1_name datatype constraints,
column2_name datatype constraints,
...
);
8. Insert Data into the Table:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
9. Select Data from the Table:
SELECT * FROM table_name;
10. Update Data in a Table:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
11. Delete Data from a Table:
DELETE FROM table_name
WHERE condition;
12. Drop a Table:
DROP TABLE table_name;
13. Show Table Structure:
DESCRIBE table_name;
14. Create a New User:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
15. Grant Privileges to a User:
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
16. Exit MySQL:
exit;
Common MySQL Command Summary:
- Login: mysql -u username -p
- Create Database: CREATE DATABASE database_name;
- Show Databases: SHOW DATABASES;
- Use Database: USE database_name;
- Create Table: CREATE TABLE table_name (column1 datatype, ...);
- Insert Data: INSERT INTO table_name (column1, column2) VALUES (...);
- Select Data: SELECT * FROM table_name;
- Update Data: UPDATE table_name SET column = value WHERE condition;
- Delete Data: DELETE FROM table_name WHERE condition;
- Drop Table: DROP TABLE table_name;
- Exit: exit;