How to dynamically set up TreeView - wpf

Using WPF I would like to have a TreeView that displays data from a Compact Database variably. That means I would like to change the view based on how the user would like it sorted.
Assuming that the database has the following tables and schema, how can I set up my views? As I said, they should be variable, but I would consider multiple HierarchialDataTemplates if that is possible. A few potential views that come to mind are listed below.
Thanks for any help offered!
Employee
ID - Int primary Unique AutoIncrement
Name – string
Department - int (foreign relation to Department. ID)
Supervisor - int (ID of Employee in this table)
Division
ID - Int primary Unique AutoIncrement
Name – Unique string
DivisionLeader - int (foreign relation to Employee. ID)
Department
ID - Int primary Unique AutoIncrement
Name – Unique string
DepartmentLeader - int (foreign relation to Employee. ID)
Division – int (foreign relation to Division. ID)
CustomerSupportDepartments
ID - Int primary Unique AutoIncrement
Name – string
Department - int (foreign relation to Department. ID)
Continent - int (foreign relation to Continent. ID)
Country - int (foreign relation to Country. ID)
State - int (foreign relation to State. ID)
City - int (foreign relation to City. ID)
Customer
ID - Int primary Unique AutoIncrement
Name – string
SalesRep - (foreign relation to Employee. ID)
Revenue . int
Continent - int (foreign relation to Continent. ID)
Country - int (foreign relation to Country. ID)
State - int (foreign relation to State. ID)
City - int (foreign relation to City. ID)
Continent
ID - Int primary Unique AutoIncrement
Name – Unique string
Country
ID - Int primary Unique AutoIncrement
Name – Unique string
State
ID - Int primary Unique AutoIncrement
Name – Unique string
City
ID - Int primary Unique AutoIncrement
Name – Unique string
Potential Views
View 1
Employees
Employee Name 1
Supervisor
Name
Department
Name
Customers
Name 1
Name 2
Name 3
Employee Name 2
Supervisor
Name
Department
Name
Customers
Name 1
Name 2
Name 3
View 2
Departments
Department Name 1
Division
Name
Supervisor
Name
Employees
Employee Name 1
Employee Name 2
Department Name 2
Division
Name
Supervisor
Name
Employees
Employee Name 1
Employee Name 2
View 3
Divisions
Division Name 1
Departments
Department Name 1
Department Name 2
Division Name 2
Departments
Department Name 1
Department Name 2
View 4
Continents
Continent Name 1
Country Name 1
City Name 1
Customers
Customer Name 1
Sales Representative
Employee Name
Revenue
Sum
Customer Name 2
Sales Representative
Employee Name
Revenue
Sum
Country Name 2
City Name 1
Customers
Customer Name 1
Sales Representative
Employee Name 1
Revenue
Sum
City Name 2
Customers
Customer Name 1
Sales Representative
Employee Name 1
Revenue
Sum
Customer Name 2
Sales Representative
Employee Name
Revenue
Sum
View 5
Customers
Customer Name 1
Sales Representative
Employee Name
Sales Representative’s Supervisor
Employee Name
Location
Continent
Country
State
City
Revenue
Sum
Customer Name 2
Sales Representative
Employee Name
Sales Representative’s Supervisor
Employee Name
Location
Continent
Country
State
City
Revenue
Sum

I wound up using a generic object.

Related

How to auto count Current Employees for department in CREATE TABLE statement?

The employee has a default department that company workers will associate with, also the system will track which department the worker is currently working in.
How to count CurrentEmployeesCount?
CREATE TABLE Departments
(
DepartmentID INT PRIMARY KEY,
DepartmentName NVARCHAR(100),
CurrentEmployeesCount INT --???
);
CREATE TABLE Employees
(
EmployeeID INT PRIMARY KEY,
DepartmentID INT DEFAULT '???',
FOREIGN KEY(DepartmentID)
REFERENCES Departments (DepartmentID)
);

Parse a simple JSON string

