Drupal - How do I create table in custom module
You can use hook install to create your own tables, if you really want to, although Drupal has many other more out-of-the-box solutions. But if you do want to roll your own, there's an example module, called dbtng_example, in the examples module: https://www.drupal.org/project/examples, complete with routing for adding and removing your custom database entries.
I'd recommend installing it and trying it out, then you can use that as a basis for creating your own database table in Drupal 8.
Another approach to consider is to create a config entity or a content entity.
If you use the drupal command line tool there's even a command for generating these "drupal generate:entity:config", and "drupal generate:entity:content".
Also in the examples module, there is config_entity_example and content_entity_example.
From https://www.previousnext.com.au/blog/understanding-drupal-8s-config-entities: "The main difference between content and config entities is how they are stored and (at the moment) config entities are not fieldable".
There are some other more out of the box solutions, such as the config_pages module that allow you to easily spin up places to store data on your site: https://www.drupal.org/project/config_pages
We recently used these for one off landing pages, by creating a controller and route. This has a huge advantage over a custom table as you can easily add new fields and you can use data types such as images and entity references easily, you can also set field formatters, and load the data formatted using the config_pages entity view builder.
hook_schema()
is still used from Drupal 8 modules to create custom database tables used from the module. Even the User and the Node modules implement it, although user_schema()
and node_schema()
don't define the schema for the respective entities, which are created in a different way.
The configuration schema is not used to create custom database tables a module uses, but describes the structure of configuration files. The PDF file found in the documentation I linked makes clear the relation between a configuration file and its schema.
Content entities don't use hook_schema()
; Drupal creates the database table for them basing on the base fields the content entity class defines.
Which method to use depends on what the module defines. It's up to the module define a content entity, a configuration entity, or just use a custom table. A module could even use all of them.