Receiving endless errors with the insert - statement - sql

I am trying to insert some values into customer and then i get the ora-00904 error.
After i research about this problem, instead of using '' i used "". But now im receiving the ora-00984.
INSERT INTO customer (c_id, name, age)
VALUES (1, 'Carl', 45)
OUTPUT:
SQL-ERROR: ORA-00904: "name": invalid ID
00904. 00000 - "%s: invalid identifier"
then i tried this way.
INSERT INTO customer (c_id, name, age)
VALUES (1, "Carl", 45)
OUTPUT:
00984. 00000 - "column not allowed here"
My DDL - Code:
CREATE TABLE adress (
adress_id INTEGER NOT NULL,
state VARCHAR2(60) NOT NULL,
country VARCHAR2(60) NOT NULL
);
ALTER TABLE adress ADD CONSTRAINT adress_pk PRIMARY KEY ( adress_id );
CREATE TABLE contract (
con_id INTEGER NOT NULL,
length DATE
);
ALTER TABLE contract ADD CONSTRAINT contract_pk PRIMARY KEY ( con_id );
CREATE TABLE customer (
c_id INTEGER NOT NULL,
name VARCHAR2(60) NOT NULL,
age CHAR(2) NOT NULL,
adress_adress_id INTEGER
);
ALTER TABLE customer ADD CONSTRAINT customer_pk PRIMARY KEY ( c_id );
CREATE TABLE relation_1 (
customer_c_id INTEGER NOT NULL,
contract_con_id INTEGER NOT NULL
);
ALTER TABLE relation_1 ADD CONSTRAINT relation_1_pk PRIMARY KEY ( customer_c_id,
contract_con_id);
ALTER TABLE customer
ADD CONSTRAINT customer_adress_fk FOREIGN KEY ( adress_adress_id )
REFERENCES adress ( adress_id );
ALTER TABLE relation_1
ADD CONSTRAINT relation_1_contract_fk FOREIGN KEY ( contract_con_id )
REFERENCES contract ( con_id );
ALTER TABLE relation_1
ADD CONSTRAINT relation_1_customer_fk FOREIGN KEY ( customer_c_id )
REFERENCES customer ( c_id );

The syntax uses VALUES() for the list of values:
INSERT INTO customer (c_id, name, age)
VALUES (1, 'Carl', 45);
You can also use a SELECT:
INSERT INTO customer (c_id, name, age)
SELECT 1, 'Carl', 45
FROM dual;
Note that in both cases, the delimiter for the string is single quotes not double quotes.

In SQL we use single quotes for string literals. Double quotes are used (optionally) for identifiers such as table and column names. You have used double quotes around the value of carl and that's why Oracle hurls ORA-00984.
Why you got ORA-00904 for the first statement is a mystery. Probably there is something awry with your definition of customer but unless you post the table structure we cannot be sure.

Related

Foreign key references invalid column error in MS SQL

