MS Access loop query for every row on a table - ms-access

I'm not very experienced with MS Access and I'm trying to loop through every record on a table (tbl_jobs_main) and run this query against it.
SELECT division.[Department]
FROM division
WHERE tbl_jobs_main.[org1] = division.[ORG1];
My hope is that the new column "Department" in the tbl_jobs_main table can be auto filled from the division table with the Department values by matching the ORG1 values for each row. To clarify, the ORG1 values in the division table are unique and the org1 values in tbl_jobs_main are duplicated.
Any assistance would be greatly appreciated.
Edit:
(Sorry, not enough rep to post images on this profile.)
Example data in Division table:
https://i.stack.imgur.com/LYDh3.png
Example data in tbl_jobs_main table:
https://i.stack.imgur.com/zIel9.png
I need for the Department column to populate with the corresponding Department in the Division table based on the matching ORG1 value.
Ideally I could run something so the tbl_jobs_main becomes this:
https://i.stack.imgur.com/VU1ko.png

Whilst you can loop through tbl_jobs_main and update the Department based on data in the Division table, you can also use an update Query. The SQL will look like:
UPDATE tbl_jobs_main AS J INNER JOIN Division AS D
ON D.Org1=J.Org1
SET J.Department=D.Department
However, I am not sure why you would want to do this. If the name of "Dept1" in the division table changes, you then have to re-update tbl_jobs_main to reflect this. Far better to use Access as a relational database, where you store the department information in the Division table, and link the tables.
Regards,

Related

How to sort a column based on another column to resolve can't add or update child row issue

I have a table which contains id and supervisorId. Id is the PK and supervisorId is the foreign key (Self Join).
When I try to insert the data into the table, mysql throws unable to add or update a child row error. Because of relationship between two columns, I can't populate the supervisorId column without Id column.
Is there any way to insert the data in a single query?
You need to insert the rows in two steps:
Insert the rows without supervisors (i.e. supervisor_id = NULL.
Then update the rows with the correct supervisor, when you know their ids.
It is not clear how you are processing the data. If you are trying to move rows from one table to another, you should probably ask another question, with sample data, desired results, and a clear explanation of what you want to accomplish.

Multiple rows inside each table row (but only on certain columns)

I'm not quite sure how to word this so I've not managed to find an answer for it!
I want to create a table, where in certain columns there is multiple rows. As shown in the picture
How do I structure this?
An example of what I'm trying to achieve is imagine a table that listed all users in an application. Each row is a user, but I want to also have a sub row for each of the photos that a user may have.
You cannot have multi-valued columns. As they do not satisfy first normal form (For detailed information about 1st Normal form: https://en.wikipedia.org/wiki/First_normal_form)
Let your table have the following columns:
(A,B,C,D)
As per the picture provided by you, I am making the following assumptions:
1) A is the primary key
2) C and D are both are multi-valued and the rows have values like (A1,B1,C1,D1),(A1,B1,C2,D2). That is for single A value we have multiple pairs of C and D.
Do comment if any of the assumption is wrong.
What you can do is make two tables.
TABLE1 (A,B)
TABLE2 (A,C,D)
Where A is the primary key in TABLE1 and foreign key in TABLE2.
As asked for a snippet,you can have tables like these:
Tables

Create a new lookup table where data already exists

I am working on a database in MS Access 2013 which has a considerable amount of non-normalised data, and I want to move them out to alternate tables and use them as lookups in the main table. However, when I create a lookup column, MS Access deletes the data and there is far too much data to reset every record by hand.
Is there a way in Access 2013 to create such a lookup without losing the data?
Please don't comment about how using lookup tables in Access is bad. I have read posts like the one below and I disagree with most of the points there, and some of them are just simply wrong.
http://access.mvps.org/access/lookupfields.htm
Below is a sample of my data. I need to extract the 2nd and 3rd fields to other tables. If I can do this with them, I can do it with the others.
Presently this is stored as text in the fields. I would like to remove them and replace them with FK id's.
You could create your second table and add the data to the table. Then update the first table to match the records to each other
Let say you have the following table:
CustOrders
ID Customer DateOrdered
123 K-Mart 01/01/2013
124 K Mart 01/05/2013
125 Walmart 02/05/2013
126 Walmart 03/07/2013
127 Miejers 03/11/2013
128 K-Mart 03/12/2013
You could find out all of the Customers that are in the CustOrders table by performing the following:
SELECT DISTINCT Customer From CustOrders
Then create a record in the following table for each:
Customers
ID Customer
1 K-Mart
2 Walmart
3 Miejers
Then you could Update the CustOrders table by performing the following:
UPDATE CustOrders SET Customer = 1 WHERE Customer = 'K-Mart' OR Customer = 'K Mart'
Of course you would have to do this for each distinct customer you have.
Then if you want you could change the data type of the Customer field in the CustOrders table to Long Integer.
Lastly I would create a combo box on any entry/edit form that has the following RowSource:
SELECT ID, Customer FROM Customers ORDER BY Customer
Set the combo box to limit from list and bind to column 1.

Adding a database record with foreign key

Let's say there is a database with two tables: one customer table and one country table. Each customer row contains (among other things) a countryId foreign key. Let's also assume that we are populating the database from a data file (i.e., it is not an operator that is selecting a country from a UI).
What is the best practice for this?
Should one query the database first and get all ID's for all countries, and then just supply the (now known) country id's in the insert query? This is not a problem for my 'country' example, but what if there is a large number of records in the table that is being referred?
Or should the insert query use a sub query to get the country id based on the country name? If so, what if the record for the country does not exist yet and has to be added?
Or another approach? Or does it depend? :)
I would suggest using a join in your insert query to get the country id based on the country name. However, I don't know if that's something possible with every SGBD and you don't give more precision on the one you're using.

MySQL: LIKE Query Help?

I have a column in my table called student_id, and I am storing the student IDs associated with a particular record in that column, delimited with a | character. Here are a couple sample entries of the data in that column:
243|244|245
245|1013|289|1012
549|1097|1098|245|1099
I need to write a SQL query that will return records that have a student_id of `245. Any help will be greatly appreciated.
Don't store multiple values in the student_id field, as having exactly one value for each row and column intersection is a requirement of First Normal Form. This is a Good Thing for many reasons, but an obvious one is that it resolves having to deal with cases like having a student_id of "1245".
Instead, it would be much better to have a separate table for storing the student IDs associated with the records in this table. For example (you'd want to add proper constraints to this table definition as well),
CREATE TABLE mytable_student_id (
mytable_id INTEGER,
student_id INTEGER
);
And then you could query using a join:
SELECT * FROM mytable JOIN mytable_student_id
ON (mytable.id=mytable_student_id.mytable_id) WHERE mytable_student_id.student_id = 245
Note that since you didn't post any schema details regarding your original table other than that it contains a student_id field, I'm calling it mytable for the purpose of this example (and assuming it has a primary key field called id -- having a primary key is another requirement of 1NF).
#Donut is totally right about First Normal Form: if you have a one-to-many relation you should use a separate table, other solutions lead to ad-hoccery and unmaintainable code.
But if you're faced with data that are in fact stored like that, one common way of doing it is this:
WHERE CONCAT('|',student_id,'|') LIKE '%|245|%'
Again, I agree with Donut, but this is the proper query to use if you can't do anything about the data for now.
WHERE student_id like '%|245|%' or student_id like '%|245' or student_id like '245|%'
This takes care of 245 being at the start, middle or end of the string. But if you aren't stuck with this design, please, please do what Donut recommends.