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 8874301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:41:08+00:00 2026-06-14T18:41:08+00:00

I am currently running on SQL plus and i have 2 tables. If i

  • 0

I am currently running on SQL plus and i have 2 tables.

If i enter values into dept ( nm_employees ) and if that value is not there in dept2 ( nm_departments ), then I have to write a PL in SQL to enter the value in dept2. I have the code written but it is incorrect. any ideas?

CREATE TABLE nm_employees(
name varchar(20),
dept varchar(20),
CONSTRAINT empPK PRIMARY KEY (dept)
);

CREATE TABLE nm_departments(
dept2 varchar(20),
CONSTRAINT departments FOREIGN KEY (dept2) REFERENCES nm_employees(dept)
);

INSERT INTO nm_employees values ('nancy','engineer');
IF NOT EXISTS (SELECT dept FROM nm_employees where dept='engineer'
)
THEN    
INSERT INTO nm_departments values ('engineer');
END IF;
  • 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-06-14T18:41:09+00:00Added an answer on June 14, 2026 at 6:41 pm

    First point is that you don’t use the fancy inverted commas for SQL. Use standard single-quotes (').

    Second point is that IF..THEN is used only in programming blocks, like triggers. In regular SQL batches, you can use INSERT...SELECT.

    CREATE TABLE nm_employees(
    name varchar(20),
    dept varchar(20),
    CONSTRAINT empPK PRIMARY KEY (dept)
    );
    
    CREATE TABLE nm_departments(
    dept2 varchar(20),
    CONSTRAINT departments FOREIGN KEY (dept2) REFERENCES nm_employees(dept)
    );
    
    
    INSERT INTO nm_employees values ('nancy','engineer');
    
    INSERT INTO nm_departments
    select 'engineer' from dual
    WHERE NOT EXISTS (SELECT dept FROM nm_departments where dept2='engineer');
    

    You also had the wrong table in the EXISTS test, where you should be checking in nm_departments, not nm_employees.

    For what it’s worth, this looks like an exercise, so here are some additional pointers:

    1. the relationship between the tables should be Employee [M] ->--|- [1] Department, i.e. employee belongs to one department, each department has many employees
    2. the foreign key should be in the employee table referencing department. This also means that the record in the department table needs to be created before the record in employee
    3. the primary key in employees (without using surrogate keys) should be a combination of (name, dept). As currently defined, you can only have one employee per department
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is currently running against a 32-bit SQL Server 2005
Windows Server 2003. I have an existing DotNetNuke website. SQL Server is currently running
We have an install that is currently running Magento CE v1.5 (it has been
We are currently running a SQL Job that archives data daily at every 10PM.
I want to drop all the SQL Agent Jobs which are not currently running
I am currently running the following SQL statement on my MySQL database: INSERT INTO
Upon running the below SQL statement (which works perfectly) I've found that all tables
I currently have an ASP.NET MVC 3 project, plus a class library project, that
I have an SQL script (currently running against SQLite, but it should probably work
I'm currently running some SQL which uses an IN expression to match against multiple

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.