Unique identifier across multiple tables? - mysql

Unique identifier across multiple tables?
Using MySQL, I'm trying to associate products with a purchase order.
Those products can become from different tables probably with different schema's.
I cant associate because of the identity of each product by schema, not by all kinds of products.
Cellphone
+-----+--------+---------+-------+----------+-------+
| id | brand | version | color | capacity | price |
+-----+--------+---------+-------+----------+-------+
| 1 | iphone | 5s | black | 16 | 9500 |
| 2 | iphone | 5s | white | 32 | 10000 |
| 3 | iphone | 5s | blue | 32 | 10000 |
+-----+--------+---------+-------+----------+-------+
Course
+-----+-----------+------------+-------+
| id | topic | idschedule | price |
+-----+-----------+------------+-------+
| 1 | Photoshop | 1 | 9500 |
| 2 | HTML5 | 2 | 10000 |
| 3 | CSS3 | 3 | 10000 |
+-----+-----------+------------+-------+
I've made a table Product, that together with a product identity, produce an unique identifier.
Product
+-----+-----------+
| id | Schema |
+-----+-----------+
| 1 | Cellphone |
| 2 | Course |
+-----+-----------+
ProductEspecification
+-----+-----------+------------------+----------+------------+
| id | idproduct | idespecification | quantity | idpurchase |
+-----+-----------+------------------+----------+------------+
| 1 | 1 | 1 | 10 | 1 |=> 10 iphones
| 2 | 2 | 3 | 1 | 1 |=> 1 CSS3 course
+-----+-----------+------------------+----------+------------+
Is there a better approach?

You need to use abstraction and Table Inheritance.
An abstract thing you sell (good, service, etc) is called a Product.
An abstract physical good you sell is called a Good. Good inherits Product.
An abstract service you offer is called a Service. Service inherits Product.
Cellphone inherits Good.
Course inherits Service.

Related

MySQL 5.7 - Total quantity column for Bill of Materials

I would like to ask for a help.I use MySQL 5.7 and I have a Bill of Materials table called BOM.
I have many columns there which I will add later to the query, but for this questionID, Name, ParentID and Quantity is important. I would like to add a calculated column totalQty to my table that will multiply children quantities with parent quantities up to the top level. here is an example of what I want to achive:
+-------+------+------+----------+----------+--+
| ID | Name | Qty | ParentID | TotalQty | |
+-------+------+------+----------+----------+--+
| 1 | A | 1 | 0 | 1 | |
| 1.1 | AA | 2 | 1 | 2 | |
| 1.1.1 | AAA | 1 | 1.1 | 2 | |
| 1.2 | AB | 5 | 1 | 5 | |
| 1.2.1 | ABA | 2 | 1.2 | 10 | |
| 2 | B | 3 | 0 | 3 | |
| 2.1 | BA | 2 | 2 | 6 | |
+-------+------+------+----------+----------+--+
I need this to create a list of materials used per project, so I will multiply totalQTY with unit mass and unit length to get total mass and total length of part. Then I will group and aggregate them by material type.
I have searched the forums, and I have found out that I need to use Common table expressions (CTE) for this. But I have found no explanation in plain english on how to do it for my case.
Thank you very much for any help or hint how to understand the concept.

Whats the best way to store different prices for one entity in SQL?

So I am building a webpage that shows a bunch of video games located in a SQL database and one suggestion I had was to have the different prices from each region display based on a drop down. My question is trying to figure out whats the best way to store int in the database. Would it be like:
GAME
CountryID
price1
CountryID
price2
CountryID
price3 ...
Or is there a better way to do this?
Just a heads up I've only been developing web applications for a year or so and I'm still pretty new to SQL.
Thanks for your input!
I would use multiple tables, one for games and one for region pricing.
Games
+--------+----------+
| GameID | GameName |
+--------+----------+
| 1 | Game1 |
| 2 | Game2 |
| 3 | Game3 |
| 4 | Game4 |
+--------+----------+
RegionPricing
+----------+--------+-------+
| RegionID | GameID | Price |
+----------+--------+-------+
| 1 | 1 | 60 |
| 1 | 2 | 55 |
| 1 | 3 | 45 |
| 1 | 4 | 80 |
| 2 | 1 | 50 |
| 3 | 2 | 30 |
| 3 | 3 | 25 |
| 3 | 4 | 45 |
| 4 | 1 | 60 |
| 4 | 2 | 55 |
| 4 | 3 | 45 |
| 4 | 4 | 80 |
+----------+--------+-------+
By using separate tables you minimize duplicate data and allow for easy granular changes. You may also consider adding a column to RegionPricing for currency. This would also need a Region table, with RegionID and RegionName.

Union tables into 1 with a flag indicating the respective table entries

