how can i Add commission_pct column in oracle - sql

I put the schema manually, but there is no commission_pct column. How can i add it?
I can't find commission_pct data anywhere...
plz help me :(
select employee_id as 사원번호,
first_name || ' ' || last_name as 사원명,
nvl( salary + (salary * commission_pct), salary)*12 as 연봉,
department_id as 부서번호
from employees
where department_id in(30,50,60) and
nvl( salary + (salary * commission_pct), salary)*12 >= 20000 and
nvl( salary + (salary * commission_pct), salary)*12 <= 60000
order by 부서번호, 연봉 desc;```
ORA-00904: "COMMISSION_PCT": 부적합한 식별자
00904. 00000 - "%s: invalid identifier

That's kind of strange; how are you supposed to work with column that doesn't exist? Are you sure you're connected to right schema and have all tables you should?
As of your question, you'd add a new column into a table with alter table. Here's how: my employees table is a dummy one, it does not represent your table.
SQL> desc employees
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPLOYEE_ID NOT NULL NUMBER
FIRST_NAME NOT NULL VARCHAR2(30)
LAST_NAME NOT NULL VARCHAR2(30)
HIRE_DATE NOT NULL DATE
SALARY NOT NULL NUMBER(8,2)
SQL> alter table employees add commision_pct number;
Table altered.
To set a value, you'd update table as
SQL> update employees set commision_pct = 15;
1 row updated.
SQL>

Related

Problem inserting into table in oracle using PopSQL

The problem i'm having is that using PopSQL, when i insert something into a table it says success but then it inserts nothing into the table by saying 0 rows affected and i can't figure out what is the problem because PopSQL works fine with postgres and Mysql. Any help is much appreciated.
CREATE TABLE employees
(
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
hire_date DATE NOT NULL,
salary NUMBER(8,2) NOT NULL
);
INSERT INTO EMPLOYEES VALUES (100, 'Malik', 'Makkes', '01-JAN-2010', 9000);
COMMIT;
SELECT * FROM employees;
Seem a Sys error to me
Try this :
INSERT INTO employees (employee_id, first_name, last_name, hire_date, salary)
VALUES (100, 'Malik', 'Makkes', '01-JAN-2010', 9000);
COMMIT;
SELECT * FROM employees;
Make sure to change the VALUES clause to match the correct number of columns and data types in the employees table.
As Jonas commented, "date" value (string, actually; you just enclosed some digits and letters into single quotes) looks suspicious. Read his comment once again.
I'm in Croatia. Let's see what happens in my database.
SQL> CREATE TABLE employees
2 (
3 employee_id NUMBER PRIMARY KEY,
4 first_name VARCHAR2(30) NOT NULL,
5 last_name VARCHAR2(30) NOT NULL,
6 hire_date DATE NOT NULL,
7 salary NUMBER(8,2) NOT NULL
8 );
Table created.
SQL> INSERT INTO EMPLOYEES VALUES (100, 'Malik', 'Makkes', '01-JAN-2010', 9000);
INSERT INTO EMPLOYEES VALUES (100, 'Malik', 'Makkes', '01-JAN-2010', 9000)
*
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
Error; can't say I didn't expect it. What does default NLS value (in my database) say about dates?
SQL> select sysdate right_now,
2 to_char(sysdate, 'dd-mon-yyyy') another_Format
3 from dual;
RIGHT_NOW ANOTHER_FORMAT
---------- --------------------
04.02.2023 04-vel-2023
SQL>
Right; format doesn't match, nor does the language. How to fix it?
One option is to use date literal instead of a string:
SQL> INSERT INTO EMPLOYEES VALUES (100, 'Malik', 'Makkes', date '2010-01-01', 9000);
1 row created.
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAME LAST_NAME HIRE_DATE SALARY
----------- --------------- --------------- ---------- ----------
100 Malik Makkes 01.01.2010 9000
SQL>
That works. Another option is to use to_date function with appropriate format model:
SQL> INSERT INTO EMPLOYEES VALUES (100, 'Malik', 'Makkes',
2 to_date('01-jan-2010', 'dd-mon-yyyy', 'nls_date_language = english'), 9000);
1 row created.
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAME LAST_NAME HIRE_DATE SALARY
----------- --------------- --------------- ---------- ----------
100 Malik Makkes 01.01.2010 9000
SQL>
That works too. Yet another option is to alter your session (or, if you must, NLS settings for the whole database, but for that you'll need to talk to your DBA):
SQL> alter session set nls_date_format = 'dd-mon-yyyy';
Session altered.
SQL> alter session set nls_date_language = 'english';
Session altered.
SQL> INSERT INTO EMPLOYEES VALUES (100, 'Malik', 'Makkes', '01-JAN-2010', 9000);
1 row created.
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAME LAST_NAME HIRE_DATE SALARY
----------- --------------- --------------- ----------- ----------
100 Malik Makkes 01-jan-2010 9000
SQL>
Works again.
Now you've seen a few valid ways to insert date value into a date datatype column. Pick one you find the most appropriate. My choice would be a date literal.
On the other hand, if that's not the issue, provide more info (illustrate execution of your code; post exact error you got).

Why do I get a 00904. 00000 - "%s: invalid identifier" error in this code?

I am trying to create 3 tables, and insert data into them by having employees with below 25% of average salary in EMPLOYEE_BOTTOM_25, employees above 10% of salary range in EMPLOYEE_TOP_10, and all other employees in EMPLOYEE_OTHERS.
I used a conditional insert for this, and used a select statement, but it says I keep having an error in the "Salary" right before the select statement (In "** **", Bolded.) How should I resolve this?
CREATE TABLE EMPLOYEE_BOTTOM_25 (
Employee_id VARCHAR2(10),
Employee_name VARCHAR2(35),
Job_title VARCHAR2(35),
Salary NUMBER (9,2)
);
CREATE TABLE EMPLOYEE_TOP_10 (
Employee_id VARCHAR2(10),
Employee_name VARCHAR2(35),
Job_title VARCHAR2(35),
Salary NUMBER (9,2)
);
CREATE TABLE EMPLOYEE_OTHERS (
Employee_id VARCHAR2(10),
Employee_name VARCHAR2(35),
Job_title VARCHAR2(35),
"Salary" NUMBER (9,2)
);
INSERT ALL
WHEN Salary <= salarywage * 0.25 THEN
INTO EMPLOYEE_BOTTOM_25
VALUES(Employee_id,Employee_name,Job_title,Salary)
WHEN Salary >= salarywage * 0.9 THEN
INTO EMPLOYEE_TOP_10
VALUES(Employee_id,Employee_name,Job_title,Salary)
WHEN Salary <= salarywage * 0.25 AND Salary >= Salary * 0.9 THEN
INTO EMPLOYEE_OTHERS
VALUES(Employee_id, Employee_name, Job_title, **Salary**)
Select employeeid, firstname || ', ' || lastname as name, jobtitle, avg(salarywage) as salaraywage
From Employee
group by employeeid, firstname || ', ' || lastname, jobtitle;

How to rename a column in a query in ORACLE

Is this query right for changing the name of a column in the employees table:
select first_name, rename_column('first_name' to 'empName') from employees;
there are two ways
1) give alias name to the column
SELECT first_name AS emp_name
FROM employee;
2) change column name
alter table employee
rename first_name to emp_name ;
To set an alias for a field use the following:
SELECT first_name AS "empName", hire_date AS "Tenure On December 31 2014"
FROM employees;
alter table employee
rename column first_name to emp_name ;
If you are going to change the name just in your current result-set, you can do following:
select first_name AS emp_name from employees;
if you are going to change the name permanently, you have to work with ALTER TABLE:
ALTER TABLE employees CHANGE first_name emp_name VARCHAR2(255);

PL/SQL creating a trigger problems

I have to create a trigger on an Employee table. If an INSERT or UPDATE statement is issued for the Employee table the trigger launches and makes sure that the value of 'salary' field meets the criteria in the job_min_sal table. After trying over and over I got a mutating table error and now am very frustrated and don't know what to do.
JOB_MIN_SALARY TABLE:
JOB VARCHAR2(50) PRIMARY KEY
MIN_SAL NUMBER(7,2) NOT NULL
The JOB_MIN_SAL table is populated with a variety of job titles and salaries. I am confused working with my trigger and wondering if I could get some assistance where to go from here
CREATE OR REPLACE TRIGGER employee_job_salary
BEFORE INSERT OR UPDATE OF SALARY on employee
FOR EACH ROW
DECLARE
v_salary NUMBER;
BEGIN
SELECT minimum_salary
INTO v_salary
FROM job_min_salary
WHERE UPPER(job) = UPPER(:NEW.job);
I know I am really far off I am just looking for help as for what this requires and what steps I need to take to get this. Thanks!
The EMPLOYEE table:
(
EMPLOYEE_ID NUMBER(4)
EMPLOYEE_NAME VARCHAR2(20)
JOB VARCHAR2(50)
MANAGER_ID NUMBER(4)
HIRE_DATE DATE
SALARY NUMBER(9)
COMMISION NUMBER(9)
DEPARTMENT_ID NUMBER(4)
);
i am supposing you are doing something like
comparing new salary with min salary criterion and update only if :new.SALARY >= v_salary
what are you doing if this is not met, are u trapping an exception or just ignoring the error or returning an error code to debug.
post more info
CREATE TABLE job_min_salary
(
job VARCHAR2(50) PRIMARY KEY,
min_sal NUMBER(7,2) NOT NULL
);
INSERT INTO job_min_salary VALUES('CEO','100');
-- 1 rows inserted.
CREATE TABLE employee
(
employee_id NUMBER(4),
employee_name VARCHAR2(20),
job VARCHAR2(50),
manager_id NUMBER(4),
hire_date DATE,
salary NUMBER(9),
commision NUMBER(9),
department_id NUMBER(4)
);
INSERT INTO employee VALUES(1, 'Name', 'CEO', 1, TO_DATE('2000-01-01', 'YYYY-MM-DD'), 80, 80, 1);
-- 1 rows inserted.
CREATE OR REPLACE TRIGGER employee_job_salary
BEFORE INSERT OR UPDATE OF salary ON employee
FOR EACH ROW
DECLARE
v_salary NUMBER(1);
BEGIN
SELECT 1
INTO v_salary
FROM job_min_salary
WHERE UPPER(job) = UPPER(:NEW.job)
AND :NEW.salary >= min_sal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR(-20999, 'Salary value is too low for given job');
END;
-- TRIGGER EMPLOYEE_JOB_SALARY compiled
SELECT * FROM employee;
-- 1 Name CEO 1 2000-01-01 00:00:00 80 80 1
UPDATE employee
SET salary = 10
WHERE job = 'CEO';
-- ORA-20999: Salary value is too low for given job
UPDATE employee
SET salary = 100
WHERE job = 'CEO';
-- 1 rows updated.
SELECT * FROM employee;
-- 1 Name CEO 1 2000-01-01 00:00:00 100 80 1

How to write a trigger to move data I want to delete from a table to antother before deleting it

I wrote this trigger which monitor a table called employees , and when deleting an employee from that table the trigger should fire , and copy the employee we want to delete from the table employees and put in another table called deleted_employees , but when I put the following sql statement
delete from employees where employee_id = 100
SQL Error says :
Error starting at line 17 in command:
delete from employees where employee_id = 100
Error report:
SQL Error: ORA-02292: integrity constraint (HR.DEPT_MGR_FK) violated - child
record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign
dependency.
*Action: delete dependencies first then parent or disable constraint.
CREATE OR REPLACE TRIGGER EMPLOYEE_DELETED
BEFORE DELETE ON EMPLOYEES
DECLARE
CURSOR CUR_EMP IS
SELECT EMPLOYEE_ID , FIRST_NAME , LAST_NAME , EMAIL ,
PHONE_NUMBER , HIRE_DATE , JOB_ID , SALARY ,
COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID
FROM EMPLOYEES;
EMP_REC CUR_EMP%ROWTYPE;
BEGIN
OPEN CUR_EMP;
while(CUR_EMP%FOUND) LOOP
FETCH CUR_EMP INTO EMP_REC;
INSERT INTO deleted_employees
(EMPLOYEE_ID , FIRST_NAME , LAST_NAME ,
EMAIL ,
PHONE_NUMBER , HIRE_DATE ,job_id , salary ,
COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID)
VALUES
(EMP_REC.EMPLOYEE_ID ,EMP_REC.FIRST_NAME ,EMP_REC.LAST_NAME ,
EMP_REC.EMAIL , EMP_REC.PHONE_NUMBER , EMP_REC.HIRE_DATE ,
EMP_REC.JOB_ID , EMP_REC.SALARY , EMP_REC.COMMISSION_PCT ,
EMP_REC.MANAGER_ID , EMP_REC.DEPARTMENT_ID);
END LOOP;
CLOSE CUR_EMP;
END;
I don't know how to test the trigger , any ideas ?!
One thing I observed that you are, by mistake, inserting all the records from EMPLOYEE table to DELETED_EMPLOYEES.
I think you want to insert the employee detail into deleted_employee table which is currently in delete action.
For this in Oracle you can using :OLD keyword to refer the current running record.
well if you want to delete related chile records as well .. then you can do in this trigger itself without any problem -
CREATE OR REPLACE TRIGGER EMPLOYEE_DELETED
BEFORE DELETE ON EMPLOYEES
FOR EACH ROW
DECLARE
BEGIN
INSERT INTO deleted_employees
(
EMPLOYEE_ID , FIRST_NAME , LAST_NAME , EMAIL ,
PHONE_NUMBER , HIRE_DATE ,job_id , salary ,
COMMISSION_PCT , MANAGER_ID , DEPARTMENT_ID
)
VALUES
(:OLD.EMPLOYEE_ID ,:OLD.FIRST_NAME ,:OLD.LAST_NAME , :OLD.EMAIL ,
:OLD.PHONE_NUMBER , :OLD.HIRE_DATE , :OLD.JOB_ID , :OLD.SALARY ,
:OLD.COMMISSION_PCT , :OLD.MANAGER_ID , :OLD.DEPARTMENT_ID);
DELETE from DEPARTMENT where EMPLOYEE_ID = :OLD.EMPLOYEE_ID; -- If you are deleting child record then you will not get ORA:02292 error
END;
Just edit delete part in this trigger as per your database.
for this you need to first check for the key constraint DEPT_MGR_FK.
I guessed that this key is present in DEPARTMENT table on EMPLOYEE_ID column.
so check for this key and change the second last line and then compile.