How to create database tables with a WP plugin

While writing WordPress plugins you`ll occasionally need a way of creating a database table with your plugin, to store data for your features, oprtions etc.. This approach is not the best but it does the work. Important: Always do house-keeping on plugin uninstall.

<?php
/*
 * Create database for SERP History
 */
global $wpdb;
if (!$wpdb->get_results("SELECT * FROM `wp_serphistory` WHERE 1")) {
 $sql = "CREATE TABLE IF NOT EXISTS `wp_serphistory` (
 `id` int(10) NOT NULL AUTO_INCREMENT,
 `blog_id` int(10) NOT NULL,
 `title` varchar(300) NOT NULL,
 `description` varchar(600) NOT NULL,
 `change_date` datetime NOT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
 dbDelta($sql);
}
?>