inserting unique primary key exception - sql

I was trying to insert records to a table using statement like
insert into table
(table_id, xxx,xxx)
values
(100,xxx,xxx)
and get the unique violation on the primary key.
and i did a select
select * from table where table_id = 100;
There is no record there.Thought insert before then, delete record.
And if i dont insert the table id, i got a null violation.
I was wondering if there is anyway to deal with this(maybe sequence)? I need to insert several thousands records using the inserting statement. some hundered have this prob.
Thanks in Advance!

Related

concurrent insertion with unique key constraint

I have a web application with multiple instance running all connected to a p-SQL with a table - myTable that have only two columns with one column is having unique key constraint.
This application will receive at least 15K request in a second from multiple instance and my requirement is to avoid the duplicate insertion in the table.
myTable:
CREATE TABLE myTable (
id SERIAL PRIMARY KEY,
myData VARCHAR (50) UNIQUE);
Insert statement:
INSERT INTO myTable (myData) VALUES ('data-1');
My question is if all the instances try to insert same data (assume the above insert statement from all the instances), all at a time to the table will I get only one record inserted and one instance will get inserted success message and all other instance will get a duplicate key error?

How can I Insert and update in one query

Trying to write insert and update in the query. So my scenario is - If a row exists then do upsert and insert a new row. But row didn't exist only do insert.
So here what I am trying -
Select exist(Select 1 from table where condition)
if(exists){
Update table set column =value where condition.
Insert into table (column) values (" ").
}else{
Insert into table (column) values (" ")
}
As I am writing here so many queries, is there any possibility I can cover all these in one query.
Thanks in advance
You can write just insert query and check for CONFLICT CLAUSE.
Postgresql supports conflict clause, which is triggered when there was some conflict regarding particular CONSTRAINT.
Lets say you have table t with primary key column id and name field:
CREATE TABLE t ( id integer primary key, name varchar(255));
You can check primary key constraint, when you insert:
INSERT INTO t (id, name) VALUES (1, 'Name1')
ON CONFLICT ON t_id_constraint
DO UPDATE SET name='Name1';
t_id_constraint is constraint name for id column uniqueness.
This query will insert new values to table when there is no conflict and will update when new id is found on table.
Original answer

Oracle inserting into table with composite key

I have a table, TBL_1, with the following fields:
TBL_ID NUMBER (pk),
CREATE_DATE DATE (pk),
TBL_IND VARCHAR2(1)
The primary key is on TBL_ID and CREATE_DATE, I am trying to perform an insert statement but getting an error ORA-00001: unique constraint (primary key) violated.
There is a before insert trigger setting the NEW.CREATE_DATE as SYSDATE. The insert statement looks like:
INSERT INTO TBL_1 (tbl_id,tbl_ind)
SELECT tbl_id,'Y'
FROM tbl_info;
The actual query is a little more complex but I just wanted to point out it is a INSERT INTO SELECT statement. Is it possible if there is a duplicate tbl_id the trigger used the same exact date for both rows thus causing a duplicate error? How can I avoid this?
I don't think it is a good idea to have create_date as part of the primary key. I would suggest that you use a sequence value instead.
If you don't mind reducing the number of rows, you can do:
INSERT INTO TBL_1 (tbl_id,tbl_ind)
SELECT DISTINCT tbl_id, 'Y'
FROM tbl_info;
Or, if you still want all rows inserted, then restructure your data to use a sequence instead of the creation date.

SQL Inserts sql Developer

I am using Oracale SQL Developer, and I am wondering when I insert data into a table, will the data cascade to another table that has the first tables primary key as a foreign key for example if I run
insert into DEPARTMENT values ('1', 'Karate');
Will the first value in Column 'DEPARTMENTID' also be inserted into the other table I.E 'TEACHER' or do I have to manually insert the value into the both tables.
Thanks
No, inserts will not cascade. You'll have to insert data in parent table and then in child tables.

SQL Insert from a source table to another table

Not sure how to google this... Here's what I need to do but I'm not sure how to do the insert and generate the NewID at the same time.
I have 2 tables one (pcx_candidate_to_pcx_vacancyId) is empty and only has 3 fields candidateid, vacancyId and its primary key, all 3 fields are guids. I need to get the data from a second table that has the matching fields but I also have to create and insert a guid at the same time. The source table (pcx_vacancyassociationExtensionBase) has 2 matching fields. Finally I will use NewID() to generate the new guid for the primary key.
You can insert directly from one table to another via an insert into ... select ... query:
insert into pcx_candidate_to_pcx_vacancyId (id, candidateid, vacancyId)
select NewID(), candidateid, vacancyId
from pcx_vacancyassociationExtensionBase