This is all in MS SQL.
I've seen this issue pop up a lot, and I searched around but I still can't figure out my issue. I'm getting the error telling me my foreign key references in invalid column in an uncreated table. However, I created the table as it shows up on my database table list, so it shows it is created.
I tried to move that said table above the table that references it, yet I'm still receiving the error. Would anyone know how to fix this?
My code:
CREATE SCHEMA usr_in;
go
CREATE TABLE gender_interst (
id int IDENTITY (1,1),
gend_id int
CONSTRAINT gender_interstpk PRIMARY KEY (id)
)
CREATE TABLE gender (
id int IDENTITY (1,1),
gend VARCHAR (20)
CONSTRAINT genderpk PRIMARY KEY (id)
);
SELECT * FROM gender_interst LEFT JOIN gender on gender_interst.id = gender.id;
SELECT * from gender_interst;
CREATE TABLE user_info (
id int PRIMARY KEY IDENTITY (1,1),
usr_name VARCHAR (30) NOT NULL,
f_name VARCHAR (30) NOT NULL,
l_name VARCHAR (30),
b_day DATE,
email VARCHAR (120) NOT NULL,
genderpkid VARCHAR (10) NOT NULL,
CONSTRAINT gender_fk FOREIGN KEY (genderpkid) REFERENCES gender (genderpk),
);
INSERT
INTO user_info
( usr_name, f_name, l_name, b_day, genderpkid, email)
VALUES
('JMAN', 'JOHN', 'DOE', '1990-01-01','M','EX#EMAIL'),
('JAM','JANE', 'DOE', '1995-05-02','F','EX#EMAIL'),
('NMAN','NICK', 'WEBB', '1999-06-22','M','EX#EMAIL'),
('LOBA','LOLA', 'LILLY', '1994-01-08','F','EX#EMAIL'),
('NOTSPMAN','PETER', 'PARKER','1985-11-25','M','EX#EMAIL');
SELECT * FROM user_info;
Your foreign key needs to reference the name of a table column (which is either id or gend) and not the name of the primary key (genderpk).
Therefore the foreign key script (CONSTRAINT gender_fk FOREIGN KEY (genderpkid) REFERENCES gender (genderpk)) should look something like CONSTRAINT gender_fk FOREIGN KEY (genderpkid) REFERENCES gender (id)
However to create the relationship the two columns need to have the same data type. user_info .genderpkid is VARCHAR (10) and user_info.id is int.
The other problem you might encounter is that the insert scripts insert the data into genderpkid column as M or F. Therefore int is not going to work
If you were to use use the values M or F for gender, then you can create a script like:
CREATE TABLE gender (
id VARCHAR (1),
gender VARCHAR (20)
CONSTRAINT gender_pk PRIMARY KEY (id)
);
CREATE TABLE user_info (
id int PRIMARY KEY IDENTITY (1,1),
usr_name VARCHAR (30) NOT NULL,
f_name VARCHAR (30) NOT NULL,
l_name VARCHAR (30),
b_day DATE,
email VARCHAR (120) NOT NULL,
genderId VARCHAR (1) NOT NULL,
CONSTRAINT gender_fk FOREIGN KEY (genderId) REFERENCES gender (id),
);
INSERT INTO gender (id, gender)
VALUES
('F', 'Female'),
('M', 'Male')
INSERT
INTO user_info
( usr_name, f_name, l_name, b_day, genderId, email)
VALUES
('JMAN', 'JOHN', 'DOE', '1990-01-01','M','EX#EMAIL'),
('JAM','JANE', 'DOE', '1995-05-02','F','EX#EMAIL'),
('NMAN','NICK', 'WEBB', '1999-06-22','M','EX#EMAIL'),
('LOBA','LOLA', 'LILLY', '1994-01-08','F','EX#EMAIL'),
('NOTSPMAN','PETER', 'PARKER','1985-11-25','M','EX#EMAIL');
SELECT * FROM user_info;
A better approach could be, to pass the whole phraze (male / female) to the table user_info. The table gender can be used to enforce referential integrity. All your information is then contained in SELECT * FROM user_info
For example:
CREATE TABLE gender (
[name] VARCHAR (20)
CONSTRAINT gender_pk PRIMARY KEY ([name])
);
CREATE TABLE user_info (
id int PRIMARY KEY IDENTITY (1,1),
usr_name VARCHAR (30) NOT NULL,
f_name VARCHAR (30) NOT NULL,
l_name VARCHAR (30),
b_day DATE,
email VARCHAR (120) NOT NULL,
gender VARCHAR (20) NOT NULL,
genderIntrest VARCHAR (20) NOT NULL,
CONSTRAINT gender_fk FOREIGN KEY (gender) REFERENCES gender ([name]),
CONSTRAINT genderIntrest_fk FOREIGN KEY (genderIntrest) REFERENCES gender ([name]),
);
INSERT INTO gender ([name])
VALUES
('Female'),
('Male')
INSERT
INTO user_info
( usr_name, f_name, l_name, b_day, gender, genderIntrest, email)
VALUES
('JMAN', 'JOHN', 'DOE', '1990-01-01','Male','Female', 'EX#EMAIL'),
('JAM','JANE', 'DOE', '1995-05-02','Female','Female', 'EX#EMAIL'),
('NMAN','NICK', 'WEBB', '1999-06-22','Male','Female','EX#EMAIL'),
('LOBA','LOLA', 'LILLY', '1994-01-08','Female','Female','EX#EMAIL'),
('NOTSPMAN','PETER', 'PARKER','1985-11-25','Male','Female','EX#EMAIL');
SELECT * FROM user_info;
You can remove the gender table and let the app that consumes it pass in the data. However for a learning exercises, it's probably better to leave it in

