- Log In to MySQL: To log in to MySQL, open your terminal and run the following command. Replace
<username>
with your MySQL username, and you will be prompted for your password.
C:> mysql -u <username> -p
- Switch to a Database: After logging in, you can switch to a specific database using the
USE
command.
mysql> USE <database_name>;
- Show Databases: To list all available databases, you can use the following command:
mysql> SHOW DATABASES;
- Create a Database: To create a new database, use the
CREATE DATABASE
command:
mysql> CREATE DATABASE <database_name>;
- Create a Table: To create a new table within a database, use the
CREATE TABLE
command:
mysql> CREATE TABLE <table_name> (
column1 datatype,
column2 datatype,
...
);
- Insert Data into a Table: You can insert data into a table using the
INSERT INTO
command:
mysql> INSERT INTO <table_name> (column1, column2, ...)
VALUES (value1, value2, ...);
- Select Data from a Table: To retrieve data from a table, use the
SELECT
statement:
mysql> SELECT * FROM <table_name>;
- Update Data in a Table: To update existing data in a table, use the
UPDATE
statement:
mysql> UPDATE <table_name>
SET column1 = value1, column2 = value2, ...
WHERE condition;
- Delete Data from a Table: To delete data from a table, use the
DELETE
statement:
mysql> DELETE FROM <table_name> WHERE condition;
- Show Table Structure: To view the structure of a table, you can use the
DESCRIBE
orSHOW COLUMNS
command:
mysql> DESCRIBE <table_name>;
mysql> SHOW COLUMNS FROM <table_name>;
- Show Tables: you can use the SHOW TABLES command to list all the tables in the currently selected database, USE :
mysql> SHOW TABLES;
- Exit MySQL: To exit the MySQL command-line client, you can type:
mysql> EXIT;