MySQL

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.

1

  • After you start the  MySQL server, you can connect to the program using a client to work in it.

2

3.png

  • 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.

show databases;

4

  • Then I want to create a new database to store my tables.

create database <database-name>

5

It has successfully created a new database.

6.png

  • Next I want to add tables to the database. Before that I have to select a database to work with.

use <database-name>

7.png

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> )

8.png

  • This will create a new table inside our database. You can view your tables using the below command,

show tables

9

  • Then we can Insert data in to our new table.

insert into <table-name> values (<cloumn1-value>,<cloumn2-value>,..,<columnn-value>)

10.png

  • 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.

11

  • 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>

12.png

It has successfully changed the value.

13

  • Then let’s look at deleting a row from a table.

delete from <table-name> where <column-name>=<searching-value>

14.png

It has successfully removed the specific row from the table.

15.png

  • 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>

16

17

It has successfully removed the table from the database.

18

  •  Next I want to remove the database from the server. It’s same as dropping a table from the database.

drop database <database-name>

19

20

It has successfully removed the database from the server.

21

  • After you are done working with the MySQL server you can logout from the session and shutdown the server using the following commands.

exit

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’

22

23

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!

Advertisements

2 thoughts on “MySQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s