Skip to main content

How to Create table in database in wordpress?

Create table in database in wordpress


<?php
/*=====================Create table in wordpress database====================*/

  /* ++++++ Created user_tbl table +++++++*/

function create_users(){

    global $wpdb;

    $create_cases_table_name = $wpdb->prefix . "users";

    $create_cases_table_query = "CREATE TABLE $create_cases_table_name (

ID mediumint(9) NOT NULL AUTO_INCREMENT,

status ENUM('0', '1') DEFAULT '1' COMMENT '1 = active 0 = not active',

type ENUM('google', 'facebook', 'apple') NOT NULL  COMMENT 'google, facebook, apple',

first_name Varchar(255) NULL,

last_name Varchar(255) NULL,

email Varchar(255) NULL,                  

device_type LONGTEXT NOT NULL,

created TIMESTAMP DEFAULT '0000-00-00 00:00:00',

updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

UNIQUE KEY ID (ID)

    ) $charset_collate;";

    

     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

    dbDelta( $create_cases_table_query );

}

     add_action('admin_init','create_users');



    

/* ++++++ Created podcast table +++++++*/

function create_podcast(){

  global $wpdb;

  $create_user_podcast = $wpdb->prefix . "podcasts";

  $create_user_podcast_table_query = "CREATE TABLE $create_user_podcast (

ID mediumint(9) NOT NULL AUTO_INCREMENT,

user_id Int NOT NULL,

podcast_id Varchar(255) NOT NULL,

is_in_favorites ENUM('0', '1') DEFAULT '0' COMMENT '0 = No 1 = Yes',

rating Int NOT NULL,

views Int NOT NULL,

created TIMESTAMP DEFAULT '0000-00-00 00:00:00',

updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

UNIQUE KEY ID (ID)

  ) $charset_collate;";

  require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

  dbDelta( $create_podcast );

}

add_action('admin_init','create_podcast');




/*=========================How do you set a default value for a MySQL Datetime column?================*/

ALTER TABLE `test_table`

CHANGE COLUMN `created_dt` `created_dt` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',

CHANGE COLUMN `updated_dt` `updated_dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;


  

/*=========================insert created date by trigger================*/

CREATE TRIGGER trig_test_table_insert BEFORE INSERT ON `test_table`

FOR EACH ROW SET NEW.created_dt = NOW();

Comments

Popular posts from this blog

How to check date format in PHP?

Check date format in PHP function isCorrectDateFromat($date){     if(!empty($date)){         $dateString = $date; // Replace this with your date string         $format = "Y-m-d"; // Replace this with your expected date format         $dateTime = DateTime::createFromFormat($format, $dateString);         if ($dateTime === false) { /*             echo "The date is not in the correct format."; */         } else {             $errors = DateTime::getLastErrors();             if (empty($errors)) { /*                 echo "The date is in the correct format."; */                 return true;             } else { /*                 echo "...