error: value larger than specified precision allowed for this column - sql

I am creating tables based on the SQL Developer, however, got couple problems from my code:
My table can be established one by one, but cannot run as a whole and get error:
ORA-00922: missing or invalid option
When I am trying to insert values, get the error:
ORA-01438: value larger than specified precision allowed for this column
I have no idea about how these two error come from and everything look like fine on my code.
create table FACULTY (
faculty_id VARCHAR(25),
fac_name VARCHAR(25),
office VARCHAR(25),
salary NUMBER(9,2),
primary key (faculty_id)
)
create table student (
student_id VARCHAR(25),
std_name VARCHAR(25),
home_phone CHAR(12),
total_credits NUMBER,
gpa NUMBER(1,2),
advisor_id VARCHAR(25),
PRIMARY KEY (student_id),
foreign key (advisor_id) REFERENCES FACULTY (faculty_id)
)
INSERT INTO student table, I have already insert values successfully for FACULTY table;
insert into student
values('111111111', 'Marcus', '21-4748363', 0, 3.11, '90421');
For 1st question:
Error report -
ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
For 2nd question:
Error starting at line : 40 in command -
insert into student
values('111111111', 'Marcus', '21-4748363', 0, 3.11, '90421')
Error report -
ORA-01438: value larger than specified precision allowed for this column

For 1st question
Use semicolon ; after the first table definition.
For 2nd question
You should define your table like this:
create table student (
student_id VARCHAR(25),
std_name VARCHAR(25),
home_phone CHAR(12),
total_credits NUMBER,
gpa NUMBER(3,2),
advisor_id VARCHAR(25),
PRIMARY KEY (student_id),
foreign key (advisor_id) REFERENCES FACULTY (faculty_id)
)
For gpa column it should be NUMBER(3,2) instead of NUMBER(1,2) to support 3.11 Value.

Related

ORA-01008: not all variables bound ORA-06512: at "SYS.DBMS_SQL", line 1721. I am learning SQL and I got this error

I created a simple table with one PRIMARY Key column and two VARCHAR columns
CREATE TABLE student(
student_id INT PRIMARY KEY,
fName VARCHAR(20) NOT NULL,
lName VARCHAR(20)
);
INSERT INTO student VALUES(1, "Rahul", "Krub");
INSERT INTO VALUES(&student_id, &fName, &lName);
SELECT * FROM student;
I have no idea why the latter is not working while the former does? I want to dynamically enter data into the table (User Inputted). Please help me out I cannot find the answer for this anywhere as all of my searches keep leading me to dynamic SQL.

ORA-00904: : Invalid Identifier in SQL

I am currently working on a SQL assignment, but first I need to create tables which it is not letting me do. I have 3 tables which are Customer, PurchasedDeal, and Usage. I manage to create the first 2 tables successfully, but I am having a little difficulty with creating the Usage table. For some reason it is giving me this error.
Error at line 2:
ORA-00904: : Invalid Identifier
If anyone could help me understand why it is giving me this error I would really appreciate it. Thanks. It is saying it is having problems with uID INT and I am using putty to create these tables.
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
CustomerName VARCHAR(100),
Phone VARCHAR(15)
);
CREATE TABLE PurchasedDeal(
DID INT NOT NULL PRIMARY KEY,
dealName VARCHAR(100),
cost FLOAT,
totalValue FLOAT,
balance FLOAT,
CustomerID INT,
FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
);
CREATE TABLE Usage(
uID INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal(DID)
);
The term ‘uid’ is reserved in Oracle. This term cannot be used as a column name in the Oracle environment.
Query: SELECT uid FROM t1
Result: will execute correctly
Query: SELECT U.”uid” FROM x.t1 U
Result: will through error:
ORA-00904: “U”.”uid”: invalid identifier 00904. 00000 – “%s: invalid identifier”
Query: SELECT U.”UID” FROM x.t1 U
Result: will execute correctly as uid is replaced with UID (in capital)
Rectification in below table and we are good to go.
CREATE TABLE Usage(
uID1 INT,
uDate DATE,
cost FLOAT,
DealID INT,
PRIMARY KEY(DealID, uID1),
FOREIGN KEY(DealID) REFERENCES PurchasedDeal_test(DID)
);

"ORA-01733: virtual column not allowed here" when inserting into a view

