I try to insert query alphanumeric sequence in table but it is not working - sql

I am trying to insert an alphanumeric sequence in Oracle but it is not working.
create sequence LIB start with 1 increment by 1;
select 'LIBR'||to_char(seq_no.nextval,'FM0000099') from dual;
create table addLib(
USER_ID VARCHAR2(20) PRIMARY KEY,
NAME VARCHAR2(20),
PASSWORD VARCHAR2(20),
FATHER_NAME VARCHAR2(20),
DOB DATE,
QUALIFICATION VARCHAR2(20),
DOJ DATE,
STATE VARCHAR2(20),
ADDRESS VARCHAR2(20),
PINCODE NUMBER(6));
INSERT INTO addLibrarian
values(
LIB.nextval(LIBR),
'abc',
'1234',
'xyz',
to_date('19970503','YYYYMMDD'),
'b.tech',
to_date('19970308','YYYYMMDD'),
'tanakpur',
262309);
I expect it to insert all values into the table but an error shows not enough values.

There are multiple issues:
LIB.nextval(LIBR) is not a valid syntax.
table name is not valid.
value for address is missing in VALUES clause.
Try this:
create table addLib( -- changed table name
USER_ID VARCHAR2(20) PRIMARY KEY,
NAME VARCHAR2(20),
PASSWORD VARCHAR2(20),
FATHER_NAME VARCHAR2(20),
DOB DATE,
QUALIFICATION VARCHAR2(20),
DOJ DATE,
STATE VARCHAR2(20),
ADDRESS VARCHAR2(20),
PINCODE NUMBER(6));
INSERT INTO addLib -- changed table name
values(
'LIBR' || LAPD(LIB.nextval, 7, 0), -- use something like this
'abc',
'1234',
'xyz',
to_date('19970503','YYYYMMDD'),
'b.tech',
to_date('19970308','YYYYMMDD'),
'tanakpur',
'<address>', -- add this value
262309);
Cheers!!

You have 10 columns and insert 9 values: but error show not enough values. Add the last correct value to your insertion.
This type of error can be prevented by formatting your code or use a prettifier to do it for you automatically.

Related

Why am I getting this error when I want the the employee to NOT be below 18 and above 60?

the previous error was fixed. A group member was inserting the Salary amount into 0 precision-specified numbers.
Basically, I want to give an error when the age of an employee is less than 18 or more than 60
please suggest how I can do it.
Trigger statement
create or replace TRIGGER check_birth_date
BEFORE INSERT OR UPDATE ON employee
FOR EACH ROW
BEGIN
IF( months_between(sysdate,:new.DateOfBirth)/12 < 18 or
months_between(sysdate,:new.DateOfBirth)/12 > 60 )
THEN
RAISE_APPLICATION_ERROR(
-20001,
'EMPLOYEE MUST BE ABOVE 18 AND BELOW 60' );
END IF;
END;
The insert statement I am trying to insert
INSERT INTO EMPLOYEE VALUES(1002, 'DAVID', '21/apr/2011', 'M', '21/jun/2010', 28000.00, 'HR', 'HR MANAGER', 77845322, 'C');
The errors
- ORA-06512
- ORA-04088
- ORA-20001
CREATE TABLE STATEMENT
CREATE TABLE EMPLOYEE(
EmployeeID NUMBER(4),
Name VARCHAR2(20),
Hiredate DATE NOT NULL,
Gender VARCHAR(1),
DateOfBirth DATE NOT NULL,
Salary NUMBER(8,2),
DName VARCHAR(20),
PName VARCHAR(20),
Phone NUMBER(8) NOT NULL,
GLETTER VARCHAR(1),
CONSTRAINT EMPLOYEE_EMPLOYEEID_PK PRIMARY KEY(EMPLOYEEID),
CONSTRAINT EMPLOYEE_DNAME_FK FOREIGN KEY(DNAME) REFERENCES DEPARTMENT(DNAME),
CONSTRAINT EMPLOYEE_PNAME_FK FOREIGN KEY(PNAME) REFERENCES POSITION(PNAME),
CONSTRAINT EMPLOYEE_GLETTER_FK FOREIGN KEY(GLETTER) REFERENCES GRADE(GLETTER),
CONSTRAINT GENDER_CK CHECK (GENDER='M' or GENDER='F')
);
Your code is right, if you check your input data and run it by yourself you can see it. I run it and the result is:
You trigger is working fine. I have created employee table without any foreign key constraints (since no reference table is available to me) and the trigger is working as it should be.
It's denying entry of any record with age than 18 and more than 60 but it's allowing any record within age 18 to 60.
Please try to insert below line:
INSERT INTO EMPLOYEE VALUES(1002, 'DAVID', '21/apr/2000', 'M', '21/jun/2000', 28000.00, 'HR', 'HR MANAGER', 77845322, 'C');

