I have a table A that contains the following columns
ID(PK) id_1
1 4
2 10
3 15
4 4
Now Im trying to create a table B with columns such that
ID(PK) Description id_1_a_id (composite foreign key(ID,id_1))
1 Apple (1,4)
2 Orange (2,10)
3 Banana (3,15)
4 dog (4,4)
5
Does this design make sense? Or is there a better way to do this? (SQL rookie)
Composite foreign keys are common and useful, but you don't have one. If you did, it would look like this.
ID(PK) Description A_id id_1
--
1 Apple 1 4
2 Orange 2 10
3 Banana 3 15
4 dog 4 4
But you wouldn't ordinarily do that, either. Ordinarily, you'd reference a unique set of columns in table A. The unique set of columns is just the single column A.ID. So your table would usually look like this.
ID(PK) Description A_id
--
1 Apple 1
2 Orange 2
3 Banana 3
4 dog 4
You wouldn't usually duplicate the values of A.id_1 in table B. If you need the values from A.id_1, write a query with a JOIN.
select B.ID, B.Description, A.ID, A.id_1
from B
inner join A on A.ID = B.A_id;
If the only unique constraint you have in your tables is on the ID numbers, you're doing it wrong. But that's a different question.
Related
I have this data:
Table A
pk name
1 Peter
2 Jon
Table B
pk trait fk_A
1 funny 1
2 generous 1
3 rude 2
I want to get this result set:
pk name traits
1 Peter funny,generous
2 Jon rude
How can I aggregate the trait column of table B and attach it as a CSV field 'traits' to table A?
This question already has answers here:
Need SQL Query to find Parent records without child records
(6 answers)
Closed 3 years ago.
So I have 2 tables. Table B has a foreign key relationship with Table A.
For example.
Table A
id column 1 column 2
1 some thing
2 foo bar
3 hello world
4 this that
5 x y
Table B
id Table A id testcolumn
1 5 blah
2 5 blah
3 2 aggg
4 2 aggg
5 4 a
6 4 b
7 4 c
What I want is to select all of the elements from table A where there is no foreign key match in table B. In the case of the example I would want to select the row with id of 1 and 3 from table A. How would I accomplish that in a MS SQL database?
You can use an anti join:
select a.*
from a
left join b on a.id = b.table_a_id
where b.table_a_id is null
I have 2 Tables in Access and I am trying to build a LookUp Query. (I am new to SQL)
Dogs
ID
DogName
Type (int) Either 1,2,3,4
ClassResults
ID
ClassEntered (int) 1-24
DogName
So in my lookup I am trying to find all the dogs from the DOGS table that if the ClassEntered is less than 12 select all the dogs with a TYPE 1 or 2.
Dogs Data Sample:
ID DogName Type
0 AAA 1
1 BBB 3
2 CCC 1
3 DDD 2
4 EEE 4
ClassResults Data Sample:
ID ClassEntered DogName
0 6 ?????
So, the Drop Downlist for the DogName should be Showing:
0, AAA, 1
2, CCC, 1
3, DDD, 2
SELECT DISTINCT Dogs.DogName FROM Dogs, ClassResults
WHERE (IIf([ClassResults].[ClassEntered] < 10,[Dogs.Type]<3,[Dogs.Type]>2)) ORDER BY Dogs.DogName;
SELECT DISTINCT Dogs.DogName FROM Dogs, ClassResults
WHERE (IIf([ClassResults].[ClassEntered] < 10,[Dogs.Type]<3,[Dogs.Type]>2)) ORDER BY Dogs.DogName;
I hope that makes sense.
Stephan
It's better to use joins:
select * from dogs d
left join ClassResults c on d.dogname=c.dogname
where c. ClassEntered<12 and d.type in (1,2)
If you want to pick data from two different tables, you have to utilize joins.
I have to get “suggested” groupings of records based off of existing data.
Table A has a composite key of Akey+Bkey. Table B has a primary key of Bkey.
Akey is generated from a SQL 2012 Sequence object and there is a one to many relationship between table A and table and Table B on Bkey.
The structure and sample data are listed below.
Table A
Akey Bkey ItemSequence
---- ---- ------------
1 1 1
1 5 2
1 7 3
2 7 1
3 2 1
3 3 2
Table B
Bkey GroupValue Data HashString
---- --------------- -------------- ----------
1 Ford Festiva AIR BAG HASH1
2 Ford Festiva RADIATOR CAP HASH2
3 Ford Festiva FUEL PUMP HASH3
4 Ford Mustang AIR FILTER HASH4
5 Ford Explorer AIR FILTER HASH5
6 Ford Edge RADIATOR CAP HASH2
7 Ford Edge FUEL PUMP HASH3
The query has to insert new groups into Table A that match existing sets of Bkeys where the HASH value matches in Table B. To depict the point I use the example of a car model. The hash values of the items within the model of the car could match exactly ( but they might not ). So if all items exist within of OVER the GroupValue then I want to insert a new set of records into Table A with the values where the exact matches were found within that group.
In the sample data you can see that AKey 3 has BKeys 2 and 3 and Bkeys 6 and 7 are a match so these 2 records would be inserted into Table A and a new sequence # generated for the AKey.
I am not sure I understand the part about the groups and GroupValue field.
According to what I understand, you want to insert into Table A any rows from Table B that match the HashString for the Bkey of each AKey and don't already exist in Table A?
If I understand correctly: for Akey1, row TableB.Bkey 7 should be added to Table A. Is that right?
If the logic is good, the following query should select those rows.
SELECT
a.Akey, a.Bkey, b3.*
FROM b
INNER JOIN a ON a.Bkey=b.Bkey
INNER JOIN b b2 ON b2.HashString=b.HashString and b2.Bkey=a.Bkey
INNER JOIN b b3 ON b3.HashString=b2.HashString AND b3.Bkey <> b.Bkey
WHERE a.Akey=3 --Comment this line for the full matches
I have two tables Table A and Table B as follows.
TableA:
Id name
1 abc
2 john
3 jack
4 jill
Table B:
Id city phn
1 london 9876345
5 bangalore 2345678
3 chennai 5637473
I want records which are present in tableA but not in Table B.But the result should be
TableA:
Id name
1 abc
2 john
3 jack
4 jill
i.e even though 1 and 3 ids are present in Table B but they are still in table A.I want those records too.
5 bangalore 2345678
this records is not present in Table A.so i should not take this.
Really -- this simple? Don't think you need any joins then...
SELECT * FROM TableA
Good luck.
You need a left outer join.
Look into it here: http://en.wikipedia.org/wiki/Join_(SQL) and here: http://www.w3schools.com/sql/sql_join_left.asp
EDIT:
Your question makes no sense to be honest. In the heading you mention: "All the values in A and only common values in B" and then, you go on to state in the explanation that you need values from 'A' only and not B.. for that
select * from TableA will do.