When i insert value in the hire table it shows invalid identifier in the Vehicle_ID. There was no error while creating tables

-- Create a Database table to represent the "Vehicle" entity.
CREATE TABLE Vehicle(
Vehicle_ID INTEGER NOT NULL,
Purchase_Date DATE,
Vehicle_No_Plate VARCHAR(20),
Brand VARCHAR(20),
Vehicle_Type VARCHAR(20),
Model VARCHAR(20),
Color VARCHAR(20),
-- Specify the PRIMARY KEY constraint for table "Vehicle".
-- This indicates which attribute(s) uniquely identify each row of data.
CONSTRAINT pk_Vehicle PRIMARY KEY (Vehicle_ID)
);
-- Create a Database table to represent the "Hire" entity.
CREATE TABLE Hire(
Hire_ID INTEGER NOT NULL,
Hire_Date DATE,
Drop_In_Day DATE,
fk1_Customer_ID INTEGER NOT NULL REFERENCES Customer (Customer_ID),
fk2_Vehicle_ID INTEGER NOT NULL REFERENCES Vehicle (Vehicle_ID) ,
-- Specify the PRIMARY KEY constraint for table "Hire".
-- This indicates which attribute(s) uniquely identify each row of data.
CONSTRAINT pk_Hire PRIMARY KEY (Hire_ID)
);
INSERT INTO Hire
(Hire_ID, Hire_Date, Drop_In_Day, Customer_ID, Vehicle_ID)
VALUES (10, '8/3/2017', '8/7/2017', 101, 112);
ORA-00904: "VEHICLE_ID": invalid identifier
Try to examine this code snippet, it works for me.
CREATE TABLE Vehicle( Vehicle_ID INTEGER NOT NULL,
Purchase_Date DATE,
Vehicle_No_Plate VARCHAR(20),
Brand VARCHAR(20),
Vehicle_Type VARCHAR(20),
Model VARCHAR(20),
Color VARCHAR(20),
CONSTRAINT pk_Vehicle PRIMARY KEY (Vehicle_ID) );
CREATE TABLE Hire
(
Hire_ID NUMBER,
Hire_Date DATE,
Drop_In_Day DATE,
Customer_ID NUMBER,
Vehicle_ID NUMBER,
CONSTRAINT vei FOREIGN KEY (Vehicle_ID)
REFERENCES Vehicle (Vehicle_ID),
CONSTRAINT cust FOREIGN KEY (Customer_ID)
REFERENCES Customer (Customer_ID)
);
CREATE TABLE Customer(Customer_id NUMBER,
Name VARCHAR(20),
CONSTRAINT pk_Cust PRIMARY KEY (Customer_ID));
INSERT INTO Customer VALUES(100, 'Joe');
INSERT INTO vehicle VALUES(113, SYSDATE, 'PU-000-PU', 'FORD', null, null, null);
INSERT INTO Hire (Hire_ID, Hire_Date, Drop_In_Day, Customer_ID, Vehicle_ID)
VALUES (10, '8/3/2017', '8/7/2017', 100, 112);
Edit
In your case the INSERT should look as follow if you want to keep the fk1_* naming.
INSERT INTO Hire
(Hire_ID, Hire_Date, Drop_In_Day, fk1_Customer_ID, fk2_Vehicle_ID)
VALUES (12, '8/3/2017', '8/7/2017', 101, 112);
The dates should work if the format fit your NLS_DATE_FORMAT but it is better to use the TO_DATE function.
The table DDL and INSERT statement posted by you are:
-- Create a Database table to represent the "Hire" entity.
CREATE TABLE Hire(
Hire_ID INTEGER NOT NULL,
Hire_Date DATE,
Drop_In_Day DATE,
fk1_Customer_ID INTEGER NOT NULL REFERENCES Customer (Customer_ID),
fk2_Vehicle_ID INTEGER NOT NULL REFERENCES Vehicle (Vehicle_ID) ,
-- Specify the PRIMARY KEY constraint for table "Hire".
-- This indicates which attribute(s) uniquely identify each row of data.
CONSTRAINT pk_Hire PRIMARY KEY (Hire_ID)
);
INSERT INTO Hire
(Hire_ID, Hire_Date, Drop_In_Day, Customer_ID, Vehicle_ID)
VALUES (10, '8/3/2017', '8/7/2017', 101, 112);
These are couple of observations:
The column name "Vehicle_ID" used in INSERT INTO Hire statement doesn't exist in the HIRE table.
Your HIRE table should have the column to create the foreign key constraint to reference the primary key.
CREATE TABLE Hire
(
Hire_ID INTEGER NOT NULL,
Hire_Date DATE,
Drop_In_Day DATE,
Customer_ID INTEGER NOT NULL,
Vehicle_ID INTEGER NOT NULL,
CONSTRAINT fk1_Customer_ID FOREIGN KEY (Customer_ID)
REFERENCES Customer (Customer_ID),
CONSTRAINT fk2_Vehicle_ID FOREIGN KEY (Vehicle_ID)
REFERENCES Vehicle (Vehicle_ID)
);
So, you have Vehicle_ID column in HIRE table as foreign key referencing to Vehicle_ID column in VEHICLE table which is the primary key of that table. Same applies to Customer_ID.
'8/3/2017' is NOT a DATE, it is a STRING. Always use TO_DATE or ANSI DATE literal DATE 'YYYY-MM-DD' to explicitly convert a string in to date.
For example:
TO_DATE('8/3/2017', 'DD/MM/YYYY')
DATE '2017-03-08'

