MySQL basic commands part 4 | final one  

So here, we'll use different combinations of SELECT command, add new column and delete table and database.

As mentioned after SELECT word, comes names of attribute that we want to list. If want all of them then we put * . If we want more then one, then we separate their names by , sign.
For example:
if you want to list all people's names and surnames then you type: SELECT name, surname FROM person;
Same goes for names and birth dates, or more of those atributes, you just specify them in there...

next part of SELECT command is FROM place. In FROM place of command we are specifing from where to select those atributes. In our case it is from one table, table person. But for cases where we want to select atributes from more tables, then we specify more tables, in this part. Using this command for more tables i will explain in next series of mysql commands.

Next part of SELECT is WHERE. In where part we are putting condition that will affect rows. It is same as we used it with UPDATE command...

so if we want to select (show) name and surnames of people named edin we would use next command:
SELECT name, surname FROM person WHERE name='edin';
exmpl:


Also we could use some other functions named: COUNT, MIN, MAX, SUM, AVG in SELECT part of statement.
Here is example of one of them:
SELECT COUNT(*) FROM person WHERE name='edin';

No matter what you write inside () of COUNT, this select command will bring you back one column with number of rows where name is edin from table person.



Addin new column named age.
Lets sey we want to add new column where will be number that represents age of specific person. Adding column is in SELECT part of command, but mysql gives us ability to do calculations right in there. So it will be:

SELECT name, surname, height, weight, birth, YEAR(CURDATE())-YEAR(birth) AS age FROM person;



now you can do playing with SELECT command, for example in place of COUNT you can put MIN(birth) so you can see oldest date in birth column, or min(birth) for youngest.

Deleting datas: DELETE FROM person;
All datas will be deleted

Deleting table: DROP TABLE person;
Table person will be deleted weather it has or hasn't got datas

Deleting database: DROP DATABASE students;
Database will be deleted weather it has or hasn't got datas(tables with datas) in it

We have finished very basics of MySQL commands. These can be used within php, when writing it, or C, C++, C#, Pascal(Delphi) or almost every other programing language.

Now that you have learned basics, you can move to more advanced tutorials, that can be found all over internet (google.com, mysql.org) or inside mysql documentation that comes with mysql installation.

If you don't need right now that advanced knowlage it is good that you have read this, because you now know basic of mysql, and when we cover rest of mysql than you will fully know it;)

other parts about MySQL:
MySQL basic commands part 1
MySQL basic commands part 2
MySQL basic commands part 3
25.3.06 at 13:04 by ./mt

Post a Comment