I have 3 type of user roles: Patient, Doctor, Pharmasist
And Tables:
Users
id | name | surname | username | password | etc..
Roles
id | name
Patient
Doctor
Pharmasist
users_roles
id | user_id | role_id
And I want to implement tables such as: doctor_info, patient_info, pharmasist_info.
For example:
doctor_info
id | experience | qualification | user_id
What relationship should I use to tie users with doctor_info, patient_info, pharmasist_info and how to implement it correctly?
Assuming all id column from each table is primary key.
users:
id, name, ...
roles:
id, name
users_roles:
id, user_id, role_id (make user_id UNIQUE key, so 1 user can only have 1 role)
doctor_info:
id, user_id, ... (make user_id UNIQUE key as well, so 1 user can only be 1 doctor)
patient_info:
similar to doctor_info
pharmasist_info:
similar to doctor_info
(OPTIONAL) If 1 user has 1 role and 1 role is belonged to many users, you could remove users_roles table completely, and just add role_id in users table.
One issue is that you can have: 1 user can be a doctor, a patient and a pharmasist altogether with your table structure. You'll have to add some validation in your code to make sure it won't happen.
Related
I'm a beginner concerning coding and especially SQL and PHP.
I deal with app. 120 users.
The users can acquire app. 300 different collectible items.
When a user acquires a specific item, I would like the ID number of that particular item to be stored in the row of the user who acquired it, so that there is some information about what items the user already has (and to avoid duplicate items in his possession).
Is there a good way to store such information?
Is it even possible to set a column type to array and store it there?
Please note: I'm not lazy and I've been digging around and searching for the answer for 2 hours. I couldn't find a solution. I know of the rule that one should insert only one piece of information into one cell.
MySQL does not support storing arrays. However, you can use a second table to emulate an array by storing the relation between the users and items. Say you have the table users:
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
...
);
And you have a table defining items:
CREATE TABLE items (
item_id SERIAL PRIMARY KEY,
...
);
You can relate what items a user has using a table similar to user_items:
CREATE TABLE user_items (
id SERIAL PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
item_id BIGINT UNSIGNED NOT NULL,
...,
FOREIGN KEY (user_id)
REFERENCES users (user_id),
FOREIGN KEY (item_id)
REFERENCES items (item_id)
);
Then, to determine what items user 123 has acquired, you could use JOINs similar to:
SELECT items.*
FROM users
INNER JOIN user_items
ON user_items.user_id = users.user_id
INNER JOIN items
ON items.item_id = user_items.item_id
WHERE users.user_id = 123; -- Or some other condition.
I assume you have 2 tables for example, users and items. To control which user already has a specific item, i would create an associative table, including the UserID from users and ItemID from items. This way you can now check in your user_items table if the user already has this item.
Here is a small example:
users (UserID is PK):
+--------+----------+
| UserID | UserName |
+--------+----------+
| 1 | Fred |
| 2 | Joe |
+--------+----------+
items (ItemID is PK):
+---------+----------+
| ItemID | ItemName |
+---------+----------+
| 5 | Book |
| 6 | Computer |
+---------+----------+
user_items (ItemID referencing items.ItemID, UserID referencing users.UserID):
+---------+--------+
| ItemID | UserID |
+---------+--------+
| 5 | 1 |
| 6 | 2 |
+---------+--------+
I have a Users table that belongs to a Role, and has one Server or no one (depends on role), but should to have a Server table with user_id field or should I put all Server info in Users table and when the role hasn't Servers, the fields will be null?
I just think that if a User have one server (or not), this shouldn't be a new row in Servers table, maybe if the user_id field be unique, then it will be correct, I don't know.. I'm confused.
Just explain to me which is the best way to build this thing.
-- edit
This is my tables actually
Roles
id (PK) | name
1 | Administrator
Users
id (PK) | role_id | name
1 | 1 | Juliano
Servers
id (PK) | user_id (UNIQUE) | name
1 | 1 | Test
I don't know.. in servers, user_id should be UNIQUE or PK?
ONE TO ONE Relation , Then put the server in the user table.
Users Table:
ID(Pk)
RoleID
Name
ServerID
favorite
If I'm designing the backend for a messaging app that allows group texts and has the following structure:
Message Table
message id | creator | message data | date created
Conversation Table
conversation id | creator | date created
ConversationtoMessage Table
conversationtomessage id | conversation id | message id
ConversationtoUser Table
conversationtouser id | conversation id | user id
Does it makes sense to have the creator of the conversation column in the conversation table, or is that redundant information given that the creator will also be linked in the conversationtouser table
I think it can be done using two tables only:
users
id (unique id)
username
name
created
messages
id (unique id)
user_id
type (1 for individual messages, 2 for group messages)
message_identifier ( unique id for particular conversation)
message_text
created
I have 3 three tables as follows a user can have many contacts and a contact can be listed by many users...
//user table
user_id | username|password|fname|lname|email|contactnumber
//contact table
contact_id | fname | lname |email|contactnumber
//user_contact table
user_id |contact id | relationship type |relationship state
My query must display all the contacts that is link to the selected user ...any advise will be helpful
so it will look like this
Result:
user fname | user lname | email address | contact number of user | contact first name | contact last name | relationship type | relationship state
Correct me if I understand your question wrong:
so the user table and contact table has a many to many relation?
then you can do
select u.fname,u.lname,u.email,u.contactnumber,c.fname,c.lname,uc.relationship_type,uc.relationship_state
from user as u
inner join user_contact as uc on u.user_id=uc.user_id
inner join contact as c on uc.contact_id=c.contact_id
where u.user_id=<userId>
My system uses a table of users with several elements including their online status. Now I would like to add a feature so each user can add a contact i.e an existing user from the database into their list of friends or contacts showing their name and online status. However I need any changes in the online status to updated on the contact list as they happen so creating a new table wouldn't help.
I have been looking into views for this but don't have too much experience with databases to I would like to know if this is the correct way of going about it and a bit more detail on how to do it.
Here are the steps I was thinking of:
When users registers, create a view i.e view_name = username_view.
To add a contact select data from main users table and add to user's view
To delete a contact delete selected data from view.
I am not sure if this is possible with views so if it isn't can some please help me out.
Thanks.
ER databases = Entites and Relations databases handle that by have one table whit the entites "users", and one table for the relationship "friends" that connect a user to another user.
a query for that can be:
SELECT users.*
FROM friends
LEFT JOIN users ON (users.user_id = friends.friend_id)
WHERE friends.user_id = :user
Exemple:
a user table, for the entity user
CREATE TABLE user (user_id SERIAL, name TINYTEXT NOT NULL);
exemple users
user_id | name
1 | Anna
2 | Bertil
3 | Carl
4 | David
5 | Erik
a friends tabel, for relations between two users, (not one per user, just one)
CREATE TABLE friends (
user_id BIGINT UNSIGNED NOT NULL,
friend_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (user_id, friend_id)
);
if Anna adds Beril and Carl as friends, and Carl adds David and Erik as friends, the content of the table going to be:
user_id | friend_id
1 | 2
1 | 3
3 | 4
3 | 5
if we want to list the names of Annas friends, and we allredy know that Annas user_id = 1, then we can use this query (like the one above)
SELECT users.name
FROM friends
LEFT JOIN users ON (users.user_id = friends.friend_id)
WHERE friends.user_id = 1