Question

I am new to SQL and I would like to develop a database with two tables(project, task). One to many relationship. How can I create a constraint in SQL (javaDB, Apache Derby) to check that each project must have at least one or more task and each task must be assigned to at most one project. This is just an example but I am interested mainly in the mandatory participation at the 1: site. In SQL Anywhere I can use Check Exist constraint something like this: CHECK(EXIST(SELECT * FROM task WHERE project.project_pk = task.task_pk). The problem is that CHECK EXIST constraint is not in Apache Derby.

Was it helpful?

Solution

you can't have both of these constraints in effect at the same time.

consider how these will be created - whichever one you create first will fail the constraint.

I think typically, the parent object, in your example the project, would not have a constraint about the children. however the child, in your example the task, would contain a NOT NULL column that was a Foreign Key to the parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top