populate many to many table from existing tables - sql

I have two existing tables with data populated
table A
--tableA_id
--contentA
table B
--tableB_id
--contentB
Now, I want to create a many to many relationship table
table A_B
--tableA_id
--tableB_id
the question is that how to write a sql script (I am new to sql) to populate table A_B using the existing data from table A and table B. Many thanks,
Mark

If you want to populate table A_B, you'd have to do this:
INSERT INTO A_B (tableA_id, tableB_id)
SELECT A.ID, B.ID FROM A CROSS JOIN B
CROSS JOIN will relate each row in table A with each row in table B.
If you want to relate some rows in table A with some rows in table B, you need to be more specific, and do something like:
INSERT INTO A_B (tableA_id, tableB_id)
SELECT A.ID, B.ID FROM A INNER JOIN B
ON A.some_field = B.some_other_field

Related

Delete data comparing two tables

I have two tables: Table A and Table B. Both tables have a column like a Name, Location, Level. Table A is an initial table and Table B is the updated version of Table A. That means there will be new data present in Table B. I want to write a query that deletes the data from Table B if data is present in Table A but not in Table B. I don't want to delete the data from Table B if the table has a new row of data.
My approach was like this
Delete From TableB Where Exist(
SELECT * FROM dbo.TableB AS TB
EXCEPT
SELECT * FROM dbo.TableA as TA)
This one deletes the data, but it deletes the data from Table B if it is new inserted data as well. Any kind of suggestion is appreciated.
DELETE FROM TableB
WHERE EXIST(
SELECT 1
FROM TableA
WHERE TableA.Name = Name AND TableA.Location = Location AND TableA.Level = Level
)
If TableA has a primary key, then it's enough to check only this key in WHERE condition. Furthermore, if you have this primary key (e.g. it's Name), then you can do like this:
DELETE FROM TableB
WHERE Name IN (SELECT Name FROM TableA)

SQL Update, update inserted data from table

I'm not sure whether my approach is correct or not. I have two table, A and B. I need to create ID on table A then the details on table B. Column ID in table A is auto incremented.
INSERT INTO TABLE A (DESCRIPTION)
SELECT REPLACE(DESCRIPTION, 100, #INCREMENT)
FROM TABLE A
Now I have created the ID in table A, I need to create new row on table B based on EXISTING data on table B. Below is my query.
INSERT INTO TABLE B (ID, JOBCODE)
SELECT ID, JOBCODE
FROM TABLE B INNER JOIN TABLE A ON A.ID = B.ID
How do I update the recently created row on table B so it match the ID on table A.

How to find what one table have elements of second table?

I have 2 Tables, that related with third by one-to-many relation.
For example I have Table A, Table B and Table C.
Table A and B related with table C as many-to-one. So in Table C I have 2 fields like tableAId and tableBId. As a result I need to find a list which includes all Elements from table C which related with table A and compare them to all elements from table C which related with table B.
I tried do it with except, minus statements, but it works incorrect.
Here is what I try to do:
SELECT tableAId FROM tableC
except
select tableBId FROM tableC
UPDATE
Here is my 3 tables :enter image description here
I'm not 100% following what you require but i think below is what you want
SELECT a.ID, b.ID
FROM TableA a
JOIN TableC c ON a.ID = c.TableCID
JOIN TableB b ON c.TableBID = b.ID

Insert new/Changes from one table to another in Oracle SQL

I have two tables with same number of columns :-Table A and Table B
Every day I insert data from Table B to Table A. now the insert query is working
insert into table_a (select * from table_b);
But by this insert the same data which was inserted earlier that is also getting inserted. I only want those rows which are new or are changed from the old data. How can this be done ?
You can use minus:
insert into table_a
select *
from table_b
minus
select *
from table_a;
This assumes that by "duplicate" you mean that all the columns are duplicated.
If you have a timestamp field, you could use it to limit the records to those created after the last copy.
Another option is, assuming that you have an primary key (id column in my example) that you can use to know whether a record has already been copied, you can create a table c (with the same structure as a and b) and do the following:
insert into table c
select a.* from table a
left join table b on (a.id=b.id)
where b.id is null;
insert into table b select * from table c;
truncate table c;
You need to adjust this query in order to use the actual primary key.
Hope this helps!
If the tables have a primary or unique key, then you could leverage that in an anti-join:
insert into table_a
select *
from table_b b
where not exists (
select null
from table_a a
where
a.pk_field_1 = b.pk_field_1 and
a.pk_field_2 = b.pk_field_2
)
You don't say what your key is. Assuming you have a key ID, that is you only want ID's that are not already in Table A. You can also use Merge-Statement for this:
MERGE INTO A USING B ON (A.ID = B.ID)
WHEN NOT MATCHED THEN INSERT (... columns of A) VALUES (... columns of B)

How to get a row from a Table with no ids

I have two tables, table A and table B, table A has and Id,a,b and c rows , Table B just has a,b and c and what i want (but i dont know how to do it) is to get a single row from Table B
(i would be something like comparinga,b,and c at the same time)
thanks!
SELECT A.id, B.a, B.b, B.c
FROM A
JOIN B
ON(A.a=B.a)
AND(A.b=B.b)
AND(A.c=B.c)
But I'd change your database structure and add foreign keyto table B referencing table A. It would really help you with this and later cases.
May I suggest changing your table structure.
TableA ( id primary key)
TableB(colA,colB,colC,fid foreign key refrencing TableA.id )
So now you dont have to store colA,colB,colC values in both TableA and TableB
Select B.*
From TableA A
Join TableB B
On A.id=B.fid