Skip to header Skip to main navigation Skip to main content Skip to footer
  • Log in
Home
DrupalVIP
Freelancer service with great care and enthusiasm.
  • Home
  • Products
  • Blog
  • Support
    • Dictionary
    • Drupal Resources
    • External Resources

how to check if table exist in update procedure

Breadcrumb

  • Home
  • DrupalVIP Support
  • how to check if table exist in update procedure

In Drupal 8/9, you can programmatically check if a specific table name exists in the database using the schema()->tableExists() method provided by the database API.

 

<?php

use Drupal\Core\Database\Database;


function drupalvip_module_update_9202() {
    // check if table exist, if not install schema
    $table['group'] = [
        'description' => 'Stores IP group addresses.',
        'fields' => [
            'id' => [ 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'unique ID',  ],
            'name' => [ 'type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'description' => 'group name', ],

        ],
        'primary key' => ['id'],     
    ];
        
    $schema = \Drupal::database()->schema();
    if (!$schema->tableExists('drupalvip_group')) {
        $schema->createTable('drupalvip_group', $table['group']);
    }
}

 

* Create new database table {mytable2}.
*/
function mymodule_update_9000() {
  $schema['mytable2'] = [
     // table definition array goes here
  ];
  \Drupal::database()->schema()->createTable('mytable2', $schema['mytable2']);
}
Code Snippet
Read More
API Reference: Check if a table exists.
function hook_update_N
Database API examples
Tags
Database API
Drupal 9
Code Example
Module Development
hook_update

The Freelancer Assistance

Fullstack Service

Fullstack Service

Drupal Shared Space

Drupal Shared Space

Drupal Development

Drupal Development

Proactive Maintenance

Proactive Maintenance

Site Management

Site Management

Drupal Training

drupal training session
Review All Services

Buy Hourly Support

 

Drupal platform support for complex websites. The service includes support, training, troubleshooting, fixes, updates, tool creation and module building.
* Minimum order is for 5 hours of support
* For every 20 hours of order you will be entitled to an additional hour of support
* For every 50 hours of order you will be entitled to 5 additional hours of support

 

>> Payment <<

 
cards

מופעל על-ידי paypal

Legal

  • Home
  • Contact
  • Products

Footer menu

  • Home
  • Contact
  • Products

Copyright © 2026 DrupalVIP project of Automatic Frameworks - All rights reserved

Developed and Maintain by Jonathan Ben Hur Freelancer