Database design - tracking details per order from parent - database

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.

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

SQL Server table identity specification and composite key

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.

How to select a data slice for a specific date from a SQL SERVER database tables which track changes on the row level

All tables in the database has a Date column named EffectiveDate.
Data is imported into the database using a logic which detects and inserts changed records only.
Let us assume 5 imports happened between 1/1/2014 and 5/1/2014
So Table A has:
EffectiveDate id1 column1 column2
-------------- ---- -------- --------
01/01/2014 1 ABC 123
02/01/2014 1 ABC 999
05/01/2014 1 XXX 999
01/01/2014 2 CCCC 555
03/01/2014 2 CCCC 444
04/01/2014 2 DDDD 444
01/01/2014 3 xxxxx 333
and Table B has
EffectiveDate id2 column1 column2
-------------- ----- -------- --------
01/01/2014 1 ZZZZ AAAAA
03/01/2014 1 ZZZZ AABBB
01/01/2014 2 TTTT AAAAA
05/01/2014 2 TTTT AABBB
Now The task is to create 3 set of views for all tables:
The first set is to give the Effective data as of current date
The second set is to give latest data
The third set is to give the data changes after today date (just next changes not the latest)
Consideration:
All views should return only one row for each id with applicable effective date.
If effective date is not available then the maximum effective date in the table less then the requested effective date should be used.
I was able to come up with solution for the Effective and Latest views but not for the third set of views (Next changes)
Any idea how to address this?
You'll need to use the Row_Number function to get this. For each id, the first future row (whatever that means...) will have a row_number of 1.
with RowNumbers as
(select
id1,
effectivedate,
row_number() over (partition by id1 order by effectivedate) as RowNumber
from
a
where
effectivedate > getdate()
)
select
a.*
from
A
inner join RowNumbers
on a.id1 = Rownumbers.id1
and rownumbers.rownumber = 1
and a.effectivedate = rownumbers.effectivedate
SQL Fidldle

What is a sensible approach for enabling/disabling features on a database object?

Say I have a User database table with the regular username, password, email fields. What is a sensible way to add additional boolean fields that enable/disable features for any given user.
e.g.,
user_can_view_page_x
user_can_send_emails
user_can_send_pms
etc
Adding a bunch of boolean columns to the existing user table seems like the wrong way to go.
Yes, I would think that this is the wrong approach.
I would rather create a
User_Features Table
with columns something like
UserID
FeatureName
And check if a given user has the feature in question enabled/entered in the table.
You could even go as far as creating a Users_Groups table, where users are also assosiated with groups and features can be inherited/disallowed from group settings.
I would use three tables.
One is your existing user table:
USER table
----
user_id (pk)
name
email
...
Another is a table containing possible user privileges:
PRIVILEGE table
----
privilege_id (pk)
name
Lastly is a join table containing an entry for each privilege setting for each user:
USER_PRIVILEGE table
----
user_id (pk) (fk)
privilege_id (pk) (fk)
allowed
Here is some sample data for two users, one with the send email privilege and the send pms privilege and another with a view page privilege:
USER data
USER_ID NAME EMAIL
------- ----- -------------------
1 USER1 user1#somewhere.com
2 USER2 user2#somewhere.com
PRIVILEGE data
PRIVILEGE_ID NAME
------------ -----------
1 view_page_x
2 send_email
3 send_pms
USER_PRIVILEGE data
USER_ID PRIVILEGE_ID ALLOWED
------- ------------ -------
1 1 'N'
1 2 'Y'
1 3 'Y'
2 1 'Y'
2 2 'N'
2 3 'N'

Entity relation

Is the following entity relation correct? I am trying to link employee, timesheet and approver table.
There are few other tables and fields but for this question my main concern is following three tables. One employee can have more than timesheet approver.
Employee
--------
EmpID pk
Name
TimeSheet
------------
TSHEET_ID PK
FK_EmpID FK
Approved_By
Timesheet_Approver (one employee can have more than one approver
but only one will be approving the timesheet)
------------------
EmpID
Approver_EmpID
Employee Table Data:
EmpID Name
----- -----
1 john
2 david
3 mark
Timesheet Approver Data:
EmpID Approver
----- --------
1 2
1 3
2 3
Timesheet
TSID EMPID APPROVED_BY
---- ----- -----------
101 1 2
102 1 3
103 2 3
OR this looks okay?
My Suggestion
Why do you want Approver as a seperate table? Can you keep a Bit Field like IsApprover in Employee table? Otherwise you are creating redundancy
I feel something wrong in the design. This is my idea.
Employee
EmpID pk
Name
TimeSheet
TSHEET_ID PK
FK_EmpID
Approved_By
Third table is unnecessary.
If there are several approves we can't add a column called Approved_by to the TimeSheet table.
Then we can add two more relations. One is for approvers list.
Approvers
ApproverID
Department
Name
TimeSheetApproves
TimeSheetID
Department1ApproverID
Department2ApproverID
..........................so on
Don't forgot to remove Approved_by to the TimeSheet realtion.
Employee
EmpID - Pk
Name
TimeSheet
TSHEET_ID - PK
EmpID - FK
ApproverID - FK
Approvers
ApproverID -PK
Department
OK this is my final answer. If it doesn't suit you can try your own one. I don't have time to read all those comments. And I was trying to help you.
Using the Approvers relation you can get approvers in a department and other things. Check sql queries to get those data.

Resources