Home
Constraints should not be applied to types that cannot be constrained
Description
This rule states that constraints should not be applied to data types that cannot be constrained. This is because applying constraints to data types that cannot be constrained will result in an error. For example, if a constraint is applied to a data type such as a BLOB (Binary Large Object) or CLOB (Character Large Object), the constraint will not be enforced and an error will be thrown. Therefore, it is important to ensure that constraints are only applied to data types that can be constrained.
Key Benefits
- Ensures type safety: By not applying constraints to types that cannot be constrained, the compiler can be assured that the type is safe and valid.
- Avoids incorrect usage: Applying constraints to types that cannot be constrained can lead to incorrect usage, which can cause errors and unexpected behavior.
- Improves readability: By not applying constraints to types that cannot be constrained, code is easier to read and understand.
Non-compliant Code Example
Create TABLE EMPLOYEE
(
EMP_ID number(10) NOT NULL,
FIRSTNAME NVARCHAR2(75),
LASTNAME NVARCHAR2(75),
DEPT_ID int,
ADDRESS NVARCHAR2(250) NOT NULL,
Photo blob(300), --Non compliant code
CONSTRAINT employee_pk PRIMARY KEY (EMP_ID)
);
Compliant Code Example
Create TABLE EMPLOYEE
(
EMP_ID number(10) NOT NULL,
FIRSTNAME NVARCHAR2(75),
LASTNAME NVARCHAR2(75),
DEPT_ID int,
ADDRESS NVARCHAR2(250) NOT NULL,
Photo blob(), --Compliant code
CONSTRAINT employee_pk PRIMARY KEY (EMP_ID)
);