SQL doesn't allow me to insert data - sql

creating table and inserting data into a table and now it giving me an error
SQL Error: ORA-02291: integrity constraint (S21403051.SYS_C007300)
violated - parent key not found 02291. 00000 - "integrity
constraint (%s.%s) violated - parent key not found" *Cause:
CREATE TABLE CUSTOMER(
CUSTOMER_ID VARCHAR(10) PRIMARY KEY,
FIRST_NAME VARCHAR(10),
SURNAME VARCHAR(15),
CUSTOMER_TEL VARCHAR(12),
CUSTOMER_EMAIL VARCHAR(30)
)
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_102', 'EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_53');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_107','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_106','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1','CUST_108','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_104','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_109','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_52');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_51');

When you have defined 6 columns CUSTOMER_ID , FIRST_NAME , SURNAME ,
CUSTOMER_TEL,CUSTOMER_EMAIL and when you try to insert values, it takes in order of table definition. Instead you can try this way
Example:
INSERT INTO CUSTOMER_CRUISES
(column1, clumn2,column3)
values ('xx','xy','yz')
Yet, you can not insert duplicate value into first column as primary key is defined on it. And, it cant be null.
Hope this helps

Cause: A foreign key value has no matching primary key value.
This problem arises when you attempt to insert a record containing the Customer_ID column into the child table (CUSTOMER_CRUISES) and that this Customer_ID is not present in the parent table (CUSTOMER). When the Customer_ID (Foreign key) in CUSTOMER_CRUISES table does not get to reference the Customer_ID (primary key) in CUSTOMER table, an error is raised.
One workaround is to insert and make sure that the value is present in the CUSTOMER table first before inserting the values into the CUSTOMER_CRUISES table.

Related

SQL table with primary key over two columns and unique values in both columns

How to create a table in SQL with the following attributes?
The table has two columns A and B.
The primary key of the table is (A, B).
All values in A are unique. Pseudo code: (Count(A) == COUNT(SELECT DISTINCT A)).
All values in B are also unique.
CREATE TABLE IF NOT EXISTS myTable(
A VARCHAR(32) PRIMARY KEY NOT NULL, -- A HAS DISTINCT VALUES
B VARCHAR(32) NOT NULL -- B HAS DISTINCT VALUES
);
INSERT INTO myTable VALUES ('A1', 'B1') --> Add value
INSERT INTO myTable VALUES ('A1', 'B2') --> Do not add value
INSERT INTO myTable VALUES ('A2', 'B2') --> Add value
INSERT INTO myTable VALUES ('A3', 'B3') --> Add value
INSERT INTO myTable VALUES ('A4', 'B3') --> Do not add value
INSERT INTO myTable VALUES ('A4', 'B4') --> Add value
INSERT INTO myTable VALUES ('A5', 'B6') --> Add value
To define a compound PRIMARY KEY:
CREATE TABLE myTable
(
A VARCHAR(32) NOT NULL,
B VARCHAR(32) NOT NULL,
CONSTRAINT PK_AB primary key (A,B),
CONSTRAINT UQ_A UNIQUE(A),
CONSTRAINT UQ_B UNIQUE(B)
);
Please note: a table with just 2 columns with both columns in the primary key smells funny.

How to delete from 3 tables with one query using where exists

