Use many to many relationship or multiple columns? - database

I have a product, which can belong to one (or more) of 5 possible categories.
The number/name/structure of categories will not change
Should I be using a many to many relationship and insert 5 records in the category table, add a join table and add foreign keys etc? Or just add 5 fields to the product table? I feel the latter seems more efficient, but goes against the principles of normalization.
i.e.
Product:
- id
- name
- is_cat_a : bool
- is_cat_b : bool
- is_cat_c : bool
- is_cat_d : bool
- is_cat_e : bool
OR
Product
- id
- name
Category
- id
- name
ProductCategories
- product_id
- category_id

As you suggested:
A product, which can belong to one (or more) of 5 possible categories only.
Then, in my opinion, create two tables:
**1. Category**
Id int,
Name varchar,
IsActive bool and other regular columns
**2. Product**
- id int,
- name varchar,
- cat_a : int,
- cat_b : int,
- cat_c : int,
- cat_d : int,
- cat_e : int
Keep all category column's default as zero.

Related

SQL Server - selecting a specific ID from a table

I will explain what I have done so far and ask my question at the end.
First I created my external & internal variables (note that #EDelivery is a table type that included the columns FoodID and Quantity):
#EDelivery DeliveryTblType readonly
Declare #NumberOfMinutes smallint, #FoodWeight int, #WeightCapacity int,
#DroneID int, #TimeOfWeek varchar
I then read this in, giving me the total weight of all items ordered and read in the weight capacity from the DronesTbl:
select #FoodWeight = SUM(FoodWeight * Quantity)
from #EDelivery as del
inner join Foods on del.FoodID = Foods.FoodID
select #WeightCapacity = Drones.WeightCapacity
from dbo.Drones
My question is how do I select a specific drone to compare its weightcapacity to the food total weight
You need to set the variable #WeightCapacity according to the #droneid value. Right now, you are setting this variable to last drone in your drones table.
If you change your second query to what's below then it should work.
select #WeightCapacity =
Drones.WeightCapacity
from dbo.Drones
where DroneID =#droneID

merge two crossreference tables?

I have three tables with many-to-many relationships:
profile, role and type
Then, I have two cross-reference tables:
profile_role:
role_id : int
profile_id : int
profile_type:
profile_id : int
type_id : int
The common external id in both tables is profile_id.
My idea is that I create a table that contains the three external ids:
profile_role_type:
role_id : int
profile_id : int
type_id : int
My question : is the two designs are equivalent (no data loss or redundancy)?
profile_role_type would only be a valid replacement if there is exactly one row in profile_role with the same profile_id for every row in profile_type.
That's the long and the short of it.

How to get value from many2one-One2many in (new) product for a calculation in odoo 8?

I've made a custom field in product.template to select a product quality, based on a Many2one relationship with qualities.
qualities_id = field.Many2one(comodel_name="qualities", string="quality")
In the class: qualities there is a One2Many relationship with the class: quality.supplier.
reseller_ids = fields.One2many('supplierinfo', 'product_qual_id', 'Supplier')
Here we can select a quality type e.g. ( id=16 type=B321 ). This quality type (16) can have more then one supplier. So in supplierinfo we have the field product_qual_id (id = 16) that holds multi suppliers (id = 24, 25, 26)
product_qual_id = fields.Many2one('qualities', 'Quality Template')
Supplierinfo holds suppliers and each supplier holds more then one quantity and price. There is a relationship between supplierinfo and supplier prices using the field suppinfo_id.
pricelist_ids = fields.One2many('supplierprice', 'suppinfo_id', 'Supplier Price')
Because of this relationship, supplierprice: suppinfo, holds more then one quantity and corresponding price based on supplierinfo_id. Field that holds these records:
suppinfo_id = fields.Many2one('supplierinfo', 'Supplier information')
Qualities = ( id=16 type=B321 ). This quality type (16) can have more then one supplier. So in supplierinfo we have the field product_qual_id (id = 16) that holds multi suppliers (id = 24, 25, 26) where Record id: 24 in relation to supplierprice has multi qty and prices:
supplierprice id=30 supplierinfo id=24 qty=200, price=285
supplierprice id=31 supplierinfo id=24 qty=300, price=265
supplierprice id=32 supplierinfo id=24 qty=500, price=248
With additional automation, I need the price from supplierprice, based on (less or more) quantity in the main product. (product.template, price). Can anyone push me in the right direction. How can I get the price from supplierprice into a (new) field in my product.template?
I've added an image of the relationship model.
relationship

Help with Grocery List Database design

I want to create a Grocery List Table that will accept both the Category and the Specific Products.
For Example: I add to my grocery list:
Fruit (Category)
Fuji Apple (Product)
Shampoo (Category)
Dove Energize Shampoo (Product)
I have Product Table w/ Manufacturer(reference table), Category Table w/ SubCategoryId.
I want the user to enter either the Category or Product into the Grocery List, but have a way knowing that item entered is either a Category or Product. Please advise.
Any help is much appreciated.
Bogdan solution is a very good solution and if i have your problem i would implement his solution, but if you insist on one column in one table for your grocery list, i guess you have to use substring
like e.g
CREATE TABLE grocery_list (
[groceryItem] varchar(100)
);
Insert into grocery_list ([groceryItem]) values ('(c)Fruit')
Insert into grocery_list ([groceryItem]) values ('(p)Fuji Apple')
Insert into grocery_list ([groceryItem]) values ('(c)Shampoo')
Insert into grocery_list ([groceryItem]) values ('(c)Dove Energize Shampoo')
and to access your table substring the first 2 chars to check the type of your item
Select [item] = case when substring(groceryItem,2,1) = 'c' then
right(groceryItem,len(groceryItem) - 3) + ' (Category)'
when substring(groceryItem,2,1) = 'p' then
right(groceryItem,len(groceryItem) - 3) + ' (Product)'
end
from grocery_list
This will give you the below result
**item**
Fruit (Category)
Fuji Apple (Product)
Shampoo (Category)
Dove Energize Shampoo (Category)
I would create two separate columns, one for categories and one for products:
CREATE TABLE grocery_list (
'category_id' INTEGER NULL FOREIGN KEY categories 'id',
'product_id' INTEGER NULL FOREIGN KEY products 'id',
...
);
Entries will only put a value in one of the two columns, leaving the other as NULL. Then you can query your grocery list like this:
SELECT * FROM grocery_list, categories, products
WHERE categories.id=grocery_list.category_id
AND products.id=grocery_list.product_id;

How to handle multiple wildcards with SQL Server

I have some filters in the following form:
Object A
+/- Start End
----------------------------------------------
+ 000000080000 000000090000
- 000000800500
+ 054*
Object B
+/- Start End
----------------------------------------------
+ 000000090000 000000100000
+ 00??00900500
- 000000900500
+ 055*
It means:
Numbers between 000000080000 and 000000090000 except 000000800500, and numbers starting with 054 are associated with object A.
Numbers between 000000090000 and 000000100000 except 000000900500, numbers matching 00??00900500 (except 000000900500 of course), and numbers starting with 055 are associated with object B.
Example of the table structure:
CREATE TABLE dbo.Filter
(
IDFilter int IDENTITY PRIMARY KEY
)
CREATE TABLE dbo.FilterRow
(
IDFilterRow int IDENTITY PRIMARY KEY
,IDFilter int FOREIGN KEY REFERENCES dbo.Filter(IDFilter) NOT NULL
,Operator bit --0 = -, 1 = + NOT NULL
,StartNumber varchar(50) NOT NULL
,EndNumber varchar(50)
)
CREATE TABLE dbo.[Object]
(
IDObject int IDENTITY PRIMARY KEY
,Name varchar(10) NOT NULL
,IDFilter int FOREIGN KEY REFERENCES dbo.Filter(IDFilter) NOT NULL
)
I need a way to make sure no numbers can get associated with more than 1 object, in SQL (or CLR), and I really have no clue how to do such a thing (besides bruteforce).
I do have a CLR function Utils.fIsInFilter('?8*', '181235467895') that supports wildcards and would return 1, if it helps...
Can you use a CLR function in SQL 2005?
It's possible in raw SQL using LIKE JOINS (where ? becomes [0-9] and * becomes %), perhaps followed by CAST, but this is what CLR functions are for...

Resources