Chapter 13. Creating and Managing Database Objects

No book about T-SQL would be complete without spending some time talking about how to create and manage database objects. As a development DBA, I work predominantly with application developers, who believe that creating and managing databases is boring and strictly for uptight DBA types. However, anyone who works with T-SQL, whether a DBA or a developer, will invariably find themselves having to create and sometimes manage database objects. It may be just a temporary object inside a stored procedure, or a new object to support an application change, but it will happen, so you should probably learn how to do it correctly.

Anything you can do with SQL Server's graphical tools you can do with script. The opposite, however, is not true. Most of the menu options in Management Studio actually create and use T-SQL script to perform changes. Because this is a book about T-SQL, we will not spend any time explaining how to use the graphical tools. Instead, we will concentrate on how to use the T-SQL language to create and manage database objects. For many of the examples in this section I am going to use a table called MyTable. In order to duplicate the examples in this chapter, you will need to drop the MyTable table repeatedly, because the table can exist only once. To drop a table, simply run the command DROP TABLE tablename.

Data Definition Language

You can do only three things with any database object, other than actually use it: you can ...

Get Beginning T-SQL with Microsoft® SQL Server® 2005 and 2008 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.