MySQL error - I don't understand what it is saying - sql

Query:
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`)
VALUES (6, '5')
Error:
Cannot add or update a child row: a foreign key constraint fails (mydb.job_listing_has_employer_details, CONSTRAINT job_listing_has_employer_details_ibfk_2 FOREIGN KEY (employer_details_id) REFERENCES employer_details (id))
What does this mean? The two ID's I am inserting into the table exist.

It means it can't find '5' in the id column of the employer_details table. If there is a 5 in that column of that table, then maybe the data is numeric, so must be passed without quotes. If so, try:
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`) VALUES (6, 5)

Related

How can I insert values into a table with a composite primary key?

These are the tables I already have:
CREATE TABLE Gyartok
(
GyID INT IDENTITY(2, 3),
Nev VARCHAR(20),
CONSTRAINT PK_Gyartok PRIMARY KEY (GyID)
)
CREATE TABLE Focicsuka
(
CsID INT IDENTITY(2, 2),
Meret INT,
CONSTRAINT PK_Focicsuka PRIMARY KEY (CsID)
)
CREATE TABLE FcsGyartjaGya
(
GyID INT IDENTITY(3, 2),
CsID INT,
Ar INT,
CONSTRAINT FK_FcsGyartjaGya1
FOREIGN KEY (GyID) REFERENCES Gyartok(GyID),
CONSTRAINT FK_FcsGyartjaGya2
FOREIGN KEY (CsID) REFERENCES Focicsuka(CsID),
CONSTRAINT PK_FcsGyartjaGya
PRIMARY KEY (GyID, CsID)
)
The problem is that every time I try to add new values to the table (like such)
INSERT INTO FcsGyartjaGya (Ar) VALUES (300);
I get an error saying I didn't initialize the CsID INT column:
Cannot insert the value NULL into column 'CsID', table 'Lab3.dbo.FcsGyartjaGya'; column does not allow nulls. INSERT fails.
I know I must initialize it with something, but I have no idea what do to it with, because IDENTITY(x, y) doesn't work (it's occupied by another column already) and adding another parameter to the code (like such)
INSERT INTO FcsGyartjaGya (Ar, CsID) VALUES (300, 7);
creates another error which says
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FcsGyartjaGya1". The conflict occurred in database "Lab3a", table "dbo.Gyartok", column 'GyID'.
It is important to note that I already filled every column with data, so that couldn't be the problem.
As I mention in the comments, your INSERT will work fine, provided the stars align correctly. For your table Gyartok you have GyID as your PRIMARY KEY, which is defined as a IDENTITY(2,3); so the first value generated is 2 and then each row attempted to be INSERTed will increment by 3.
So, if we run the following, we get the IDs 2, 5, 7 and 17. (11 and 14 are skipped as the INSERT failed).
CREATE TABLE Gyartok (
GyID INT IDENTITY(2, 3),
Nev VARCHAR(20),
CONSTRAINT PK_Gyartok PRIMARY KEY (GyID)
);
GO
INSERT INTO dbo.Gyartok (Nev)
VALUES ('asdfjahsbvd'),
('ashjkgdfakd'),
('kldfbhjo');
GO
INSERT INTO dbo.Gyartok (Nev)
VALUES (REPLICATE('A',25)), --Force a truncation error
('ashjkgdfakd');
GO
INSERT INTO dbo.Gyartok (Nev)
VALUES (REPLICATE('A',15));
Let's now add some data for your other table:
CREATE TABLE Focicsuka (
CsID INT IDENTITY(2, 2),
Meret INT,
CONSTRAINT PK_Focicsuka PRIMARY KEY (CsID)
)
INSERT INTO dbo.Focicsuka (Meret)
VALUES(12),
(25);
Now we want to INSERT into the table FcsGyartjaGya, defined as the following:
CREATE TABLE FcsGyartjaGya (
GyID INT IDENTITY(3, 2),
CsID INT,
Ar INT,
CONSTRAINT FK_FcsGyartjaGya1 FOREIGN KEY (GyID) REFERENCES Gyartok(GyID),
CONSTRAINT FK_FcsGyartjaGya2 FOREIGN KEY (CsID) REFERENCES Focicsuka(CsID),
CONSTRAINT PK_FcsGyartjaGya PRIMARY KEY (GyID, CsID)
)
This has a IDENTITY on GyID, but defined as an IDENTITY(3,2), so the first value is 3 and then incremented by 2.
As this has 2 foreign keys, on GyID and CsID when we INSERT the row the values must appear in the respective tables. As GyID is defined as anIDENTITY(3,2) however, this is where we need to rely on the Stars luck for the INSERT to work. Why? Well 2 + (3*n) and 3+(2*n) can give very different numbers. The first are as you saw at the start of this answer. For the latter, we have numbers like 3, 5, 7, 9, 11. As you can see, only 1 in 3 of these numbers match a number in our original sequence, so luck is what we are going to be relying on.
Let's, therefore, try a single INSERT.
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(2,1);
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FcsGyartjaGya1". The conflict occurred in database "Sandbox", table "dbo.Gyartok", column 'GyID'.
Well, that didn't work, but it was expected. 3 isn't a value in the table Gyartok. Let's try again!
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(2,2);
It worked! The stars Luck was our side, and the IDENTITY value was a value in the table Gyartok. Let's try a couple of rows this time!
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(4,3),
(4,4);
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FcsGyartjaGya1". The conflict occurred in database "Sandbox", table "dbo.Gyartok", column 'GyID'.
No!! Not again. :( That's because the stars didn't align; 7 and 9 aren't in the other table. But wait, 11 was in the sequence, so let's try that:
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(4,5);
Error, again?! No, it cannot be!!! :( Oh wait, I forgot, the stars were against us before, because that INSERT failed against Gyartok for the value of 11. I need to wait for 17!
--13 fails
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(4,6);
GO
--15 fails
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(4,6);
GO
--17 works!
INSERT INTO dbo.FcsGyartjaGya (CsID,Ar)
VALUES(4,6);
And now we have another row in our table.
So what is the problem? Your design. GyID is defined as an IDENTITY and a FOREIGN KEY; meaning you are at the "whims" of SQL Server generating a value valid. This is not what you want. Just don't define the column as an IDENTITY and then INSERT the data with all 3 of your columns defined:
CREATE TABLE FcsGyartjaGya (
GyID int,-- IDENTITY(3, 2),
CsID INT,
Ar INT,
CONSTRAINT FK_FcsGyartjaGya1 FOREIGN KEY (GyID) REFERENCES Gyartok(GyID),
CONSTRAINT FK_FcsGyartjaGya2 FOREIGN KEY (CsID) REFERENCES Focicsuka(CsID),
CONSTRAINT PK_FcsGyartjaGya PRIMARY KEY (GyID, CsID)
)
GO
INSERT INTO dbo.FcsGyartjaGya (GyID, CsID, Ar)
VALUES(2,2,1),
(2,4,2),
(5,4,3),
(8,2,4),
(8,4,5);
And all these rows insert fine.
I think there is a bit confusion, if I understand correctly what You're trying to do, then you have two tables each with their own id, which is based on an identity column, so you get new values in those for free.
Then you are trying to make a relation table with extra data.
Issue 1: You cannot have FcsGyartjaGya.GyID be identity if it refers to Gyartok.GyID because you will want to insert into it and not rely on an auto increment. If it doesn't refer to the same it should have another name or my head will possibly explode :))
Issue 2: When populating a relation table you need to insert it with what pairs you want, there is no way SQL server can know how it should match these identity pairs in the relation table
I think this is what people are aiming at in the comments, for example
to insert a relationship between row with Focicsuka.CsID = 1 to Gyartok.GyID 7 and adding Ar = 300 have to look like
INSERT INTO FCSGYARTJAGYA(GYID, CSID, AR)
VALUES(7, 1, 300)
Unless You've forgotten to mention that you want to put some value for each of some value or based on something which can be scripted, in other words unless You have logics to define the pairs and their values, relationship tables cannot have defaults on their foreign key fields.

Adding rows to table with self-referencing foreign key

I've created a table called TableTest with two columns ent and dep. ent is the primary key and dep is the foreign key which references ent. I create the table using:
CREATE TABLE TableTest (
ent varchar(2) NOT NULL,
dep varchar(2),
PRIMARY KEY (ent),
FOREIGN KEY (dep) REFERENCES TableTest(ent)
);
I must show that the three values (A1,A2,A3) depend on one another. A3 is dependent of A1 etc. However when I try to insert a row into my table such as:
INSERT INTO TableTest(ent, dep)
VALUES ('A1','A3');
I get the following error and after doing research I'm still stuck on how to get by this. I'm very new to SQL.
ORA-02291: integrity constraint violated - parent key not found
Any help is greatly appreciated!
First, you need to insert the root value.
> insert into TableTest values ('A1', null);
> insert into TableTest values ('A3', 'A1');
There are cases, just like the one you posted, where circular references (which are absolutely fine by the way, no logical issue there) conflict with the normal way relational integrity constraints work. This is because relational integrity has some "directional" features (primary key comes first, then foreign key) even though dependencies can be circular, as you have seen.
There are several work-arounds. The easiest is to make the foreign key constraint deferred. That means that the constraint is checked only when you commit, not after each individual insert.
Another is to insert all values at the same time (in the same INSERT statement); for example:
insert into tabletest(ent, dep)
select 'A1', 'A3' from dual union all
select 'A3', 'A2' from dual union all
select 'A2', 'A1' from dual
;
Pablo's answer is okay but you can do something alse too if you don't want to have nulls; First insert same value for both PK and FK and then insert the relation:
insert into TableTest values ('A1', 'A1');
insert into TableTest values ('A3', 'A1');

H2 INSERT SELECT ON DUPLICATE KEY UPDATE throws "Unique index or primary key violation" error

H2 (started with MODE=MYSQL on) supports INSERT ON DUPLICATE KEY UPDATE statement only with VALUES clause, while throws a "Unique index or primary key violation" error when using INSERT SELECT statement.
Here is an example:
-- creating a simple table
CREATE TABLE test_table1 (
id INT NOT NULL,
value VARCHAR(255) NOT NULL,
PRIMARY KEY (id))
ENGINE = InnoDB;
-- inserting a value
INSERT INTO test_table1
VALUES (1, 'test1');
-- trying to insert on duplicate key update: it works!
INSERT INTO test_table1
VALUES (1, 'test2')
ON DUPLICATE KEY UPDATE value='test2';
-- trying using INSERT SELECT: it throws Unique index or primary key violation: "PRIMARY KEY ON PUBLIC.TEST_TABLE1(ID)"
INSERT INTO test_table1
SELECT 1, 'test2'
FROM test_table1
ON DUPLICATE KEY UPDATE value='test2';
I'm using H2 db version 1.4.192.
Is it a bug? Or is there something wrong with my code?
Thank you
On you H2 console, if you have 'HIBERNATE_SEQUENCES' table make sure to check what is the NEXT_VAL for SEQUENCE_NAME = 'default'.
In my case, I had 2 row (insert statement) in my /src/main/resources/data.sql and the NEXT_VAL was 2 which was causing problems. I changed to 3 with update statement, and it now works fine.
Is there something wrong with my code?
Yes, there is. Why are you inserting into an auto-increment column? You should be specifying the columns with non-autogenerated data. So:
INSERT INTO test_table1(value)
VALUES ('test1');
And:
INSERT INTO test_table1(value)
SELECT 'test2'
FROM test_table1
ON DUPLICATE KEY UPDATE value = VALUES(value);
Your are getting the error because ON DUPLICATE KEY resets value, but that has nothing to do with the primary key on the table.

DB2 is_autoincrement and primary key issue

Good day,
I have a table, which some columns, and BELTID set to primary key, and IS_AUTOINCREMENT set to YES.
I wish to insert a row of data inside this data without key in the BELTID, I am expect the BELDID will auto generate.
The query is as follow:
INSERT INTO mySchema.TABLE1(TYPE, ORIGINALBATCHID, MANUAL)
VALUES ('TEST', 124, 1);
I get this error:
SQLSTATE: 23505. A violation of the constraint imposed by a unique index or a unique constraint occurred.
Then I change my query to:
INSERT INTO mySchema.TABLE1(BELTID, TYPE, ORIGINALBATCHID, MANUAL)
VALUES (123, 'TEST', 124, 1);
And I am getting another error:
SQLSTATE: 428C9 A ROWID column cannot be specified as the target column of an INSERT or UPDATE.
Kindly advise on what mistake I make.

oracle sql: insert spatial data from more existing table

I have created a table umriss which I filled with data and I still need to insert the geometry data from existing tables (usrdemo.glets_1850, usrdemo.glets_1973, ...). How does that work?
umriss is a "weak entity" and has references to the tables gletscherstand (glst_id) and gletscher (gletscher_id)
create table umriss
(
umr_nr number (4) not null,
umr_datum date,
GLST_ID number (4) not null,
shape mdsys.sdo_geometry,
GLETSCHER_ID number (3) not null,
se_anno_cad_data blob
);
alter table umriss
add constraint umriss_glst_pk
primary key (umr_nr, GLST_ID, GLETSCHER_ID);
ALTER TABLE umriss
ADD CONSTRAINT umriss_gletscherstand_fk
FOREIGN KEY (GLST_ID, GLETSCHER_ID)
REFERENCES GLETSCHERSTAND(GLST_ID, GLETSCHER_ID);
I manually inserted the data for the attributes umr_nr, umr_datum, glst_id and gletscher_id. As you can see from umr_nr there are 3 shapes and I now want to add the spatial data from usrdemo_glets_1850 which has 3 shapes and the attributes: objectid (= umr_nr in table umriss), shape and se_anno_cad_data.
I tried this...
INSERT INTO umriss u
(u.shape, u.se_anno_cad_data)
SELECT usrdemo.glets_1850.shape, usrdemo.glets_1850.se_anno_cad_data
FROM usrdemo.glets_1850;
...and got the message: Ora-01400 - cannot insert NULL into ..."umriss"."umr_nr"
How does this work?
You haved tried to insert new records into table umriss without setting values for the primary key column umr_nr.
If I have understood correctly, you have already manually inserted records into table umriss, and now what you need to do is to add addtional columns only, then you should use UPDATE instead.