i have 2 tables.
1.home_shop
+---+------------+
|id | product |
+---+------------+
| 1 | soap |
| 2 | cake |
| 3 | biscuit |
+---+------------+
2.office_shop
+---+------------+
|id | product |
+---+------------+
| 1 | key |
| 2 | lock |
| 3 | pen |
+---+------------+
what i want is union this two tables into a new table "complete_shop" with a flag indicating "home" and "office"
for example:
+---+------------+-------------+
|id | product | flag |
+---+------------+-------------+
| 1 | soap | home |
| 1 | key | office |
| 2 | cake | home |
| 2 | lock | office |
| 3 | biscuit | home |
| 3 | pen | office |
+---+------------+-------------+
how do i do this union in mysql please help me. i am a beginner
Do a UNION query and introduce the flag column using the appropriate values.
SELECT id, product, 'home' AS flag
FROM home_shop
UNION ALL
SELECT id, product, 'office' AS flag
FROM office_shop
ORDER BY id, flag
Note that you don't need to use a subquery to order here, you can just specify the columns you want to use.

Should I have two separate tables if they differ by only one column?

I am developing a movie database with both credits for actors and filmmakers. Currently, the actor and filmmaker credits are all together in one table because they only differ by one column (the "role_id" foreign key never gets used for filmmaker credits -- it is always NULL). Is it still best practice to keep them in one table ("movie_credits") or would it be better to separate ("actor_credits" and "filmmaker_credits")?
Here is a very basic schema I've defined, and I'm including some example data in it:
movie_credits
+----+-----------+---------+---------------+----------+
| id | talent_id | role_id | identity_id | title_id |
+----+-----------+---------+---------------+----------+
| 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 1 | 2 |
| 3 | 1 | NULL | 2 | 4 |
+----+-----------+---------+---------------+----------+
talent
+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1 | Clint | Eastwood |
| 2 | Sylvester | Stallone |
+----+------------+-----------+
roles
+----+---------------+
| id | name |
+----+---------------+
| 1 | Walt Kowalski |
| 2 | Rocky Balboa |
+----+---------------+
identities
+----+--------------+
| id | identity |
+----+--------------+
| 1 | actor |
| 2 | director |
| 3 | producer |
| 4 | screenwriter |
+----+--------------+
titles
+----+-----------------+
| id | name |
+----+-----------------+
| 1 | Gran Torino |
| 2 | Rocky |
| 3 | Creed |
| 4 | American Sniper |
+----+-----------------+
If you see a completely different way of structuring the data for this scenario, please let me know. I am open to any solution that may be even more efficient and scalable.
Basically, both actor and filmmaker are persons.
So they must be stored in a single table.
We must avoid null and redundancy from the database. But in this case, making the extra table will increase much overhead than the profit of removing null.
Also, you can simply assign filmmaker as the role to avoid nulls.
That is, there will be a role named filmmaker.

build dynamic sql insert query to merge multiple tables into single table using mapping table

