11.5. Practical Database Tips

The following list provides some practical tips on working with databases.

  • Since the database is often at the heart of a web site, you need to carefully consider its design. It's especially important to think of a good design up front, before you start building your site on top of it. When you have a number of pages that access your database, it will become harder to make changes, such as removing tables or renaming columns, to the data model.

  • Always consider the primary key for your table. I prefer to give each table a column called Id. The underlying data type is then an int and an identity, which gives each record a unique ID automatically. Instead of an int, you can also consider the uniqueidentifier data type, which ensures uniqueness even across database or application boundaries.

  • Give your database objects like tables and columns logical names. Avoid characters like spaces, underscores, and dashes. A name like GenreId is much easier to read than colGen_ID_3.

  • Don't use SELECT * to get all columns from a database. By using SELECT * you may be selecting more columns that you actually need. By explicitly defining the columns you want to retrieve, you make your intentions to others clearer and increase the performance of your queries at the same time.

  • Always create relationships between tables when appropriate. Although querying for the reviews and genres you saw in this chapter without a relationship between the two tables works just fine, it's relationships ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.