We can use "hook_schema" to create a database table. This hook must be placed in the .install file of the module so that the table creation process run only when the module is installed.
<?php
// File:- my_module.install
/**
* Implements hook_schema().
*/
function my_module_schema() {
$schema['games'] = [
'description' => 'List of games with description and download links.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Record id',
],
'name' => [
'description' => 'Name',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'description' => [
'description' => 'Description of game.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'url' => [
'description' => 'Download url.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
],
'primary key' => ['id'],
'indexes' => [
'name' => [
'name',
],
],
];
return $schema;
}
When the module that created the database table is uninstalled, the table will automatically be deleted as well.
Output -
Check Drupal Schema API for other data types.
https://www.drupal.org/docs/7/api/schema-api/data-types