Error with insterting values - sql

I have problem with inserting values into my table.
Here is the script:
IF OBJECT_ID ('DEPT', 'table') IS NOT NULL
DROP TABLE DEPT;
GO
CREATE TABLE DEPT (
DEPTNO NUMERIC(2) NOT NULL,
DNAME VARCHAR(14),
LOC VARCHAR(13),
CONSTRAINT DEPT_PRIMARY_KEY PRIMARY KEY (DEPTNO));
GO
INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');
GO
IF OBJECT_ID ('EMP', 'table') IS NOT NULL
DROP TABLE EMP;
GO
CREATE TABLE EMP (
EMPNO NUMERIC(4) NOT NULL,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR NUMERIC(4) CONSTRAINT EMP_SELF_KEY REFERENCES EMP (EMPNO),
HIREDATE DATETIME,
SAL NUMERIC(7,2),
COMM NUMERIC(7,2),
DEPTNO NUMERIC(2) NOT NULL,
CONSTRAINT EMP_FOREIGN_KEY FOREIGN KEY (DEPTNO)
REFERENCES DEPT(DEPTNO),
CONSTRAINT EMP_PRIMARY_KEY PRIMARY KEY (EMPNO));
GO
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,'17-11-1981',5000,NULL,10);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,'1-05-1981',2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,'9-06-1981',2450,NULL,10);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,'2-04-1981',2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,'28-09-1981',1250,1400,30);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,'20-02-1981',1600,300,30);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,'8-09-1981',1500,0,30);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,'3-12-1981',950,NULL,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,'22-02-1981',1250,500,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,'3-12-1981',3000,NULL,20);
INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,'17-12-1982',800,NULL,20);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,'09-12-1982',3000,NULL,20);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,'12-01-1983',1100,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,'23-01-1982',1300,NULL,10);
There is error while doing this:
INSERT INTO EMP VALUES (7859, 'PHILIPPE', 'CLERK', 8219,
'1988-06-11', 1300, NULL, 10), (7594, 'SEBASTIEN', 'SALESMAN', 1292,
'1978-06-09', 3500, NULL, 10);
INSERT INTO EMP VALUES (7956, 'MARTIN', 'DEALER', 4322, '1981-04-11',
2300, 0, 20), (7947, 'SIMON', 'CLERK', 7655, '1981-07-12', 1800, 200,
20);
INSERT INTO EMP VALUES (7382, 'HUGO', 'ANALYST', 4564, '1982-05-11',
1300, 0, 30), (7291, 'MARC', 'SALESMAN', 9768, '1981-03-09', 3500,
NULL, 30);
INSERT INTO EMP VALUES (7734, 'JOHN', 'MANAGER', 8678, '1983-11-11',
4300, 0, 40), (7359, 'TOMMY', 'CLERK', 5340, '1981-04-10', 1200, 100,
40);
Error:
The instruction INSERT is in conflict with constraint FOREIGN KEY SAME
TABLE "EMP_SELF_KEY".
Can someone help? Thanks !

In your second block of inserts into EMP, none of the MGR values (8219, 4322, 4564, 8678) appear as EMPNO values in your previous inserts, hence the foreign key violation. Those "managers" must be inserted as "employees" before they can be referenced by the foreign key.

Related

parent key not found ORA-06512: at "SYS.DBMS_SQL"