I created a view called "view_employee" like this:
CREATE VIEW view_employee AS
SELECT employee.surname || ', ' || employee.name AS comp_name, employee.sex, sections.name AS section_name, employee_age
FROM sections, employee WHERE employee.section = sections.sect_code;
And I would like to insert data into the table using the view, like this:
INSERT INTO view_employee VALUES ('Doe, John', 'm', 'Marketing', 34);
Here are the tables' columns and constraints:
create table sections(
sect_code number(2),
name varchar2(20),
income number(5,2)
constraint CK_sections_income check (income>=0),
constraint PK_sections primary key (sect_code)
);
create table staff(
ident number(5),
document char(8),
sex char(1)
constraint CK_staff_sex check (sex in ('f','m')),
surname varchar2(20),
name varchar2(20),
address varchar2(30),
section number(2) not null,
age number(2)
constraint CK_staff_age check (age>=0),
marital_status char(10)
constraint CK_employee_marital_status check (marital_status in
('married','divorced','single','widower')),
joindate date,
constraint PK_employee primary key (ident),
constraint FK_employee_section
foreign key (section)
references sections(sect_code),
constraint UQ_staff_document
unique(document)
);
The error message I get when attempting to insert is the following:
Error starting at Command Line: 1 Column : 1
Error report -
SQL Error: ORA-01733: virtual column not allowed here
01733. 00000 - "virtual column not allowed here"
*Cause:
*Action:
How could I insert those values into the table using the view? Thanks in advance.
A view must not contain any of the following constructs. So, it can be updateable.
A set operator
A DISTINCT operator
An aggregate or analytic function
A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause
A collection expression in a SELECT list
A subquery in a SELECT list
A subquery designated WITH READ ONLY
Joins, with some exceptions, as documented in Oracle Database
Administrator's Guide.

Msg 547, The INSERT statement conflicted with the FOREIGN KEY constraint

