Tutorial
a reference video: [Link]
to run MySQL on my pc open cmd and type this command:
1 mysql -u root -p
it will ask for password, use the one that you set.
in my case it is: karmanya
Creating a database
To see the all the databases available:
1 show databases;
To create a database:
1 create database <name>;
Now, to select the database:
1 use <name>;
Create a Table
Once the database is selected, we can create a table:
1 CREATE TABLE employee(
2 -> <Name of coloumn 1> <Data type>,
3 -> <Name of coloum 2> varchar(),
4 -> );
Data type includes: int/float/bool/list/tuple and etc.
Note: str is NOT a datatype
MySQL doesn’t have str .
👉 Use VARCHAR , CHAR , INT , etc.
Also note, that it is optional to specify the both data type or varchar.
CONSTRAINTS
To check the structure of the table
1 DESC <table name>;
DESC stands for describe , it shows the following:
Inserts records in a Table
After creating a blank structure of a table we would like to store data in it.
INSERT INTO: Command used to insert a row in the specified table
1 INSERT INTO <table name> VALUES
2 -> (<value1>,>value2>,<value3>,......<value n>)
for example:
Select
Command used to Fetch/View data from the table.
1 SELECT * FROM <table name>;
the * means that you are selecting everything.
This, command will allow you to see the contents of table like this:
let's say we only want to show only one column in the table
1 SELECT EmpID FROM employee;
for more than two coloumns:
1 SELECT EmpID, EmpName FROM employee;
Selecting the DISTINCT statement
let's say from this command we get:
1 SELECT EmpAGE FROM employee;
but from the DISTINCT function the duplicate values are erased.
WHERE clause
Operators for WHERE clause:
= , > , < , >= , <= , <> , BETWEEN , LIKE , IN
Alter Table
Add a column
1 ALTER TABLE employee
2 -> ADD <column_name> <constraint> ;
constraints are optional
Delete a cloumn
1 ALTER TABLE employee
2 -> DROP COLUMN <column_name>;
Modify a Table
we will change a column type
1 ALTER TABLE employee
2 -> MODIFY COLUMN <column_name> <constraint>;
Functions
Sum() function
1 SELECT SUM(<column_name>)
2 -> FROM <table_name>;
Avg() function
1 SELECT AVG(<column_name>)
2 -> FROM <table_name>
Count() function
1 SELECT COUNT(<column_name>)
2 -> FROM <table_name>;
we can also use the condition to use the count function:
TO COUNT THE NUMBER OF COLUMNS IN A TABLE:
1 SELECT COUNT(*)
2 FROM <table_name>;
§
Max()
1 SELECT MAX(<column_name>)
2 FROM <table_name>;
§
Min()
1 SELECT MIN(<column_name>)
2 FROM <table_name>;
§
Drop The Table
1 DROP TABLE <table_name>;