Today I’m going to write about MySQL, which is an open source data base which can be used with any Operating System to work with databases. It will help you to create, update, delete databases, tables and also to work with data inside those tables. I’m using MySQL on Ubuntu and I have set aliases to start and stop MySQL too. Let’s see some of the basic commands and how to execute them in MySQL through the terminal.
- First of all we have to start the MySQL server installed on your computer. This can be done using the following command,
sudo /etc/init.d/mysql.server start
But I’m using an alias to do this.
alais mysqlon=’sudo /etc/init.d/mysql.server start’
So using the alias I can simply start MySQL server by just typing ‘mysqlon’ command on the terminal.
- After you start the MySQL server, you can connect to the program using a client to work in it.
- After you successfully logged in to the MySQL, now you can execute queries and start working with the data.
- Now let’s see how to view the existing databases in the server. If you are using the server for the first time, then there will be only few default databases as below.
- Then I want to create a new database to store my tables.
create database <database-name>
It has successfully created a new database.
- Next I want to add tables to the database. Before that I have to select a database to work with.
Now we are inside the database. So now we can create tables and do other stuff so on.
- Next I’m going to create a table inside my new database.
create table <table-name> (
<column1-name> <data-type>(<size>) <other-information> ,
<column2-name> <data-type>(<size>) <other-information> ,
<columnn-name> <data-type>(<size>) <other-information> )
- This will create a new table inside our database. You can view your tables using the below command,
- Then we can Insert data in to our new table.
insert into <table-name> values (<cloumn1-value>,<cloumn2-value>,..,<columnn-value>)
- To view the data inside a table, we can use the following query.
select <column-name>,<another-cloumn-name> from <table-name> where <column-name>=<searching-value>
Here we can use ‘*’ to get all columns from a specific row. Also if you want to get all the data of the table, then remove the ‘where’ clause as well.
- Now let’s see how to update an existing value in a table.
update <table-name> set <column-name>=<new-value> where <column-name>=<searching-value>
It has successfully changed the value.
- Then let’s look at deleting a row from a table.
delete from <table-name> where <column-name>=<searching-value>
It has successfully removed the specific row from the table.
- Now I want to remove the table from the database. The query is like below to remove a table from the database,
drop table <table-name>
It has successfully removed the table from the database.
- Next I want to remove the database from the server. It’s same as dropping a table from the database.
drop database <database-name>
It has successfully removed the database from the server.
- After you are done working with the MySQL server you can logout from the session and shutdown the server using the following commands.
sudo /etc/init.d/mysql.server stop
But I have created an alias to make the server shutdown process easy.
alais mysqloff=’sudo /etc/init.d/mysql.server stop’
From this text I showed you how to start the MySQL server, connect as a client, how to execute basic queries in MySQL and then how to shutdown the server. Hope this text will help you to understand basic things about the MySQL. Hope to see you soon with another interesting topic. Thank You!