How to overcome a persistent oracle 'invalid identifier' error on a basic insert?

I am trying to create a basic table using subtypes and insert some data into this in Oracle Express 11g.
My table is successfully created but i am having issues with inserting data.
The result of my insert statement always throws back an error 'SQL Error: ORA-00904: "BRANCH_PHONE": invalid identifier'.
The column which shows up in the error message is always the column which is at the end of the insert statement, despite the column existing in the table. I have tried the following code:
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone int(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchID('2364'),
addressType('12 Rooster','Atlantis','A13 4UG'),
branch_phone('01316521311'));
I would really appreciate any ideas.
I made some changes, including changing the branch_phone to varchar2. A Phone number, while is "numbers" is not a data type of number. it is a string of characters. Also you were passing branchID as a string, but you are declaring it as a number, so changed that also. BranchID and branch_phone are primitive data types, so no constructor needed.
create type addressType as object(
street varchar2(20),
city varchar2(20),
postCode varchar2(8))
not final
/
create type branchType as object(
branchID int,
branch_address addressType,
branch_phone varchar2(11))
not final
/
create table Branch of branchType(
constraint branch_pk primary key(branchID));
/
insert into Branch values (
branchtype(2364,
addressType('12 Rooster','Atlantis','A13 4UG'),
'01316521311') )

Column not allowed here pl sql

i am trying to create a procedure to add a new sale, but for some reason i am getting an error that says the column not allowed here
Here is the code:
CREATE OR REPLACE PROCEDURE AddSale
(
In_CID customers.CID%Type,
In_EmpID Employees.EmpID%Type,
In_car_num cars.car_num%Type,
In_Pay_M Sales.Pay_method%Type,
In_Payment_duration Sales.Pay_duration%Type,
In_Payment_type Sales.S_type%Type,
In_sh_id Sales.sh_id%Type,
)
IS
number := 0;
Id_sale number;
BEGIN
INSERT INTO Sales(Sales_ID, CID, EmpID, car_num, S_time,
S_type, Pay_method, Pay_duration, sh_id)
VALUES (Sales_ID_Seq.nextval, In_CID, In_EmpID, In_car_num, SYSDATE,
In_Payment_type, In_Payment_M, In_Payment_duration, In_sh_id);
And here is the table definition:
create table Sales
(
Pay_duration number(15),
Car_num number(15),
Sales_ID number(15),
S_type varchar2(15),
CID number(15),
Pay_method varchar2(32),
S_time timestamp,
EmpID number(15),
Sh_id number(15),
discount number(5,3),
Disc_status varchar2(20),
ApprovedBy number(15)
);
I've formatted the code in your request. You will see that proper formatting is a great help when coding.
In your declaration you have:
In_Pay_M
In your insert statement you have:
In_Payment_M
Then, there is a comma too many after your last parameter:
In_sh_id Sales.sh_id%Type,
)

sql query ORA-01843

