Archive for the ‘MySQL Basics’ category

How to insert data in MySQL Table

September 28th, 2011

Let’s start inserting data in the table created in our post, table name was employee, we also discussed how to check the table structure in one of our post

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we have created table

mysql> use demo;

Step 3) check the structure of the database

mysql> desc employee;
+————–+————–+——+—–+———+——-+
| Field        | Type         | Null | Key | Default | Extra |
+————–+————–+——+—–+———+——-+
| id           | int(11)      | YES  |     | NULL    |       |
| employeename | varchar(252) | YES  |     | NULL    |       |
| address      | varchar(256) | YES  |     | NULL    |       |
+————–+————–+——+—–+———+——-+
3 rows in set (0.07 sec)

so this tells us that the table has 3 columns id , employeename  and address , lets start by writing the insert statement  syntax used will be

Insert into employee(id ,employeename , address) values ( 1, ‘DemoEmployee’, ‘US’) ;

mysql> Insert into employee(id ,employeename , address) values ( 1, ‘DemoEmployee’, ‘US’) ;

Query OK, 1 row affected (0.02 sec)

Lets check if the record is been inserted properly.

mysql> select * from employee;

+——+————–+———+

| id   | employeename | address |

+——+————–+———+

|    1 | DemoEmployee | US      |

+——+————–+———+

1 row in set (0.00 sec)

Excellent we have successfully inserted out first record.

How to get list of tables in a MySQL database

September 28th, 2011

lets discuss now  how we can check  how many tables exist in a MySQL database .

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to check table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3) we will use command show tables ;

+—————-+
| Tables_in_demo |
+—————-+
| employee       |
+—————-+
1 row in set (0.00 sec)

this will list all the tables which are under demo database.

In our next post we will discuss how to insert data in a MySQL Table.

How to Check table structure in MySQL

September 28th, 2011

in our previous  post we discussed how to  add more columns in MySQL table , lets see how we can check table structure in MySQL.

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to check table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3) we have 2 methods of checking the table structure.

a) Using Desc , we will now check the table structure of table employee

mysql> desc employee;
+————–+————–+——+—–+———+——-+
| Field        | Type         | Null | Key | Default | Extra |
+————–+————–+——+—–+———+——-+
| id           | int(11)      | YES  |     | NULL    |       |
| employeename | varchar(252) | YES  |     | NULL    |       |
| address      | varchar(256) | YES  |     | NULL    |       |
+————–+————–+——+—–+———+——-+
3 rows in set (0.07 sec)

This command lists all the columns and data type used for each column.

b) Using show create table command

syntax : show create table tablename

mysql> show create table employee;

+———-+———————————————————————————————————————————————————————————+

| Table    | Create Table                                                                                                                                                                    |

+———-+———————————————————————————————————————————————————————————+

| employee | CREATE TABLE `employee` (

`id` int(11) default NULL,

`employeename` varchar(252) default NULL,

`address` varchar(256) default NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1 |

+———-+———————————————————————————————————————————————————————————+

1 row in set (0.00 sec)

This command gives us create table structure for table employee.

In next post we will discuss how to check how many tables exists in a database.

How to Create a Database in MySQL

September 27th, 2011

Once MySQL Server setup is done , lets start by creating a database , in our example we will see how to create a MySQL Database

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) Now we will be creating a database demo
mysql> create database demo;
Query OK, 1 row affected (0.00 sec)

Step 3) Checking if the database is created ;

mysql>show databases;

+——————–+
| Database           |
+——————–+
| information_schema |
| demo                                |
| test                                    |
+——————–+
3 rows in set (0.00 sec)

we can see that the database demo is there in the list . in next post we will see how to create tables in a MySQL.

How to create table in MySQL

September 27th, 2011

In Our previous post we discussed how to create a database in MySQL , lets see how to create tables in MySQL database.

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to create table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3)  Create table  lets assume our table will be called employee and it will have 2 columns  id and employeename we are starting with very basic table structure and then will be adding more columns to it.

mysql> create table employee (  id int , employeename varchar(252) );
Query OK, 0 rows affected (0.01 sec)

step 4) Lets check that if table is created.

mysql> show tables;
+—————-+
| Tables_in_demo |
+—————-+
| employee       |
+—————-+
1 row in set (0.00 sec)

so gr8 we are able to create our first table. in our next post we will discuss how to check the table structure and alter the table structure.

How to add more columns in exsiting MySQL table

September 27th, 2011

in our previous  post we discussed how to create table in MySQL Database , lets check now how we can add more columns to existing table .

Step 1) Login to MySQL

we will be using user : demo and password as demouser

mysql -u demo -p demouser

Step 2) use database in which we need to create table , as in our previous post we created database demo we will be using same database

mysql> use demo;

Step 3)  we created table employee , now we will be looking how to add more columns in existing table .lets add one more column address having data type varchar and length as 256

mysql> alter table employee add column address varchar(256) ;

Query OK, 0 rows affected (0.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

here we go we can see that the table is being altered . in our next post we will be discussing how to check the  existing table structure.