One-to-Many Restriction

We have two tables with a one-to-many relationship. We would like to apply a restriction to at least one child record for this parent record.

Is it possible?

If not, will you replace the circuit with a more complex one to support this limitation? If so, how would you do it?

Edit: I am using SQL Server 2005

+3
source share
5 answers

, " ". , , , , .

.

+7

, back-end , PostgreSQL.

+1

?

Create Table ParentTable
(
ParentID
ChildID not null,
Primary Key (ParentID), 
Foreign Key (ChildID ) references Childtable (ChildID));
)

- , , before insert trigger .

CREATE or REPLACE TRIGGER trigger_name
BEFORE INSERT
    ON ParentTable
    FOR EACH ROW 
BEGIN

    -- ( insert new row into ChildTable )
    -- update childID column in ParentTable 

END;
0

- " ", . , , .

, . . , , , .

- SQL Server.

1) - , . . , , - , .

2) CTE , CTE , ( ). , , .

3) , , . . , , , .

, , , , , .

, . , null ( ). ( ), , .

, SQL Server , .

- , !

P.S. , , , , , , .

P.P.S , null , , , , . , , . , , . , , , , .

0

, Oracle rel.11.2.4.

  • , FK PK . - .
  • , , :

, PK, COUNT PK. - , NO_DATA_FOUND 0.

CHILD_COUNT .

CHECK CHILD_COUNT CHILD_COUNT > 0

:

  • ​​ . , , CHILD_COUNT > 0 CHECK .
  • ​​ ​​ ; , COMMIT.
  • ​​ , CHILD_COUNT COMMIT .
  • , FOREIGN KEY . ( )
  • , CHILD_COUNT .

NOTE. I do not need a virtual column if Oracle allowed to use CHECK constraintsbased on user-defined functions in rel.11.2.4.

0
source

Source: https://habr.com/ru/post/1706465/


All Articles