I am looking to take data from multiple mysql tables with different structures and insert it into one master table containing different columns/data from the sources,
I have created a column name mapping table with the following data:
TABLE: affiliate_datafeed_magento_mapping
+----+-------------+----------------+
| id | datafeed | magento |
+----+-------------+----------------+
| 1 | SKU | sku |
| 1 | Name | name |
| 1 | Description | description |
| 1 | Url | affiliate_link |
| 1 | Price | price |
| 1 | Brand | brand |
| 1 | ModelNumber | model |
| 2 | SKU | sku |
| 2 | Name | name |
| 2 | Description | description |
| 2 | Url | affiliate_link |
| 2 | Price | price |
| 2 | Currency | affiliate_cur |
+----+-------------+----------------+
id corresponds to the id of the affiliate program in the table affiliate_programs
datafeed corresponds the column name in the source table with the corresponding id in the table affiliate_program
magento corresponds the the column name in the table to receive the data
TABLE: affiliate_programs
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
| id | affiliate_network | affiliate_network_short | affiliate_program | affiliate_program_long | affiliate_program_short | affiliate_program_datafeed_url | affiliate_program_datafeed_csv_filename | affiliate_program_datafeed_last_get | order |
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
| 1 | CommissionFactory | cf_ | Lifestyle Clotheslines | lifestyleclotheslines | lcl | http://dashboard.commissionfactory.com.au/Affiliate/Creatives/DataFeeds/jPuB5NPigbec7prnjLCX5MHygO$A5sTlhOHb8t3nmufzta#k5uyn5qqxrLDr86GysLTj$bTyoKaj77Pltfmh9dvnkOCS4MHzjvTSlK6Dfg==/ | cf_lifestyleclotheslines.csv | 2013-08-12 22:23:02 | NULL |
| 2 | CommissionFactory | cf_ | MacArthur Baskets | macarthurbaskets | mcb | http://dashboard.commissionfactory.com.au/Affiliate/Creatives/DataFeeds/jPSA4dbg17SY7svvjeSX5Jf1iO#b5JXshOfY#ovjzeKj4PGivuyn5qqxrLDr86GysLTj$bTyoKaj77Pltfmh9dvnkOCS4MHzjvTSlK6Dfg==/ | cf_macarthurbaskets.csv | 2013-08-12 22:23:02 | NULL |
| 3 | ClixGalore | cg_ | Boutique Wineries | boutiquewineries | btw | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=9522&AfID=264058 | cg_boutiquewineries.csv | 2013-08-12 22:23:02 | NULL |
| 4 | ClixGalore | cg_ | Brewtopia | brewtopia | bwt | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4817&AfID=264058 | cg_brewtopia.csv | 2013-08-12 22:23:02 | NULL |
| 5 | ClixGalore | cg_ | MacArthur Baskets | macarthurbaskets | mcb2 | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4909&AfID=264058 | cg_macarthurbaskets.csv | 2013-08-12 22:23:02 | NULL |
| 6 | ClixGalore | cg_ | Winemakers Choice | winemakerschoice | wmc | http://www.is1.clixGalore.com/DataFileRequest.aspx?AdID=4282&AfID=264058 | cg_winemakerschoice.csv | 2013-08-12 22:23:02 | NULL |
+----+-------------------+-------------------------+------------------------+------------------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------+-------------------------------------+-------+
id corresponds to the same id in the table affiliate_datafeed_magento_mapping
the concatenation of affiliate_programs.affiliate_network_short and affiliate_programs.affiliate_program_long make up the name of the source table
TABLE: cf_lifestyleclotheslines
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
| id | DateCreated | DateModified | SKU | Name | Category | Description | Url | OriginalUrl | Image | Image50 | Image100 | Image120 | Image200 | Image300 | Image400 | Price | Brand | ModelNumber |
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
| 1 | 2012-10-19 10:52:50 | 2013-06-20 02:07:37 | 30 | Airaus Ceiling Mounted Clothes Airer | Home & Garden > Household Supplies > Laundry Supplies > Drying Racks & Hangers | Watch the Product Video Just arrived from Europe! Lifestyle Clotheslines is now stocking the latest ceiling mounted clothes airers from Europe. Ceiling mounted clothes airers are the perfect alternative for those with limited space situations or who need the benefit of an indoor clothesline or airer. Boasting 6 individually adjustable drying rods which are made of steel not plastic, these indoor airers can be used in a range of situations and are ideally suited for the disabled as they can easily be lowered or raised to the height of the user. An indoor airer can also help extend the life of your clothes by reducing the exposure to harsh UV rays and they can also be a big power saver for your home when used to dry your washing instead of a tumble dryer. Discover today how one of our new ceiling mounted clothes airers can save you money and take all the hard work and fuss out of drying your washing. // Customer Video Reviews &nbsp... | https://track.commissionfactory.com.au/p/10604/1718691 | http://www.lifestyleclotheslines.com.au/airaus-ceiling-mounted-clothes-airer/ | http://content.commissionfactory.com.au/Products/7228/1718691.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#50x50.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#100x100.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#120x120.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#200x200.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#300x300.jpg | http://content.commissionfactory.com.au/Products/7228/1718691#400x400.jpg | 99.0000 AUD | | AIRAUS |
+----+---------------------+---------------------+------+------------------------------------------------------+--------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------+---------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+-------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+---------------------------------------------------------------------------+--------------+-------------+-------------+
So with the data provided I'm looking to go through all the tables (concatenation of affiliate_programs.affiliate_network_short and affiliate_programs.affiliate_program_long columns in affiliate_programs and build a insert query for them all based on the mapped columns in the table affiliate_datafeed_magento_mapping to insert the data into the table magento_export
Any assistance would be awesome!
Tim
I dont think you need the mapping table at all
I would use a series of UNION queries. One query per source. In the SELECT clause for each of those queries you should have a 1:1 column for each coulmn in your destination table, including mapping the value NULL where that destination column is not applicable to that source. (This is where the mapping really happens)
Then your INSERT statement is a straight-forward mapping of all the UNIONED queries into the destination table.
Pseudo code
INSERT INTO Destination(Col1, Col2, Col3, Col4)
SELECT T1.Field1 as Col1
,NULL as Col2
,T1.Field7 as Col3
,T1.Field2 as Col4
FROM FirstSource T1
UNION
SELECT T2.Field1 as Col1
,Some Calulation as Col2
,T2.Field3 as Col3
,NULL as Col4
FROM FirstSource T1