Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 225815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:27:02+00:00 2026-05-11T19:27:02+00:00

What kind of sql tricks you use to enter data into two tables with

  • 0

What kind of sql tricks you use to enter data into two tables with a circular reference in between.

Employees
    EmployeeID <PK>
    DepartmentID <FK> NOT NULL

Departments
    DepartmentID <PK>
    EmployeeID <FK> NOT NULL

The employee belongs to a department, a department has to have a manager (department head).

Do I have to disable constraints for the insert to happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:27:03+00:00Added an answer on May 11, 2026 at 7:27 pm

    Q: Do I have to disable constraints for the insert to happen?
    A: In Oracle, no, not if the foreign key constraints are DEFERRABLE (see example below)

    For Oracle:

        SET CONSTRAINTS ALL DEFERRED;
        INSERT INTO Departments values ('foo','dummy');
        INSERT INTO Employees values ('bar','foo');
        UPDATE Departments SET EmployeeID = 'bar' WHERE DepartmentID = 'foo';
        COMMIT;
    

    Let’s unpack that:

    • (autocommit must be off)
    • defer enforcement of the foreign key constraint
    • insert a row to Department table with a “dummy” value for the FK column
    • insert a row to Employee table with FK reference to Department
    • replace “dummy” value in Department FK with real reference
    • re-enable enforcement of the constraints

    NOTES: disabling a foreign key constraint takes effect for ALL sessions, DEFERRING a constraint is at a transaction level (as in the example), or at the session level (ALTER SESSION SET CONSTRAINTS=DEFERRED;)

    Oracle has allowed for foreign key constraints to be defined as DEFERRABLE for at least a decade. I define all foreign key constraints (as a matter of course) to be DEFERRABLE INITIALLY IMMEDIATE. That keeps the default behavior as everyone expects, but allows for manipulation without requiring foreign keys to be disabled.

    see AskTom: http://www.oracle.com/technology/oramag/oracle/03-nov/o63asktom.html

    see AskTom: http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:10954765239682

    see also: http://www.idevelopment.info/data/Oracle/DBA_tips/Database_Administration/DBA_12.shtml

    [EDIT]

    A: In Microsoft SQL Server, you can’t defer foreign key constraints like you can in Oracle. Disabling and re-enabling the foreign key constraint is an approach, but I shudder at the prospect of 1) performance impact (the foreign key constraint being checked for the ENTIRE table when the constraint is re-enabled), 2) handling the exception if (when?) the re-enable of the constraint fails. Note that disabling the constraint will affect all sessions, so while the constraint is disabled, other sessions could potentially insert and update rows which will cause the reenable of the constraint to fail.

    With SQL Server, a better approach is to remove the NOT NULL constraint, and allow for a NULL as temporary placeholder while rows are being inserted/updated.

    For SQL Server:

        -- (with NOT NULL constraint removed from Departments.EmployeeID)
        insert into Departments values ('foo',NULL)
        go
        insert into Employees values ('bar','foo')
        go
        update Departments set EmployeeID = 'bar' where DepartmentID = 'foo'
        go
    

    [/EDIT]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer D isn't really meant for use in real-time applications, mostly… May 12, 2026 at 12:39 am
  • Editorial Team
    Editorial Team added an answer "Add a reference to MySql.Data.dll" means you need to add… May 12, 2026 at 12:39 am
  • Editorial Team
    Editorial Team added an answer To the best of my knowledge, you will not be… May 12, 2026 at 12:39 am

Related Questions

This seems to me to be the kind of issue that would crop up
Does anyone have any experience that indicates what kind of performance hit a developer
I work as a ERP programmer, mostly with MS SQL and some kind of
I am trying to see from an SQL console what is inside an Oracle

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.