SQL Trigger Statement will create but will not run - sql

---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

Related

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.

Create trigger ORACLE

I have to create a trigger that records order numbers in the purchase table. PURCHASE the training database and audit information into the AUDIT_TAB table. This is a task from the book Christopher Allen How to write powerful and flexible programs on PL / SQL.
CREATE TABLE audit
(ORDER_NUMBER NUMBER(10),
person_code VARCHAR2(3),
user_name CHAR(30),
user_machine CHAR(20),
change_in_quant NUMBER(5),
transaction_time DATE,
FOREIGN KEY (person_code) REFERENCES persoon);
CREATE SEQUENCE order_num_seq;
CREATE OR REPLACE TRIGGER audit_trigger
BEFORE INSERT OR UPDATE ON purchase
FOR EACH ROW
DECLARE
no_name_change EXCEPTION;
quant_change NUMBER(5) := 0;
BEGIN
IF (UPDATING
AND
(:NEW.product_name <> :OLD.product_name))
THEN
RAISE no_name_change;
END IF;
IF (((UPDATING)
AND
(:OLD.ORDER_NUMBER IS NULL))
OR
((INSERTING)
AND
(:NEW.ORDER_NUMBER IS NULL)))
THEN
SELECT order_num_seq.NEXTVAL
INTO :NEW.ORDER_NUMBER
FROM dual;
END IF;
IF (UPDATING)
THEN
quant_change := :NEW. quantity - :OLD. quantity;
ELSE
quant_change := :NEW. quantity;
END IF;
INSERT INTO audit
VALUES ( :NEW.ORDER_NUMBER,
:NEW.salesperson,
USER,
USERENV ( 'TERMINAL' ) ,
quant_change,
SYSDATE) ;
EXCEPTION
WHEN no_name_change
THEN
dbms_output.put_line ('Change of product name not allowed');
dbms_output.put_line ('Aborting and resetting to old values');
:NEW.product_name := :OLD.product_name;
:NEW. salesperson := :OLD.salesperson;
:NEW.ORDER_NUMBER := :OLD.ORDER_NUMBER;
:NEW. quantity := :OLD.quantity;
END audit_trigger ;
/
And trigger compiled with warnings. Why? What's my mistake?
My database:
CREATE TABLE person (
person_code VARCHAR2(3) PRIMARY KEY,
first_name VARCHAR2(15),
last_name VARCHAR2(20),
hire_date DATE
);
CREATE INDEX person_name_index
ON person(last_name, first_name);
ALTER TABLE person
ADD CONSTRAINT person_unique UNIQUE (
first_name,
last_name,
hire_date
);
INSERT INTO person VALUES
('CA', 'Charlene', 'Atlas', '01-ФЕВ-02');
INSERT INTO person VALUES
('GA', 'Gary', 'Andersen', '15-ФЕВ-02' );
INSERT INTO person VALUES
('BB', 'Bobby', 'Barkenhagen', '28-ФЕВ-02');
INSERT INTO person VALUES
('LB', 'Laren', 'Baxter', '01-МАР-02 ');
INSERT INTO person VALUES
('LN', 'Linda', 'Norton', '01-ИЮН-03');
CREATE TABLE product (
product_name VARCHAR2(25) PRIMARY KEY,
product_price NUMBER(4,2),
quantity_on_hand NUMBER(5,0),
last_stock_date DATE
);
ALTER TABLE product ADD (
CONSTRAINT positive_quantity CHECK(
quantity_on_hand IS NOT NULL
AND
quantity_on_hand >= 0)
);
INSERT INTO product VALUES
('Small Widget', 99, 1, '15-ЯНВ-03' );
INSERT INTO product VALUES
( 'Medium Wodget', 75, 1000, '15-ЯНВ-02' );
INSERT INTO product VALUES
('Chrome Phoobar', 50, 100, '15-ЯНВ-03' );
INSERT INTO product VALUES
('Round Chrome Snaphoo', 25, 10000, null);
INSERT INTO product VALUES
('Extra Huge Mega Phoobar +', 9.95, 1234, '15-ЯНВ-04' );
INSERT INTO product VALUES ('Square Zinculator',
45, 1, TO_DATE ('Декабрь 31, 2002, 11:30',
'Month dd, YYYY, HH:MI'));
INSERT INTO product VALUES (
'Anodized Framifier', 49, 5, NULL);
INSERT INTO product VALUES (
'Red Snaphoo', 1.95, 10, '31-ДЕК-01');
INSERT INTO product VALUES (
'Blue Snaphoo', 1.95, 10, '30-ДЕК-01');
CREATE TABLE purchase (
product_name VARCHAR2(25),
salesperson VARCHAR2(3),
purchase_date DATE,
quantity NUMBER(4,2)
);
ALTER TABLE purchase
ADD PRIMARY KEY (product_name,
salesperson,
purchase_date
);
ALTER TABLE purchase ADD (
CONSTRAINT reasonable_date CHECK(
purchase_date IS NOT NULL
AND
TO_CHAR(purchase_date, 'YYYY-MM-DD') >= '2000-06-30')
);
ALTER TABLE purchase
ADD CONSTRAINT purchase_fk_product FOREIGN KEY
(product_name) REFERENCES product;
ALTER TABLE purchase
ADD CONSTRAINT purchase_fk_person FOREIGN KEY
(salesperson) REFERENCES person;
CREATE INDEX purchase_product
ON purchase(product_name);
CREATE INDEX purchase_salesperson
on purchase(salesperson);
INSERT INTO purchase VALUES
('Small Widget', 'CA', '14-ИЮЛ-03', 1);
INSERT INTO purchase VALUES
('Medium Wodget', 'BB', '14-ИЮЛ-03', 75);
INSERT INTO purchase VALUES
('Chrome Phoobar', 'GA', '14-ИЮЛ-03', 2);
INSERT INTO purchase VALUES
('Small Widget', 'GA', '15-ИЮЛ-03', 8);
INSERT INTO purchase VALUES
('Medium Wodget', 'LB', '15-ИЮЛ-03', 20);
INSERT INTO purchase VALUES
('Round Chrome Snaphoo', 'CA', '16-ИЮЛ-03', 5);
INSERT INTO purchase VALUES
('Small Widget', 'CA', '17-ИЮЛ-03', 1);
CREATE TABLE purchase_archive (
product_name VARCHAR2 (25),
salesperson VARCHAR2(3),
purchase_date DATE,
quantity NUMBER (4, 2)
);
INSERT INTO purchase_archive VALUES
('Round Snaphoo', 'BB', '21-ИЮН-01', 10);
INSERT INTO purchase_archive VALUES
('Large Harflinger ' , 'GA', '22-ИЮН-01', 50);
INSERT INTO purchase_archive VALUES
('Medium Wodget', 'LB', '23-ИЮН-01', 20);
INSERT INTO purchase_archive VALUES
('Small Widget', 'ZZ', '24-ИЮН-02', 80);
INSERT INTO purchase_archive VALUES
('Chrome Phoobar', 'CA', '25-ИЮН-02', 2);
INSERT INTO purchase_archive VALUES
('Small Widget', 'JT', '26-ИЮН-02', 50);
There are few errors i can see.
1) Your Purchase table is not having ORDER_NUMBER column which you are trying to use in your code. Add that column and yoour code should work then.
2) Definition of Audit table is not correct. Its referencing a table which doesnot exist. persoon table doesnot exist. Also AUDIT is a reserve keyword, so it should not be used.
You can check those and compile again.
Error you are getting because of spaces between :OLD. quantity . See the working version of code.
CREATE OR REPLACE TRIGGER audit_trigger
BEFORE INSERT OR UPDATE
ON PURCHASE
FOR EACH ROW
DECLARE
no_name_change EXCEPTION;
quant_change NUMBER (5) := 0;
BEGIN
IF (UPDATING AND (:NEW.product_name <> :OLD.product_name))
THEN
RAISE no_name_change;
END IF;
IF ( ( (UPDATING) AND (:OLD.ORDER_NUMBER IS NULL))
OR ( (INSERTING) AND (:NEW.ORDER_NUMBER IS NULL)))
THEN
SELECT order_num_seq.NEXTVAL INTO :NEW.ORDER_NUMBER FROM DUAL;
END IF;
IF (UPDATING)
THEN
quant_change := :NEW.quantity - :OLD.quantity;
ELSE
quant_change := :NEW.quantity;
END IF;
INSERT INTO audit1
VALUES (:NEW.ORDER_NUMBER,
:NEW.salesperson,
USER,
USERENV ('TERMINAL'),
quant_change,
SYSDATE);
EXCEPTION
WHEN no_name_change
THEN
DBMS_OUTPUT.put_line ('Change of product name not allowed');
DBMS_OUTPUT.put_line ('Aborting and resetting to old values');
:NEW.product_name := :OLD.product_name;
:NEW.salesperson := :OLD.salesperson;
:NEW.ORDER_NUMBER := :OLD.ORDER_NUMBER;
:NEW.quantity := :OLD.quantity;
END audit_trigger;
/

