CREATE TABLE customer_master(
cust_id NUMBER(6) CUSTOMER_SEQ.Nextval PRIMARY KEY,
cust_password VARCHAR2(10),
cust_fname VARCHAR2(30),
cust_lname VARCHAR2(30),
cust_gender VARCHAR2(8),
cust_dob DATE,
cust_email VARCHAR2(30),
cust_password VARCHAR2(30),
cust_mobile NUMBER(10),
cust_add VARCHAR2(100),
newsletter VARCHAR2(5) CHECK (newsletter IN('yes','no')),
mob_service VARCHAR2(5) CHECK (mob_service IN('yes','no'))
);
Try to use trigger.
CREATE TABLE customer_master(
cust_id NUMBER(6) PRIMARY KEY,
cust_password VARCHAR2(10),
cust_fname VARCHAR2(30),
cust_lname VARCHAR2(30),
cust_gender VARCHAR2(8),
cust_dob DATE,
cust_email VARCHAR2(30),
cust_mobile NUMBER(10),
cust_add VARCHAR2(100),
newsletter VARCHAR2(5) CHECK (newsletter IN('yes','no')),
mob_service VARCHAR2(5) CHECK (mob_service IN('yes','no'))
);
CREATE OR REPLACE TRIGGER INS_customer_master
BEFORE INSERT ON customer_master
FOR EACH ROW
DECLARE
i_LAST NUMBER;
BEGIN
SELECT CUSTOMER_SEQ.NEXTVAL INTO i_LAST FROM Dual;
:NEW.cust_id := i_LAST;
END;
/
Related
create table EMPLOYEES
(
EMPLOYEE_ID NUMBER(6) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(25),
EMAIL VARCHAR2(25),
PHONE_NUMBER VARCHAR2(20) DEFAULT NULL,
HIRE_DATE DATE(7),
JOB_ID VARCHAR2(10),
SALARY NUMBER(8,2) DEFAULT NULL,
COMISSION_PCT NUMBER(2,2) DEFAULT NULL,
MANAGER_ID NUMBER(6) DEFAULT NULL,
DEPARTMENT_ID NUMBER(4) DEFAULT NULL
);
This is what I want to create (constraints and other information)
date datatype doesn't accept the length so no need to write date(7) just simply write date and after you run the command desc EMPLOYEES; it will show you that the length of date is by default 7.
create table EMPLOYEES(
EMPLOYEE_ID NUMBER(6) PRIMARY KEY,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(25) NOT NULL,
EMAIL VARCHAR2(25) NOT NULL,
PHONE_NUMBER VARCHAR2(20) DEFAULT NULL,
HIRE_DATE DATE NOT NULL,
JOB_ID VARCHAR2(10) NOT NULL,
SALARY NUMBER(8,2) DEFAULT NULL,
COMISSION_PCT NUMBER(2,2) DEFAULT NULL,
MANAGER_ID NUMBER(6) DEFAULT NULL,
DEPARTMENT_ID NUMBER(4) DEFAULT NULL
);
--dept table
create table department(
dept_id number(5) ,
dept_name varchar2(100),
dept_city varchar2(100) ,
dept_country varchar2(100),
CONSTRAINT dept_pk PRIMARY KEY(dept_id)
);
insert into department( dept_id, dept_name, dept_city, dept_country )values(1,'hr','hyderabad','india');
insert into department( dept_id, dept_name, dept_city, dept_country )values(2,'marketing','banglore','india');
insert into department(dept_id, dept_name, dept_city, dept_country)values(3,'sales','dhaka','bangladesh');
create sequence s1
start with 1
increment by 1;
create table employee(
employee_id number(10) ,
employee_name varchar2(100) NOT NULL,
employee_age number(3) ,
employee_sal number(9,2),
dept_id number(5),
CONSTRAINT employee_pk PRIMARY KEY(employee_id),
constraint dept_fk foreign key(dept_id) references department(dept_id)
);
CREATE OR REPLACE TRIGGER trg_before_emp_insr
BEFORE INSERT
on employee_details
FOR EACH ROW
DECLARE
emp_age number;
BEGIN
IF (employee_age < 18) THEN
RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.');
END IF;
END;
/
insert into employee(employee_id, employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'ravi',45,7333,1);
insert into employee(employee_id, employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'sai',74,4451,2);
insert into employee(employee_id, employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'chandu',35,9428,3);
insert into employee( employee_id,employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'raju',7,25422,2);
insert into employee( employee_id,employee_name, employee_age, employee_sal,dept_id )values(s1.nextval,'teja',36,7955,1);
select * from employee
You want to use the :NEW record to get the value from the row being inserted (and to use the EMPLOYEE table rather than EMPLOYEE_DETAILS):
CREATE OR REPLACE TRIGGER trg_before_emp_insr
BEFORE INSERT
on employee
FOR EACH ROW
BEGIN
IF (:NEW.employee_age < 18) THEN
RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.');
END IF;
END;
/
db<>fiddle here
However, you should consider storing date of birth rather than age as tomorrow (or definitely next year) the age value will be outdated but storing the date of birth and calculating the age would not.
create table employee(
employee_id number(10) ,
employee_name varchar2(100) NOT NULL,
employee_dob DATE,
employee_sal number(9,2),
dept_id number(5),
CONSTRAINT employee_pk PRIMARY KEY(employee_id),
constraint dept_fk foreign key(dept_id) references department(dept_id)
);
CREATE OR REPLACE TRIGGER trg_before_emp_insr
BEFORE INSERT
on employee
FOR EACH ROW
BEGIN
IF :NEW.employee_dob > TRUNC(ADD_MONTHS(SYSDATE, -18*12)) THEN
RAISE_APPLICATION_ERROR(-20000,'Employee age must be greater than or equal to 18.');
END IF;
END;
/
db<>fiddle here
I have been tasked to populate a table called Sales_Facts with a PL/SQL block but I return 0 results. The procedure is executed with out error and I run my script to populate my table but my SELECT COUNT script returns nothing. I cannot see what I am doing wrong.
Here's what I have:
CREATE TABLE Sales (
sale_ID VARCHAR2(10) NOT NULL,
salesperson_ID VARCHAR2(10) NOT NULL,
cust_ID VARCHAR2(10) NOT NULL,
sale_date DATE,
VIN VARCHAR2(20) NOT NULL,
mileage INT,
vehicle_status VARCHAR2(15),
gross_sale_price NUMBER(8,2) NOT NULL,
PRIMARY KEY (sale_ID),
CONSTRAINT FK_Customer_ID FOREIGN KEY (cust_ID) REFERENCES Customers(cust_ID),
CONSTRAINT FK_VIN_ID FOREIGN KEY (VIN) REFERENCES Sale_Vehicles(VIN));
CREATE TABLE Times (
sale_day DATE NOT NULL, --populated from Sales sale_date
day_type VARCHAR2(50) NOT NULL,
PRIMARY KEY (sale_day));
CREATE TABLE Vehicles (
vehicle_Code VARCHAR2(10),
description VARCHAR2(100),
PRIMARY KEY (vehicle_Code));
Vehicles is populated with this:
CREATE SEQUENCE veh_code_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
COMMIT;
--PL/SQL Block
SET SERVEROUTPUT ON
DECLARE
vehType VARCHAR2(50);
v_make OLTP_Vehicles.make%type;
v_model OLTP_Vehicles.model%type;
CURSOR v_type IS SELECT DISTINCT make, model FROM OLTP_Vehicles;
BEGIN
OPEN v_type;
LOOP
FETCH v_type INTO v_make, v_model;
vehType := v_make || ', ' || v_model;
INSERT INTO Vehicles (vehicle_Code, description)
VALUES (veh_code_seq.NEXTVAL, vehType);
EXIT WHEN v_type%notfound;
END LOOP;
CLOSE v_type;
END;
/
CREATE TABLE Financing_Plans (
plan_ID VARCHAR2(10) NOT NULL,
institution VARCHAR2(25) NOT NULL,
loan_type VARCHAR2(15) NOT NULL,
percentage DECIMAL(4,2) NOT NULL,
min_down NUMBER(8,2) NOT NULL,
max_loan_amt NUMBER(8,2) NOT NULL,
max_term INT NOT NULL,
PRIMARY KEY (plan_ID));
CREATE TABLE Dealerships (
dealer_ID VARCHAR2(5) NOT NULL,
location VARCHAR(30) NULL,
region_ID VARCHAR(5) NULL,
street_address VARCHAR2(100) NOT NULL,
city VARCHAR2(25) NOT NULL,
state VARCHAR2(15) NOT NULL,
zip VARCHAR2(5) NOT NULL,
phone VARCHAR2(10) NOT NULL,
sqft NUMERIC(8,2) NULL,
opened_date DATE,
manager VARCHAR2(100) NULL,
district_ID VARCHAR2(5) NOT NULL,
PRIMARY KEY (dealer_ID),
CONSTRAINT UC_Dealership UNIQUE (dealer_ID,district_ID));
CREATE TABLE Sales_Facts (
sale_day DATE NOT NULL,
vehicle_Code VARCHAR2(10) NOT NULL,
plan_ID VARCHAR2(10) NOT NULL,
dealer_ID VARCHAR2(5) NOT NULL,
vehicles_sold NUMBER(8,2) NOT NULL,
gross_sales_amt NUMBER(8,2) NOT NULL,
CONSTRAINT PK_Sales_Facts PRIMARY KEY (sale_day, vehicle_Code, plan_ID, dealer_ID),
CONSTRAINT FK_Sale_Day FOREIGN KEY(sale_day) References Times(sale_day),
CONSTRAINT FK_Vehicle_Code FOREIGN KEY(vehicle_Code) References Vehicles(vehicle_Code),
CONSTRAINT FK_Fin_Plan_ID FOREIGN KEY(plan_ID) References Financing_Plans(plan_ID),
CONSTRAINT FK_Dealer_ID FOREIGN KEY(dealer_ID) References Dealerships(dealer_ID));
And here is my procedure that is not returning any results:
CREATE OR REPLACE PROCEDURE Populate_Sales_Facts
AS
l_sale_day DATE;
l_vehicle_Code VARCHAR2(10);
l_plan_ID VARCHAR2(10);
l_dealer_ID VARCHAR2(5);
l_vehicles_sold NUMBER(8,2);
l_gross_sales_amt NUMBER(8,2);
CURSOR c1 IS SELECT sale_day,vehicle_Code,fp.plan_ID,d.dealer_ID,
COUNT (*) AS vehicles_sold,
SUM (s.gross_sale_price) AS gross_sales_amt
FROM Times t, Sales s, Financing_Plans fp, Dealerships d, Vehicles v
WHERE t.sale_day = s.sale_date
GROUP BY sale_day, vehicle_Code, fp.plan_ID, d.dealer_ID;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO l_sale_day, l_vehicle_Code, l_plan_ID, l_dealer_ID, l_vehicles_sold, l_gross_sales_amt;
EXIT WHEN c1%NOTFOUND;
IF l_vehicles_sold <> 0 THEN
INSERT INTO SALES_FACTS (sale_day,vehicle_Code,plan_ID,dealer_ID,vehicles_sold, gross_sales_amt)
VALUES (l_sale_day,l_vehicle_Code,l_plan_ID,l_dealer_ID,l_vehicles_sold,l_gross_sales_amt);
END IF;
END LOOP;
CLOSE c1;
END;
/
BEGIN
Populate_Sales_Facts;
END;
/
I was given the fields to populate the Sales table and they cannot be changed
per my requirements but I did fix my WHERE statement to pull sale_day from the Times table where equal to the sale_date in the Sales table because those are the only fields that linked. So I was able to get the table to populate but now instead of getting no more than 200 rows, I am getting 61065 rows of data. Here are my requirements: get every possible combination of the dimension tables’ primary keys and then the total vehicles sold and gross sales amount for each combination. If these values for Total_Vehicles_Sold and Gross_Sales_Amount for a combination are zero then don’t INSERT a row into the SALES_FACT table. Only insert rows for combinations of the four foreign key columns where there
were some vehicles sold. Maybe I am just misunderstanding the task but I feel like I am getting too many rows now.
I'm trying to do this query using IN, but if the type is CLOB doesn't works.
I need that the parameter p_rut is CLOB and the query I need to use IN, because there it will a select that return more rows to compare.
How can I use parameter CLOB in a IN?
PROCEDURE INSERT_TEST (o_dat out o_cursor, p_rut in CLOB)
AS
BEGIN
OPEN o_dat FOR
select * from cliente
where rut in (p_rut);
END;
The table CLIENTE
CREATE TABLE CLIENTE
(
PRODUCTO VARCHAR(50),
RUT VARCHAR2(50),
DV VARCHAR2(50),
FONO1 VARCHAR2(50),
FONO2 VARCHAR2(50),
FONO3 VARCHAR2(50),
FONO4 VARCHAR2(50),
FONO5 VARCHAR2(50),
FONO6 VARCHAR2(50),
COMUNA VARCHAR2(50),
EDAD NUMBER(8),
SEXO NUMBER(8),
DIASMORA NUMBER(8),
AÑODEUDA NUMBER(8),
PAGOMINIMO NUMBER(8),
)
;
You can use:
DBMS_LOB.INSTR (
lob_loc IN CLOB CHARACTER SET ANY_CS,
pattern IN VARCHAR2 CHARACTER SET lob_loc%CHARSET,
offset IN INTEGER := 1,
nth IN INTEGER := 1)
RETURN INTEGER;
i.e.
PROCEDURE INSERT_TEST (o_dat out o_cursor, p_rut in CLOB)
AS
BEGIN
OPEN o_dat FOR
select * from cliente
where DBMS_LOB.INSTR (p_rut,rut)>0;
END;
using IN it can't use CLOB, so I just did a CAST to varchar2.
select * from cliente
where rut in (CAST (p_rut AS VARCHAR2(100));
i make a procedure in plsql it return this error anyone know about it i post my code and error snap shot here
CREATE OR REPLACE PROCEDURE CHR.ATT_INSERT_TEST
IS
CURSOR ATT
IS
SELECT emp.EMPLOYEE_ID EMPLOYEE_ID,
io.CHECKTYPE CHECKTYPE,
io.MACHINE_NUM MACHINE_NUM,
io.ATT_ID ATT_ID,
io.SWIPE_DATE SWIPE_DATE,
io.SWIPE_TIME SWIPE_TIME
FROM INOUT_LIVE_MACHINE_TEST io, CHR_EMGT_EMPLOYEE emp
WHERE emp.EMPLOYEE_CODE = io.EMPLOYEE_CODE
AND io.ATT_ID NOT IN (SELECT ATT_ID_REF
FROM CHR_TA_EMP_SWIPE_IN_OUT
WHERE io.ATT_ID = ATT_ID_REF);
BEGIN
FOR I IN ATT
LOOP
INSERT INTO CHR_TA_EMP_SWIPE_IN_OUT (EMPLOYEE_ID,
SWIPE_DATE,
SWIPE_TIME,
SWIPE_ID,
SWIPE_TYPE,
CREATED_BY,
CREATION_DATE,
CLIENT_IP)
VALUES (I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
I.SWIPE_TIME,
CHR_TA_SWIPE_IN_OUT_SEQ.NEXTVAL,
I.CHECKTYPE,
I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
'192.168.0.71'
);
END LOOP;
COMMIT;
END;
/
this is my procedure i make subquery mostly time but this time it return me this kind of error.
i think this error show some issue is regarding alias but i already define alias for each column
i read this artical but my code is still stuck
PLS-00402: alias required in SELECT list of cursor to avoid duplicate column names
this is my table structure
CREATE TABLE CHR.CHR_TA_EMP_SWIPE_IN_OUT
(
EMPLOYEE_ID NUMBER(10) NOT NULL,
SWIPE_DATE DATE NOT NULL,
SWIPE_TIME DATE NOT NULL,
SWIPE_ID NUMBER(10) NOT NULL,
SWIPE_TYPE VARCHAR2(1 BYTE),
CREATED_BY NUMBER(10),
CREATION_DATE DATE,
LAST_UPDATED_BY NUMBER(10),
LAST_UPDATE_DATE DATE,
IS_MANUAL VARCHAR2(1 BYTE),
REASON_LKP NUMBER(10),
CLIENT_IP VARCHAR2(50 BYTE),
IS_REQUESTED VARCHAR2(1 CHAR),
TERMINAL_ID VARCHAR2(50 BYTE),
ATT_ID_REF NUMBER
)
CREATE TABLE CHR.CHR_EMGT_EMPLOYEE
(
EMPLOYEE_ID NUMBER(10) NOT NULL,
EMPLOYEE_CODE VARCHAR2(30 BYTE) NOT NULL
)
CREATE TABLE CHR.INOUT_LIVE_MACHINE_TEST
(
ATT_ID NUMBER,
USERID NUMBER,
EMPLOYEE_CODE NUMBER,
SENSORID NUMBER,
MACHINE_NUM VARCHAR2(20 BYTE),
CHECKTIME VARCHAR2(20 BYTE),
CHECKTYPE VARCHAR2(20 BYTE)
)
Your procedure has no mistakes. Only problem is in your table "INOUT_LIVE_MACHINE_TEST". it has missing SWIPE_DATE & SWIPE_TIME columns. See below the working demo.
Created table with both the columns:
CREATE TABLE INOUT_LIVE_MACHINE_TEST
(
ATT_ID NUMBER,
USERID NUMBER,
EMPLOYEE_CODE NUMBER,
SENSORID NUMBER,
SWIPE_DATE DATE NOT NULL,
SWIPE_TIME DATE NOT NULL,
MACHINE_NUM VARCHAR2(20 BYTE),
CHECKTIME VARCHAR2(20 BYTE),
CHECKTYPE VARCHAR2(20 BYTE)
)
Procedure compiled :
CREATE OR REPLACE PROCEDURE ATT_INSERT_TEST
IS
CURSOR ATT
IS
SELECT emp.EMPLOYEE_ID EMPLOYEE_ID,
io.CHECKTYPE CHECKTYPE,
io.MACHINE_NUM MACHINE_NUM,
io.ATT_ID ATT_ID,
io.SWIPE_DATE SWIPE_DATE,
io.SWIPE_TIME SWIPE_TIME
FROM INOUT_LIVE_MACHINE_TEST io, CHR_EMGT_EMPLOYEE emp
WHERE emp.EMPLOYEE_CODE = io.EMPLOYEE_CODE
AND io.ATT_ID NOT IN (SELECT ATT_ID_REF
FROM CHR_TA_EMP_SWIPE_IN_OUT
WHERE io.ATT_ID = ATT_ID_REF);
BEGIN
FOR I IN ATT
LOOP
INSERT INTO CHR_TA_EMP_SWIPE_IN_OUT (EMPLOYEE_ID,
SWIPE_DATE,
SWIPE_TIME,
SWIPE_ID,
SWIPE_TYPE,
CREATED_BY,
CREATION_DATE,
CLIENT_IP)
VALUES (I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
I.SWIPE_TIME,
CHR_TA_SWIPE_IN_OUT_SEQ.NEXTVAL,
I.CHECKTYPE,
I.EMPLOYEE_ID,
TO_DATE (i.SWIPE_DATE, 'MM/DD/YYYY'),
'192.168.0.71'
);
END LOOP;
COMMIT;
END;
/