Not expected result of the query

There is a query:
select * from
(
select
p.ID
from
MY_PRODUCT_PARENT_LINK pLink
inner join MY_PRODUCT p on pLink.FK_PARENT=p.ID
order by
pLink.DESCENDANT_LVL desc
)
where rownum <= 1
;
The inner query returns one row with not null value of p.ID.
The outer query - one row with null value of p.ID. Here expected not null value.
The database sever is Oracle 11g.
There is a code to reproduction:
CREATE TABLE MY_PRODUCT
(
FK_PARENT NUMBER(19),
ID NUMBER(19) NOT NULL,
NAME VARCHAR2(4000 BYTE),
CONSTRAINT PK_MY_PRODUCT PRIMARY KEY (ID)
);
INSERT INTO MY_PRODUCT (FK_PARENT, ID,NAME) VALUES (null, 111111,'PRODUCT_NAME_01');
INSERT INTO MY_PRODUCT (FK_PARENT, ID,NAME) VALUES (null, 111112,'PRODUCT_NAME_02');
CREATE TABLE MY_PRODUCT_PARENT_LINK
(
ID NUMBER(19) NOT NULL,
FK_PRODUCT NUMBER(19) NOT NULL,
FK_PARENT NUMBER(19) NOT NULL,
DESCENDANT_LVL NUMBER(19) NOT NULL,
primary key (ID)
);
ALTER TABLE MY_PRODUCT_PARENT_LINK ADD
CONSTRAINT MY_PRD_PARENT_TO_MY_PRODUCT
FOREIGN KEY (FK_PRODUCT)
REFERENCES MY_PRODUCT (ID) ON DELETE CASCADE;
ALTER TABLE MY_PRODUCT_PARENT_LINK ADD
CONSTRAINT MY_PRD_PARENT_TO_PARENT
FOREIGN KEY (FK_PARENT)
REFERENCES MY_PRODUCT (ID) ON DELETE CASCADE;
INSERT INTO MY_PRODUCT_PARENT_LINK (ID, FK_PRODUCT, FK_PARENT, DESCENDANT_LVL) VALUES (211111, 111112, 111111, 1);
Try and select the ID in the outer query and see if that returns the result. so instead of select * in the outer do select ID.

Unable to insert row into table with constraint