Can't get manager output in SQL

Creating Department table
CREATE TABLE department
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL UNIQUE,
ADDRESS VARCHAR2(20) NOT NULL);
Populating Department Table
INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;
Creating Employee table
CREATE TABLE employee
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),
HIRE_DATE DATE NOT NULL,
SALARY NUMBER(9, 2) NOT NULL,
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES department(DEPARTMENT_ID));
Populating Employee table
INSERT INTO employee
VALUES(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee
VALUES(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL, 10);
INSERT INTO employee
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee
VALUES(7999, 'WOLFE', 'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee
VALUES(7952, 'LANCASTER', 'SALESMAN', 7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900, NULL, 40);
INSERT INTO employee
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee
VALUES(8000, 'BREWSTER', 'TBA', NULL, '22-AUG-13', 2500, NULL, NULL);
COMMIT;
QUESTION: Based on the DEPARTMENT and EMPLOYEE tables created in write a PL/SQL anonymous block that accepts an employee ID from the user input and displays 1) the employee’s name, hire date, and total pay (salary + commission), 2) his/her manager’s name, hire date, and total pay (salary + commission), 3) the name of the department where the employee works, 4) the number of employees in the department where the employee works, and 5) the sum of total pay (salary + commission) for the department where the employee works,
So far I've got the query below I am not able to get the manager's name, hire date and total pay (salary + comission) how can I do that?
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate EMPLOYEE.HIRE_DATE%TYPE;
BEGIN
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
DBMS_OUTPUT.PUT_LINE(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE employee_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('USER INPUT:');
DBMS_OUTPUT.PUT_LINE(v_input_emp_id);
DBMS_OUTPUT.PUT_LINE('----------EMPLOYEE----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE manager_id = v_input_emp_id;
IF v_n != 0 THEN
SELECT employee_name ,salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name , v_pay, v_hiredate
FROM employee
WHERE employee.manager_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
END IF;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
ELSE
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NOT IN THE TABLE');
END IF;
END;
You should first get employees manager_id, and then query the manager.
This works for me:
SET SERVEROUTPUT ON
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_mangager_id employee.manager_id%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate employee.hire_date%TYPE;
BEGIN
SELECT COUNT(*) INTO v_n FROM employee WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
dbms_output.put_line(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name
,salary + nvl(commission, 0)
,hire_date
,manager_id
INTO v_emp_name
,v_pay
,v_hiredate
,v_mangager_id
FROM employee
WHERE employee_id = v_input_emp_id;
dbms_output.put_line('USER INPUT:');
dbms_output.put_line(v_input_emp_id);
dbms_output.put_line('----------EMPLOYEE----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
dbms_output.put_line(rpad('HIRE DATE:', 10) || lpad(v_hiredate, 20));
dbms_output.put_line('TOTAL PAY:' || lpad(to_char(round(v_pay, 2), '$9,990.00'), 21));
SELECT COUNT(*) INTO v_n FROM employee WHERE employee_id = v_mangager_id;
IF v_n != 0 THEN
SELECT employee_name
,salary + nvl(commission, 0)
,hire_date
INTO v_emp_name
,v_pay
,v_hiredate
FROM employee
WHERE employee.employee_id = v_mangager_id;
dbms_output.put_line('----------MANAGER----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
dbms_output.put_line(rpad('HIRE DATE:', 10) || lpad(v_hiredate, 20));
dbms_output.put_line('TOTAL PAY:' || lpad(to_char(round(v_pay, 2), '$9,990.00'), 21));
END IF;
dbms_output.put_line('----------MANAGER----------');
dbms_output.put_line(rpad('NAME:', 10, ' ') || lpad(v_emp_name, 15));
ELSE
dbms_output.put_line('EMPLOYEE NOT IN THE TABLE');
END IF;
END;
/
And the result:
USER INPUT:
7610
----------EMPLOYEE----------
NAME: WILSON
HIRE DATE: 2001.12.03
TOTAL PAY: $3,000.00
----------MANAGER----------
NAME: JOST
HIRE DATE: 2001.05.04
TOTAL PAY: $4,500.00
----------MANAGER----------
NAME: JOST
PL/SQL procedure successfully completed

SQL Help(Date Functions)

I need help on SQL Queries.
TABLE:
Create Table Employees
( employee_id number(3) Primary Key,
first_name varchar2(10),
last_name varchar2(10),
dept_code varchar2(3),
hire_date date,
credit_limit number(4,2),
phone_ext varchar2(4),
manager_id number(3)
);
Entries:
insert into Employees values (201, 'Susan', 'Brown', 'Exe', To_Date('01-Jun-1998','DD-Mon-YYYY'), 30, '3484', null);
insert into Employees values (202, 'Jim', 'Kern', 'Sal', To_Date('16-Aug-1999','DD-Mon-YYYY'), 25, '8722', 201);
insert into Employees values (203, 'Martha', 'Woods', 'Shp', To_Date('02-Feb-2004','DD-Mon-YYYY'), 25, '7591', 201);
insert into Employees values (204, 'Ellen', 'Owens', 'Sal', To_Date('01-Jul-2003','DD-Mon-YYYY'), 15, '6830', 202);
I need a query to List all the employees, their hire dates and the number of days each person will have worked for the company as of January 1, 2007.
You can use YEAR() on a date field:
SELECT * FROM Employees WHERE YEAR(hire_date)='2003'
if it doesn't work, you can use EXTRACT():
SELECT * FROM Employees WHERE EXTRACT(YEAR FROM hire_date)='2003'
and, as Alan Hadsell rightly commented:
SELECT COUNT(*) FROM Employees WHERE EXTRACT(YEAR FROM hire_date)='2003'

Error with insterting values

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.