This post is for plugin developers. Sometimes wordpress plugin developers need to create a table in database when the plugin is activated. For this purpose, we need to use register_activation_hook. Using this hook, we can execute a callback function when the plugin is activated.
Inside the callback function, we write the code to check if the needed table is already created. If not we create a new table. To create a new table for the plugin, we will not write direct mysql_connect code. Instead we use dbDelta function which is a part of wordpress assigned to execute sql commands. To use this function, we need to include wp-admin/includes/upgrade.php explicitly.
If callback_plugin() is the callback function for register_activation_hook, here is a sample code to create table in wordpress using php code.