Archive for September, 2011

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.

How To Empty Postfix Mail Queue

September 26th, 2011

This command will delete one specific email from the mailq
mailq | tail +2 | grep -v ‘^ *(‘ | awk  ‘BEGIN { RS = “” } { if ($8 == “email@manoj.com” && $9 == “”) print $1 } ‘ | tr -d ‘*!’ | postsuper -d -

Deleting the postfix mail queue

I would get a ton of messages sent to the postfix queue which would all be sent out when I reconnected to the internet. Deleting the postfix mail Queue is suprisingly easy:

/usr/sbin/postsuper -d ALL

This command will delete all messages in the Postfix queue. If you need more selective deleting, this can use ‘man postsuper’ to find out all of the available options.

The other thing that helped with this was checking for a local network connection before doing the server checks. You can check it using below command.

/sbin/ifconfig | grep -i mask|grep -v 127.0.0.1 | awk {‘print $2′}

Thanks
Manoj

Creating a UTF-8 database

September 20th, 2011

1. You can create a UTF-8 database with binary UTF-8 collation.
CREATE DATABASE test CHARACTER SET utf8 COLLATE utf8_bin;

2. You will also need to set the Server Characterset to utf8. This can be done by adding the following in my.ini for Windows or my.cnf for other OS like linux. It has to be declared in the Server section, which is the section after [mysqld]:

[mysqld]
default-character-set=utf8

3. Select the database using below command
use database name

4. Use the status command to verify database character encoding information.

mysql> status;
————–
mysql  Ver 14.12 Distrib 5.0.85, for pc-linux-gnu (i686) using readline 5.1

Connection id:          2464150
Current database:       databasename
Current user:           manoj@192.168.200.83
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ”
Using delimiter:        ;
Server version:         5.5.8-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             192.168.2.85 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
TCP port:               3306
Uptime:                 15 days 1 hour 10 min 31 sec

Threads: 103  Questions: 539733512  Slow queries: 32331  Opens: 354  Flush tables: 1  Open tables: 237  Queries per second avg: 415.105

5. In some cases, the individual tables collation and character encoding may differ from the one that the database as a whole has been configured to use. Please use the command below to ensure all tables within your manoj database are correctly configured to use UTF-8 character encoding and binary UTF-8 collation:

use manoj;
show table status;

Check for the value listed under the Collation column, to ensure it has been set to utf8_bin (that is, case-sensitive) collation for all tables.
If not, then this can be changed by the following command, executed for each table in the manoj database:

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;

Please substitute the <tablename> above, with each table within the manoj database.

6. How to create database for utf8_bin
CREATE DATABASE Manoj CHARACTER SET utf8 COLLATE utf8_bin;

Thanks
Manoj