I have the following JSON String stored in table
create table #t1 (Caseno int, CaseText text)
insert into #t1 values
(1,'[{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PCNForEnrollmentCreationDesc","DisplayName":"Header","FieldSchemaName":null,"FieldValue":"","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PrimaryEntityInformation","DisplayName":"Primary Entity Information","FieldSchemaName":null,"FieldValue":"","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityName","DisplayName":"Primary Entity Name","FieldSchemaName":null,"FieldValue":"Mau \"Informatsionnye tekhnologii\"","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityAddress","DisplayName":"Primary Entity Address","FieldSchemaName":null,"FieldValue":"123 Main St, ","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityCity","DisplayName":"Primary Entity City","FieldSchemaName":null,"FieldValue":"Kaliningrad","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityPhone","DisplayName":"Primary Entity Phone","FieldSchemaName":null,"FieldValue":"772 322-977","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityPostalCode","DisplayName":"Primary Entity Postal Code","FieldSchemaName":null,"FieldValue":"236029","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityCountry","DisplayName":"Primary Entity Country","FieldSchemaName":null,"FieldValue":"Russia","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PrimaryContactInformation","DisplayName":"Primary Contact Information","FieldSchemaName":null,"FieldValue":"","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollContactTypeIndividual","DisplayName":"Contact Type","FieldSchemaName":null,"FieldValue":"Individual","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContactFirstName","DisplayName":"Given Name: ","FieldSchemaName":null,"FieldValue":"Elena","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContactLastName","DisplayName":"Family Name: ","FieldSchemaName":null,"FieldValue":"Andre","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityCountry","DisplayName":"Primary Entity Country","FieldSchemaName":null,"FieldValue":"Russia","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityAddress","DisplayName":"Primary Entity Address","FieldSchemaName":null,"FieldValue":"123 Main St, ","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityCity","DisplayName":"Primary Entity City","FieldSchemaName":null,"FieldValue":"Kaliningrad","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityPhone","DisplayName":"Primary Entity Phone","FieldSchemaName":null,"FieldValue":"772 322-977","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContEmailAddress","DisplayName":"Email Address","FieldSchemaName":null,"FieldValue":"info#us.me","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContDataLanguage","DisplayName":"Data Language","FieldSchemaName":null,"FieldValue":"Russkij","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PCNforEnrollPrimaryEntity","DisplayName":"PCN for Enrollment Primary Entity","FieldSchemaName":null,"FieldValue":"123455","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":"FreeText","IsRequired":"true"},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"NoticesEntityInformation"')
I want to extract the value from the string and present this way:
Caseno FieldName FieldValue
1 Header
1 Primary Entity Information
1 Primary Entity Name Mau \"Informatsionnye tekhnologii
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Primary Entity Postal Code 236029
1 Primary Entity Country Russia
1 Primary Contact Information
1 Contact Type Individual
1 Given Name: Elena
1 Family Name: Andre
1 Primary Entity Country Russia
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Email Address info#us.me
1 Data Language Russkij
1 PCN for Enrollment Primary Entity 123455
Basically I want to extract DisplayName and FieldValues.
Example String: "DisplayName":"Primary Entity Name","FieldSchemaName":null,"FieldValue":"Mau \"Informatsionnye tekhnologii\""
If you use SQL Server 2016+, just parse the JSON with OPENJSON() using explicit schema (WITH clause). The data in the question is truncated (and the JSON is not valid), but the following example is a working solution:
Table and JSON (fixed and simplified):
create table #t1 (Caseno int, CaseText text)
insert into #t1 values
(1,'[
{
"RowType":"KV",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"PCNForEnrollmentCreationDesc",
"DisplayName":"Header",
"FieldSchemaName":null,
"FieldValue":"",
"IsDynamic":true,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
},
{
"RowType":"H2",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"PrimaryEntityInformation",
"DisplayName":"Primary Entity Information",
"FieldSchemaName":null,
"FieldValue":"",
"IsDynamic":false,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
},
{
"RowType":"KV",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"EnrollPrimaryEntityName",
"DisplayName":"Primary Entity Name",
"FieldSchemaName":null,
"FieldValue":"Mau \"Informatsionnye tekhnologii\"",
"IsDynamic":true,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
}
]')
Statement:
SELECT t.CaseNo, j.*
FROM #t1 t
CROSS APPLY OPENJSON(t.CaseText) WITH (
DisplaydName varchar(100) '$.DisplayName',
FieldValue varchar(100) '$.FieldValue'
) j
Result:
CaseNo DisplaydName FieldValue
1 Header
1 Primary Entity Information
1 Primary Entity Name Mau "Informatsionnye tekhnologii"
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Primary Entity Postal Code 236029
1 Primary Entity Country Russia
1 Primary Contact Information
1 Contact Type Individual
1 Given Name: Elena
1 Family Name: Andre
1 Primary Entity Country Russia
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Email Address info#us.me
1 Data Language Russkij
1 PCN for Enrollment Primary Entity 123455

How do we give constraint on foreign key column based on ERD cardinality?

