Basic MySQL Commands

Basic Mysql commands are select, update, delete, insert into. you can use them with parameters like where, order, group by, distinct and limit.

if you don`t have any idea what the Mysql database is please look at our older lessons : What is Database


Here is basic Mysql Commands:

SELECT FROM

This command used when you want to read some records. here is simple record table and example select command.

Table name is "Singers" , imagine this is the best of 5 singers toplist.

Number
Name
Surname
Gender
1
Alice
Smith
female
2
Britney
Spears
female
3
Mariah
Carey female
4
Michael
Jackson
male
5
Michael Crawford male

Here is mysql command and its result:

SELECT Name,Surname FROM singers

Name
Surname
Alice
Smith
Britney
Spears
Mariah
Carey
Michael
Jackson
Michael Crawford

Another command for reading names and genders

SELECT Name,Gender FROM singers

Name
Gender
Alice
female
Britney
female
Mariah
female
Michael
male
Michael male

Now we need to read everything about male singers (* means everthing)

SELECT * FROM singers WHERE Gender=`male`

Number Name Surname Gender
4 Michael Jackson
male
5 Michael Crawford male

Another example for just show you another where statement.

SELECT Name,Surname FROM singers WHERE Gender=`female` AND Number < 3

Name
Surname
Alice
Smith
Britney
Spears

 


 

DELETE FROM

This command can erase one ore more record from table.

Here is simple delete example:

DELETE FROM singers WHERE Number=3

Now "3.Mariah Carey" deleted from singer list.

 


 

UPDATE SET

Update command can changes one or more record.

UPDATE singers SET Gender=`male`

Now all records has changed on singers table. this is result table:

Number
Name
Surname
Gender
1
Alice
Smith
male
2
Britney
Spears
male
3
Mariah
Carey male
4
Michael
Jackson
male
5
Michael Crawford male

You need to use Numbers as possible. Becaus using names can be dangerous on WHERE clause. here is an example:

UPDATE singers SET Surname=`Jordan` WHERE name=`Michael`

Here is final result, Look at two michael changed unexpectedly.

Number
Name
Surname
Gender
1
Alice
Smith
male
2
Britney
Spears
male
3
Mariah
Carey male
4
Michael
Jordan
male
5
Michael Jordan male

 


INSERT INTO

Adds new record to mysql table:

example:

INSERT INTO Singers (`Name`,`Surname`)VALUES(`Julio`,`Iglesias`);

 

New record added to Singers table.

Number
Name
Surname
Gender
1
Alice
Smith
male
2
Britney
Spears
male
3
Mariah
Carey male
4
Michael
Jordan
male
5
Michael Jordan male
6
Julio
Iglesias

Notice: Number row is Automatic incremental property on it. and gender is empty because we ddin`t write it on INSERT INTO command.

Related: Mysql CommandsMysql SELECT FROMMysql DELETEMysql INSERT INTOMysql UPDATE SET

Comments about : Mysql tutorial