0

Background

I'm trying to understand the SQL queries that Drupal generates when I select different content to be displayed on my site so I can write my own php filters.

Issue

I see this in the query every time

SELECT...
FROM
{node} node
WHERE...

Question

What does the {node} mean does it refer to the content type I'm using?

  • ok but why is it node node? – Joe Lloyd Sep 1 '15 at 14:14
  • It's an alias @JoeLloyd. Could just as easily be {node} n or similar – Clive Sep 1 '15 at 14:16
3

It's the abstract way of referring to the node table in the database. This notation is converted by Drupal to the real name of the table right before the query is executed (in most cases, for most configurations, {node} will simply become node at execution time). Using this notation allows an instance of Drupal to use table prefixes without the code generating the queries having to know anything about those details.

The table name has the appearance of being repeated ({node} node) so that the table name {node} gets mapped to an alias node that can be used elsewhere in the query. Consider if you have configured your site to use a table prefix of "dev_" then SELECT node.status FROM {node} node... would get translated to SELECT node.status FROM dev_node node..., thus allowing your node.status field to refer to the correct table without the code generating the query having to know anything about the fact that there's a table prefix configured for the site. For this simple example it's silly because you could just write {node}.status, but for more complex examples, particularly with multiple joins it can be useful to set up the alias.

  • Oh I see, so does that mean the node table has a reference to all of my content? because I just got a count back and its got 700000+ items in it – Joe Lloyd Sep 1 '15 at 14:20
  • @JoeLloyd The node table has one record for every node on your site. It does not necessarily (indeed almost certainly does not) contain all field data and there are almost certainly other types of entities on your site such as taxonomy terms that are in other tables. – Aaron Sep 1 '15 at 14:23
  • ok, Thanks Aaron you've made this much clearer. just one last question, how would I know what records are held in node, is it the nid field on the other tables? – Joe Lloyd Sep 1 '15 at 14:26
  • Yes -- I'd recommend exploring the database structure. It might be easiest with a GUI but you can also do it with SHOW and DESCRIBE queries. The Drupal database structure is fairly complex. Since nodes are just one type of entity, most tables that need to relate data to entities will have both an "entity_type" and "entity_id" field which, taken in tandem, allows their data to relate to any type of entity in the system. In those cases the data that relates to a node would have "entity_type" as "node" and "entity_id" as the nid of the node. – Aaron Sep 1 '15 at 14:31
  • Awesome Aaron, yeah this makes things much much clearer. thanks for the help. – Joe Lloyd Sep 1 '15 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.