Can't figure out the problem, and will appreciate the help. There was no problem while defining the tables and alterations. For some reason it can't figure out the parent key reference in another table/ in the same table. What could be the possible solution for such problems?
Problem is caused by the EMP table constraint.
create table DPT (
DNO varchar2(3) constraint pri_key primary key,
DNAME varchar2(10) constraint unq unique,
constraint sta_letr check(DNO like 'D%')
);
create table PROJECTS (
DNO varchar2(3) constraint dno_fork references DPT(DNO) constraint dno_nullState not null,
PRJ_NO varchar2(5) constraint rgexCheck check(PRJ_NO like 'P%') constraint prj_nullState not null,
PRJ_NAME varchar2(10),
PRJ_CREDITS number(2) constraint credRange check(PRJ_CREDITS between 1 and 10),
START_DATE date,
END_DATE date,
constraint prKey primary key(DNO, PRJ_NO),
constraint dateChecker check(END_DATE > START_DATE)
);
create table EMP (
EMPNO number(4),
ENAME varchar2(10),
EJOB varchar2(9) default 'CLRK' constraint jobCheck check(EJOB in('CLRK', 'A.MGR', 'MGR', 'GM', 'CEO')),
MGR_ID number(4),
BIRTH_DATE date,
SAL number(7,2) default 20001 constraint salCheck check(SAL > 20000),
COMM number(7,2) default 1000,
DEPTNO varchar2(3) constraint deptFk references DPT(DNO),
PRJ_ID varchar2(9) default 'P1',
DATE_OF_JOIN date,
constraint supervisor foreign key(MGR_ID) references EMP(EMPNO),
constraint pri_ky primary key(EMPNO)
);
--Insertion, Modifications and Alterations
alter table EMP modify PRJ_ID varchar2(5);
alter table EMP drop constraint deptFk;
alter table EMP add constraint deptRef foreign key(DEPTNO, PRJ_ID) references PROJECTS(DNO, PRJ_NO);
alter table DPT add LOCATIONS varchar2(9);
alter table DPT modify LOCATIONS default 'BNG';
alter table DPT add constraint oth_val check(LOCATIONS in ('BNG', 'MNG', 'MUB', 'HYD', 'CHN'));
alter table DPT modify DNAME varchar2(15);
insert into DPT (DNO, DNAME, LOCATIONS) values ('D1', 'Marketing', 'CHN');
insert into DPT (DNO, DNAME, LOCATIONS) values ('D2', 'Research', 'MNG');
insert into DPT (DNO, DNAME, LOCATIONS) values ('D3', 'Administrator', 'BNG');
insert into DPT (DNO, DNAME, LOCATIONS) values ('D4', '', 'BNG');
insert into DPT (DNO, DNAME, LOCATIONS) values ('D5', 'IT', 'BNG');
insert into DPT (DNO, DNAME, LOCATIONS) values ('D6', 'Corporate', 'HYD');
select * from DPT;
insert into PROJECTS (DNO, PRJ_NO, PRJ_NAME, PRJ_CREDITS) values ('D1', 'P1', '', 2);
insert into PROJECTS (DNO, PRJ_NO, PRJ_NAME, PRJ_CREDITS) values ('D2', 'P1', '', 2);
insert into PROJECTS (DNO, PRJ_NO, PRJ_NAME, PRJ_CREDITS) values ('D3', 'P2', '', 7);
insert into PROJECTS (DNO, PRJ_NO, PRJ_NAME, PRJ_CREDITS) values ('D1', 'P3', '', 5);
insert into PROJECTS (DNO, PRJ_NO, PRJ_NAME, PRJ_CREDITS) values ('D4', 'P2', '', 7);
select * from PROJECTS;
--Statement where the problem is occurring.
insert into EMP (EMPNO, ENAME, EJOB, MGR_ID, BIRTH_DATE, SAL, DEPTNO, PRJ_ID, DATE_OF_JOIN) values (100, 'Ravi', 'MGR', 111, to_date('10-10-1985', 'dd-mm-yyyy'), 32000, 'D1', 'P1', to_date('2-10-2001', 'dd-mm-yyyy'));
Error Code: ORA-02291: integrity constraint (SQL_OZPTHTLYAAVUSNISLXUTJKQNF.SUPERVISOR) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
You must insert the records in the EMP table in the order of the hierarchy, e.g. first the manager, than the employee
Your first insert of the employee 100 complains that the manager with EMPNO 111 does not exists...
insert into EMP (EMPNO, ENAME, EJOB, MGR_ID, ...
values (100, 'Ravi', 'MGR', 111, ...

Why is my sql query returning no records?

Schema:
created table dept
CREATE table dept
(dept_id varchar (20) default 'department' NOT NULL,
locat varchar (25) default 'location' NOT NULL,
nme varchar (20) default 'name' NOT NULL);
inserting values
INSERT INTO dept(dept_id, locat, nme)
VALUES (1, 'dublin', 'payroll');
INSERT INTO dept(dept_id, locat, nme)
VALUES (2, 'galway', 'manufacturing');
INSERT INTO dept(dept_id, locat, nme)
VALUES (3, 'cork', 'sales');
created table emp
create table emp
(emp_id varchar (20) default 'id' NOT NULL,
NME varchar (20) default 'name' NOT NULL,
job_title varchar (25) default 'job' NOT NULL,
HIRE_DATE DATE,
SALARY INT (25) default '0' NOT NULL,
dept_id varchar (20) default 'dept' NOT NULL);
inserting values
INSERT INTO emp
VALUES (123, 'byrne', 'clerical', DATE ('2012-06-12'), 28000, 1);
INSERT INTO emp
VALUES (124, 'barry', 'operater', DATE ('11-07-11'), 33000,2);
INSERT INTO emp
VALUES (125, 'hynes', 'senior_operator', DATE ('26-09-13'), 36500,2);
INSERT INTO emp
VALUES (126, 'WILLIAMS', 'manager', DATE ('30-10-13'), 51000,3);
THE FOLLOWING QUERY IS RETURNING NO RECORDS
SELECT NME FROM emp
where HIRE_DATE between ('01-01-10') AND ('01-01-14');
How to change the code to get the desired output.
You proper date formats. In Oracle, you can introduce constants with the date keyword:
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (123, 'byrne', 'clerical', DATE '2012-06-12', 28000, 1);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (124, 'barry', 'operater', DATE '2011-07-11', 33000, 2);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (125, 'hynes', 'senior_operator', DATE '2013-09-26', 36500, 2);
INSERT INTO emp (emp_id, NME, job_title , HIRE_DATE, SALARY, dept_id)
VALUES (126, 'WILLIAMS', 'manager', DATE '2013-10-30', 51000, 3);
select NME
from emp
where HIRE_DATE between date '2010-01-01' and date '2014-01-01';
I thought 2-digit years when out in 1999. Oracle might not be interpreting two-digit years the same way that you do.
Also, include the column list when doing an insert.

Find sum of salary from department where sum of salary is smaller than 20000

I'm trying to write a query that returns the total salary of every department.
So it needs to look who works in a department and count the salary up and do this for every department. Then it should only show me the departments that have a total salary lower than 20000.
My database looks like this
create table dept
(
deptno number(2,0),
dname varchar2(14),
loc varchar2(13),
constraint pk_dept primary key (deptno)
);
create table emp
(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
mgr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0),
constraint pk_emp primary key (empno),
constraint fk_deptno foreign key (deptno) references dept (deptno)
);
insert into dept
values(10, 'ACCOUNTING', 'NEW YORK');
insert into dept
values(20, 'RESEARCH', 'DALLAS');
insert into dept
values(30, 'SALES', 'CHICAGO');
insert into dept
values(40, 'OPERATIONS', 'BOSTON');
insert into emp
values(7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981','dd-mm-yyyy'), 5000, null, 10);
insert into emp
values(7698, 'BLAKE', 'MANAGER', 7839, to_date('1-5-1981','dd-mm-yyyy'), 2850, null, 30);
insert into emp
values(7782, 'CLARK', 'MANAGER', 7839, to_date('9-6-1981','dd-mm-yyyy'), 2450, null, 10);
insert into emp
values(7566, 'JONES', 'MANAGER', 7839, to_date('2-4-1981','dd-mm-yyyy'), 2975, null, 20);
insert into emp
values(7788, 'SCOTT', 'ANALYST', 7566, to_date('13-JUL-87','dd-mm-rr') - 85, 3000, null, 20);
insert into emp
values(7902, 'FORD', 'ANALYST', 7566, to_date('3-12-1981','dd-mm-yyyy'), 3000, null, 20);
insert into emp
values(7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980','dd-mm-yyyy'), 800, null, 20);
insert into emp
values(7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-2-1981','dd-mm-yyyy'), 1600, 300, 30);
insert into emp
values(7521, 'WARD', 'SALESMAN', 7698, to_date('22-2-1981','dd-mm-yyyy'), 1250, 500, 30);
insert into emp
values(7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-9-1981','dd-mm-yyyy'), 1250, 1400, 30);
insert into emp
values(7844, 'TURNER', 'SALESMAN', 7698, to_date('8-9-1981','dd-mm-yyyy'), 1500, 0, 30);
insert into emp
values(7876, 'ADAMS', 'CLERK', 7788, to_date('13-JUL-87', 'dd-mm-rr') - 51, 1100, null, 20);
insert into emp
values(7900, 'JAMES', 'CLERK', 7698, to_date('3-12-1981','dd-mm-yyyy'), 950, null, 30);
insert into emp
values(7934, 'MILLER', 'CLERK', 7782, to_date('23-1-1982','dd-mm-yyyy'), 1300, null, 10);
I tried using this query but it keeps telling me that name is an invalid identifier.
SELECT
(SELECT d.dname
FROM dept d
WHERE d.deptno = e.deptno) AS naam,
SUM(e.sal) AS sal
FROM
emp e
WHERE
sal < 20000
GROUP BY
naam;
Does anyone know what I'm doing wrong?
Use inner join and having
SELECT d.dname, sum(e.sal) sal
FROM dept d
INNER JOIN emp e on d.deptno = e.deptno
GROUP BY d.dname
HAVING sum(e.sal) < 20000

SQL Trigger Statement will create but will not run

---create EMPLOYEE table---
CREATE TABLE EMPLOYEES
(
EMPLOYEEID INT NOT NULL,
EMPLOYNAME VARCHAR2(25) NULL,
EMPLOYAGE INT NULL,
EMPLOYADDRESS VARCHAR2(25) NULL,
SALARY NUMBER(9,2) DEFAULT (0) NULL,
CONSTRAINT EMPLOYEES_PK PRIMARY KEY(EMPLOYEEID)
);
--INSERT EMPLOYEES--
INSERT INTO EMPLOYEES
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00);
INSERT INTO EMPLOYEES
VALUES (2, 'Khilan', 25, 'Delhi', 1500.00);
INSERT INTO EMPLOYEES
VALUES (3, 'Kaushik', 24, 'Kota', 2000.00);
INSERT INTO EMPLOYEES
VALUES (4,'Chaitali', 25, 'Mumbai', 6500.00);
INSERT INTO EMPLOYEES
VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00);
INSERT INTO EMPLOYEES
VALUES (6, 'Komal', 22, 'MP', 4500.00);
--CREATE TRIGGER FOR AN UPDATE TO THE SALARY--
CREATE OR REPLACE TRIGGER display
BEFORE DELETE OR INSERT OR UPDATE ON EMPLOYEES
FOR EACH ROW
WHEN (NEW.employeeid >0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
End;
--triggering trigger with insert--
INSERT INTO EMPLOYEES (EMPLOYEEID,EMPLOYNAME,EMPLOYAGE, EMPLOYADDRESS, SALARY)
VALUES(9,'Kri', 67, 'PK', 7900.00);
UPDATE EMPLOYEES
SET SALARY = SALARY +500
WHERE EMPLOYEEID = 4;
this code will all compile in SQL developer but when I try to update and run any of the code it will not run. I enable it through the trigger tab in developer, so it should be working fine. Any help is appreciated because I am teaching this to my self.
thanks
RMan2015

How do I exclude a name from the results of a nested query?

The following is my Employees table:
create table EMPLOYEES
(EmpID char(4) unique Not null,
Ename varchar(10),
Job varchar(9),
MGR char(4),
Hiredate date,
Salary decimal(7,2),
Comm decimal(7,2),
DeptNo char(2) not null,
Primary key(EmpID),
Foreign key(DeptNo) REFERENCES DEPARTMENTS(DeptNo));
insert into EMPLOYEES values (7839,'King','President',null,'17-Nov-11',5000,null,10);
insert into EMPLOYEES values (7698,'Blake','Manager',7839,'01-May-11',2850,null,30);
insert into EMPLOYEES values (7782,'Clark','Manager',7839,'02-Jun-11',2450,null,10);
insert into EMPLOYEES values (7566,'Jones','Manager',7839,'02-Apr-11',2975,null,20);
insert into EMPLOYEES values (7654,'Martin','Salesman',7698,'28-Feb-12',1250,1400,30);
insert into EMPLOYEES values (7499,'Allen','Salesman',7698,'20-Feb-11',1600,300,30);
insert into EMPLOYEES values (7844,'Turner','Salesman',7698,'08-Sep-11',1500,0,30);
insert into EMPLOYEES values (7900,'James','Clerk',7698,'22-Feb-12',950,null,30);
insert into EMPLOYEES values (7521,'Ward','Salesman',7698,'22-Feb-12',1250,500,30);
insert into EMPLOYEES values (7902,'Ford','Analyst',7566,'03-Dec-11',3000,null,20);
insert into EMPLOYEES values (7369,'Smith','Clerk',7902,'17-Dec-10',800,null,20);
insert into EMPLOYEES values (7788,'Scott','Analyst',7566,'09-Dec-12',3000,null,20);
insert into EMPLOYEES values (7876,'Adams','Clerk',7788,'12-Jan-10',1100,null,20);
insert into EMPLOYEES values (7934,'Miller','Clerk',7782,'23-Jan-12',1300,null,10);
The following is the DEPARTMENTS table:
create table DEPARTMENTS
(DeptNo char(2) unique Not null,
DName varchar(14),
Location varchar(13),
Primary key(DeptNo));
insert into DEPARTMENTS values (10,'Accounting','New York');
insert into DEPARTMENTS values (20,'Research','Dallas');
insert into DEPARTMENTS Values (30,'Sales','Chicago');
insert into DEPARTMENTS values (40,'Operations','Boston');
The following is my query:
select ename, salary
from EMPLOYEES
where DeptNo in
(select DeptNo
from EMPLOYEES
where Ename ='blake');
How do I exclude 'blake' from my results? I was told to use a NOT IN in the subquery but it is not working.
select ename, salary
from EMPLOYEES
where DeptNo in
(select DeptNo
from EMPLOYEES
where Ename ='blake') and Ename <>'blake';
You can also make use of not in as in second query.
SQLFIDDLE DEMO