How to create custom tables for wordpress plugin ?

After creating plugin and writing required hooks, we can create tables.

We can call these function inside acivation hook. So once we activate the plugin, table creations will be done.

/* Plugin Database Creation Starts Here, call this function inside a activation hook.
Table name :mynewtable
*/
function db_initialize(){
global $wpdb; // Global object
$charset_collate = ”;
if ( ! empty($wpdb->charset) )
$charset_collate = “DEFAULT CHARACTER SET $wpdb->charset”;
if ( ! empty($wpdb->collate) )
$charset_collate .= ” COLLATE $wpdb->collate”;

$tables = $wpdb->get_results(“show tables like ‘{$wpdb->prefix}mynewtable'”);

// Checking wheather our table is exist or not

if (!count($tables)) // if not existing
$wpdb->query(“CREATE TABLE {$wpdb->prefix}mynewtable (
myid bigint(20) unsigned NOT NULL auto_increment, // fields
name varchar(255) default NULL,
mail varchar(255) default NULL,
age bigint(20),
address varchar(255) default NULL,
PRIMARY KEY (myid) // myid setting as primary key
) $charset_collate;”);

}