I have four tables Monitoring, Participant, Participant validation group, Validation group,
I would like to delete all monitorings, and there particpants also each particpant possibly (not always) has participant validation group.
Blockquote
but i have the error sql command not properly ended.
NOTE: I am on oracle database and inner joins would not work here.
I would like to achieve an optimized query
that could take only the application number of monitoring and delete all monitorings, participants for each monitoring and participant validation group.
I am not sure if its actually possible in a single query if its not then what could be the optimized way to do this. and I cant use jpa for this at the moment.
Expected results, both monitorings are deleted, and there particpants as well as participant validation groups
You can define foreign keys on child tables to "ON DELETE CASCADE". So when you delete a parent table it will delete associated rows from child tables.
create table parent (
id NUMBER(10),
value varchar2(30),
constraint parent_pk primary key (id)
);
CREATE TABLE child
( id NUMBER(10) not null,
value NUMBER(10) not null,
constraint child_pk primary key (id,value),
CONSTRAINT parent_child_fk
FOREIGN KEY (id)
REFERENCES parent(id)
ON DELETE CASCADE
);
CREATE TABLE grandchild
( id NUMBER(10) not null,
value NUMBER(10) not null,
constraint grandchild_pk primary key (id,value),
CONSTRAINT child_grandchild_fk
FOREIGN KEY (id,value)
REFERENCES child(id,value)
ON DELETE CASCADE
);
insert into parent values (1,'a');
insert into parent values (2,'b');
insert into parent values (3,'c');
insert into child values (1,1);
insert into child values (1,2);
insert into child values (1,3);
insert into child values (2,1);
insert into child values (2,2);
insert into child values (2,3);
insert into child values (3,1);
insert into child values (3,2);
insert into child values (3,3);
insert into grandchild values (1,1);
insert into grandchild values (1,2);
insert into grandchild values (1,3);
insert into grandchild values (2,1);
insert into grandchild values (2,2);
insert into grandchild values (2,3);
insert into grandchild values (3,1);
insert into grandchild values (3,2);
insert into grandchild values (3,3);
SELECT (
SELECT COUNT(*)
FROM parent
) AS parent_cnt,
(
SELECT COUNT(*)
FROM child
) AS child_cnt,
(
SELECT COUNT(*)
FROM grandchild
) AS grandchild_cnt
FROM dual
DELETE from parent where value = 'a';
SELECT (
SELECT COUNT(*)
FROM parent
) AS parent_cnt,
(
SELECT COUNT(*)
FROM child
) AS child_cnt,
(
SELECT COUNT(*)
FROM grandchild
) AS grandchild_cnt
FROM dual
PARENT_CNT CHILD_CNT GRANDCHILD_CNT
2 6 6

SQL: 2 Foreign Keys referencing one Primary Key

