Google News
logo
HSQLDB - Interview Questions
How do you add a constraint to a table in HSQLDB?
In HSQLDB, you can add constraints to a table using the ALTER TABLE SQL statement. Here's the general syntax for adding constraints to a table:
ALTER TABLE table_name
    ADD CONSTRAINT constraint_name constraint_definition;?

Replace table_name with the name of the table to which you want to add the constraint, constraint_name with the desired name for the constraint, and constraint_definition with the definition of the constraint.

Here are examples of adding different types of constraints to a table in HSQLDB :

Primary Key Constraint :
ALTER TABLE Employee
    ADD CONSTRAINT pk_employee_id PRIMARY KEY (employee_id);?

Foreign Key Constraint :
ALTER TABLE Employee
    ADD CONSTRAINT fk_department_id FOREIGN KEY (department_id) REFERENCES Department(department_id);?

Unique Constraint :
ALTER TABLE Employee
    ADD CONSTRAINT uk_email UNIQUE (email);?
Check Constraint :
ALTER TABLE Employee
    ADD CONSTRAINT ck_salary CHECK (salary >= 0);?

Not Null Constraint :
ALTER TABLE Employee
    ALTER COLUMN first_name SET NOT NULL;?

These examples demonstrate how to add various types of constraints to the Employee table in HSQLDB. You can use the appropriate constraint definition based on the type of constraint you want to add and the specific requirements of your database schema.
Advertisement