[ Team LiB ] |
Creating a Simple DatabaseFirst, you create a simple database where you accept most of the available defaults. To create a database with the Create Database Wizard, follow these steps: Tip You must have SYSADM authority to create a database. Your username must belong to the Administrators group to have SYSADM authority.
After a few minutes, you'll get a message stating that the database is created and that you should run the Configuration Advisor Wizard to tune your database. Click No to skip this step. This topic is covered in the next section. Check the main panel of the Control Center to see a folder added for the CDLIB database. At this point, the database is empty. To make it useful, you need to create tables, add data, and set up referential constraints. Creating TablesYou can create tables with the Control Center through the Create Table Wizard. The Create Table Wizard allows you to create simple tables as well as more complicated ones with referential constraints. For this example, you'll use the Create Table Wizard to create a simple table and add the constraints in a later step. To create the CATEGORY table, which will contain the music categories of the CDs in this collection, follow these steps:
If you want to look at your empty table, use the Control Center. Click the Tables folder in the main Control Center panel. In the right panel, you'll see all the tables now in the database. Most of these are DB2 system tables that you can view but can't change. Scroll down until you see the CATEGORY table just created. Right-click CATEGORY and choose Sample Contents from the pop-up menu. You'll see the two table columns that you added, with no data. Next, you'll add data to this table. Now close this window by clicking Close button. Adding Data to TablesYou can add data to tables in several ways in DB2. Most likely you'll have an application from which you can enter data into DB2 databases. As mentioned in earlier lessons, you can create your own applications by using many popular programming languages such as C++, C, Visual Basic, Power Builder, or Java, or applications such as Microsoft Access to help you enter data. If you don't have an application, you can enter data by using the Command Center and simple SQL statements. Follow these steps:
|
Click the gears button to process the command. The Results screen opens, showing that the command completed successfully.
Continue to add data in this way. Return to the Script screen, click the down arrow to recall the command, change the values as appropriate to add another row, and click the gears button again. (You need to connect to the database only once.)
To see the data that you've just entered, you can issue a SELECT statement from the Script screen:
|
Click the gears button to see the data that you've entered (see Figure 9.8).
Figure 9.8. Sampling data in the Command Center
Setting Up Referential Integrity
A database usually isn't a simple table with a list of items, as you've created so far. It's usually made up of many tables, each containing normalized data.
As discussed on Day 8, normalizing helps you avoid redundancies and inconsistencies in your data. The main idea behind normalizing is to reduce tables to a set of columns where all the non-key columns depend on the entire primary key of the table.
Each table is linked to ensure that certain business rules are followed. These rules are normally called referential integrity. For example, in the CATEGORY table, you've created a list of music categories. Now create a table that lists the record company associated with an album and another table that lists the albums available on CD for each artist. You can even take this further and add a table that contains graphic files of the band or album cover, audio samples of the CD itself, and even video clips. Each table that you create will contain a certain type of information and will be connected to one or more of the tables with foreign keys.
To see how this is done, create ALBUM, a table that contains the name of the album and its year of release and length, along with relationships to the CATEGORY, RECORDLABEL, ALBUMCOVER, and GROUP tables.
Because you want to add foreign keys to the table as you define it, use the Create Table Wizard:
Start the Control Center, if it's not already started.
Expand the object folders under the CDLIB database folder until you see the Tables folder. Right-click that folder.
Select Create | Table from the pop-up menu to start the Create Table Wizard.
On the Name page, enter the following information:
Table Schema� Select ADMINISTRATOR from the list of existing schemas.
Table Name� For this example, name the table ALBUM.
Click Next to continue.
On the Columns page of the wizard, select from a list of predefined columns to use for your table. For this example, you want to create columns for ALBUMID, TITLE, RELEASEYEAR, CATEGORYID, RECORDLABELID, ALBUMCOVERID, and LENGTH.
Click the Add button to open the Add Column dialog box and fill in the details as follows:
Column Name
Data Type
Nullable
ALBUMID
SMALLINT
No
TITLE
VARCHAR, 45
No
RELEASEYEAR
SMALLINT
No
CATEGORYID
SMALLINT
No
RECORDLABELID
SMALLINT
No
ALBUMCOVERID
SMALLINT
Yes
LENGTH
SMALLINT
Yes
See Figure 9.9 to see all the columns and their characteristics. Click the Keys page to continue.
Figure 9.9. The Create Table Wizard's Columns page
On the Keys page, click Add Primary to define a primary key for the new table (see Figure 9.10). From the list of columns in the table, select the columns you want to have as a primary key. In this example, you want to have ALBUMID as the primary key, so move ALBUMID into the Key Columns list and click OK to continue.
Figure 9.10. The Create Table Wizard's Define Primary Key dialog box.
On the Keys page, click Add Foreign to add a foreign key to the table (see Figure 9.11). Fill in these fields:
Figure 9.11. The Add Foreign Key dialog box.
Table Schema� Specify the name of the schema of the parent key. In this example, the schema for all the tables is SVISSER.
Table Name� Specify the name of the parent table. In this example, you want CATEGORY as the parent table. Choose CATEGORY; its primary key value will appear in the Primary Key box.
Primary Key� This box is filled in automatically when you select the parent table. The primary key of the CATEGORY table is CATEGORYID, so this appears in the Primary Key box.
Foreign Key� In the Available Columns box, select columns that you want to define as foreign keys. Each column you select must match a primary key column for the parent table in meaning and data type. In this example, select CATEGORYID to be the foreign key.
On Delete� From this optional list, specify the action to take place on the dependent table when a row is deleted from the parent table. In this example, the ALBUM table is dependent on the parent table CATEGORY. Suppose that you have Rock listed in the CATEGORY table and decide to delete it. Because you also have a row in the ALBUM table that uses Rock as a category and these rows are linked, you now have to decide what to do with all the rows in ALBUM. Your choices are No Action (the default), Restrict, Cascade, or Select NULL. If you select No Action or Restrict, DB2 will give an error message if you attempt to delete a row in the parent table if there are dependent rows. No rows will be deleted. If you choose Cascade, the row in the parent table is deleted along with any of the dependent rows in dependent tables. If you choose Specify NULL, when the parent row is deleted, any dependent rows will replace all columns with a NULL value if it's allowable. For this example, leave this at the default value: No Action.
On Update� From this optional list, specify the action to take place on the dependent table when a row of the parent table is updated. Your choices are No Action and Restrict. If you choose No Action or Restrict, an error will occur if you attempt to update a row in the parent table. For this example, use the default value: No Action.
Constraint Name� In this optional text box, type a name for the constraint that you're defining. If a name isn't entered, one is generated for you. It's useful to have a meaningful constraint name. If an error occurs, the constraint name is used to describe the error condition. A meaningful name helps correct the error.
Click OK to add the foreign key.
On the Constraints page of the Create Table Wizard, you can add rules to help ensure that only valid data is inserted into the table (see Figure 9.12).
Figure 9.12. The Create Table Wizard's Constraints page.
Click Add to open the Add Check Constraint dialog box to define a constraint (see Figure 9.13). For example, the column RELEASEYEAR is intended to contain the year the album or CD was released. This date is printed on the back of each CD. Depending on your collection, the release years may range from 1950 to present day. You can add a check constraint to ensure that only dates in a specific range are entered.
Figure 9.13. The Add Check Constraint dialog box launched from the Create Table Wizard's Constraints page.
You can define a check constraint that references a single column by specifying that column in the Check Condition box. For example, you can add a check constraint to validate the year entered for the release date of a CD. A check condition of releaseyear between 1950 and 2020 would be suitable. (In Day 15, "Using System Administration Tools," you'll add a trigger to ensure that a year beyond the current year isn't entered.)
Note
Check constraints aren't checked for inconsistencies, duplicate conditions, or equivalent conditions. Therefore, contradictory or redundant check constraints can be defined, resulting in possible errors at execution time.
Although the Constraint Name and Comment text boxes are optional, they're useful. If you don't enter values for these text boxes, the system generates a unique but incomprehensible name for the constraint. If you violate a check constraint condition, having a useful name appear in the message makes it easier to correct the error.
You can add multiple check constraints before closing this dialog box. Click Add on the Add Check Constraint dialog box to add the constraint and click Close when you've finished adding constraints.
Click Finish to create the table.
Altering Tables
After you create a table, the attributes that you can change are limited. You can do the following:
Change the size of a column�for example, increase the size of a column defined with the data type VARCHAR.
Change the comment and the data capture for propagation option.
Add new columns and change, remove, or rearrange the new columns.
Add unique, foreign, and partitioning keys for the table.
Add or drop columns from the primary keys for the table or change the constraint name.
Add, change, or remove new check constraints.
Change the lock size, percentage of free space, and other performance-related details.
Tip
If you're altering a table by adding a foreign key, you need to turn off constraint checking before you alter the table. After you add the foreign key, you need to turn constraint checking back on.
Note
If you turn off constraint checking for a parent table, the foreign key constraints of all its dependent and descendent tables are put in check-pending state.
Right-click the table you want to alter and select Alter from the pop-up menu. The Alter Table dialog box opens. If you want to change an attribute in the table but it isn't in this list, you'll need to create a new table with the new attributes and import the data from the existing table.
[ Team LiB ] |
No comments:
Post a Comment