How to create a view - oracle

I was trying to create a view with information of latest hired employee in each branch. But i was not succeed in that. Can anyone direct me towards the right path.
CREATE TABLE BRANCH(
BRANCH_ID NUMBER(8) PRIMARY KEY,
BRANCH_NAME VARCHAR2(100) NOT NULL,
SCHEDULE_LINK_NUM NUMBER(8),
MAIN_BRANCH_ID number(8),
BRANCH_MGR_ID NUMBER(8),
BRANCH_OPEN_DATE DATE,
EMAIL VARCHAR2(50),
URL VARCHAR2(50)
);
CREATE TABLE EMPLOYEE(
EMPLOYEE_ID NUMBER(8) PRIMARY KEY,
FIRST_NAME VARCHAR2(50),
LAST_NAME VARCHAR2(50),
MIDDLE_NAME VARCHAR2(50),
GENDER CHAR(1),
SSN NUMBER(9) NOT NULL,
DOB DATE,
MARITAL_STATUS VARCHAR2(30),
SPOUSE_NAME VARCHAR2(50),
HOME_PHONE NUMBER(10),
CELL_PHONE NUMBER(10),
OFFICE_PH_EXTN NUMBER(6),
EMPLOYEE_TYPE VARCHAR2(40),
SALARY NUMBER(10,2),
TAX_DEDUCTION NUMBER(10,2),
BRANCH_ID NUMBER(8),
MGR_ID NUMBER(8),
CONSTRAINT EMP_BRANCH_FKEY FOREIGN KEY(BRANCH_ID) REFERENCES BRANCH(BRANCH_ID),
CONSTRAINT EMP_EMPTYPE_CHECK CHECK(EMPLOYEE_TYPE IN ('MANAGER', 'MECHANIC', 'SECRETARY', 'SALES PERSON')),
CONSTRAINT EMP_MARITSTATUS_CHECK CHECK(MARITAL_STATUS IN ('SINGLE', 'MARRIED', 'DIVORCED'))
);
CREATE TABLE EMP_WORK_HISTORY(
BRANCH_EMP_NUM NUMBER(8) PRIMARY KEY,
BRANCH_ID NUMBER(8),
EMPLOYEE_ID NUMBER(8),
JOIN_DATE DATE NOT NULL,
RELIEVING_DATE DATE,
EMPLOYEE_TYPE VARCHAR2(40),
DESCRIPTION VARCHAR2(200),
CONSTRAINT BRANCH_WRKHIS_BID_FKEY FOREIGN KEY(BRANCH_ID) REFERENCES BRANCH(BRANCH_ID),
CONSTRAINT BRANCH_WRKHIS_EID_FKEY FOREIGN KEY(EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT EMP_WRKHIS_EMPTYPE_CHECK CHECK(EMPLOYEE_TYPE IN ('MANAGER', 'MECHANIC', 'SECRETARY', 'SALES PERSON'))
);
I have written the view like below:
CREATE VIEW LAST_EMPLOYEE_BRANCH AS
WITH LAST_EMP_BRANCH AS
(SELECT MAX(EWH.JOIN_DATE) AS LAST_HIRED_DATE, EWH.BRANCH_ID
FROM EMP_WORK_HISTORY EWH GROUP BY EWH.BRANCH_ID)
SELECT E.FIRST_NAME || E.LAST_NAME AS EMPLOYEE_NAME,
B.BRANCH_NAME, LEB.LAST_HIRED_DATE, E.SALARY,
EXTRACT(YEAR FROM NUMTOYMINTERVAL(MONTHS_BETWEEN(TRUNC(SYSDATE),E.DOB),'MONTH')) AS AGE
FROM EMPLOYEE E, BRANCH B, LAST_EMP_BRANCH LEB
WHERE E.BRANCH_ID = B.BRANCH_ID
AND LEB.BRANCH_ID = E.BRANCH_ID;
By running the above view i am getting all employees records. Can anyone help on this?

Looks like you forgot to include the EMP_WORK_HISTORY table and the LAST_HIRED_DATE column in the join. Try the query below:
CREATE OR REPLACE VIEW LAST_EMPLOYEE_BRANCH AS
WITH LAST_EMP_BRANCH AS
(SELECT MAX(EWH.JOIN_DATE) AS LAST_HIRED_DATE, EWH.BRANCH_ID
FROM EMP_WORK_HISTORY EWH GROUP BY EWH.BRANCH_ID)
SELECT E.FIRST_NAME || E.LAST_NAME AS EMPLOYEE_NAME,
B.BRANCH_NAME, LEB.LAST_HIRED_DATE, E.SALARY,
EXTRACT(YEAR FROM NUMTOYMINTERVAL(MONTHS_BETWEEN(TRUNC(SYSDATE),E.DOB),'MONTH')) AS AGE
FROM EMPLOYEE E, BRANCH B, LAST_EMP_BRANCH LEB, EMP_WORK_HISTORY EWH
WHERE E.BRANCH_ID = B.BRANCH_ID
AND LEB.BRANCH_ID = E.BRANCH_ID
AND E.EMPLOYEE_ID = EWH.EMPLOYEE_ID
AND B.BRANCH_ID = EWH.BRANCH_ID
AND LEB.LAST_HIRED_DATE = EWH.JOIN_DATE;

Related

TRIGGERS_function

please help me to find the solution i have a project .
so my project is about a mobile repair store , we have some tables :
customers, mobiles, repairjob
//customer table
CREATE TABLE CUSTOMERS (
CUSTOMER_CPR NUMBER(9) PRIMARY KEY,
FIRST_NAME VARCHAR2(30) NOT NULL,
MID VARCHAR2(30)
LAST_NAME VARCHAR2(30) NOT NULL,
EMAIL VARCHAR2(30) NOT NULL UNIQUE,
HOME_TEL NUMBER(8) UNIQUE,
HOUSE_NO VARCHAR2(30) NOT NULL,
ROAD_NO VARCHAR2(30) NOT NULL,
BLOCK_NO VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) NOT NULL
);
//mobiles table
CREATE TABLE MOBILES (
MOB_ID NUMBER(8,0) PRIMARY KEY,
MODEL VARCHAR2(30) NOT NULL,
MOB_DECRIPTION VARCHAR2(100),
SERIAL_NUM VARCHAR2(30) UNIQUE,
MAKE VARCHAR2(50),
customer_cpr number(9),
);
//repair job table
CREATE TABLE REPAIR JOB (
JOB_NUM NUMBER primary key,
customer_cpr number(9)
EMP_ID NUMBER(8,0) NOT NULL,
DATE_RECEIVED DATE,
DATE_TO_RETURN DATE,
ITEM_ID NUMBER
);
so my question is to make a trigger function , we have assumed that the customer cannot have more than 3 repair jobs or can't fix more than 3 mobiles at one time until it finishes all. so how i make a triiger function ?

