Related
I need to create tables and input values. All but two rows when inserted give me an error, Unique Constraint Violated. The last row in DEPT_LOCATIONS table and the second to the last row in DEPENDENT table. I'm not sure why these rows aren't being added because other rows in the same table are.
Here's my code so far:
/*Create Employee Table/*
create table EMPLOYEE(FNAME varchar(30), MINIT varchar(5), LNAME varchar(30), SSN varchar(10) PRIMARY KEY, BDATE varchar(30), ADDRESS varchar(30), SEX varchar(5), SALARY NUMERIC(10,0),
SUPERSSN varchar(30),DNO varchar(30));
/*Insert values into Employee Table/*
insert into EMPLOYEE values('John','B','Smith','123456789','1965-01-09','731 Fonden, Houston, TX','M', 30000,'333445555','5');
insert into EMPLOYEE values('Franklin','T','Wong','333445555','1955-12-08','638 Voss, Houston, TX','M', 40000,'888665555','5');
insert into EMPLOYEE values('Alice','J','Zelaya','999887777','1968-01-19','3321 Castle, Spring, TX','F', 25000,'987654321','4');
insert into EMPLOYEE values('Jennifer','S','Walace','987654321','1941-06-20','291 Berry, Bellaire, TX','F', 43000,'888665555','4');
insert into EMPLOYEE values('Ramesh','K','Narayan','666884444','1962-09-15','975 Fire Oak, Humble, TX','M', 38000,'333445555','5');
insert into EMPLOYEE values('Joyce','A','English','453453453','1972-07-31','5631 Rice, Houston, TX','F', 25000,'333445555','5');
insert into EMPLOYEE values('Ahmed','V','Jabbar','987987987','1969-03-29','980 Dallas, Houston, TX','M', 25000,'987654321','4');
insert into EMPLOYEE values('James','E','Bong','888665555','1937-11-10','450 Stone, Houston, TX','M', 55000,'null','1');
/*Create Department Table*/
create table DEPARTMENT(DNAME varchar(30), DNUMBER varchar(30) PRIMARY KEY, MGRSSN varchar(30), MGRSTARTDATE varchar(30));
/*Insert values into Department Table*/
insert into DEPARTMENT values('Research', '5', '333445555', '1988-05-22');
insert into DEPARTMENT values('Administration', '4', '987654321', '1995-01-01');
insert into DEPARTMENT values('Headquarters', '1', '888665555', '1981-06-19');
/*Create Department Location Table*/
create table DEPT_LOCATIONS(DNUMBER varchar(30) REFERENCES DEPARTMENT(DNUMBER), DLOCATION varchar(30) PRIMARY KEY);
/*Insert values into Department Location Table*/
insert into DEPT_LOCATIONS values('1', 'Houston');
insert into DEPT_LOCATIONS values('4', 'Stafford');
insert into DEPT_LOCATIONS values('5', 'Bellarire');
insert into DEPT_LOCATIONS values('5', 'Sugarland');
insert into DEPT_LOCATIONS values('5', 'Houston');
/*Create Project Table*/
create table PROJECT(PNAME varchar(30), PNUMBER varchar(30) PRIMARY KEY, PLOCATION varchar(30), DNUM varchar(30) REFERENCES DEPARTMENT(DNUMBER));
/*Insert values into Project Table*/
insert into PROJECT values('ProductX', '1', 'Bellaire', '5');
insert into PROJECT values('ProductY', '2', 'Sugarland', '5');
insert into PROJECT values('ProductZ', '3', 'Houston', '5');
insert into PROJECT values('Computerization', '10', 'Stafford', '4');
insert into PROJECT values('Reorganization', '20', 'Houston', '1');
insert into PROJECT values('Newbenefits', '30', 'Stafford', '4');
/*Create Works On table*/
create table WORKS_ON(ESSN varchar(30) REFERENCES EMPLOYEE(SSN), PNO varchar(30) REFERENCES PROJECT(PNUMBER), HOURS numeric(5, 1));
/*Insert values into Works on Table*/
insert into WORKS_ON values('123456789', '1', 32.5);
insert into WORKS_ON values('123456789', '2', 7.5);
insert into WORKS_ON values('666884444', '3', 40.0);
insert into WORKS_ON values('453453453', '1', 20.0);
insert into WORKS_ON values('453453453', '2', 20.0);
insert into WORKS_ON values('333445555', '2', 10.0);
insert into WORKS_ON values('333445555', '3', 10.0);
insert into WORKS_ON values('333445555', '10', 10.0);
insert into WORKS_ON values('333445555', '20', 10.0);
insert into WORKS_ON values('999887777', '30', 30.0);
insert into WORKS_ON values('999887777', '10', 10.0);
insert into WORKS_ON values('987987987', '10', 35.0);
insert into WORKS_ON values('987987987', '30', 5.0);
insert into WORKS_ON values('987654321', '30', 20.0);
insert into WORKS_ON values('987654321', '20', 15.0);
insert into WORKS_ON values('888665555', '20', null);
/*Create Dependent table*/
create table DEPENDENT(ESSN varchar(30) REFERENCES EMPLOYEE(SSN), DEPENDENT_NAME varchar(30) PRIMARY KEY, SEX varchar(30), BDATE varchar(30), RELATIONSHIP varchar(30));
/*Insert values into Dependent Table*/
insert into DEPENDENT values('333445555', 'Alice', 'F', '1986-04-05', 'Daughter');
insert into DEPENDENT values('333445555', 'Theodore', 'M', '1983-10-25', 'Son');
insert into DEPENDENT values('333445555', 'Joy', 'F', '1958-05-03', 'Spouse');
insert into DEPENDENT values('987654321', 'Abner', 'M', '1942-02-28', 'Spouse');
insert into DEPENDENT values('123456789', 'Michael', 'M', '1988-01-04', 'Son');
insert into DEPENDENT values('123456789', 'Alice', 'F', '1988-12-30', 'Daughter');
insert into DEPENDENT values('123456789', 'Elizabeth', 'F', '1967-05-05', 'Spouse');
The tables should look like this:
enter image description here
enter image description here
Could someone please help me understand what is going on?
Thank you
DLOCATION varchar(30) PRIMARY KEY
insert into DEPT_LOCATIONS values('1', 'Houston');
insert into DEPT_LOCATIONS values('5', 'Houston');
The DLOCATION column is a primary key, but you are trying to insert duplicate values 'Houston' into the table. Therefore it throws unique constraint violated error. To overcome this, you could create it as a composite primary key with DNUMBER and DLOCATION together as:
CONSTRAINT pk_num_loc PRIMARY KEY (DNUMBER, DLOCATION)
DDL & inserts:
/*Create Department Location Table*/
CREATE TABLE dept_locations (
dnumber VARCHAR(30)
REFERENCES department ( dnumber ),
dlocation VARCHAR(30),
CONSTRAINT pk_num_loc PRIMARY KEY ( dnumber, dlocation ));
insert into DEPT_LOCATIONS values('1', 'Houston');
insert into DEPT_LOCATIONS values('4', 'Stafford');
insert into DEPT_LOCATIONS values('5', 'Bellarire');
insert into DEPT_LOCATIONS values('5', 'Sugarland');
insert into DEPT_LOCATIONS values('5', 'Houston');
SELECT * FROM dept_locations;
Output:
DNUMBER DLOCATION
------- ------------------------------
1 Houston
4 Stafford
5 Bellarire
5 Houston
5 Sugarland
Similarly, you need to do the same for rest of the tables which are failing due to unique constraint error.
In both cases you violated the primary key of the table, which must be unique. "Houston" is repeated for DEPT_LOCATIONS, as is "Alice" for DEPENDENTS.
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.
I have written a DLSQL Table which draws the parameters from an excel file. I am getting errors such as too many values or the table does not process. The order of tables is not correct on the table as you can see the foreign keys in some of the first tables corrspond to the table below.
Here is the Excel file: https://drive.google.com/file/d/0Bzum6VJXi9lUdzJYa0tZbHhpbUk/view?usp=sharing
BEGIN
EXECUTE IMMEDIATE 'drop table DEPARTMENT cascade constraint';
EXECUTE IMMEDIATE 'drop table EMPLOYEE cascade constraint';
EXECUTE IMMEDIATE 'drop table DEPENDENT cascade constraint';
EXECUTE IMMEDIATE 'drop table DEPT_LOCATION cascade constraint';
EXECUTE IMMEDIATE 'drop table LOCATION cascade constraint';
EXECUTE IMMEDIATE 'drop table STAGING_DEPLOCATION cascade constraint';
EXECUTE IMMEDIATE 'drop table PROJECT cascade constraint';
EXECUTE IMMEDIATE 'drop table WORKS_ON cascade constraint';
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('');
END;
/
CREATE TABLE DEPARTMENT
(DEPT_NO NUMBER(15,0) NOT NULL,
DEPARTMENTNAME VARCHAR2(15) NOT NULL,
MANAGER_SSN NUMBER(9,0) NULL,
MANAGER_STARTDATE DATE NULL,
PRIMARY KEY (DEPT_NO));
/
/
CREATE TABLE EMPLOYEE
(SSN NUMBER(9,0) NOT NULL,
FIRSTNAME VARCHAR2(15) NOT NULL,
MI VARCHAR2(5) NULL,
LASTNAME VARCHAR2(15) NOT NULL,
DOB DATE NULL,
CITY VARCHAR2(20) NULL,
STATE VARCHAR2(20) NULL,
ZIP VARCHAR2(5) NULL,
SEX VARCHAR2(1) NULL,
SALARY NUMBER(15,0) NULL,
SUPERVISOR_SSN NUMBER(9) NULL,
DEPT_NO NUMBER(15,0) NULL,
PRIMARY KEY (SSN),
FOREIGN KEY(DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO));
/
CREATE TABLE DEPENDENT
(DEPENDENT_NO NUMBER(15,0) NOT NULL,
SSN NUMBER(9,0) NOT NULL,
FIRSTNAME VARCHAR2(15) NOT NULL,
LASTNAME VARCHAR2(15) NULL,
DOB DATE NULL,
SEX VARCHAR2(1) NULL,
RELATIONSHIP VARCHAR2(1) NULL,
PRIMARY KEY (DEPENDENT_NO),
FOREIGN KEY(SSN) REFERENCES EMPLOYEE(SSN));
/
CREATE TABLE DEPT_LOCATION
(DEPT_NO NUMBER(15,0) NOT NULL,
LOCATION_NO NUMBER(15,0) NOT NULL,
ISACTIVE CHAR(10) NULL,
PRIMARY KEY (DEPT_NO, LOCATION_NO),
FOREIGN KEY(DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO)
FOREIGN KEY(LOCATION_NO) REFERENCES LOCATION(LOCATION_NO));
/
CREATE TABLE EMPLOYEE
(SSN NUMBER(9,0) NOT NULL,
FIRSTNAME VARCHAR2(15) NOT NULL,
MI VARCHAR2(5) NULL,
LASTNAME VARCHAR2(15) NOT NULL,
DOB DATE NULL,
CITY VARCHAR2(20) NULL,
STATE VARCHAR2(20) NULL,
ZIP VARCHAR2(5) NULL,
SEX VARCHAR2(1) NULL,
SALARY NUMBER(15,0) NULL,
SUPERVISOR_SSN NUMBER(9) NULL,
DEPT_NO NUMBER(15,0) NULL,
PRIMARY KEY (SSN),
FOREIGN KEY(DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO));
/
CREATE TABLE LOCATION
(LOCATION_NO NUMBER(15,0) NOT NULL,
LOCATION VARCHAR2(15) NULL,
PRIMARY KEY (LOCATION_NO));
/
CREATE TABLE STAGING_DEPLOCATION(
LOCATION VARCHAR2(50) NOT NULL,
DEPARTMENT VARCHAR2(50) NOT NULL,
ISACTIVE CHAR(10) NULL,
PRIMARY KEY (DEPT_NO, LOCATION_NO),
FOREIGN KEY(DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO)
FOREIGN KEY(LOCATION_NO) REFERENCES LOCATION(LOCATION_NO));
/
CREATE TABLE PROJECT
(PROJECT_NO NUMBER(15,0) NOT NULL,
DEPT_NO NUMBER(15,0) NOT NULL,
LOCATION_NO NUMBER(10,0) NULL,
PROJECTNAME VARCHAR2(20) NULL,
PRIMARY KEY (PROJECT_NO),
FOREIGN KEY(DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO),
FOREIGN KEY(LOCATION_NO) REFERENCES LOCATION(LOCATION_NO));
/
CREATE TABLE WORKS_ON
(SSN NUMBER(15,0) NOT NULL,
PROJECT_NO NUMBER(15,0) NOT NULL,
HOURS NUMBER(15,0) NULL,
PRIMARY KEY (SSN, PROJECT_NO));
create sequence dependent_seq
start with 1
increment by 1
nocache
nocycle;
SET DEFINE OFF;
INSERT INTO DEPARTMENT (DEPT_NO, DEPARTMENTNAME, MANAGER_SSN, MANAGER_STARTDATE) VALUES ('1', 'headquarters', '888665555', '1971-06-19');
INSERT INTO DEPARTMENT (DEPT_NO, DEPARTMENTNAME, MANAGER_SSN, MANAGER_STARTDATE) VALUES ('2', 'administration', '987987987', '1985-01-01');
INSERT INTO DEPARTMENT (DEPT_NO, DEPARTMENTNAME, MANAGER_SSN, MANAGER_STARTDATE) VALUES ('3', 'research', '333445555', '1978-05-22');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('123456789', 'Roberto', 'B', 'Tamburello', '1955-01-09', '731 Fondren', 'Houston', 'TX', '11233'. 'M', '30000', '333445555', '5');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('333445555', 'David', 'T', 'Bradley', '1945-12-08', '38 Voss', 'Houston', 'TX', '11233', 'M', '40000', '888665555', '5');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('453453453', 'Mary', 'A', 'Dempsey', '1962-07-31', '5631 Rice', 'Houston', 'TX', '11233', 'F', '25000', '333445555', '5');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('666884444', 'Ramesh', 'K', 'Narayan', '1952-08-15', '975 Fire Oak', 'Humble', 'TX', '11232', 'M', '38000', '333445555', '5');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('888665555', 'James', 'E', 'Borg', '1991-11-10', '450 Stone', 'Houston', 'TX', '11233', 'M', '55000', '', '1');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('987654321', 'Terry', 'S', 'Duffy', '1990-06-20', '231 Berry', 'Bellaire', 'TX', '11236', 'F', '43000', '888665555', '4');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('987987987', 'Jossef', 'V', 'Goldberg', '1959-03-29', '980 Dallas', 'Houston', 'TX', '11233', 'M', '25000', '987654321', '4');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('999887777', 'Terry', 'J', 'Zelaya', '1958-07-19', '3321 Castle', 'Spring', 'TX', '11239', 'F', '25000', '987654321', '4');
INSERT INTO EMPLOYEE (SSN, FIRSTNAME, MI, LASTNAME, DOB, CITY, STATE, ZIP, SEX, SALARY, SUPERVISOR_SSN, DEPT_NO) VALUES ('999887777', 'John', '', 'Clay', '1938-07-19', '3321 Castle', 'Spring', 'TX', '11239', 'F', '35000', '987654321', '3');
ALTER TABLE DEPARTMENT ADD CONSTRAINT fk_dept FOREIGN KEY (MANAGER_SSN) REFERENCES EMPLOYEE(SSN);
ALTER TABLE EMPLOYEE ADD CONSTRAINT fk_empdept FOREIGN KEY (DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO);
CREATE SEQUENCE dependent_seq START WITH 1;
nsert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,123456789, 'Lili','Tamburello',to_date( '1978-12-31','yyyy-mm-dd'),'F','DAUGHTER');
Insert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,123456789, 'Anna','Tamburello',to_date( '1957-05-05','yyyy-mm-dd'),'F','SPOUSE');
Insert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,123456789, 'Gregory','Tamburello',to_date( '1978-01-01','yyyy-mm-dd'),'M','SON');
Insert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,333445555, 'Alice','Bradley',to_date( '1976-04-05','yyyy-mm-dd'),'F','DAUGHTER');
Insert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,333445555, 'Theodore','Bradley',to_date( '1973-10-25','yyyy-mm-dd'),'M','SON');
Insert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,987654321, 'Abner','Duffy',to_date( '1969-02-28','yyyy-mm-dd'),'M','SPOUSE');
Insert into DEPENDENT (DEPENDENT_NO,SSN,FIRSTNAME,LASTNAME,DOB,SEX,RELATIONSHIP) VALUES (dependent_seq.nextval,987654321, 'Aby','John',to_date( '1970-02-28','yyyy-mm-dd'),'M','SPOUSE');
INSERT INTO DEPT_LOCATION(DEPT_NO, LOCATION_NO, ISACTIVE)
SELECT DEPT_NO, LOCATION_NO, ISACTIVE FROM STAGING_DEPLOCATION A
INNER JOIN LOCATION B ON B.LOCATION = A.LOCATION
INNER JOIN DEPARTMENT D ON D.DEPARTMENTNAME = A.DEPARTMENTNAME;
INSERT INTO DEPT_LOCATION (DEPT_NO, LOCATION_NO, ISACTIVE) VALUES ('headquarters', 'Houston', '');
INSERT INTO DEPT_LOCATION (DEPT_NO, LOCATION_NO, ISACTIVE) VALUES ('administration', 'Stafford', '');
INSERT INTO DEPT_LOCATION (DEPT_NO, LOCATION_NO, ISACTIVE) VALUES ('research', 'Bellaire', '');
INSERT INTO DEPT_LOCATION (DEPT_NO, LOCATION_NO, ISACTIVE) VALUES ('research', 'Houston', '');
INSERT INTO DEPT_LOCATION (DEPT_NO, LOCATION_NO, ISACTIVE) VALUES ('research', 'Sugarland', '');
INSERT INTO LOCATION (LOCATION_NO, LOCATION) VALUES ('1', 'Bellaire');
INSERT INTO LOCATION (LOCATION_NO, LOCATION) VALUES ('2', 'Houston');
INSERT INTO LOCATION (LOCATION_NO, LOCATION) VALUES ('3', 'Stafford');
INSERT INTO LOCATION (LOCATION_NO, LOCATION) VALUES ('4', 'Sugarland');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('1', '5', '1', 'ProductX');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('2', '5', '4', 'ProductY');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('3', '5', '2', 'ProductZ');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('10', '4', '3', 'Computerization');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('20', '1', '2', 'Reorganization');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('30', '4', '3', 'Newbenefits');
INSERT INTO PROJECT (PROJECT_NO, DEPT_NO, LOCATION_NO, PROJECTNAME) VALUES ('1', '5', '1', 'ProductX');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('123456789', '1', '33');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('123456789', '2', '8');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('333445555', '2', '10');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('333445555', '3', '10');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('333445555', '10', '10');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('333445555', '20', '10');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('453453453', '1', '20');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('453453453', '2', '20');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('666884444', '3', '40');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('888665555', '20', '');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('987654321', '20', '15');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('987654321', '30', '20');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('987987987', '10', '35');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('987987987', '30', '5');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('999887777', '10', '10');
INSERT INTO WORKS_ON (SSN, PROJECT_NO, HOURS) VALUES ('999887777', '30', '30');
COMMIT;
Because, when you are trying to INSERT INTO EMPLOYEE where you have mentioned 12 columns, but you are passing 13 values.
You need to pass exactly same number of values, which you have specific after INSERT INTO EMPLOYEE
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
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.