Database code events
Database code events are carried out at the database level, but are implemented in the application layer.
Database code events allow you to:
- Validate data from your application before it reaches the database. This ensures that data satisfies constraints, passes referential integrity, and so on. Code events allow you to perform these checks before reaching the database engine, which prevents the database engine from returning an error at a later stage.
- Perform administrative tasks that might otherwise be performed in SQL triggers.
- Specify a default value.
- Implement check constraints.
When generating the application code, the BAM implements code to check these database table constraints, as defined
by the database meta-schema file (4dbx):
- Not null, to ensure that a column cannot have a
NULL
value. - Unique, to ensure that each value in a column is unique.
- Primary key, to ensure that the value of a column (or set of columns) uniquely identify a row in a table. A primary
key cannot be
NULL
or empty. - Foreign key, to ensure the value of a column matches exactly a value that uniquely identifies a row in another table.
- Default, to set a default value for a column when no value is specified.
SQL CHECK
constraints are not implemented by the BAM
You must handle SQL CHECK
constraints in a code event. The Check constraint ensures that all values
in a column satisfy a specific condition. This constraint is not part of the database meta-schema file
(4dbx), and therefore cannot be implemented when the BAM generates code.
Tip:
It is recommended that you check for these constraints in code events, rather than letting the database engine handle these errors.
For example, if you have a SQL
CHECK
constraint in your database creation or update script, such
as:CONSTRAINT CHK_Person CHECK (Age>=18)
this check is not implemented in the
BAM-generated code. You should code for this in your Check Table Constraints code event. If you do not, when the users inserts or updates data that violates this
constraint, an error will be raised by the database engine.Locate the event
Select a table in the diagram of the database meta-schema file (4dbx). The code events are listed under Database Data Events in the Properties view.