I have the following DDL and DML statements :
create table emp_details (
ID number(2) constraint t_pk primary key,
F_Name varchar(10) not null,
L_Name varchar(10) not null,
DOB date,
Mob_no number(10),
City varchar(10),
PIN number(5),
Gender char(1),
Designation varchar(15),
Join_Date date,
);
insert into emp_details values (01,'John','Wick','1990-07-05',9856482358,'Goa',403001,'M','SDE II', '2015-01-08');
then, I get the error of ORA-01843. So, what could be the problem?
The easiest thing to do here is to use ANSI date literals instead of strings for the dates (using strings will depend on the value of NLS_DATE_FORMAT and you don't want to play around with that if you don't have to):
INSERT INTO emp_details
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
I have to add that explicitly listing the columns into which you're inserting values is a good habit to have. Otherwise, your INSERT query will break if you or someone else adds a column from your table:
INSERT INTO emp_details
( id, f_name, l_name, dob, mob_no, city, pin, gender, designation, join_date )
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
Last, another good practice is to use VARCHAR2 instead of VARCHAR when you're working with Oracle. Currently they work the same, but Oracle "reserves the right" to change VARCHAR to meet with the ANSI standard under which NULL values and the empty string won't be the same (with VARCHAR2 they will always be the same). IOW, the behavior of VARCHAR values in Oracle can change.
It seems when you query with
select * from nls_session_parameters p where p.parameter = 'NLS_DATE_FORMAT';
you won't get YYYY-MM-DD or YYYY-DD-MM from your result of error ORA-01843.
This problem is due to inserting wrong-formatted value for date columns DOB and Join_date.
There may be two ways to prevent this error :
Assume you get DD/MM/YYYY from above query, then use 05-07-1990 for
DOB, and 08/01/2015 for Join_Date columns, respectively.
Format your values as to_date('1990-07-05','YYYY-MM-DD') for
DOB and to_date('2015-01-08','YYYY-MM-DD') for Join_Date

ORA-00936: missing expression error when inserting values

I spend lot of time searching where i made the mistake but i was unable to find it when its going to insert the last record there is a error message showing "ORA-00936: missing expression" How to solve this please help me
create type pearson_types as object(
name varchar2(50),
sysID char(6)
)NOT FINAL;
create type doctor_types under pearson_types(
regNo char(10),
specialization varchar2(25)
)
create table doctor of doctor_types(
regNo primary key
)
create type hospVisits_types as object(
hosChg float,
vDate varchar2(20),
refDoc REF doctor_types,
docChg float
)
create type hospvisits_tbl_types as table of hospVisits_types
create type phone_arr as VARRAY(3) of char(10)
create type patient_types under pearson_types
(
id char(10),
dob varchar(20),
phone phone_arr,
hospVisits hospvisits_tbl_types
)
create table patients of patient_types(
id primary key
)nested table hospVisits store as Hospital_tables
alter table Hospital_tables add scope for (refDoc) is doctor
insert into doctor values ('Dr.k.perera','D001','1223441234','Gynecologist');
insert into doctor values ('Dr.p.weerasingha','D002','1234421131','Dermatalogist');
insert into doctor values ('Prof .S. Fernando','D003','2342111322','Pediatrician');
insert into doctor values ('Dr.k.Sathgunanathan','D004','2344114344','Pediatrician');
insert into patients values('Sampath Weerasingha','P001','732821122V','23-JAN-73',phone_arr('0332124222'),hospvisits_tbl_types(hospVisits_types(50.00,'24-MAY-06',select ref (a) from doctor a where a.regNo='1223441234',500.00)))
Add parentheses to SELECT statements inside a SQL statement:
insert into patients values(
'Sampath Weerasingha','P001','732821122V','23-JAN-73',phone_arr('0332124222'),
hospvisits_tbl_types(hospVisits_types(50.00,'24-MAY-06',
( -- ADD ME
select ref (a) from doctor a where a.regNo='1223441234'
) -- ADD ME
,500.00))
);