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.
Related
I am creating a DB where in one table it holds store location information like location ID, address info, and a column for the inventory:
Location_ID
paint_inventory
address info
1001
red,blue,black
address stuff
1002
blue,orange
address stuff
The database also has a table that gives each paint an ID:
paint_ID
color_name
1
red
2
blue
3
purple
4
black
5
orange
How can I efficiently store this information where the location table looks like this (using an ID to reference the color info in the other table):
Location_ID
paint_inventory
address info
1001
1,2,4
address stuff
1002
2, 5
address stuff
NOTE:
I have seen posts where people say storing information in a delimited string is poor practice, however, I am basically recreating an already existing DB and the first location table is how the data is formatted.
UPDATE:
To rephrase a little: how could I efficiently store the paint_inventory column? I was given the data as it is in the first table and my only thought of how to store this is the way I provided (Once again I understand you should never store data as a delimited string but this is how I was given the data)
You should create a joining table -- that table would look like this
id int -- unique generated id for relationship
paint_id int -- pointer to the paint
location_id int -- pointer to the location
I like to call the tables by the things they join ordered alphabetically so I would call this table location_paint
There might be additional information for a can of paint at a location you could put in this table -- how full it is, when it was purchased, when it was used up, etc.
There could also be meta data in the table -- when the record was created when it was edited, who created it -- etc.
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,
I have a old database (using MariaDB) and I have to make a new one that's close to the same but has a few differences, and I have to insert all the data from the old one into the new one. I've populated the new one with all the equal data, but I'm stuck on getting the 'new' data into it.
The change is in the old database there was a column in multiple tables containing a country name, but in the new database Country has it's own table, so instead of a country name in a column, it is instead just the foreign key CountryID from the country table.
So the issue is I have to populate the new columns CountryID with whatever the Countries CountryID is. For example, if the country field in the customers table in the old database was USA, when I translate the data, instead of putting USA it has to go to the new Country table, find the CountryID that is equal to USA, and put that ID in the field instead. (Something like this)
Old Customers Table
--------------------
Country
USA
Canada
New Customers Table
-------------------
CountryID
3
7
CountryTable
----------
CountryID CountryName
3 USA
7 Canada
I know it's probably just a simple insert into with some condition but can't figure out the proper syntax for it.
I've tried different insert into statements similar to the following but keep getting errors:
insert into newDatabase.customers(CountryID)
select oldDatabase.customers.Country
from oldDatabase.customers
where oldDatabase.customers.country = newDatabase.countryTable.CountryName;
insert into newDatabase.customers(CountryID)
select oldDatabase.customers.Country
from oldDatabase.customers
inner join newDatabase.countryTable as c on c.countryName = oldDatabase.customers.Country
where oldDatabase.customers.country = newDatabase.countryTable.countryName;
The end goal is you want to insert the id from CountryTable into your new customers table, which means you are going to need that table. You are inserting the data from the old customers table, so it'll look like this:
INSERT INTO newdb.customers(CountryID)
SELECT ct.CountryID
FROM olddb.customers as oldc
INNER JOIN newdb.country_table as ct
ON ct.CountryName = oldc.Country;
You don't need a WHERE clause because you aren't trying to filter the rows from the old customers table. You just need the ID from the country table to be mapped with your old customers table. For that reason you are JOINing by the country's name to the country table to get that extra information.
I am creating a site that is sort of ecommerce-ish. I want to give my users a perfect search ability using specific attributes that differ from product to product. I plan to create 1 products table storing the basic information that is shared among products i.e Name, Description, Price and a few others. Then I plan to create several "details" table say categories_computers with columns Processor, HDD, RAM, etc and another table say table_shoes with columns MATERIAL, SIZE, GENDER, etc.
I am new to Mysql but not to the concept of Databases. I don't think I will have a problem storing this data to each table. My issue comes about from reads. It won't be hard to query a product id but I think it would be extremely wasteful to query all details tables to get the details of the product since 1 product can only have 1 details.
So my question is how can I store a reference to a table in a column so that a product has say ID, Name, Description, Price, Details_Table_ID or something similar to save on queries. Do tables have unique ids in Mysql? Or how does the Stackoverflow community suggest I go about this? Thanks.
EDIT
Silly me, I have just remembered that every table name is uniques so I can just use that, so my question changes to how I can write a query that contains one cell in a table A to be used as a reference to a Table name.
Don't use separate details tables for each category, use a generic details table that can store any attribute. Its columns would be:
Product_ID INT (FK to Products)
Attribute VARCHAR
Value VARCHAR
The unique key of this table would be (Product_ID, Attribute).
So if Product_ID = 1 is a computer, you would have rows like:
1 Processor Xeon
1 RAM 4GB
1 HDD 1TB
And if Product_ID = 2 is shoes:
2 Material Leather
2 Size 6
2 Gender F
If you're worried about the space used for all those attribute strings, you can add a level of indirection to reduce it. Create another table Attributes that contains all the attribute names. Then use AttributeID in the Details table. This will slow down some queries because you'll need to do an additional join, but could save lots of space
Think about just having a single ProductDetails table like this:
ProductDetailID (PK)
ProductID (foreign key to your Products table)
DetailType
DetailValue
this way you do not have to create new columns every time you add a new product detail type. and you'll have many ProductDetail rows for each productid, which is fine and will query ok. Just be sure to put an index on ProductDetails.ProductID !
Since this is an application so you must be generating the queries. So lets generate it in 2 steps. I assume you can add a column product_type_id in your Product table that will tell you which child table to user. Next create another table Product_type which contains columns product_type_id and query. This query can be used as the base query for creating the final query e.g.
Product_type_id | Query
1 | SELECT COMPUTERS.* FROM COMPUTERS JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
2 | SELECT SHOES.* FROM SHOES JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
Based on the product_id entered by the user lookup this table to build the base query. Next append your where clause to the query returned.
Sorry for the ambiguous title.
I have two tables:
table 1: mailing_email
table 2 (dynamic table but for now is): membership
table 1 contains a list of all email accounts in the database and few ancillary fields such as name. It also has a column called communicate.
communicate is basically my terminology for subscribed. Any unsubscribe link will set communicate to false.
Both mailing_email and membership have a email and communicate column.
I need to write a query where the following happens:
mailing_email.communicate gets updated to the current status of membership.communicate where mailing_email.email = membership.email. If an email exists in mailing_email which does not exist in membership, the communicate field stays the same.
How would i go about doing this the fastest possible way? Each table will have thousands of rows this sync command would run often.
MySQL offers an update join syntax:
UPDATE mailing_email
JOIN membership ON mailing_email.email = membership.email
SET mailing_email.communicate = membership.communicate