How I can Make Schema for Tables (Client, Companies and Users) - mysql

I am developing a program in PHP and I need to create MYSQL Schema for the following tables
Users
Clients
Companies
My Requirements are
A Client may have many companies and a company must belong to a client
Client can be able to login into the application as a user
A Company may have many users and each user can to login to an application
There is a superuser, which does not belong to any client|company but still can be login into the application
To handle the login system, I introduced a table users and created the schema as given below
table: clients
id as PK
name
table: companies
company_id as PK
name
client_id as FK
table: users
id as PK
company_id as FK
client_id as FK
is this ok? the way I am created the schema.

Related

Mysql table relation for a user with multiple business

How to properly implement mysql table relation such that a user can create multiple business and each of those business can have multiple users. thanks
Basically going to be two (possibly 3 tables):
Business Table:
business_id int (this will be the primary key)
business_name
whatever extra fields that relate to the business
User Table:
user_id whatever type (this will be the primary key)
user_name
whatever extra fields that relate to the business
The relational part you can implement in one of two ways:
add the business id to the user table. So user table will have an additional field business_id and will relate back to one of the rows in the business table. This implies a user can only relate to 1 business.
If you want a user to be able to relate to one or more businesses then you need a separate table:
Business_User Table:
business_id int
user_id (whatever type you picked)

Trying to implement friendship between users. What kind of relationship do I need between the User model and the Friend model

Currently I have a Users table and a User model. The Users table looks like this:
Table: Users
Columns: id, username, password, email
I assume if I want to implement friendship between users, I'd need to create a Friend model and Friends table which would contain the ids of the 2 users that have become friends
Table: Friends
Columns: id, userId, userId
Where both userId would be a foreign keys from the Users table. I'm using Sequelize ORM but I'm not sure what the relationship between the User and Friend models should be. I'm thinking many-to-many relationship but I'm a bit confused how to do it since both foreign keys are from the same table, therefore I need only 2 tables instead of 3.

Data consistency and using session instead of foreign key and relations

I have a database with 15 tables that are connected between each other by different relations.
The main table is the login table:
login_id
user_name
user_pass
area_id
The area_id means that each area have it's own user and password so each action made, we can figure out in which area it happened.
Now instead of connecting all 15 tables into the login table with 1 to many relation and area_id is the foreign key, can I save the area_id in a session (because the backend language is PHP) and add it in each table without the relation.
So now, the user_info table contains:
name
address
phone
mother_name
contact
alternative_contact
area_id
But here this table is not connected directly to login table but it contains a similar field area_id and which it's value is added from session, and so we can now create a query by saying:
SELECT something FROM login JOIN user_info WHERE user_info.area_id = login.area_id
Does this makes data consistent or should I connect the login table with all other tables ?

Database Design for table to summarize multiple tables

My database has multiple tables that store user information
users (user_id, name, email, phone, dob, picture)
All other profile details are in differnt tables with user_id as foreign key
profile_work (one-to-many)
profile_education (one-to-many)
profile_emails (one-to-many)
profile_locations (one-to-many)
There are 4 other similar tables.
My application requires details of users together so I send array of user_id's as input to and it returns complete profile details of all users as a Json object.
As writes to user profile are very less and processing speed is very important for the app, I am planning to create another table that combines all the data of user and stores it. I would update this table everytime any of the profile table changes. I would use this table to fetch the data of about 25 users at a time.
How should the design of new table be?
| user_id| name | email | picture | basic | work | education |
with json data in each of these coloumns (work & education)
or
| user_id | json |
compiling everything as json and inserting in a table
Some of my services require only name, email and picture While others require work and education also.
What are the issues I would need to think through/ expect in future.
Other info
I am using mysql
My Db currently has 100K profiles
Expecting the db to reach about 1 Mn profiles in about a year, but will need to be scalable in future.

Normalization of database for timesheet tool and ensure data integrity

I'm creating a timesheet application. I have the following entities (amongst others):
Company
Employee = an employee associated with a company
Client = a client associated with a company
So far I have the following (abbreviated) database setup:
Company
- id
- name
Employee
- id
- companyId (FK to Company.id)
- name
Client
- id
- companyId (FK to Company.id)
- name
Now, I want an employee to be associated with a client, but only if that client is associated with the company the employee works for. How would you guarantee this data integrity on a database level? Or should I just depend on the application to guarantee this data integrity?
I thought about creating a many to many table like this:
EmployeeClient
- employeeId (FK to Employee.id)
- companyId \ (combined FK to Client.companyId, Client.id)
- clientId /
Thus, when I insert a client for an employee along with the employee's company id, the database should prevent this when the client is not associated with the employee's company id. Does this make sense? Because this still doesn't guarantee the employee is associated with the company. How do you deal with these things?
UPDATE
The scenario is as followed:
A company has multiple employees. Employees will only be linked to one company.
A company has multiple clients also. Clients will only be linked to one company.
(Company is a sandbox, so to speak).
An employee of a company can be linked to a client of it's company, but only if the client is part of the company's clientele.
In other words:
The application will allow a company to create/add employees and create/add clients (hence the companyId FK in the Employee and Client tables). Next, the company will be allowed to assign certain clients to certain of it's employees (EmployeeClient table).
Imagine an employee working on projects for a few clients for which s/he can write billable hours, but the employee must not be allowed to write billable hours for clients they are not assigned to by their employer (the company). So, employees will not automatically have access to all their company's clients, but only to those that the company has selected for them. Hopefully this has shed some more light on the matter.
If you want to do it from the database level then I would put the logic in a stored procedure. The stored proc code will then associate the two if applicable but this means that (given you put the foreign key to the employee in the client table) a client is only associated with one employee. Is this what you want?
Also take note though that an employee in your table is indirectly associated with all such clients via its company association. If all employees are automatically associated with all new clients of their company then perhaps you just want to write a query that checks for this.
(This is not an answer, but it didn't really fit in as a question comment.)
The data presented for your design question begs a number of questions:
Are employees to be associated with companies and clients? Or...
Are employees only associated with clients, and (thus) the company associated with that client?
If employess and clients are associated with companies, is an employee thus associated with all employees of that company, or must you pick and choose?
Update
As far as data modelling is concerned, it seems like all you need to do is expand the foreign key in EmployeeClient into Employee like so:
EmployeeClient
- companyId
- employeeId
- clientId
Compound primary key on all three columns.
Foreign key on (companyId, clientId) into Client
Foreign key on (companyId, employeeId) into Employee
Thus, all relations defined in EmployeeClient require both Client and Employee to share the same clientId.