Let's say I have a 2 tables like below :
Department:
DeptId Name
Employee:
EmpId Name DeptId
Here I have a 0-to-many relationship between Department and Employee table.
Now when I read the relationship from Department to Employee, it says 1 department can have 0 or more employees.
Does this mean while creating Employee table, I will set DepartmentId (foreign key) as null due to zero or many relationship?
And how do we represent 1 or many relationship like for eg If relationship in ERD says 1 department can have 0 or more employees then DepartmentId (foreign key) will be non nullable in Employee table?
Basically I am just trying to understand how do we give constraint on foreign key based on ERD cardinality (1 to many relation,1 to zero or more relationship).
Can anyone please help me clear this confusion? I will really really appreciate that :)
I will set DepartmentId(Foreign key) as null due to zero or many
relationship?
No, the Foreign key should be not null. Imagine you have no employee in table Employee with DeptId = 1. So it represents the Department has zero relationships.
Otherwise, the Employee table contains multiple items with DeptId = 2, So it represents the 1-n relationships between Department and Employee table.
ERD tools are great, but the most powerful design tool is still natural language, predicates, and constraints. So, when in doubt, use a plain text editor instead of an ERD tool. Also do not allow NULLs in the design phase.
All attributes (columns) NOT NULL
[p x] = predicate x
(c x.y) = constraint x.y
PK = Primary Key
AK = Alternate Key (Unique)
FK = Foreign Key
Starting with the department, this part does not change.
[p 1] Department identified by number DEPT_ID, named DEPT_NAME exists.
(c 1.1) Department is identified by department number.
(c 1.2) For each department name, exactly one department has that name; for each department, that department has exactly one name.
department {DEPT_ID, DEPT_NAME} -- p 1
PK {DEPT_ID} -- c 1.1
AK {DEPT_NAME} -- c 1.2
Case 1: Emp * ---- 1 Dept
[p 2] Employee identified by number EMP_ID, named EMP_NAME, works for department DEPT_ID.
(c 2.1) Employee is identified by employee number.
(c 2.2) Each employee has exactly one name; for each employee name, more than one employee can have that name.
(c 2.3) Each employee works for exactly one department; for each department more than one employee may work for that department.
(c 2.4) If an employee works for a department, then that department must exist.
employee {EMP_ID, EMP_NAME, DEPT_ID} -- p 2
PK {EMP_ID} -- c 2.1, c 2.2, c 2.3
FK {DEPT_ID} REFERENCES department {DEPT_ID} -- c 2.4
Case 2: Emp * ---- 0..1 Dept
[p 2] Employee identified by number EMP_ID, named EMP_NAME, exists.
(c 2.1) Employee is identified by employee number.
(c 2.2) Each employee has exactly one name; for each employee name, more than one employee can have that name.
[p 3] Employee identified by number EMP_ID works for department identified by number DEPT_ID.
(c 3.1) Each employee works for at most one department; for each department more than one employee may work in that department.
(c 3.2) If an employee works for a department, then that employee must exist.
(c 3.3) If an employee works for a department, then that department must exist.
employee {EMP_ID, EMP_NAME} -- p 2
PK {EMP_ID} -- c 2.1, c 2.2
emp_dept {EMP_ID, DEPT_ID} -- p 3
PK {EMP_ID} -- c 3.1
FK1 {EMP_ID} REFERENCES employee {EMP_ID} -- c 3.2
FK2 {DEPT_ID} REFERENCES department {DEPT_ID} -- c 3.3
Case 3: Emp * ---- * Dept
[p 2] Employee identified by number EMP_ID, named EMP_NAME, exists.
(c 2.1) Employee is identified by employee number.
(c 2.2) Each employee has exactly one name; for each employee name, more than one employee can have that name.
[p 3] Employee identified by number EMP_ID works for department identified by number DEPT_ID.
(c 3.1) Each employee may work for more than one department; for each department more than one employee may work in that department.
(c 3.2) If an employee works for a department, then that employee must exist.
(c 3.3) If an employee works for a department, then that department must exist.
employee {EMP_ID, EMP_NAME} -- p 2
PK {EMP_ID} -- c 2.1, c 2.2
emp_dept {EMP_ID, DEPT_ID} -- p 3
PK {EMP_ID, DEPT_ID} -- c 3.1
FK1 {EMP_ID} REFERENCES employee {EMP_ID} -- c 3.2
FK2 {DEPT_ID} REFERENCES department {DEPT_ID} -- c 3.3
Case 4: Emp * ---- 1..* Dept
This one is tricky, combine cases 1 and 3 by introducing the concept of a home (primary, default) department.
[p 2] Employee identified by number EMP_ID, named EMP_NAME, works for home department HOME_DEPT_ID.
(c 2.1) Employee is identified by employee number.
(c 2.2) Each employee has exactly one name; for each employee name, more than one employee can have that name.
(c 2.3) Each employee works for exactly one home department; for each home department more than one employee may work for that home department.
(c 2.4) If an employee works for a home department, then that department must exist.
[p 3] In addition to the home department, employee identified by number EMP_ID also works for another department identified by number DEPT_ID.
(c 3.1) In addition to the home department, each employee may work for more than one other department; for each department more than one employee may work for that department.
(c 3.2) If an employee works for a department, then that employee must exist.
(c 3.3) If an employee works for a department, then that department must exist.
employee {EMP_ID, EMP_NAME, HOME_DEPT_ID} -- p 2
PK {EMP_ID} -- c 2.1, c 2.2, c 2.3
FK {HOME_DEPT_ID} REFERENCES department {DEPT_ID} -- c 2.4
emp_dept {EMP_ID, DEPT_ID} -- p 3
PK {EMP_ID, DEPT_ID} -- c 3.1
FK1 {EMP_ID} REFERENCES employee {EMP_ID} -- c 3.2
FK2 {DEPT_ID} REFERENCES department {DEPT_ID} -- c 3.3
On the application level check that, for a given employee, HOME_DEPT_ID is not repeated as EMP_ID in the emp_dept.
It wouldn't be null because every employee has a department. A department can have zero employees though, meaning its ID is in none of the employee rows.

