Building Tables with the DB2 Designer and Visual Studio 2005 - Page 3September 12, 2006 If you wanted to create a table that had columns from a number of tables, this would be a great feature to use. Many customers I work with have best practice 'skeleton' tables used for different topics. For example, you may have a table that includes all customer information while another has defined columns for transactional operations. You could use this feature to 'discover' and automatically add these columns to your table. This is a real rapid application development booster and it's unique to the DB2 9 product! 6. The Columns field is populated with all of the columns in the EMPLOYEE table (or the columns across a combination of tables if you selected them), as shown below:
7. Select the EMPNO column and delete it by clicking Delete Column ( 8. Rename the EMPNO1 column to EMPNO by double-clicking the column and entering EMPNO. The Columns section of the table designer should now look like this:
9. Select the EMPNO column and expand the Column Properties window, as shown below:
You can use this section of the table designer to alter characteristics of each of the columns in your table. For example, you can create an Identity Column with specifications for the number of rows to initially cache, the increment level, and so on. 10. Click the column to the right of Primary Key and set it to True, as shown below:
11. Click the Keys icon (
You can use this panel to add foreign keys to your table as well, to rename or alter the primary key, and so on. 12. Click the Index icon ( 13. Add a descending index on the EMPNO column by clicking Add Index (
You can use the Index Properties field to change properties for the index in the same manner in which you can change a column's properties for a table. For example, you can make this index a clustered index. You can use the other icons in the table designer to accomplish other tasks on your table, as shown in the following figure: You can see that you can create constraints, grant privileges to users and groups, create triggers, and include pre- and post-scripts to run after the table is generated. For example, you may want to call a script that pulls data from a production database and then imports that data into your new table after it is created using a post-execution script. Or, perhaps you want to grant certain access rights to your new table. I recommend that you experiment with these features after finishing the example in this article. 14. Press Ctrl+S or simply close the designer by clicking
Note that if you chose to close the designer and execute the script by clicking
When you click Yes, Visual Studio 2005 runs the script that was generated by the table designer, as shown below (which you can see at any time within the designer by clicking View Script (
If an error occurred when running the generated script, the IBM Message Output Pane window would detail that error. For example, when I first created this table, I had an error in my check constraint. I was able to quickly locate this problem using this message facility. If the script ran successfully, you should now be able to see the EMPLOYEEBYDESIGNER table in the Server Explorer, as shown below. (You may need to refresh the Tables folder to see the new table.)
Wrapping it Up...You can see how the table designer made creating a table (and some complex associated objects and characteristics) very simple indeed. Also, note that Visual Studio 2005 generated a script, which was then run for you automatically. This is likely the function you want when working in a development environment. In a production environment, it's more likely the case that you would deploy the script in a solution that the DBA who owns the database would deploy (which you can also do). However you choose to finally create the table, I'm sure you'll agree that the table designer is a powerful tool that lets you bypass the novice steps of a wizard and at the same time leverage the auto-syntax generation that they provide and save your valuable time. About the Author
Trademarks IBM, DB2 and DB2 Universal Database are trademarks or registered trademarks of International Business Machines Corporation in the United States, other countries, or both. Microsoft and Windows are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both. Other company, product, and service names may be trademarks or service marks of others. Copyright International Business Machines Corporation, 2006. All rights reserved. Disclaimer The opinions, solutions, and advice in this article are from the author's experiences and are not intended to represent official communication from IBM or an endorsement of any products listed within. Neither the author nor IBM is liable for any of the contents in this article. The accuracy of the information in this article is based on the author's knowledge at the time of writing. |