For example, I want to store the shopping list for each day.
If I write a db table like:
Date Item1 Item2 ... Item100
Obviously, this will leave many slots empty and we have a limited number. How can I store this?
Thanks.
Something like this:
Table orders:
order_id | customer_id | created_at | other_info_you_need | ...
1 1 yesterday whatever
2 1 today whatever
Table items:
maybe_an_auto_increment_column | order_id | item | how_many
1 1 thing_a 1
2 1 thing_b 10
3 2 thing_c 2
For a detailed answer on why there's not enough room here. Read up about Normalization and database design in general.
Related
I a working on a database that is going to have a product with multiple expiration date, multiple cost prices and therefore there will multiple stock entries for the same product, I have made an initial database design for this and I wanted to ask you guys if this is a good practice or not. If not please advise me on how to do it the right way.
This is what I have thought about so far.
Creating 3 tables (1. Product_info - 2.Product_Stock - 3.units)
and below is the detailed structure:
Units Table
--------------------------
id Name
|------|
1 |Piece |
2 |Pack |
3 |Kilos |
Here I will list all the units that I will use as the base product unit.
Product Information Table
-----------------------------------------------------------------------------------------------------------------------
id Name AvgCostPrice AvgPrice AvgPackCostPrice AvgPackPrice totalQuantity BaseUnitID multiplier PackBarcode Barcode
|------|------------|--------|----------------|------------|-------------|----------|----------|------------|--------|
1 |Soda | | | | | 108 | 1 | 12 | 111111 | 111222 |
2 |Water | | | | | 50 | 1 | 6 | 222222 | 222111 |
in the above table the average cost price and selling price for the packs and piece will be calculated from the different stocks I have for the said product.
The multiplier column will be for how much pieces does a product pack hold.
The Total Quantity will hold the sum of different stock quantities I have in the (Product Stock Table) ,Also it will only sum the quantity for base unit of the product.
for example: if the base unit of soda is pack, then it will sum the (PackQTY) Column in (Product Stock Table). and if else it will sum (Quantity) in that table.
Product Stock Table
---------------------------------------------------------------------------------------------------------------------------
id ProdID UnitID CustomBarcode Quantity PackQTY CostPrice Price PackCostPrice PackPrice expDate Enabled
|------|-------|--------------|-------------|-------|----------|-------|--------------|---------|--------------|---------
1 |1 | 1 | | 84 | 7 | 2.0 | 2.4 | 24.0 | 29 | 20/may/2019 | 1
2 |1 | 1 | | 24 | 2 | 1.5 | 1.9 | 18.0 | 23 | 10/aug/2019 | 1
2 |2 | 3 | | 50 | 0 | 3.0 | 5.0 | 0.0 | 0 | 10/Feb/2019 | 1
1.The enabled column will work as a (Boolean) to determine whether to use this stock while selling.
for example: if I wanted to sell a soda Can and I have two Stocks for it. if stock number one is 0 then enable column will be false and therefore it will only subtract the quantity sold from stock number two and use its price and cost price in the (SalesDetails Table)
Custom Barcode Column will be used to separate stocks when having a discount on almost expired stock.
And I also thought of separating the different units for each product stock in (Stock Table)
So, when I want to sell 24 pieces of soda and 3 packs of soda it will choose the oldest stock depending on its (Enabled Column Value = True)and subtract that quantity from it and if it reaches zero then (Enabled column) Value will change to false.
after that it will go again and do the same but this time it will change the value of PackQtY from 7 to 4 and the Quantity Column Value will be calculated through this [ Product_Stock.Quantity= Product_Stock.Quantity - (QtySold * Prodcut_info.Multiplier Column Value) ] which will be 84-(3*12)= 48
And the sales details structure output will be like this:
Sale Details Table
----------------------------------------------------------
id ProdID UnitID Quantity CostPrice Price total CostTotal
|------|-------|-----------|-------------|-------|------|---------|
1 |1 | 1 | 24 | 2.0 | 2.4 | 57.6 | 48.0 |
2 |1 | 2 | 3 | 18.0 | 23.0 | 69.0 | 54.0 |
Product Stock Table (After Selling 24 pieces of Soda and 3 packs of Soda)
---------------------------------------------------------------------------------------------------------------------------
id ProdID UnitID CustomBarcode Quantity PackQTY CostPrice Price PackCostPrice PackPrice expDate Enabled
|------|-------|--------------|-------------|-------|----------|-------|--------------|---------|--------------|---------
1 |1 | 1 | | 48 | 4 | 2.0 | 2.4 | 24.0 | 29 | 20/may/2019 | 1
2 |1 | 1 | | 0 | 0 | 1.5 | 1.9 | 18.0 | 23 | 10/aug/2019 | 0
Sorry if I didn't explain it very well.
Thank you very much in advance.
Firstly, you need to be careful about how you use nouns.
For example: "Price" does not mean the same as "Cost" and "CostPrice" sounds like an oxymoron. I suggest that you restrict your yourself to using either Cost or Price.
Van Ng asks if you have done an Entity Relationship diagram. Well, at the stage that you seem to be at, it is probably unwise to start with an ER diagram because an ER diagram is helpful as a summary of a model that you have already defined - and you are not yet at that stage.
Averages: If you design your database schema correctly then you can calculate data such as averages. You don't need averages as base tables.
I recommend that you consider using the fact-based modeling method called "object-role modeling"(ORM) because you can start with "the facts" before thinking about drawing ER diagrams.
Example:
I used the NORMA ORM tool to create the following example:
First, I read your text, extracted facts and then used the facts to design an object-role model.
Then I used the NORMA tool to generate a "logical view" of the object-role model. (happens in milliseconds)
I did not add everything that you mention but I hope that this will be enough to help you to make progress.
The example contains two artefacts:
1: The logical model that was generated by the NORMA tool.
2: The facts from which the logical model was generated.
[
I was wondering how to make a table in python 3 that has 7 columns for each day of the week and a variable defined amount of rows. I need to input employee hours for each separate person and be able to add them up later. This problem is really throwing me for a loop.
For example:
employeeAmount = int(input("Enter the amount of employees here"))
Then the table goes down "employeeAmount" of times.
I need something like this
Employee 1 | 7 | 9 | 8 | 8| 0 | 9 | 7 |
Employee 2 etc
Employee 3 etc
I hope you help me out a little with my sql problem. I am using SQL Server 2008. So what I basically have are two tables
Table (MAIN TABLE) that consists of the following information:
id|QUANTITY|AMOUNT|DATE
example:
iD|mainQuantity|mainAMOUNT|DATE | subQUANTITY | subAMOUNT |
-----------------------
1 | 200 | 1200 |02.02.2016| ? | ?
2 | 500 | 700 |20.03.2016| ? | ?
2. Table (SUB TABLE) that consists of the following information:
ID|subQUANTITY|subAMOUNT|DATE
-----------------------
1 | 280 | 1600 |07.02.2016
2 | 140 | 110 |22.02.2016
So my problem is.
I want to implement the following logic into my MS SQL Server.
Check if the DATE of the FIRST row in the SUB TABLE is identically with the date of the first row of the main Table or between 02.02.2016 and 20.03.2016 (Information of the Main Table).
If it is correct, then insert INTO MAINTABLE the VALUES of first row in the Column QUANTITY and AMOUNT in the Maintable into the subquantity and subamount UNTIL it reaches the maximum value of mainQuantity and mainAMOUNT.
The residual amount and the remaining quantity should be insertet into the next row into the column subquantity and subamount.
Do the same thing for every next row and insert the remaining quantity and amount in the next row of the main table.
example:
first ROW: ID1 -> DATE 07.02.2016 -> check if it is equal or between 02.02.2016 and 20.03.2016. -> YES IT is. -> INSERT INTO MAINTABLE:
result:
ID|mainQUANTITY|mainAMOUNT|DATE | subQUANTITY | subAMOUNT |
-----------------------
1 | 200 | 1200 |02.02.2016| 200 | 1200
2 | 500 | 700 |20.03.2016| 220 | 510
EDIT: I forgot to mention it: I dont have 1 column for the Date. I have two columns. A startingDate and an EndDate. The Date condition logic is still the same. So the logic should check if a date is exaclte equal to starting date or between startingDate and EndDate
I tried to do my best to explain it as easy as possible.I hope you understand the logic :)
Iam not that good in T-SQL that I program it by myself. So that is why I really badly hope that you can please help me.
Thank you very much for your time and your effort.
Hallo StackOverflow Users
I am struggling with transferring values between Access database tables which I will use in a Delphi program to tally election votes and determine the winning candidates. I have a total of six tables. One is my overall table, tblCandidates which identifies each candidate and contains the amount of votes they received from each party, namely the Grade Heads, the Teachers and the Learners. When it comes to the Learners we have four participating grades, namely the grade 8’s, 9’s, 10’s and 11’s, and each grade again has multiple participating classes, namely class A, B, C, etc.
Now, I have set up tables for each grade that contains all the classes in that grade. I named these tables tblGrX with X being the grade represented by 8 through 11. Each one of these tables has two extra fields, namely a field to identify a candidate and a field that will add up all the votes that candidate received from each of the classes in that grade. Lastly I have another table, tblGrTotals with fields Total_GrX (once again with X being the grade), that will contain all the total votes a candidate received from each grade, adding them up in another field for my tblCandidates table to use in its Total_Learners field.
So in short, I want, for example, tblGrTotals to use the value in the field Total of tblGr8 in its Total_Gr8 field, and then tblCandidates to use the value in field Total of tblGrTotals in its Total_Learners field. Is there any way to keep these values updated between tables like cells are updated in Excel the moment a change is made?
Thank you in advance!
You need to rethink your table design. I guess your background is Excel, and your tables are laid out like you would do in Excel sheets, but a relational database works differently.
Think about the objects you are modelling.
Candidates - that's easy. ID, Name, perhaps additional info that belongs to each candidate. But nothing about votes here.
"Groups that are voting" or Parties. Not so trivial, due to the different types of parties. Still I would put them in one table, with Grade and Class only set for Learners, NULL for Heads and Teachers.
e.g.
+----------+------------+-------+-------+
| Party_ID | Party_Type | Grade | Class |
+----------+------------+-------+-------+
| 1 | Head | | |
| 2 | Teacher | | |
| 3 | Learner | 8 | A |
| 4 | Learner | 8 | B |
| 5 | Learner | 8 | C |
| 6 | Learner | 9 | A |
| 7 | Learner | 9 | B |
| 8 | Learner | 10 | A |
+----------+------------+-------+-------+
Votes: they are a Junction Table between Candidates and Parties.
e.g.
+----------+--------------+-----------+
| Party_ID | Candidate_ID | Num_Votes |
+----------+--------------+-----------+
| 1 | 1 | 5 |
| 1 | 2 | 17 |
| 3 | 1 | 2 |
| 3 | 2 | 6 |
| 3 | 3 | 10 |
+----------+--------------+-----------+
Now: if you want to know the votes of Class 8A:
SELECT Candidate_ID, SUM(Num_Votes)
FROM Parties p INNER JOIN Votes v
ON p.Party_ID = v.Party_ID
WHERE p.Party_Type = 'Learner'
AND p.Grade = 8
AND p.Class = 'A'
GROUP BY Candidate_ID
Or of all Grade 8? Simply omit the p.Class criteria.
For the votes per candidate you join Candidates with Votes.
Edit:
for the votes counting differently, this is an attribute of Party_Type.
We don't have a table for them yet, so create one:
+------------+---------------+
| Party_Type | Multiplicator |
+------------+---------------+
| Head | 4 |
| Teacher | 3 |
| Learner | 1 |
+------------+---------------+
and to count all votes:
SELECT c.Candidate_ID, c.Candidate_Name, SUM(v.Num_Votes * t.Multiplicator) AS SumVotes
FROM Parties p
INNER JOIN Votes v ON p.Party_ID = v.Party_ID
INNER JOIN Party_Types t ON p.Party_Type = t.Party_Type
INNER JOIN Candidates c ON v.Candidate_ID = c.Candidate_ID
GROUP BY c.Candidate_ID, c.Candidate_Name
With a design like this, you don't need to keep updating data from one table into another - you calculate it when and how you need it, and it's always current.
The magic of databases. :)
This is frying my brain, I really need help!
Here is the thing i want to achieve.
I have a Table name Product.
The product may or may not have up to two Optional field. Example Color and Size.
If the product does not have the optional field, it will have only one row of Price and Quantity, else for each row of optional field, there will be one price and Quantity.
I know this sound Confusing, pardon me. I'm confused too. ):
But i can give you guys fews example below.
So the one million dollar question is, what are the tables and it's field i should create?
[ Product Without Optional Field ]
Price | Quantity
$1.00 | 2
[ Product With One Optional Field ]
Price | Quantity | Size
$1.00 | 2 | Large
$2.00 | 1 | Small
[ Product With Two Optional Field ]
Price | Quantity | Size | Color
$1.00 | 2 | Large | Green
$2.00 | 1 | Small | Blue
I come up with an idea of having Two entity named Product and Optional to have many to many relationship with the Optional Entity to store the field name, example Size and the junction-entity name Product_Optional will store the value, example Large.
However I'm still stuck with the issue of how to bind the Two Optional Field of one product to the same price and quantity! SORRY to be confusing :(
Edit:
Since your options are unknown, you could do something like this
Products
id
product_name
product_description
...
ProductOptions
id
option (size, color, whatever)
value (large, blue, anything)
ProductInventory
id
product_id
product_option_id
quantity
price
Then your records in ProductInventory would look like:
1 | 1 | 1 | 5 | 2.00
1 | 1 | 2 | 3 | 3.00
etc etc
More detailed example, using the table structure above:
Products
1 | Product 1 | Prod 1 Description
2 | Product 2 | Prod 2 Description
3 | Product 3 | Prod 3 Description
ProductOptions
1 | Size | Small
2 | Size | Medium
3 | Size | Large
4 | Color | Blue
5 | Color | Red
6 | Color | Green
7 | Width | 10 Inches
8 | ... (as many as you want)
ProductInventory
1 | 1 | 1 | 5 | 2.00
(says for product 1, size small, there are 5 quantity, and cost is 2.00
2 | 1 | 2 | 17 | 3.00
(says for product 1, size medium, there are 17 quantity, and cost is 3.00
etc
I have run into the same problem, and I think you need more than one table as well. try this:
products
id
product_name
product_price
.....
Product_options
id
product_option_name
product_options_values
link your product_options_values to your product_options using product_options_id as foriegn key. example: product_option 'size' has the product_option_values of 'small', 'medium', 'large'*
id
product_options_id (fk)
options_value
options_value_price
product_options_to_products
this table links your products to your options. here you'd assign different options to different products.
id
product_id
product_options_id