I'm a newbie in plugin development. I tried following this documentation: https://codex.wordpress.org/Function_Reference/register_activation_hook
I've created a function on the main plugin file (wp-content/plugin-folder/plugin.php). It creates the MySQL tables upon plugin activation.
CODE:
<?php
function create_db() {
global $wpdb;
$table_names = [
$wpdb->prefix . "banks",
$wpdb->prefix . "roles",
$wpdb->prefix . "levels",
$wpdb->prefix . "training_areas",
$wpdb->prefix . "disciplines",
$wpdb->prefix . "themes",
$wpdb->prefix . "subjects",
$wpdb->prefix . "fields",
$wpdb->prefix . "modalities",
$wpdb->prefix . "difficulties",
$wpdb->prefix . "questions"
];
$charset_collate = $wpdb->get_charset_collate();
$sqls = [
"CREATE TABLE $table_name[0](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[1](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[2](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[3](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[4](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[5](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
discipline_id INT(9) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (discipline_id) REFERENCES $table_name[4](id)
) $charset_collate;",
"CREATE TABLE $table_name[6](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[7](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[8](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[9](
id INT(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;",
"CREATE TABLE $table_name[10](
id INT(9) NOT NULL AUTO_INCREMENT,
code string NOT NULL,
question VARCHAR(255) NOT NULL,
bank_id INT(9),
role_id INT(9),
level_id INT(9),
training_area_id INT(9),
theme_id INT(9),
subject_id INT(9),
field_id INT(9),
modality_id INT(9),
dificulty_id INT(9),
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (bank_id) REFERENCES $table_name[0](id),
FOREIGN KEY (role_id) REFERENCES $table_name[1](id),
FOREIGN KEY (level_id) REFERENCES $table_name[2](id),
FOREIGN KEY (training_area_id) REFERENCES $table_name[3](id),
FOREIGN KEY (theme_id) REFERENCES $table_name[5](id),
FOREIGN KEY (subject_id) REFERENCES $table_name[6](id),
FOREIGN KEY (field_id) REFERENCES $table_name[7](id),
FOREIGN KEY (modality_id) REFERENCES $table_name[8](id),
FOREIGN KEY dificulty_id) REFERENCES $table_name[9](id),
) $charset_collate;"
];
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
foreach ($sqls as $sql) {
dbDelta($sql);
}
}
register_activation_hook(__FILE__, 'create_db');