An SQL snippet is one more more statements of SQL (Structured Query Language), a relatively standard language for querying databases. An SQL-enabled database engine, such as MySQL or PostgreSQL, will interpret a sequence of statements (queries) and execute them. Statements can request data, insert or update existing data, or alter the structure of the database. Because SQL statements can change (e.g., completely destroy) your database, one must be very careful in executing SQL snippets. Therefore, consider this an advanced topic. You have been warned.

Always backup your database before attempting any kind of SQL administration.

The first step in using an SQL snippet is to ensure that you have access to the database. This will depend on how your Drupal installation is hosted. There are a number of general mechanisms for SQL database access.

  • command line access (e.g., via secure shell - SSH)
  • web-based database administration tools (e.g., phpMyAdmin)
  • Via executing PHP from within Drupal
  • Via Drush: example: drush sql-query "SELECT * FROM users LIMIT 1"

The precise mechanism for executing an SQL snippet depends on which database you are using, and how the database has been set up. The following sections elaborate on each of these mechanisms.