Multiple insert statement with same variable - sql

Is it possible to perform several SQL INSERT statements with the same value to multiple tables? For example, I am passing a ID and want to INSERT those into 4 tables. All tables share the same primary key, as table 2, table 3 and tabl 4 contain sub-properties of table1 elements.
INSERT INTO table1 (id)
VALUES ($phpVar);
INSERT INTO table 2 (id)
VALUES ($phpVar);
INSERT INTO table 3 (id)
VALUES ($phpVar);
INSERT INTO table 4 (id)
VALUES ($phpVar);
Further how could I bundle those into one statement, to prevent DB data failure during crashes etc?

Related

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

Proper way to make a relation between multiple rows of single table

I've got following situation: I want to connect multiple records from one table with some kind of relation. Record could have no connection to other, or could have multiple of them (1 or more). There is no hierarchy in this relation.
For example:
CREATE TABLE x
(
x_id SERIAL NOT NULL PRIMARY KEY,
data VARCHAR(10) NOT NULL
);
I've thought of two ideas:
Make a new column in this table, which will contain some relationId. It won't reference anything. When new record is inserted, I will generate new relationId and put it there. If I would want to connect other record with this one, I will simply put the same relationId.
Example:
CREATE TABLE x
(
x_id NUMBER(19, 0) NOT NULL PRIMARY KEY,
data VARCHAR(10) NOT NULL,
relation_id NUMBER(19, 0) NOT NULL
);
insert into x values (nextval, 'blah', 1);
insert into x values (nextval, 'blah2', 1);
It will connect these two rows.
pros:
very easy
easy queries to get all records connected to particular record
no overhead
cons:
hibernate entity will contain only relationId, no collection of
related records (or maybe it's possible somehow?)
Make a separate join table, and connect rows with many-to-many relation. Join table would contain two column with ids, so one entry would connect two rows.
Example:
CREATE TABLE x
(
x_id SERIAL NOT NULL PRIMARY KEY,
data VARCHAR(10) NOT NULL
);
CREATE TABLE bridge_x
(
x_id1 NUMBER(19, 0) NOT NULL REFERENCES x (x_id),
x_id2 NUMBER(19, 0) NOT NULL REFERENCES x (x_id),
PRIMARY KEY(x_id1, x_id2)
);
insert into x values (1, 'blah');
insert into x values (2, 'blah2');
insert into bridge_x values (1, 2);
insert into bridge_x values (2, 1);
pros:
normalized relation
easy hibernate entity mapping, with collection containing related
records
cons:
overhead (with multiple connected rows, every pair must be inserted)
What is the best way to do this? Is there any other way than these two?
The best way in my experience is to use normalization as you've said in your second option. What you are looking for here is to create a foreign key.
So if you use the example you've given in example 2 and then apply the following SQL statement, you will create a relational database that can have 0 to many relations.
ALTER TABLE `bridgex` ADD CONSTRAINT `fk_1` FOREIGN KEY (`xID`) REFERENCES `x`(`xID`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Insert data from one SQL Server table into another

I have to insert data from one table into another. The problem is that the table where the data has to be inserted into has a primary key.
Suppose there are 2 tables A and B.
A has two columns id (primary key) and name
B has two columns id and name
How can we insert data from table B into table A with the primary key column?
For example there are 2 tables A & B as you stated.
insert into A (id,name) values(2, 'Testing');
insert into B (id, foreign_key, name) values(1, 2, 'Hello');
Both tables have a primary key. Primary key of table A is used as a foreign key to connect to the table B so that they interact with each other
Here is its design:

inserting unique primary key exception

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!

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