On the importance of prefixes in SQL structure

Disambiguation is one of the key aspects of every good database structure. If we don't structure our data properly, we might stumble on some problems while querying a database. Consider the following two tables (MySQL):

CREATE TABLE documents (
  id INT(11) NOT NULL AUTO_INCREMENT,
  title VARCHAR(64),
  content TEXT);

CREATE TABLE posts (
  id INT(11) NOT NULL AUTO_INCREMENT,
  title VARCHAR(64),
  posts_content TEXT);

I've omitted table options and table type for the sake of brevity. If we try to run a query which selects, say, both title from the two tables, we'll see an SQL error, because this is an ambiguous query. The solution is pretty simple: always use prefixes to avoid ambiguity. This is also a good SQL coding practice.

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Leave a Reply

Note: Only a member of this blog may post a comment.