Choice of Clustered and Non-Clustered Index in SQL Server

I have two standalone tables Person and Bank in one database. Moreover I have a reference table Customers in another database.
Table: Person - Database #1
PersonId (PK) Name
_________________________________
1 Ram
2 Raj
3 John
4 Emma
Table: Bank - Database #1
BankId (PK) Name
_________________________________
1 ICICI
2 HDFC
3 SBI
Table: Customers - Database #2
CustomerId (PK) BankId PersonId UserName PIN
_____________________________________________________________
1 1 1 person1 7456bb
2 1 4 person4 NULL
3 2 1 person1 5691io
4 3 2 person2 7892yh
5 3 4 person4 1596pl
I need to execute the following queries efficiently, without INDEX it will do a FULL COLUMN search:
SELECT
c.CustomerId, p.Name, b.Name, c.UserName, c.PIN
FROM
DB2.Customers c
INNER JOIN
DB1.Person p ON p.PersonId = c.PersonId
INNER JOIN
DB1.Bank b ON b.BankId = c.BankId
SELECT *
FROM DB2.Customers c
WHERE c.UserName = 'person1'
SELECT *
FROM DB2.Customers c
WHERE c.UserName = 'person2' AND c.PIN = '7892yh'
So, I need a CREATE table SQL query for the table Customers. The following are the constraints
CustomerId int NOT NULL primary key
BankId int NOT NULL -- needs index and non-unique
PersonId int NOT NULL -- needs index and non-unique
UserName varchar(25) NOT NULL -- needs index and non-unique
PIN varchar(10) NULL -- needs index and non-unique
The said table don't have a FK relationship because the said table is in different database server. So, I need a efficient table structure to fetch records using JOIN. I don't know the which INDEX is efficient in this scenario either clustered nor non-clustered index.

Normalization the table to 3NF

I have trouble in understanding how do I represent the foreign key after I normalize the table to 3NF.
Lets consider the table this way:
student(studentid,studentname,courseid,coursename,dateofexam,marks,grade)
I can able to figure out the FD as follows:
studentid -> studentname
courseid -> coursename
{student,courseid} -> dateofexam,marks
marks -> grade
Now I'm trying to create a new table for FD that has non-ket attributes like this:
marks grade
studentid studentname
Now how do I relate the foreign key for these tables with my old table student? I'm confused on doing this.
Thanks in advance.
Your FD actually shows you how:
studentid -> studentname
courseid -> coursename
{student,courseid} -> dateofexam,marks
marks -> grade
Each row would be a separate table.
The keys that exists in other tables would be the fk.
In this case:
{student,courseid} -> dateofexam,marks
that would be studentid, courseid and marks(which probably must be an id too)
It would be something like this
Student(
studentid,
studentname)
Course(
courseid,
coursename)
Exams(
examid,
fk_studentid,
fk_courseid,
dateofexam,
fk_markid)
Marks(
markid,
grades)

Resources