In SQL Server 2012 Management studio, I tried many time to create some tables and insert into the tables some values, but the problem here is in relationship with tables :
The INSERT statement conflicted with the FOREIGN KEY constraint
The Errors :
Msg 8152, Level 16, State 14, Line 116 String or binary data would be
truncated.
Msg 547, Level 16, State 0, Line 122 The INSERT statement conflicted
with the FOREIGN KEY constraint "eworkerFK". The conflict occurred in
database "7", table "dbo.member_clup", column 'manager_id'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 128
The INSERT statement conflicted with the FOREIGN KEY constraint
"SALEDFk". The conflict occurred in database "7", table "dbo.worker",
column 'worker_number'.
Can anyone help me to insert values into the correct
tables ?
CREATE TABLE address
(
code_place int,
PLACE_NAME VARCHAR (15),
OUT_israel varchar (15)
CONSTRAINT address1 PRIMARY KEY(code_place)
)
create table member_clup
(
manager_id int,
manager_name varchar(15),
manager_last varchar(12),
phone_num int,
type varchar(10),
CONSTRAINT manager1PK PRIMARY KEY(manager_id)
)
create table worker
(
worker_number int,
worker_name varchar(50),
worker_last varchar(49),
id varchar(9),
type varchar(50),
manager_id int
CONSTRAINT workerPK PRIMARY KEY(worker_number)
CONSTRAINT eworkerFK FOREIGN KEY(manager_id)
REFERENCES member_clup(manager_id)
)
CREATE TABLE rooms(
room_number int,
floor int,
room_type varchar(8)
CONSTRAINT roomsKEY PRIMARY KEY(room_number)
)
CREATE TABLE cars
(
cars_number int,
car_type varchar(15),
car_model int,
CONSTRAINT carsPK PRIMARY KEY(cars_number)
)
CREATE TABLE tourest(
Cust_number int ,
Cust_name varchar(50),
cust_lastname varchar(50),
cust_phone varchar(10),
code_place int,
Room_id int,
Saledby int
CONSTRAINT tourest1 PRIMARY KEY(Cust_number)
CONSTRAINT addressFK FOREIGN KEY(code_place)
REFERENCES address(code_place),
CONSTRAINT SALEDFk FOREIGN KEY(Saledby)
REFERENCES worker(worker_number),
CONSTRAINT roomsFK FOREIGN KEY(Room_id)
REFERENCES rooms(room_number)
)
CREATE TABLE kesher
(
Cust_number int,
cars_number int,
CONSTRAINT custPK1 PRIMARY KEY(Cust_number,cars_number),
CONSTRAINT kesher1FK FOREIGN KEY(Cust_number)
REFERENCES cars(cars_number),
CONSTRAINT kesherFK FOREIGN KEY(Cust_number)
REFERENCES tourest(Cust_number)
)
insert into cars VALUES
(100100,'mazda',2002),
(100205,'ford',2017),
(100206,'porch',1998),
(100207,'mazda',2017),
(100208,'opel',2002),
(100209,'mazda',2016),
(100210,'pijuot',2002),
(100211,'mazda',2015),
(100212,'mazda',2010),
(100213,'volvo',2002),
(100215,'ford',20012)
insert into rooms VALUES
(100,1,'single'),
(101,1,'double'),
(102,1,'single'),
(103,1,'double'),
(201,2,'signle'),
(202,2,'signle'),
(203,2,'signle'),
(204,2,'signle'),
(300,3,'double'),
(301,3,'double'),
(302,3,'double'),
(303,3,'double'),
(304,3,'signle')
insert into address VALUES
(500,'Akko','no'),
(501,'Haifa','no'),
(502,'Nahariya','no'),
(503,'Nataniya','no'),
(504,'carmieal','no'),
(505,'Nahef','no'),
(507,'Nitsrat','no'),
(510,'OUT','yes')
insert into member_clup VALUES
(5400,'shmolek','snaa','0525732572','General'),
(5696,'malloc','ali','0525552501','Rooms'),
(5991,'ramada','hassan','0532731212','Rooms & Tips'),
(5210,'meri','yako','0525022572','General Manager')
insert into worker
VALUES(1234,'halaa','khaled',1234567,'none',5696),
(2234,'fares','adoon',6542897,'none',5696),
(6670,'halaa','khaled',1001234,'none',5991),
(2554,'halaa','khaled',5658741,'none',5210),
(9987,'halaa','khaled',1123456,'none',5400)
insert into tourest VALUES
(1510,'moshe','yke','0525732579',500,101,2234),
(1520,'ninar','lait','052655541',500,102,6670),
(1521,'hasan','ahmad','0532578741',501,101,2234),
(1522,'ameer','karm','0545222741',500,104,6670),
(1523,'aliel','sraa','0525771572',504,100,2234),
(1524,'hasa','veto','0525122579',505,303,6670),
(1525,'saed','snaa','05255632579',505,303,2234),
(1526,'yakov','mero','0528132579',502,202,6670),
(1527,'mece','loka','0525962579',502,302,9987),
(1528,'ana','yokaf','0525791179',502,302,9987),
(1529,'lelya','mandlina','0527832579',505,203,9987),
(1530,'mnal','khokha','0525758579',507,204,5991),
(1531,'moka','panana','0525805579',507,200,2234)
insert into kesher VALUES
(1510,100100),
(1520,100209),
(1521,100100),
(1522,100209),
(1523,100206),
(1524,100206),
(1525,100213),
(1526,100206),
(1527,100213),
(1528,100213),
(1529,100209)
This code is straightforward to debug, you surely can work it out. Run each statement on it's own and see its result. If it fails, split it into smaller statements and repeat. If you can't split it, it's already small enough to debug by eyesight, it won't take long to figure the issue.
Before debugging though, know this, restarting from scratch is sometimes helpful/necessary, so have restart script(s) ready. In this scenario only a bunch of DROP TABLEs are necessary, they'll remove the tables and their records.
Your CREATE TABLE statements are working fine, that's one thing passed. Even if you had problems with them though, run them one by one to debug, not all at once.
Your INSERTs are problematic, yeah. Run each INSERT block one by one, and for the block that fails, split it into individual INSERT statements, one per record, and now you'll be able to tell which row is the problem.
Even though you cite certain foreign key conflict issues, I'm getting a bunch of different foreign key issues. Double-check the code you provided. Also,
the order of what statement is ran first matters (can I furnish the third floor of my house while I don't even have third and second floors? Can you cancel a booking that you didn't book yet?)
Lastly, the data seem to have truncation issues, whether the table column should be bigger or the records' values are incorrect is something only you can tell.

Insert fails due to "column not allowed here" error

