Hey!
Welcome to our tutorial on working with the WordPress database using the wpdb class.
In this tutorial, we’ll be going over the basics of creating tables, adding, updating, and editing data in your WordPress site’s database.

First, let’s start with creating a table. To do this, we’ll use the $wpdb->query() method, which allows us to run any SQL query on the database. Here’s an example of how to create a table called ‘example_table’ with three columns: ‘id’, ‘name’, and ’email’:

global $wpdb;
$table_name = $wpdb->prefix . 'example_table';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
	id mediumint(9) NOT NULL AUTO_INCREMENT,
	name varchar(255) NOT NULL,
	email varchar(255) NOT NULL,
	PRIMARY KEY  (id)
	) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

Now that we have our table created, let’s move on to adding data to it. To do this, we’ll use the $wpdb->insert() method, which allows us to insert data into a specific table. Here’s an example of how to insert a new row into our ‘example_table’ with the name ‘John Smith’ and email ‘john@example.com’:

global $wpdb;
$table_name = $wpdb->prefix . 'example_table';
$wpdb->insert( 
$table_name, 
	array( 
	    'name' => 'John Smith', 
	    'email' => 'john@example.com' 
	)
);

Next, let’s move on to updating data in our table. To do this, we’ll use the $wpdb->update() method, which allows us to update specific rows in a table. Here’s an example of how to update the email of the row with the id of 1 in our ‘example_table’ to ‘john.smith@example.com’:

global $wpdb;
$table_name = $wpdb->prefix . 'example_table';
$wpdb->update( 
	$table_name, 
	array( 
	    'email' => 'john.smith@example.com' 
	), 
	array( 'id' => 1 )
);

Finally, let’s go over how to delete data from our table. To do this, we’ll use the $wpdb->delete() method, which allows us to delete specific rows from a table. Here’s an example of how to delete the row with the id of 1 from our ‘example_table’:

global $wpdb;
$table_name = $wpdb->prefix . 'example_table';
$wpdb->delete( $table_name, array( 'id' => 1 ) );

And there you have it! Those are the basics of working with the WordPress database using the wpdb class. With these methods, you should be able to easily create tables, add, update, and edit data in your site’s database.

More information can be found in the documentation

Thank you for your read. Peace!