SQL Server table identity specification and composite key - sql-server

hi I have two basic tables one is company and the next one is items reationship between these two tables are 1 to M (1 company has many items associated with it and one item belongs to one company only )
Company = {companyid,companyname}
_________
items = {itemid,itemname,companyid}
_______ ---------
I have set itemid identity specification to YES and now the item ID gets increased
if I have two companies id 1 & 2 A sample data table would show this
itemid itemname idcompany
----- ------- ---------
1 car 1
2 bus 2
3 bike 1
4 motorcycle 2
My issue is when showing company specific data I get this
company 1
itemid itemname idcompany
----- ------- ---------
1 car 1
3 bike 1
company 2
itemid itemname idcompany
----- ------- ---------
2 bus 2
4 motorcycle 2
how do I keep the item id sequential for each company ?
Thank you

Question
What does "sequential" even mean?
Suggestion
The Sequence possibly could change based on the business question. For instance, does sequence always mean in the order in which the row was inserted into the table, or does it mean the time at which the item was added? Regardless, you may want to implement the concept of a sequence independent of how the data is stored. For instance, based on your need, you could do something like this (which gives you the sequence of each item by company, based solely on the item_id itself):
select *,
itemsequence = row_number() over (
partition by (idcompany)
order by (itemid))
from items;
Hope this helps.

Related

Generate String Value Table Automatically in EF Core

I have a table called Customer that has several columns called National Code and Name. It also has a number of other features called Contact Numbers and Recommenders, since the number of Contact Numbers and Recommenders is more than one, so you need some other table to store them.
Also suppose I have other tables like the Customer, each of which has a number of attributes greater than one.
What is your suggestion for storing these values?
In one source, it was suggested that for each table, a table called StringValue be used for storage. Does EF core have a way to implement StringValue without writing additional code?
Example:
Customer Table:
CustomerId Name NationalCode
------------------------------------------------------------------------
1 David xxxx
------------------------------------------------------------------------
StringValue Table:
StringId CustomerId StringName Value
------------------------------------------------------------------------
10 1 PhoneNumber 915245
11 1 PhoneNumber 985452
12 1 PhoneNumber 935446
13 1 Recommenders Mr Jhon
14 1 Recommenders Mr bb
------------------------------------------------------------------------
I think it is more intutive create a new table for the field which has more than one records, then configure a one-to-many relationship between the two tables. Take your case as an example, you can divide the customer table into three tables, they can be linked by foreignkey:
1.Customer Table:
CustomerId Name NationalCode
---------------------------------------------
1 David xxxx
2.Contact Table:
Id CustomerId PhoneNumber
---------------------------------------------
1 1 915245
2 1 985452
3 1 935446
3.Recommender Table:
Id CustomerId RecommenderName
---------------------------------------------
1 1 Mr Jhon
2 1 Mr bb

Database schema for product quantity

I have some issues with creating database schema for a following scenario:
Shop, where you configure your order. Let's say user orders flowers and chocolate. So far I had the following structure:
OrderID FK_Flower FK_Chocolate
1 1 1
Where FK's pointed to the entry in database such as:
Id Name Price
1 Rose 100
The same for chocolate.
However now, there is a change: use can order multiple different flowers. So let's say, he can order 5 Roses, 3 Daisies.
What changes should I make, to solve this issue?
You want a many-to-many relationship.
Change the Orders table to something like this:
Orders:
-------
Id
1
The Flowers table stays the same:
Flowers:
------------------
Id Name Price
1 Rose 100
2 Daisy 120
Create a new table with with the Order and Flower ID's as foreign keys:
Orders_Flowers:
-------------------------
FK_Order_Id FK_Flower_ID
1 1
1 1
1 1
1 1
1 1
1 2
1 2
1 2
This way, the Order with Id = 1, has 5 Roses and 3 Daisies.

SSIS: Lookup data and merge again