I am a beginner with SQL. I have created 4 tables and added data to my SHIP table. I am having some issues with inserting data into the CRUISE table. I get the error message at the bottom.
I have researched and can not figure out what i am doing wrong. Is there an issue with my sequence and/or trigger that is not allowing me to do this or is my syntax in the CREATE TABLE CRUISE causing the error? Everything i have done has been successful up until trying to insert the first column into the CRUISE table.
The tables:
CREATE TABLE SHIP
( Ship_Name VARCHAR2(100) PRIMARY KEY,
Ship_Size INTEGER,
Ship_Registry VARCHAR2(50),
Ship_ServEntryDate INTEGER,
Ship_PassCapacity INTEGER,
Ship_CrewCapacity INTEGER,
Ship_Lifestyle VARCHAR2(40),
CONSTRAINT Size_ck CHECK (Ship_Size > 0),
CONSTRAINT Registry_ck CHECK (Ship_Registry IN ('Norway','Liberia','The Netherlands','Bahamas'))
)
CREATE TABLE CRUISE (
Cruise_ID INTEGER Primary Key,
Ship_Name VARCHAR(100),
Cruise_DeptDate DATE NOT NULL,
Cruise_DeptCity VARCHAR(80) NOT NULL,
Cruise_Duration INTEGER,
FOREIGN KEY (Ship_Name) REFERENCES SHIP(Ship_Name)
)
CREATE TABLE PASSENGERS (
Pass_ID INTEGER PRIMARY KEY,
Pass_Name VARCHAR(100) NOT NULL,
Pass_City VARCHAR(80),
Pass_Telephone VARCHAR(15),
Pass_NextOfKin VARCHAR(100)
)
CREATE TABLE RESERVATIONS (
Pass_ID INTEGER NOT NULL,
Cruise_ID INTEGER NOT NULL,
Res_TotalCost NUMERIC(9,2),
Res_BalanceDue NUMERIC(9,2),
Res_SpecialRequest VARCHAR(30),
Res_Room VARCHAR(10),
FOREIGN KEY (Pass_ID) REFERENCES PASSENGERS(Pass_ID),
FOREIGN KEY (Cruise_ID) REFERENCES CRUISE(Cruise_ID),
CONSTRAINT Cost_ck CHECK (Res_TotalCost >= 0),
CONSTRAINT BalanceDue_ck CHECK (Res_BalanceDue >= 0),
CONSTRAINT SpecialRequest_ck CHECK (Res_SpecialRequest IN ('Vegetarian','Vegan','Low salt','Gluten free','Kosher','Other'))
)
The sequence/trigger is an attempt to auto number Cruise_ID.
Create SEQUENCE cruise_id_sq
START WITH 1
INCREMENT BY 1;
CREATE OR REPLACE TRIGGER cruise_id_t
BEFORE INSERT
ON CRUISE
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if(:new.Cruise_ID is null) then
SELECT cruise_id_sq.nextval
INTO :new.Cruise_ID
FROM dual;
end if;
END;
ALTER TRIGGER cruise_id_t ENABLE;
Inserting into SHIP is okay....
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Princess',142000,'Liberia',1000,3100,1181,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Carribean Sunshine',74000,'Norway',1992,1950,760,'Premium');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Ship of Dreams',70000,'Liberia',2004,1804,735,'Contemporary');
INSERT INTO SHIP
(Ship_Name, Ship_Size, Ship_Registry,Ship_ServEntryDate, Ship_PassCapacity,Ship_CrewCapacity,Ship_Lifestyle)
Values ('Sunshine of the Seas',74000,'The Netherlands',1990,2354,822,'Luxury');
Inserting into CRUISE fails...
INSERT INTO Cruise
(Ship_Name, Cruise_DeptDate,Cruise_DeptCity,CruiseDuration)
Values ('Sunshine of the Seas',25-may-15,'Miami',10);
Error starting at line : 1 in command - INSERT INTO Cruise (Ship_Name,
Cruise_DeptDate,Cruise_DeptCity,CruiseDuration) Values ('Sunshine of
the Seas',25-may-15,'Miami',10) Error at Command Line : 3 Column : 35
Error report - SQL Error: ORA-00984: column not allowed here
00984. 00000 - "column not allowed here"
*Cause:
*Action:
Oracle thinks that 25-may-15 is the expression 25 minus may minus 15. In looking up the value for may Oracle finds that there is nothing there. Thus the error.
You can, but probably don't want to, quote it like so, '25-may-15'. This will make a string that may or may not be implicitly converted to a date, depending on the settings of NLS_DATE_FORMAT and or NLS_TERRITORY.
To form a date independent of session setting one can use the TO_DATE function with explicit date format, to_date('25-may-15', 'DD-Mon-YY'). Another option is a date literal, date '2015-05-25', which is always YYYY-MM-DD no matter the session settings..