Entity relation - sql-server

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.

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

How can I build a query to view the same value in 20 or more tables in SQL?

Scenarios:
Table 1
Name ID dept
John 112 Fin
Mary 113 Act
Table 2
Name email
John John#gmail.com
Mary Max#gmail.com
Table 3
Name Supervisor
John Kelly
Mary Adam
Table 4
Namevalue Salary
John 1000
Mary 1000
Expected Output:
I would like to know the occurrence of John and Mary in all these tables. There are more than 20 tables in difference database.
Firstly, specify the master table according to data quality, use this table as base and then use left joins to concat with other tables.
But, I think, there is a design problem in the database so maybe creating keys for each user can be useful.
For example, name + surname + User Something to make records unique then use joins otherwise all records will duplicate.

T-SQL Select, manipulate, and re-insert via stored procedure

The short version is I'm trying to map from a flat table to a new set of tables with a stored procedure.
The long version: I want to SELECT records from an existing table, and then for each record INSERT into a new set of tables (most columns will go into one table, but some will go to others and be related back to this new table).
I'm a little new to stored procedures and T-SQL. I haven't been able to find anything particularly clear on this subject.
It would appear I want to something along the lines of
INSERT INTO [dbo].[MyNewTable] (col1, col2, col3)
SELECT
OldCol1, OldCol2, OldCol3
FROM
[dbo].[MyOldTable]
But I'm uncertain how to get that to save related records since I'm splitting it into multiple tables. I'll also need to manipulate some of the data from the old columns before it will fit into the new columns.
Thanks
Example data
MyOldTable
Id | Year | Make | Model | Customer Name
572 | 2001 | Ford | Focus | Bobby Smith
782 | 2015 | Ford | Mustang | Bobby Smith
Into (with no worries about duplicate customers or retaining old Ids):
MyNewCarTable
Id | Year | Make | Model
1 | 2001 | Ford | Focus
2 | 2015 | Ford | Mustang
MyNewCustomerTable
Id | FirstName | LastName | CarId
1 | Bobby | Smith | 1
2 | Bobby | Smith | 2
I would say you have your OldTable Id to preserve in new table till you process data.
I assume you create an Identity column Id on your MyNewCarTable
INSERT INTO MyNewCarTable (OldId, Year, Make, Model)
SELECT Id, Year, Make, Model FROM MyOldTable
Then, join the new table and above table to insert into your second table. I assume your MyNewCustomerTable also has Id column with Identity enabled.
INSERT INTO MyNewCustomerTable (CustomerName, CarId)
SELECT CustomerName, new.Id
FROM MyOldTable old
JOIN MyNewCarTable new ON old.Id = new.OldId
Note: I have not applied Split of Customer Name to First Name and
Last Name as I was unsure about existing data.
If you don't want your OldId in MyNewCarTable, you can DELETE it
ALTER TABLE MyNewCarTable DROP COLUMN OldId
You are missing a step in your normalization. You do not need to duplicate your customer information per vehicle. You need three tables for 4th Normal form. This will reduce storage size and more importantly allow an update to the customer data to take place in one location.
Customer
CustomerID
FirstName
LastName
Car
CarID
Make
Model
Year
CustomerCar
CustomerCarID
CarID
CustomerID
DatePurchaed
This way you can have multiple owners per car, multiple cars per owner and only one record needs to be updated per car and or customer...4th Normal Form.
If I am reading this correctly, you want to take each row from table 1, and create a new record into table A using some of that row data, and then data from the same original row into Table B, Table C but referencing back to Table A again?
If that's the case, you will create TableA with an Identity and make thats the PK.
Insert the required column data into that table and use the #IDENTITY to retrieve the last identity value, then you will insert the remaining data from the original table into the other tables, TableB, TableC, etc. and use the identity you retrieved from TableA as the FK in the other tables.
By Example:
Table 1 has columns col1, col2, col3, col4, col5
Table A has TabAID, col1, col2
Table B has TabBID, TabAID, col3
TableC has TabCID, TabAID, col4
When the first row is read, the values for col1 & col2 are inserted into TableA.
The Identity is captured from that row inserted, and then value for col3 AND the identity are entered into TableB, and then value for col4 AND the identity are entered into TableC.
This is a standard data migration technique for normalizing data.
Hope this assists,

Database: Design customer orders

I am currently creating an Android application which should use SQLite database. In my case, I need to strore "product type", "bill" and "list of all purchased products"
Bill:
id
customer
data -> link to list of all purchased products
price
Product types:
id
name
price
My question is: One customer could have more products connected to one bill. How to design it? Is using a Use a one row for every bills:
id
bill_id
product
or rather insert more products into one row and then parse it in my application? I suppose, there is a better solution that these mine. Thank you for help.
You said in your question, "One customer could have more products connected to one bill."
Another way of saying this is, one customer can have 0 or more bills, and one bill can have 1 or more products.
After rephrasing your statement, the database normalization becomes more obvious.
Customer
--------
Customer ID
Customer Name
...
Bill
----
Bill ID
Customer ID
Bill Time Stamp
Bill-Product
------------
Bill ID
Product ID
Product
-------
Product ID
Product Name
Product Price
...
To design customer order with bill we can design it as below:
This schema having basics attributes we can add more as per requirements.
Customers Table
id PK
full_name
address
phone
MenuItems Table
id PK
item_name
item_description
item_price
Order Table
id PK
customer_id FK
order_date
OrderDetails Table
id PK
order_id FK
menu_item_id fk
quantity
Bill Table
id PK
order_id FK
amount
bill_date

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