Somehow I have a feeling that this should be an easy one - but I cannot see how to fix this in a SSIS package.
I have two tables: Order and CustomerMapping. My CustomerMapping table is not containing all my customers, but only customers that have changed Id and CustomerName.
The definitions of the tables are:
Order:
Id (int)
CustomerId (int)
CustomerName (nvarchar(50))
CustomerMapping:
Id (int)
ObsoleteId (int)
CustomerName (nvarchar(50))
ObsoleteCustomerName (nvarchar(50))
Data in Order table is:
Id CustomerId CustomerName
1 100 Customer 1
2 101 Customer 2
3 102 Customer 3
Data in CustomerMapping:
Id ObsoleteId CustomerName ObsoleteCustomerName
20 100 New Customer 1 Customer 1
21 101 New Customer 2 Customer 2
I want to use the tools provided by the SSIS package to create what I would do like this in SQL:
SELECT o.Id,
CustomerId = ISNULL(cm.Id, o.CustomerId),
CustomerName = ISNULL(cm.CustomerName, o.CustomerName)
FROM Order AS o
LEFT JOIN CustomerMapping AS cm ON o.CustomerId = cm.ObsoleteId
The result of above query is
Id CustomerId CustomerName
1 20 New Customer 1
2 21 New Customer 2
3 102 Customer 3
I now want to take this result-set and save into a new table.
I know that I can just take the above SQL and do what I want (which might be what I end up doing), but somehow I believe I can make a lookup, merge and/or merge join... But I must admit I cannot really see how I do this without splitting the lookup into two and then having to save from each new "thread".
The above is rather simplified... I have 4 columns which I have to compare and then do some other stuff with the whole lot, before saving it into a new table again, which is why I want to keep a single "thread".
Edit: image added:

One or four tables? (db structure)

Items are connected to:
one or more "region" and/or
one or more "county" and/or
one or more "city" and/or
one or more "place".
My question is how I should set up the relations:
id | thingID | regionId | countyId | cityId | placeId
or
4 tables?
id | thingId | regionId
id | thingId | countyId
id | thingId | cityId
id | thingId | placeId
or is there perhaps another better solution?
I may be overthinking this, but I think there's probably a relationship between "region", "county", "city" and "place" - an item that belongs to a "place" should also belong to the city, county and region.
You can solve this in both the designs you provide - but you need a fair amount of additional logic. In the first solution, you need to make sure that every time you insert a record, you populate the location from "left to right" - a record with only "place" is not valid.
In the second solution, you need to populate all relevant rows - an item in Chelsea must also have records for London, Middlesex and South East England.
There's another way...
Table: location
ID Name Parent
------------------------
1 South East England null
2 Middlesex 1
3 London 2
4 Chelsea 3
5 Kent 1
6 Canterbury 5
Table: item
Id name
-----------------
1 Posh Boy
2 Cricket ground
3 Rain
Table: item_location
ItemID LocationID
--------------------
1 4 //Posh boy in Chelsea
2 2 // Cricket ground in Middlesex
3 1 // Rain in the South East of England.
The second option is clearly better. It's a many-to-many relationship for each of these categories, and that's what the second option describes.
The first option would result in some very odd data. If you had a ThingId that was associated to all of the different types once, you would have one row that had all the columns filled in. Then if your ThingId needed to be tied to an additional city, you would have another row that had only the cityId filled in, with the other columns remaining null.
A table design that results in a lot of null values is usually (not always) a sign that your model is flawed.

Database design - tracking details per order from parent

I have a requirement where there are 3 tables (Orders, details & users)
OrderId OrderDesc UserId Timestamp
--------- ---------- --------- ---------------------
1 Pencils 1 02/08/2011 9:35 pm
2 Fruits 2 02/08/2011 8:44 am
Order Id DetailId DetailDesc UserId Timestamp
---------- ----------- ---------- ---------- -----------------
1 1 HB-1 2 04/09/2012 5:00 pm
1 2 HB-2 2 04/09/2012 5:00 pm
UserId UserName
-------- ---------
1 john
2 james
I want a user to enter details only for an order which has been entered into system,avoiding duplicates (I am thinking about converting an existing order to a detail in design view)
I want the user entering the orders 'john' to know about any details that may have been entered for a specific order he innitiated without introducing a field in my orders table or joining orders & details together
Thanks for any help, Damien.
To enter details for any orders that have been entered, you only allow the user to select orders that already exist either using drop downs or a checkbox
Your order details are already linked to the order by the Order Id field.

Resources