Add new column 'Aid' to B table and link it to the increment id in A - sql

I am new to database design. I am trying to create two tables which are linked by 'name' from A table and 'Aname' from table B. But what i want to do is creating a new increment int column 'id' as a representation for 'name' in A table and then add a new column in B called 'Aid' which is the foreign key.
My idea is to first (SQL SERVER)
SELECT row_number() over(order by A.name) id, A.* from A;
But how can I link this id to 'Aid' in B table?
Or if there is any existing statements for this question?
Thank you!

If you have the columns already defined, you can use join in an update:
update b
set b.a_id = a.a_id
from b join
a
on b.name = a.name;

I think you need to first look up how to add an auto-increment column to an existing dataset, which there are a number of resources for online. You can find one that is specific to your version of SQL Server. Then you can look into joining your B table to your A table on that. Don't add data to a table that is intended to be joined on a ROW_NUMBER result though. This is not good database design.

Related

Inserting values from another table into a column where values are null

I have a table A with ID's only and another table B with two columns, ID and Product Number. The ID column in table B has nulls and Product Number has Product Numbers. I would like to update table B with the ID's in column in no specific order just so that the Product Number has ID's.
I have tried to use update but that has not worked, have tried insert but it just adds the ID's in A to the bottom of the list in B. Would like to do this in Microsoft SQL.
SQL code tried:
IF OBJECT_ID('tempdb..#ProductNum') IS NOT NULL DROP TABLE #ProductNum
SELECT ID
INTO #ProductNum
FROM Products
UPDATE [ProductCatalogue] PC
SET
PC.ID = Pn.ID
FROM #ProductNum Pn
INNER JOIN
[ProductCatalogue] PC
ON Pc.ID = Pn.ID
WHERE Pc.ID IS NULL
It sounds a lot like you would be better off having the ID-Column Autoincrement, instead of giving it the IDs from table A. This is already explained in this answer.
In case you actually need the specific IDs from table A, this SO thread might help you.
Solved the issue by creating auto increment columns on each table and called it Row_ID. Then I used Row_ID to join the tables together with some logic provided by #Chris above.

Yii2 : Joining child table to two parent tables

