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

  • Home
  • SEARCH
  • 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

Related Questions

Two Part Question: What kind of actions does SQL Server process in RAM? Of
I'm kind of new to SQL Server/C#. My teacher thought I should write data
Is it possible to replicate this kind of specific sql ordering in the django
I am interested in using some kind of a command-line utility for SQL Server
Kind of like this question I have many text snippets that I use many,
I'm just wondering what kind of SQL command I could execute that would select
I am guessing that not all SQL is created equally. I am diving into
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
In Our project I have used LINQ to SQL for every kind of database
I need to create database for SQL Server, what kind of naming convention I

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.