Foreign key keeps saying not enough values

I have been trying to get this thing working for a few day now (foreign key) and it just don't work, and feel like every solution I used don't work, so i'm asking here to learn what was the problem and how to fix it
Table creation :
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB varchar(20) ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Cust_ID varchar(255),
Payment_Method varchar(30),
Payment_Date varchar(20),
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
Inserting values :
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO CUSTOMER VALUES ('3423','Bryan','Male', ( TO_Date ( '15/06/1990' , 'DD/MM/YYYY')));
INSERT INTO CUSTOMER VALUES ('4385','Mohd Shafik','Male',( TO_Date ( '20/08/1993' , 'DD/MM/YYYY')));
INSERT INTO PAYMENT VALUES ('24P','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
INSERT INTO PAYMENT VALUES ('09p','Online Transfer', ( TO_Date ( '08/04/2022' , 'DD/MM/YYYY')),25.00);
INSERT INTO PAYMENT VALUES ('10P','Cash', ( TO_Date ( '08/07/2022' , 'DD/MM/YYYY')),22.50);
The foreign keys are now working , but just for life of me can't figure out why the it spits out ORA-00947: not enough values and
ORA-01400: cannot insert NULL into ("SQL_GUUNNGDQAOXJVYPBKNMILVXJR"."PAYMENT"."PAYMENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721
Also, Please explain to me how references works, i read a few places but they use words that just confuse me. Please and thank you!
Use DATE data types to store date values (and VARCHAR2 instead of VARCHAR) in your tables:
CREATE TABLE CUSTOMER(
Customer_ID varchar2(255) NOT NULL,
Customer_Name varchar2(50),
Customer_Gender varchar2(10),
Customer_DOB DATE,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
);
CREATE TABLE PAYMENT(
Payment_ID varchar2(255) NOT NULL,
Cust_ID varchar2(255),
Payment_Method varchar2(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
);
Then name the columns in your INSERT statements:
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'1277','Jenny','Female', TO_Date('03/04/1988', 'DD/MM/yyyy')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'3423','Bryan','Male', TO_Date('15/06/1990', 'DD/MM/YYYY')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'4385','Mohd Shafik','Male', TO_Date('20/08/1993' , 'DD/MM/YYYY')
);
Then for the PAYMENT inserts, you have 5 columns in the table but only 4 pieces of data being inserted:
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'24P','Cash', TO_Date('11/02/2022', 'DD/MM/YYYY'),24.50
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'09p','Online Transfer', TO_Date( '08/04/2022' , 'DD/MM/YYYY'),25.00
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'10P','Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
You have not provided a Cust_ID value so it will default to NULL in those rows.
If you want to provide a Cust_ID then add it to the statement:
INSERT INTO PAYMENT (
payment_id, cust_id, payment_method, payment_date, payment_total
) VALUES (
'ABC', '1277', 'Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
db<>fiddle here
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB DATE ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Customer_ID varchar(255),
Payment_Method varchar(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Customer_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO PAYMENT VALUES ('24P','1277','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
SELECT * FROM Customer;
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER CUSTOMER_DOB
1277 Jenny Female 03-APR-1988
SELECT * FROM payment;
PAYMENT_ID CUSTOMER_ID PAYMENT_METHOD PAYMENT_DATE PAYMENT_TOTAL
24P 1277 Cash 11-FEB-2022 24.5

Oracle Batch Database

I'm trying to run my databases but STUDENTS AND BATCHES tables are giving the error: 'ORA-00942: table or view does not exist'. I've tried dropping them in order, but I can't tell what exactly the problem is.
Forgive me if the answer is obvious, I'm new to batches
DROP TABLE students;
DROP TABLE batches;
DROP TABLE courses;
DROP TABLE faculty;
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
Create your faculty and courses tables first. Since batches depends on one of those tables, you would want to have those tables created first.
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6),
prereq VARCHAR2(100)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Example: http://sqlfiddle.com/#!4/91909c/1 shows the sequence of table creation

Unable to use composite key as foreign key

CREATE TABLE departments
( department_id number(10) NOT NULL,
department_name varchar2(50) NOT NULL,
department_code varchar2(50) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id, department_code)
);
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id, department_code)
REFERENCES departments(department_id,department_code));
Your employees table doesn't have a department_code field, so the FOREIGN KEY (department_id, department_code) part is trying to use something that doesn't exist. Which is what the error you get from running the second statement tells you:
ERROR at line 8:
ORA-00904: "DEPARTMENT_CODE": invalid identifier
You would either have to include the department code in that table, which would denormalise the data; or change the primary key on departments to just department_id, which would be much more normal anyway, i.e.:
CREATE TABLE departments
( department_id number(10) NOT NULL,
department_name varchar2(50) NOT NULL,
department_code varchar2(50) NOT NULL,
CONSTRAINT departments_pk PRIMARY KEY (department_id)
);
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id)
REFERENCES departments(department_id));
It doesn't usually make sense to have a composite primary key like that, where there is a single column that looks like it should be unique anyway. You wouldn't expect to have the same department_id with two department_code values.
But if you do have a legitimate reason to have a composite primary key then all columns in that key will have to be duplicated on the child tables and their foreign key constraints:
CREATE TABLE employees
( employee_number number(10) NOT NULL,
employee_name varchar2(50) NOT NULL,
department_id number(10),
department_code varchar2(50) NOT NULL,
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number),
CONSTRAINT fk_departments
FOREIGN KEY (department_id, department_code)
REFERENCES departments(department_id,department_code));
Table EMPLOYEES created.
Which means that whenever you insert a record into that table you will have to supply both the ID and code for an existing department, of course.

FOREIGN KEY Connection

So I have table customers and table bookings
I want to add a Foreign Key to the script so that
the CustID from customers can make a column in bookings and connect.
create table customers(
CustID INT NOT NULL,
CustomerName VARCHAR2(25),
CustomerAddress VARCHAR2(50),
CustomerPhone NUMBER(10),
CONSTRAINT pk_cust PRIMARY KEY (CustID) );
and
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers(CustID)
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25));
error;
StartDate VARCHAR2(25),
*
ERROR at line 10:
ORA-00907: missing right parenthesis
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25),
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers(CustID));
You have to declare the primary key and other constraints after you finish declaring the columns.
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers1 (CustID),
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25));
see it carefully -- you Have to add comma after this CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers1 (CustID),

Resources