A crash course in PostgreSQL, part 2

15.09.2011

are wonderful shortcuts for queries. A query is a request to see data, like our simple SELECT * from comics; example. Queries can grow large and complex and span multiple tables. Any query can be defined in a view, and then instead of typing a long query command simply type the view name.

are fundamental to all databases. A common criticism of MySQL is that it does not support transactions. It does, depending on which storage backend you're using, but that is a long story for another day. A transaction groups all of the steps for a particular operation into a single operation that returns success only if all the steps succeed. If a single step fails then the whole transaction fails, rather than leaving the transaction in an incomplete and erroneous state. The transaction is not reported as completed until all the steps are written to disk. For example, consider all of the steps involved in ordering a book from an online seller. You want your payment credited, book moved to the shipping queue, and an order confirmation. The seller wants their inventory updated and payment processed correctly. If any of these steps fail then a well-designed ordering system reports the error, and it must be corrected or done over.

is a word you'll see a lot, and there are a lot of different ideas on what it means. Creating a formal schema for your PostgreSQL databases is optional, and it starts with the CREATE SCHEMA command. Schema is the metadata that describes all the elements of database structure: tables, operators, data types, functions, the relationships between tables, every piece of your database. There are tools for creating graphical schemas illustrating all the different relationships, which is very useful for understanding your database structure and debugging problems. In PostgreSQL schemas allow you to arbitrarily organize database objects and to give users access to arbitrary database objects, which is a nice simple and flexible way to control user access.

is one of those words that sounds important and something that needs to be done -- normalize your tables! And it is. Normalization covers a lot of ground; in a nutshell it is reducing redundancy. This makes your database more efficient, and easier to maintain. So it encompasses tasks such as designing your tables to avoid duplication, using whitespace consistently, using consistent terminology and spelling for better searches, and sanitizing user input -- which is beautifully illustrated in the XKCD comic, , in which we meet Little Bobby Tables.

This article, "," was originally published at ITworld. For the latest IT news, analysis and how-tos, follow ITworld on and