I have a database diagram and need to create a Database with different tables.
This is my code:
use FirmaLieferungen;
drop table liefert;
drop table rabatt;
drop table artikel;
drop table firma;
SET DATEFORMAT dmy;
create table firma (
fnr integer primary key,
name char(10),
jahrgruendung integer, -- Gründungsjahr
land char(3)
);
insert into firma values (101,'Schwer' ,1890,'A' );
insert into firma values (102,'Schmal' ,1901,'CH' );
insert into firma values (103,'Tief' ,1945,'I' );
insert into firma values (104,'Breit' ,1950,'A' );
insert into firma values (105,'Leicht' ,1945,'F' );
insert into firma values (106,'Hoch' ,1920,'CH' );
insert into firma values (107,'Hell' ,1900,'A' );
create table artikel (
fnr integer,
lfdnr integer,
bezeichnung char(10),
preis decimal(6,2),
einheit char(3),
land char(3),
primary key(fnr, lfdnr),
foreign key(fnr) references firma
);
insert into artikel values (101,1,'Schaufel' ,12.30,'Stk','A' );
insert into artikel values (101,2,'Hacke' ,15.20,'Stk','F' );
insert into artikel values (102,1,'Spaten' ,13.00,'Stk','A' );
insert into artikel values (103,1,'Schere' , 8.00,'Stk','A' );
insert into artikel values (103,2,'Messer' ,10.60,'Stk','F' );
insert into artikel values (103,3,'Schnur' , 1.10,'m' ,'D' );
insert into artikel values (105,1,'Schnur' , 0.40,'m' ,'D' );
insert into artikel values (106,1,'Hacke' ,20.70,'Stk','CH' );
insert into artikel values (106,2,'Draht' , 0.60,'m' ,'CH' );
create table liefert (
fnrvon integer,
fnran integer,
fnr integer,
lfdnr integer,
datum date,
menge decimal(8,2)
primary key(fnrvon, fnran, fnr, lfdnr, datum),
foreign key(fnr, lfdnr) references artikel,
foreign key(fnr) references firma
);
insert into liefert values (101,102,101,1,'01.02.1999', 3.00);
insert into liefert values (101,102,101,1,'02.01.2000', 2.00);
insert into liefert values (101,104,101,2,'13.02.2000', 11.00);
insert into liefert values (101,104,101,1,'24.11.1999', 19.00);
insert into liefert values (101,105,103,3,'31.03.2001', 1553.00);
insert into liefert values (102,101,102,1,'21.04.1999', 28.00);
insert into liefert values (102,101,101,1,'11.12.1999', 1.00);
insert into liefert values (102,104,101,1,'04.07.2000', 63.00);
insert into liefert values (103,101,103,3,'21.04.1999', 3.25);
insert into liefert values (103,104,101,1,'08.02.1998', 17.00);
insert into liefert values (104,102,105,1,'19.11.2001', 132.50);
insert into liefert values (104,106,101,1,'04.07.2000', 22.00);
insert into liefert values (106,102,101,1,'07.08.2002', 81.00);
insert into liefert values (106,102,106,2,'01.06.2002', 21.30);
insert into liefert values (106,104,101,1,'26.09.2001', 2.00);
create table rabatt (
fnrvon integer,
fnran integer,
prozent decimal (5,2),
primary key (fnrvon, fnran),
foreign key (fnrvon, fnran) references firma
);
insert into rabatt values (101,102, 5.25);
insert into rabatt values (102,101, 5.50);
insert into rabatt values (101,103,15.75);
insert into rabatt values (103,102, 7.50);
insert into rabatt values (102,103,10.50);
insert into rabatt values (105,106, 5.25);
insert into rabatt values (104,101, 7.50);
select * from rabatt;
select * from firma;
select * from liefert;
select * from artikel;
But there's an error in the 'rabatt' creation, it says that the last command is invalid.
foreign key (fnrvon, fnran) references firma
This is somehow wrong, but I don't know why... Is the diagram wrong? There are also two keys going from 'liefert' to 'firma' how do I do this? Please help me!
Thanks! (I'm using Microsoft SQL Server 2008)
When you reference another table, you should specify which column(s) you are referencing.
So, for example:
create table liefert (
fnrvon integer,
fnran integer,
fnr integer,
lfdnr integer,
datum date,
menge decimal(8,2)
primary key(fnrvon, fnran, fnr, lfdnr, datum),
foreign key(fnr, lfdnr) references artikel (fnr, lfdnr),
foreign key(fnr) references firma (fnr)
);
Mureinik is right, just answered 1 sec before me.
For the sake of the question I crated a Fiddle Improve it for further questions
http://sqlfiddle.com/#!6/6a1b7
Your firma table doesn't have columns named fnrvon or fnran so
foreign key (fnrvon, fnran) references firma
fails to figure out what is referenced.
You need to be explicit. Also, If the two columns are both referencing the same foreign key, then it needs to be two separate statements
foreign key (fnran) references firma (fnr)
foreign key (fnrvon) references firma (fnr)

SQL Error: ORA-00933: SQL command not properly ended Error at Line: 3 Column: 35

I'm creating a script that creates a TABLE and I have problem with the last command which I have to do:
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek FROM
Studenci.RokUrodzenia to_date('1980','YYYY')
This is my script; what am I doing wrong?
CREATE TABLE Studenci(
NrIndeksu NUMBER(3) PRIMARY KEY,
Nazwisko VARCHAR2(16),
RokUrodzenia NUMBER(4),
Kierunek VARCHAR2(12)
);
CREATE TABLE Wykladowcy(
IdWykladowcy NUMBER(4) PRIMARY KEY,
Nazwisko VARCHAR2(16),
Stopien VARCHAR2(6),
Stanowisko VARCHAR(8)
);
CREATE TABLE Kursy(
IdKursu NUMBER(1) PRIMARY KEY,
Nazwa VARCHAR2(18),
IdWykladowcy NUMBER(4) REFERENCES Wykladowcy
);
CREATE TABLE Rejstracje(
NrIndeksu NUMBER(3) REFERENCES Studenci ,
IdKursu NUMBER(1) REFERENCES Kursy ,
Data DATE
);
CREATE UNIQUE INDEX Ind_Kursy_naz ON Kursy(Nazwa);
CREATE INDEX Ind_Studenci_naz ON Studenci(Nazwisko);
CREATE INDEX Ind_Wykladowcy_naz ON Wykladowcy(Nazwisko);
INSERT INTO Wykladowcy VALUES (1010,'Kowalski Jan', 'Dr', 'Adiunkt');
INSERT INTO Wykladowcy VALUES (1011,'Jakubowski Emil','Dr hab','Docent');
INSERT INTO Wykladowcy VALUES (1012,'Gazda Mirosław','Dr','Profesor');
INSERT INTO Kursy VALUES (1,'Bazy danych',1010);
INSERT INTO Kursy VALUES (2,'Systemy operacyjne',1012);
INSERT INTO Kursy VALUES (3,'Multimedia',1011);
INSERT INTO Kursy VALUES (4,'Sieci komputerowe',null);
INSERT INTO Studenci VALUES (101,'Kuczyńska Ewa',1980,'Bazy danych');
INSERT INTO Studenci VALUES (102,'Lubicz Robert',1985,'Multimedia');
INSERT INTO Studenci VALUES (103,'Krajewski Bogdan',1988,'Bazy danych');
INSERT INTO Studenci VALUES (104,'Lityńska Anna',1987,'Multimedia');
INSERT INTO Studenci VALUES (105,'Marzec Marcin',1982,'Multimedia');
INSERT INTO Studenci VALUES (106,'Cichaocki Rafał',1989,'Bazy danych');
INSERT INTO Rejstracje VALUES (101,1,NULL);
INSERT INTO Rejstracje VALUES (102,3,NULL);
INSERT INTO Rejstracje VALUES (104,3,NULL);
INSERT INTO Rejstracje VALUES (106,1,NULL);
INSERT INTO Rejstracje VALUES (104,2,NULL);
INSERT INTO Rejstracje VALUES (101,4,NULL);
INSERT INTO Rejstracje VALUES (103,1,NULL);
INSERT INTO Rejstracje VALUES (103,1,NULL);
INSERT INTO Rejstracje VALUES (105,1,NULL);
UPDATE Rejstracje SET Rejstracje.IdKursu=Rejstracje.IdKursu*3
WHERE Rejstracje.NrIndeksu=105;
COMMIT UPDATE
INSERT INTO Rejstracje(Data)
VALUES (Sysdate);
SELECT *FROM Kursy
WHERE Kursy.IdWykladowcy IS NULL;
SELECT *FROM Rejstracje
WHERE Rejstracje.NrIndeksu=101;
SELECT Kursy.Nazwa
FROM Kursy
ORDER BY Nazwa ASC;
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek
FROM Studenci.RokUrodzenia to_date('1980','YYYY')
From the error message it seems like a missing ;.
This SQL:
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek
FROM Studenci.RokUrodzenia to_date('1980','YYYY');
is invalid because the to_date() call can't go there like that.
You are probably looking for something like:
SELECT Studenci.Nazwisko,
Trunc(Months_Between(Sysdate,Studenci.RokUrodzenia)/12) Wiek
FROM Studenci
WHERE RokUrodzenia = 1980;
given that RokUrodenzia is a NUMBER(4) column and not a DATE column. You may still have to do some work to get the MONTHS_BETWEEN() call to work since RokUrodenzia is not a DATE (as I said) and MONTHS_BETWEEN probably expects a date. You'll have to investigate how to fix that; you might use TO_DATE() there legitimately.

How to avoid bad data entries in table through proper Key relationship?

I have 3 tables. widgets, widget types, widget type ID. I have created direct relationship between them. How can I avoid bad data going into Widget tables based on Widget_type and Widget_sub_Type. Please see my code. There is just one small thing missing.
Create table widgets (
Widget_ID int not null primary key,
Widget_type_ID int not null,
Widget_Sub_type_ID int,
Widget_Name varchar (50)
)
Create table Widget_Type (
Widget_Type_ID int not null primary key,
)
Create table Widget_Sub_type (
Widget_Sub_Type_ID int not null primary key,
Widget_Type_ID int not null
)
---adding foregin key constraints
Alter table widgets
ADD constraint FK_Widget_Type
FOREIGN KEY (Widget_type_ID)
References Widget_Type (Widget_type_ID)
Alter table widgets
ADD constraint FK_Widget_Sub_Type
FOREIGN KEY (Widget_Sub_type_ID)
References Widget_SUB_Type (Widget_SUB_type_ID)
Alter table widget_Sub_Type
ADD Constraint FK_Widget_Type_Alter
Foreign key (widget_Type_ID)
References Widget_Type (Widget_Type_ID)
---- insert values
insert Widget_Type values (1)
insert Widget_Type values (5)
insert Widget_Sub_type values (3,1)
insert Widget_Sub_type values (4,1)
insert Widget_Sub_type values (7,5)
insert Widget_Sub_type values (9,5)
-- This will error out which is correct
insert Widget_Sub_type values (5,6)
select * from Widget_Sub_type
select * from Widget_type
--Good
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (1, 'TOY', 1, 3)
select * from widgets
--Good
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (2, 'BatMan', 5, 7)
-- How to prevenet this, 3 is not sub_type_id of type_ID 5. This is bad data, It should not be inserted.
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (3, 'Should Not', 5, 3)