Unable to insert row into table with constraint - sql

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.

Related

Receiving endless errors with the insert - statement

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.

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.

PLS-00049: bad bind variable (1)

Someone could help me to find my error ?
CREATE OR REPLACE TRIGGER Assign_Prix
BEFORE INSERT OR UPDATE
ON DetailCommande
FOR EACH ROW
DECLARE
v_NOECHELLE Integer; v_PRIX Integer;
BEGIN
SELECT NOECHELLE INTO v_NOECHELLE
FROM ECHELLE
WHERE ddebut<=:new.ddebut AND dfin>=:new.dfin;
SELECT Min(Prix) INTO v_Prix
FROM DETAILECHELLE
WHERE noechelle = v_Echelle AND qtemin<=:new.qtemin;
:NEW.PRIX:= v_Prix;
END;
CREATE TABLE Echelle
(
NoEchelle NUMBER NOT NULL,
CodeProduit VARCHAR2 (15) NOT NULL ,
DDebut DATE NOT NULL,
DFin DATE NOT NULL
)
;
ALTER TABLE Echelle
ADD CONSTRAINT Echelle_PK PRIMARY KEY ( NoEchelle ) ;
ALTER TABLE Echelle
ADD CONSTRAINT Echelle_FK_Produit FOREIGN KEY (CodeProduit)
REFERENCES Produit(CodeProduit);
CREATE TABLE DetailEchelle
(
NoEchelle NUMBER NOT NULL ,
NoDetail NUMBER NOT NULL ,
QteMin NUMBER NOT NULL ,
Prix NUMBER NOT NULL
)
;
ALTER TABLE DetailEchelle
ADD CONSTRAINT DetailEchelle_PK PRIMARY KEY ( NoEchelle, NoDetail);
ALTER TABLE DetailEchelle
ADD CONSTRAINT DetailEchelle_FK_Echelle FOREIGN KEY (NoEchelle)
REFERENCES Echelle(NoEchelle);
CREATE TABLE Produit
(
CodeProduit VARCHAR2 (15) NOT NULL ,
CodeCategorie VARCHAR2 (5) NOT NULL ,
Nom VARCHAR2 (30) NOT NULL ,
Description VARCHAR2 (100) ,
CoutFixe NUMBER ,
LimiteSupCF NUMBER ,
Delai NUMBER
)
;
ALTER TABLE Produit
ADD CONSTRAINT Produit_PK PRIMARY KEY ( CodeProduit ) ;
ALTER TABLE Produit
ADD CONSTRAINT Produit_FK_Categorie FOREIGN KEY (CodeCategorie)
REFERENCES Categorie(CodeCategorie);
CREATE TABLE DetailCommande
(
NoCommande NUMBER NOT NULL ,
LigneCommande NUMBER NOT NULL ,
Qte NUMBER NOT NULL ,
Message VARCHAR2 (100) NOT NULL,
CodeProduit VARCHAR2 (15) NOT NULL ,
CodeCouleur VARCHAR2 (4) NOT NULL ,
PrixUnitVendu NUMBER,
CFVendu NUMBER
)
;
ALTER TABLE DetailCommande
ADD CONSTRAINT DetailCommande_PK PRIMARY KEY ( LigneCommande, NoCommande ) ;
ALTER TABLE DetailCommande
ADD CONSTRAINT DetailCommande_FK_Commande FOREIGN KEY (NoCommande)
REFERENCES Commande(NoCommande);
ALTER TABLE DetailCommande
ADD CONSTRAINT DetailCommande_FK_Produit FOREIGN KEY (CodeProduit, CodeCouleur)
REFERENCES ProduitCouleur(CodeProduit, CodeCouleur);
Your trigger is defined on the DetailCommande table. This table does not have a column named ddebut, dfin, qtemin or prix. Your trigger, therefore, cannot reference those columns in the :new pseudo-record since they don't exist in the table.
Unfortunately, if those columns don't exist in the DetailCommande table, I'm hard-pressed to guess at what your trigger is supposed to do so I have no idea what the proper syntax would be.

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?

ORA-00904: : invalid identifier

I am trying to create a Table in Oracle and getting the error : ORA-00904: : invalid identifier
Here is my command. I really can't see any problem in it. Please help me to identify the error. Thanks.
CREATE TABLE Sale (
CustomerId INT NOT NULL ,
BarCode INT NOT NULL ,
SalesId INT NOT NULL ,
Date DATE NULL ,
CheckOut TINYINT(1) NULL ,
PRIMARY KEY (CustomerId, BarCode, SalesId) ,
CONSTRAINT fk_Customer_has_Product_Customer
FOREIGN KEY (CustomerId )
REFERENCES Customer (CustomerId )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_Customer_has_Product_Product1
FOREIGN KEY (BarCode )
REFERENCES Product (BarCode )
ON DELETE NO ACTION
ON UPDATE NO ACTION);
The maximum length for an Oracle identifier is 30 characters. These exceed that, are 32 chars long:
fk_Customer_has_Product_Customer
fk_Customer_has_Product_Product1
See Schema Object Naming Rules
As previously mentioned, change "DATE" to something more descriptive and not reserved. also, it seems TINYINT does not work in a table create so change that to NUMBER(1), as well as Tony's correct suggestion of reducing the name size (<=30 chrs)
CREATE TABLE Sale
(
CustomerId INT NOT NULL ,
BarCode INT NOT NULL ,
SalesId INT NOT NULL ,
SaleDate DATE NULL , --DATE is reserved, changed to SaleDate
CheckOut number(1) NULL , --tinyint(1) did not work so changed to number(1)
PRIMARY KEY( CustomerId, BarCode, SalesId ) ,
CONSTRAINT fk_SaleCustCusID FOREIGN KEY( CustomerId ) REFERENCES Customer( CustomerId ) ON
DELETE NO ACTION ON
UPDATE NO ACTION,
CONSTRAINT fk_SaleCustBarCode FOREIGN KEY( BarCode ) REFERENCES Product( BarCode ) ON
DELETE NO ACTION ON
UPDATE NO ACTION
);