In an organization's database, should there be separate department and job tables? - sql

I am creating the backend of a microservice
that will serve as a tool to see in real time how the company's employees are distributed by projects and what days they have assigned to each one.
The database I have raised looks like this:
The company has 4 departments (Development, Product, Data Science and Operations), but each department of course has its different positions. For example, in development there is Backend, Frontend, SRE and so on for the other departments. My question is if I should create another table for these positions (That is, between departments and employees), or if I should leave the position as an attribute.
To give a little more context, the company is interested in knowing every day how many employees are assigned to projects according to their position, for example, if I have any free backend developer today. If more context is needed to say, please tell me.

If you want to be able to model a future possible situation in which an employee is a member of two or more departments, then yes, you may place a bridge table between employee and department. But if that's not a realistic scenario that you see ever coming, it safeguards integrity better to force the one-to-many relationship the way you have it above. Also, I would point out that your model suggests a task can only be assigned to one employee. Are you sure that's what you want? You may need a bridge table between Tasks and Employees to allow a task to be worked on by more than one person.

Related

When to use one to many vs many to many in right situation?

i quite confuse when, or not to use one to many vs many to many. ex, user roles. in such situation many to many have advantage in reduce data size cause it just point to integer, maybe it save 1-10bytes each row, ex, senior developer char with id 7, it consume 2 bytes in smallint, instead 16 bytes. but, it makes bloat table. if such situation use many to many. why one to many should exists if many to many have the advantage? is it not always good to many to many?
Users table
id
username
password
Users_Roles table
user_id
role
Versus
Users table
Users_Roles table
user_id
role_id
Roles table
id
role
You're prematurely optimizing. A few integers here and there is unlikely to impact your data size nor your performance. If it does, the schema can be changed later, but usually there is much bigger bloat to be concerned with.
One-to-many vs many-to-many is not an optimizing issue. It's about the relationship between the tables.
If one and only one user can have a role, use one-to-many.
If many users can have the same role, use many-to-many.
For example, if you have an admin role and there can ever only be one admin user, use one-to-many. If there can be many admins, use many-to-many. You have to decide what the relationship is between users and roles.
Note: Use bigints for ids. 4 billion might seem like a lot, but it comes up fast and one of the worst things that can happen is to run out of IDs.
This is a data modeling question, and it's answer comes out of and is dictated by the analysis of the relationships of the entities involved. You have identified 2 entities you want to store data about, users and roles. Now describe their relationship in spoken language terms, looking at the relationship from both directions.
Can a user have more than one role? Can a role be held by more than one user? If the answer to both is yes, than it's a many to many relationship. Take the primary keys of both entities and bring them together as the composite primary key of an associative table. It may not have any attribute unless there is data about the relationship of a user/role itself that needs to be captured.
However, what if you are modeling entities of invoices and line items? Can an invoice have more than one line item? Yes. Can an instance of a line item on an invoice belong to more than one invoice? No (note I'm modeling a line item, not a product or part number as a line item could include special pricing for this invoice, color, logo, etc). So this is clearly a one to many relationship in the direction of one invoice can have many line items.
For more information, do some searching on data modeling, it will be a huge help in your database design efforts and you will end up with a better design for more efficient queries by designing the database correctly.
Looks like Schwern and I were typing at the same time :-)

How to design a bi system which provided to many people that everyone has different permission

We are designing a BI system, and will provide it to everyone who use this system. In this system, everyone has different permission to view different attributes on different people.
For example, CEO in a company has permission to view every people in his company, and every attributes, like phone or email. But leader of a department can only see people in his company, and some attributes of some people should be hidden, but these attributes of other people can be seen by the leader.
How to design a system to handle this problem? The data to be seen will update at any time, and user want to see these data immediately after update.

Many-to-Many Relationship with 3 FKs

I'm about to create a Java application (with Spring Boot and React in the front) that simulates a bank and, to store the data, i have chose PostgreSQL as my DBMS. This project is only for study purposes.
The app will allow the user to:
create an account;
do some banking operations (like simulate deposit and transfer to another account);
verify its own statement;
check the transaction history.
I'm doing all the application from scratch and I was modeling the data, changing from logical to physical data model, when I've encountered this problem (i'm new in the Stack Overflow, so i'm not allowed to post an image directly):
The Transaction entity is referring two accounts, because in the real world a transaction is made from one account (idSourceAccount) to another (idDestinationAccount). And like in the real world too, one account can perform many transactions; and one transaction involve two accounts (source/destination) obligatorily.
The main problem is that, in a N:M relationship we need to create an intermediate table to break the N:M relationship. In this case, Account_Transaction would have two FKs from Account, what is not breaking the N:M properly, as show the image:
Am I modeling wrong this problem? What can i do to solve this?
PS: I've searched through the internet and not found a model that makes sens with reality (like Transaction entity with only one idAccount).
UPDATE: Is this a possible approach?

