Constraints

Constraints

 

Constraints on the IBM i platform are rules defined on database tables (both DDL and DDS) to enforce data integrity by restricting the types of data that can be inserted, deleted, or updated.

This document outlines the types, benefits, and operation of the named constraints in DB2 as implemented on IBM i.

Named constraints are those that are explicitly given a unique identifier within the database, allowing easier management and clearer error reporting.

 

Types of DB2 Named Constraints on IBM i

 

CONSTRAINT               DESCRIPTION

Primary Key :         Uniquely identifies each record in a table. Cannot be NULL

Unique:                   Ensures all values in the specified column(s) are unique.

Referential:             Enforces a relationship between two dependent tables.

Check:                     Ensures column values satisfy a defined condition/expression.

 

Primary Key Constraints

 

  • Enforce the uniqueness of one or more columns to identify each row uniquely.
  • No columns defined in the Primary Key can be NULL.
  • Only one primary key constraint can be added to an existing table.
  • Existing data is checked for uniqueness before the constraint is applied.
  • Eliminates the possibility of duplicate records (rows) in the table.

 

Unique Constraints

 

  • Ensures that all values in the constrained columns are unique across the table.
  • Can be added to existing tables with a unique constraint name.
  • Existing data is checked for uniqueness before the constraint is applied.

 

Referential Constraints (Foreign Key Constraints)

 

  • Define relationships between tables by requiring that values in one or more foreign key columns (child) correspond to values in primary or unique key columns of another table.
  • Enforced during data modification operations like INSERT, UPDATE, or DELETE.
  • Existing data is checked for the relationship before the constraint is applied.
  • Both parent and dependent tables must reside in the same auxiliary storage pool (ASP).

Check Constraints

 

  • Specify a Boolean expression that must be true for data in a row.
  • Can be applied to one or multiple columns.
  • Violations prevent data modification and raise specific error statuses during I/O operations.

 

Naming and Management of Constraints

 

  • Constraint names must be unique within the table or library and distinct from other constraints.
  • Named constraints appear in error messages when violated, aiding in troubleshooting.
  • Constraint names are also used in both O/S and SQL management commands.
  • Adopting naming conventions is recommended to help identify the type and purpose of constraints more easily.

 

Benefits of Using Constraints

 

  • Embeds the control of uniqueness and relationships into the database as a single point of control.
  • Some limited degree of data validation can be embedded with the use of CHECK constraints.
  • Eliminates the need for multiple application programs to perform the same validations multiple times. This simplifies the application code and logic.
  • Improves performance by reducing the number of I/O operations in the applications.
  • Constraint violations result in standard DB2 error messages, making problems easier to identify and debug.

 

Best Practices

 

  • Always define a PRIMARY KEY for every table.
  • Use “Referential Constraints” to document and enforce parent-child relationships to prevent orphaned records.
  • Adopt a standardized naming convention for constraints.

 

Conclusion

 

Constraints on IBM i are explicit, uniquely identified rules applied to tables to centralize and enforce data integrity. They include primary key, unique, referential, and check constraints. Naming constraints facilitate management, error handling, and modification. Constraints ensure that data adheres to the defined business rules and relationships, preventing invalid data entry and maintaining database consistency.

I want to acknowledge my colleague, Tommy Atkins, for his guidance and assistance with this article.

.

Agility Deficit Disorder (ADD) on IBM i 

Database modernization

Do you get the chills at the thought of making database (DB) changes?

ADD symptoms include poor data quality, monolithic programs, poor response times(agility), technical debt, integration issues, and being forced to make massive changes instead of incremental improvements.

Data Quality: It’s not uncommon to find junk records in the database. You may or may not know how they got there. Improve data quality by adding constraints. Constraints are part of the operating system, that manages relationships between files. Bonus: You don’t have to write/maintain any code to do it. Just monitor for the error.

Monolithic Programs: The monster programs critical to your business that contain the business logic. Because of the complexity, this program usually requires a highly skilled resource to work on it. Extensive testing is also needed to avoid any unforeseen problems.  Moving the business logic to the database via event trigger programs removes a lot of complexity from a scary program. Event Trigger programs facilitate a large part of data validation and business logic. When moved into the database engine, it will standardize where it can be found, making maintaining and testing business logic changes much safer. Changing a trigger program is much simpler, faster, and safer than changing a monolithic program.

 Response times: When your users demand information currently unavailable in the database, this becomes a major issue. Adding a field to a file causes a level check and the recompile of every program referencing the file. You can dramatically minimize this pain and improve agility by adopting data-centricity. The complexity and risk can be significantly reduced, between the design and normalization of the DB, I/O servers, and the event trigger programs.

Technical debt:  the implied cost of additional work that arises when software developers choose a quick and easy solution over a more comprehensive one that would take longer to implement. The question becomes, how long can you continue with a program-centric strategy?

Integration: Without moving the business logic to the database, integration with front-end GUI programs is complicated because the business logic has to be duplicated into the API. This complicates things in two ways: the back-end programmer needs to communicate the business rules for the API’s development to the API development team and then maintain it. Using event trigger programs to validate the data, minimizes API complexities and avoids maintaining the logic in multiple places.

Massive Changes: When time and pressure finally demand a DB change, it is scary, especially if it is a commonly used file such as the customer master. These changes could involve so many programs that you must freeze other developments until completion. Agility and cognitive computing (AI) demand that you transition to a data-centric application.

ADD is caused by not keeping up with technological advancements. The cure requires some education, planning, and some pain, but the benefits are substantial.

To see what a modern data-centric application looks like, visit our open-source page.

https://databoroughservices.com/rpg-open-source-project/ 

To better understand the solution you are welcome to book a no-strings-attached discussion.

https://calendly.com/neil-woodhams-dbs/data-centric-application-example

At Databorough Services, we understand the critical role that a well-maintained database plays in ensuring an organization’s smooth operation and efficiency. As the technology landscape continues to evolve rapidly, it becomes essential for businesses to adapt and optimize their databases to meet the demands of modern-day computing.

Referential Constraints IBM i

Benifits of using Referential Constraints IBM i

Accuracy – prevent orphaned records by letting the RDBMS validate the parent/child relationships between tables 

Performance  – RDBMS runs in the operating system storage pool and provides excellent performance

Functionality- The functionality provided by the constraints is all functionality that need not be performed in the programs, resulting in less coding and testing