I have two parent tables and 1 child table in this format -
A - a_id, a_name (parent)
B - b_id, b_name (parent)
C - c_id, a_id, b_id
aid and bid are foreign keys in table C.
I am trying to create a dataProvider starting from Model A, and want to join C to the picture such that the code looks like
$query->joinWith([B,A&B.C]
But I don't know how to make C join to both A and B for the given query using Yii2 standards. Any help would be appreciated.
Thanks
To make JOIN with Yii2 through multiple DataProvider models you can use simple:
$results = $tableADataProvider->joinWith(['cRelation.bRelation')
->where([...]);
And of course in A table DataProvider you need to have relation to C table, in this example cRelation and in C table DataProvider you need to have relation to B table bRelation.
Also to mention IF you don't use any column from tables B and C in WHERE clause, but just you want to retrieve results from B then it's much better to use $tableADataProvider->with([...]) which fetches data trough another query. This way even if they are pivot tables, you will get correct count of Table A.
Worthy to note that you probably didn't get correct count (count that you wanted) if table(s) is/are pivot table(s).
(Pivot table is a table that have more than 1 record that have same unique key of other table)

How to create a query that returns only records in one table without a foreign key

How do I create a query that returns only records in one table that have no foreign key relations with a particular attribute? I'm not even sure how to properly formulate the question, so I'm giving an example below:
I have Table B. It has an ID and other stuff that's not important.
I have Table C. It has an ID, an attribute (call it Available) which is only ever 'Yes' or 'No'.
Each record in Table B may have zero or more records in Table C related to it. They are connected by Table BC_Line, where each record has a FK for B_ID and a FK for C_ID. Hence, if records C_ID=1 and C_ID=2 are related to B_ID=1, then BC_Line has two records: (B_ID_FK=1 C_ID_FK=1) and (B_ID_FK=1 C_ID_FK=2).
I want a query that returns ONLY records in B that have NO associated records in C with C_Available='No'. A record in B might have several related records in C, all with 'Yes', and that would be shown. A record in B might have several related records in C all with 'No', that would NOT be shown. A record in B might also have records in C, some 'Yes' and some 'No', but that record would still not be shown. All related C records must be 'Yes'.
I'm not sure how to do it. I understand how to create queries and how to do Joins, but I do not know how to combine them in such a way as to get what I want. It's possible this is a well known problem, but I haven't been able to find the answer, partly because I have difficulty articulating my problem.
For the record, I am using Oracle.
You could use EXISTS:
SELECT *
FROM TableB b
WHERE NOT EXISTS (SELECT 1
FROM TableC c
WHERE c.C_Available='No'
AND b.B_ID_FK =c.C_ID);
Note! I've assumed that c.C_Available is defined as NOT NULL and it could hold only Yes/No values.
You could also use the left join method.
select b.*
from tableB b left join bc_line on b.b_id = bc.b_id
left join tableC c on bc.c_id = c.c_id
and c.c_available = 'No'
where c.c_id is null

View new count column

I have 2 tables. One who is like a transactional table
userId category
Second table also has
userId (as primary key)
and other columns (not important)
I wanted to create a view based on these 2 tables. Also, I want to have one column which puts into the cells the count from the transactional table who has the same id and eventually matches a special categorie f.e "car".
I thought of maybe using nested select statement, but since i'm a beginner i'm a little bit confused.
Try something like this.
select count(b.userId) as ID, b.category from table2 a inner join transactional b
on a.userId = b.userId where b.category= 'car' group by b.category;

Update a field from one table to another, involving a 3 table join

I have a table I need to update the price field in. I need to update this field from a different price field from a different table. The only way I can get to the required table for the update is by joining another table into this query.
So in all I need to join 3 tables in the update.
Table A has the price field that needs to be updated. In this table I have the foreign key of the product.
Table A structure
-PK_TABLE_A,
-FK_TABLE_B,
-ITEM_COST,
-ITEM_PRICE (needs to be updated from Table C)
Table B is the product table which has the PK of the product. This table is used to access Table C.
I also need to filter Table B to only update a certain stock type.
Table B structure
-PK_TABLE_B,
-FK_TABLE_C,
-DESCRIPTION,
-QUANTITY,
-ITEM_TYPE (a string that needs to be added in where clause to only update records with certain type).
Table C has a foreign key back to Table B. It also contains the price field that I need to use to update the price field in table A
Table C structure
-PK_TABLE_C,
-FK_TABLE_B,
-PRICE (this is the field that I need to use to update the price field in table A)
-USED_DATE,
-ID
The DBMS I am using is Firebird.
I have tried to use sub queries to no avail. I regularly use a sub-select when using two tables to update, so something like
UPDATE table1 AS t1
SET t1.FK = (select table2.PK
FROM table2 INNER JOIN
table1
ON table2.FK = table2.PK
WHERE table2.name = t1.name)
I'm just struggling to use the same technique with a 3rd table incorporated.
I am not even sure if this is the correct way to go about this situation. I have looked on google, but most examples I have come across don't utilise the 3rd table.
Any help would be appreciated.
**edited to included more detail on table structure.
are you able to show us the table structures in more detail?
if both tableA and tableC have a foreign key that points back to tableB I don't think you need to include a three table join. you just need to
update tableA set ITEM_PRICE = SELECT(PRICE FROM TableC WHERE
TableA.FK_TABLE_B = TableC.FK_TABLE_B;
unless I'm missing something?
edited to reflect a better understanding of the problem
alright, I think I've got it this time:
update tableA set price =
(select price from tableC where tableA.fk_tableB = tableC.fk_tableB) where
(Select item_type from tableB where tableB.pk_tableB = tableA.fk_tableB) =
'$itemTypeVariable';
edited again with a better understanding of the problem