I have the following definition of table in my Database
CREATE TABLE Products
(
PartNo CHAR (6) NOT NULL ,
Description VARCHAR2 (16) NOT NULL ,
Weight SMALLINT NOT NULL
) ;
ALTER TABLE Products ADD CONSTRAINT CK_PartNo CHECK ( PartNo LIKE '[0-9][A-Z][0-9][0-9][0-9][0-9]') ;
ALTER TABLE Products ADD CONSTRAINT Products_PK PRIMARY KEY ( PartNo ) ;
When I try to insert a row I get an error of not meeting the constraint check.
insert into PRODUCTS(PARTNO, DESCRIPTION, WEIGHT)
values('1W1234', 'O-ring', 1);
*Action: do not insert values that violate the constraint.
Error starting at line : 1 in command -
insert into PRODUCTS(PARTNO, DESCRIPTION, WEIGHT)
values('1W1234', 'O-ring', 1)
Error report -
SQL Error: ORA-02290: check constraint (DEMO.CK_PARTNO) violated
02290. 00000 - "check constraint (%s.%s) violated"
*Cause: The values being inserted do not satisfy the named check
ALTER TABLE Products ADD CONSTRAINT CK_PartNo
CHECK ( regexp_like(PartNo, '\d[A-Z]\d{4}') ) ;
You can't use regular expression syntax in a LIKE, you need to use REGEXP_LIKE:
CHECK ( REGEXP_LIKE(PARTNO, '[0-9][A-Z][0-9][0-9][0-9][0-9]')
Demo:
CREATE TABLE Products
(
PartNo CHAR (6) NOT NULL ,
Description VARCHAR2 (16) NOT NULL ,
Weight SMALLINT NOT NULL
) ;
ALTER TABLE Products ADD CONSTRAINT CK_PartNo
CHECK ( REGEXP_LIKE(PARTNO, '[0-9][A-Z][0-9][0-9][0-9][0-9]') ) ;
ALTER TABLE Products ADD CONSTRAINT Products_PK PRIMARY KEY ( PartNo ) ;
insert into PRODUCTS(PARTNO, DESCRIPTION, WEIGHT)
values('1W1234', 'O-ring', 1);
1 rows inserted.

What query creates a trigger to generate composite primary key with two fk?

I'm trying to write a command to create a trigger that generates the composite primary key. This pk is in turn based on two fk.
I'll write example tables to be more specific.
(Table I'm working on)
CREATE TABLE DB.MESSAGE (
TEXT CLOB NOT NULL,
SUBJECT VARCHAR2(2000) NOT NULL,
MSG_TYPE NUMBER(1) NOT NULL,
MAIL_ID NUMBER(10) NOT NULL
)
;
ALTER TABLE DB.MESSAGE ADD CONSTRAINT MSG_PK PRIMARY KEY ( MSG_TYPE, MAIL_ID ) ;
ALTER TABLE DB.MESSAGE ADD
(
CONSTRAINT MESSAGE_TYPE_ID_FK
FOREIGN KEY ( MSG_TYPE )
REFERENCES DB.TYPES ( TYPE_ID )
);
ALTER TABLE DB.MESSAGE ADD
(
CONSTRAINT MESSAGE_MAIL_FK
FOREIGN KEY ( MAIL_ID )
REFERENCES DB.EML_MAIL ( MAILTO_ID )
);
(Referenced tables)
CREATE TABLE DB.TYPES (
TYPE_ID NUMBER(13) NOT NULL,
NAME VARCHAR2(10) NOT NULL
)
;
CREATE TABLE DB.MAIL (
MAIL_ID NUMBER(10) NOT NULL,
MAIL VARCHAR2(350) NOT NULL
)
;
My query so far
create or replace
TRIGGER DB.TRG_MESSAGE_ID
BEFORE INSERT ON DB.MESSAGE
FOR EACH ROW
BEGIN
IF INSERTING THEN
IF :NEW."MSG_ID" IS NULL THEN
SELECT DB.TYPES.TYPE_ID ??????
INTO :NEW."MSG_ID" FROM dual;
END IF;
END IF;
END;
EDIT: So the thinking behind this question was that there would be a separated column with a concatenations of both keys that compose the composite key.
A friend told me this is wrong, you just put both fields as pk and that's that. Is this true?