SQL - Contacts, Companies DB Design

Im working on a db to manage customer data for a small company.The customers are companies and institutions (schools..etc) and of course people/contacts. There will be a lot more scope added in time, but right now I'm looking for any input on the core design itself and if there's anything I'm missing here that could cause issues down the road. The image doesn't include the additional lookup tables for items like; country, teltype..etc. I'm kinda worried that I've over-normalised it and it is going to make the queries much more complicated in the long-term. Any input appreciated.
Update - 13/12/2016
I have since created a superclass in my structure called entity, which helps me merge all 3 into one as such. I'm still working on the rest as it has grown quite a lot today, so again any input is appreciated.
The first impression I get looking at the diagram is that you have over-normalised the data (unless that was your aim).
Consider the Company <-> Telephone relationship you have created:-
Creating a relationship like this reads:
A Company can have one to many Telephone Numbers
A Telephone Number can belong to one to many Companies
Evaluating this for a minute; is it likely that a telephone number is shared by more than one Company in your structure? (real-world suggests it wouldn't)
Expanding upon this, I believe the main reason you may have headed down this course would be to allow the same telephone number to apply to one or many contacts as well as a business?
Personally, in my experience, I would suggest a duplication of data (telephone number) maybe easier to maintain and manage from a development perspective. This will make you data structure and application logic less complex, and should make searching less taxing on the system.
However, it will also mean you could end up with stale data, for example, if all of your contacts used the company phone number and the company number was updated, all of the contacts data would now need updating too.
One way round that from an application perspective would be to display the company number with a company contact, then you would not need to duplicate data.
Here is an example of a de-normalised view of this relationship:
You could also apply this to email addresses, where the same concept applies.
Do you need to have bridge tables for telephone, email, and location? If there is no need to have multiple sites, e-mails, or telephone numbers; you can add the attributes to the primary entity.

Database Design - Linking two users

I need some help with some database design. I am a FE developer by trade and have only dealt with very basic DBs. I am just starting to branch out into more "advanced" web apps and would like some pointers in the right direction for the schema.
What I am looking for is an account system that can basically link two accounts. I will give you the scenario I had imagined off the top of my head.
A user signs up in a regular way, just providing name, email, password for simplicity of this question. After they have signed up, the user can then link their account to another user by entering the others email and having it accepted by the other user.
Once this link has been created, the two users can CRUD tasks together.
The bit I am struggling with is how to create the link between the two users. I obviously have my users table.
USERS:
id
name
email
password
Now, I believe I need to create another table that holds the two linked accounts, that has its own unique ID that we can use to CRUD tasks. Something like:
LINKED_USERS:
id
user1id
user2id
verified
TASKS
id
lu_id (FK, Linked_Users id)
// Any other fields for the two combined here.
Is this correct? If so, how would I setup the relationships between the users table and the linked_users table? This is the bit that is confusing me because I need the relationship to reference two users IDs. Say I wanted to display user1id and user2id names, how would the relationship work? Just really need a bit of help wrapping my head around this.
I hope this makes sense, if you need any more information I will just edit the question.
Thanks for any help in advance!
Your question in not entirely clear as to the requirements. My design assumes the following about requirements:
People are linked together in pairs
Each pair owns zero, one, or more task records.
Each person can be assigned to zero, one, or more pairs. If not currently, then perhaps over time (past pairs, current pairs, future pairs).
I think your confusion revolves around the pairing. Instead think of it as teams. The fact that a team can have at most two people is beside the point; 2, 10, 100 does not matter because any number is handled the same way. That way is a Team table that has members assigned. Each person can belong to one or more teams, and each team can have one or more members. That means we have a Many-To-Many relationship between Person and Team. A many-to-many is a problem in relational design that is always solved by adding a third intermediate or "bridge" table. In this case, that bridge table is membership_.
Each team owns zero, one, or more tasks. Each task is owned by one and only one team. This is a simple One-To-Many relationship between Team and Task.
If these assumptions and constraints are correct, then you would have the following table design in a relational database such as Postgres.
I added a start_ and stop_ pair of fields on membership_ to show the idea that people may have past, present, or future assignments to teams.