Database Import, Table Maintenance, and Constraints
Importing CSV Data in DB2 Cloud
Comma Separated Values (CSV) files store tabular data where commas separate columns and lines represent rows. CSV files can be created in text editors like Notepad App or managed in spreadsheet software like Excel. Within the DB2 cloud interface, data import is accessed via the hamburger icon under Load and Load Data. When creating a new table like pet from pets.Csv, column properties can be manually adjusted by sliding off automatic data type detection; for example, setting animal type to data type car with length 15, and adjusting Pets Name using varchar with length 15.
When importing data into pre-structured tables where columns and primary keys (such as job code in the job table) are already defined using SQL, the file (such as job5.Csv or jobfile.Csv containing 14 rows) does not require a header row. In these instances, the option for header in the first row must be toggled off during the load process. Successful data loads can be verified by executing a SELECT statement in the Run SQL interface.
Table Alterations and Maintenance
SQL statements are case-insensitive and automatically converted to uppercase by SQL, with quoted text remaining an exception as literal data. Uppercase keywords and lowercase programmer-supplied column names are used primarily to improve presentation and readability. When modifying existing tables—such as adding a birth date column with the Date data type to the employee table—the reorg table command must be executed following the alteration to finalize changes and prevent errors during updates.
If a column is incorrectly named upon creation and contains no data, it can be dropped entirely and re-added with the correct identifier, such as renaming a separation date column to SEPDATE. Any structural modifications must always be followed by executing the reorg table command.
Referential Integrity and Table Constraints
Referential integrity ensures database accuracy and prevents invalid values from being inserted into tables like job history. Foreign keys enforce this integrity by performing lookups against referenced parent tables before accepting data into columns such as employee number, DEPCODE, and JOBCODE. Syntax for setting foreign keys utilizes the REFERENCES keyword to specify the target lookup table. SQL object identifiers like table names cannot contain spaces.
Data constraints further enforce database validity. A CHECK constraint, such as Salary CK, can be applied to prevent negative values by restricting inputs lower than zero. In Entity-Relationship Diagrams (ERD), primary keys are indicated by underlined column names.
Questions & Discussion
A student asked Jerry if declaring three columns as the primary key is allowed. Jerry explained that using a combination of multiple columns as a primary key forms a Composite Key, which ensures the uniqueness of each row.
A student asked Jerry whether declaring employee number and department code as both part of the primary key and foreign keys causes any conflicts. Jerry clarified that there is no conflict, as a primary key establishes row uniqueness while a foreign key serves as a link between tables.