I have many tables with the follow structure
id(int autoinc)
name (varchar)
for definition of productoCategory, clientType, employeePosition, etc. It is a valid aproach group all this in one table with a name like "groupings"
id (int autoinc)
name (varchar)
table (varchar) <-- here especify the table that below this info(product, client, employee)
is a valid aproach? there will be problems with the scalability?
Related
in the above scenario 'signs and symptoms' is a multi selection and if 'others' selected 'specify-others' field must be filled . how to store this .
what is the best table structure for performance and querying
Either to provide 15 columns in single table and store null if no value or to store foreign key of symptoms in another table (in this strategy how to store 'others symptom' description column ie specify-other field data).
There is no universal answer, your choice may depend on multiple factors including external issues, i.e. coding framework you use to support database (if any). The "classic" way to do it:
1. Patient table:
id (PK)
name
2. Symptom table:
id (PK)
symptom
3. Patient to Symptom table:
id (PK)
patient_id (FK)
symptom_id (FK)
other_symptoms (text)
But once again, any approach (including this one) has its own pros and cons and this is not a universal solution.
I would definitely exclude the 15 columns in a table option because whenever a new symptom would be needed to be added, and it will be needed rather sooner than later, you'll have to:
alter the table schema
the code that displays the symptoms
the code that inserts/updates patient records
who knows what else.
I'd go with a classic many to many relationship, with tables similar to:
patients: patient_id, name, etc
symptoms: symptom_id, name, description, etc
patient_symptoms: patient_id, symptom_id
Even better would be an extra table:
visits: doctor_id, patient_id, date, other_symptoms
And then, your patient_symptoms table can be related to an actual visit to a doctor:
patient_symptoms: visit_id, symptom_id
I want to create a database structure that can store data about families (primary key, name and its members).
I came up with the idea of doing it using these two tables:
TABLE "family":
- id (INT)
- name (VARCHAR)
- members (?)
TABLE "members"
- id (INT)
- name (VARCHAR)
I would like to reference members of the family by the id in the members table. But since MySQL doesn't have arrays, how am I supposed to store multiple members in one column? Or are there better DB structures for this case?
Kenta1561
Don't try to store multiple value in a single cell as arrays. It'll be nightmare for you later when you have to search rows based on one of the value in those arrays or join table with that column and other operations of the sort.
You can create a separate mapping table for that to keeps the things normalized.
You can create a separate table family_members with, say, three columns:
id (auto increment),
family_id (FK to family table),
member_id (FK to members table)
Or have an extra column in the members table as FK to family table if there is one to many mapping.
The mapping table helps if there can be many to many mappings.
Create Foreign key to Member_Id in tblFamily table
TABLE "tblFamily":
- Fam_Id (INT)
- Family_Name (VARCHAR)
- Member_Id (INT)
Create Primary key to Member_Id in tblMember
TABLE "tblMembers"
- Member_Id (INT)
- Member_Name (VARCHAR)
You can try below structure :
Table "Family"
- FAMILY_ID INT (PK)
- FAMILY_NAME VARCHAR
Now as your family can have multiple members, so instead of storing member_id in family table, it will be good to store FAMILY_ID in MEMBERS table.
Table "Member"
- MEMBER_ID INT (PK)
- MEMBER_NAME VARCHAR
- FAMILY_ID INT (FK) REFERENCES FAMILY.FAMILY_ID
This way you will be able to store multiple members for a family and using join on these tables will be able to print all information about the family in single query.
I have two tables:
Friends :
id name
1 jhon
2 peter
Teammates:
id name
3 juan
i am looking for a way two auto increment the id of the second table (teammates) according to the first table ( Friends ).
When I add a new register to Teammates it never match with an id of Friends
I think this is not good practice. If you do so, you are introducing an implicit functional dependency between both tables outside of the declared design. If you want to it anyway, you can use a trigger to asign the value instead of making the column autoincrement.
I would suggest to have a table for all people with the real autoincrement id, then you can use several approaches:
i) Make your two actual tables take id values as foreign keys of this new table, with the corresponding integrity constraint.
ii) Simply create 2 views of the table: One for friends, other for teammates.
Table_Friends: (id, name, role)
View_Friends: Select id, name from table_Friends where role = value_for_friend_role
View_Mates: Select id, name from table_Friends where role = value_for_teammate_role
In a relational supertype/subtype structure where, for example, I have a supertype table entity with an entity_type column and a number of subtype tables, is there any way I can go about querying all entities with their full records, that is somehow joining to each of the subtype tables automatically?
So, with:
TABLE entity
-- entity_id (INT pk)
-- entity_type_id (INT fk)
TABLE entity_type
-- entity_type_id (INT pk)
-- name //Person, Building, Animal (TEXT)
TABLE person
-- entity_id (INT fk)
-- person_name (TEXT)
-- person_age (INT)
TABLE building
-- entity_id (INT fk)
-- age_built (INT)
etc.
what if I wanted to query all entities, and in my result set get all person-specific columns (person_name, etc.) if the record was a person and age_built, etc. if the record was a building? I thought about storing the subtype table names in the type table but understand you can't dynamically reference those like that.
Am I being an ignorant DB newb here or is this in any way possible without explicitly defining the join and doing a query for each subtype table?
I'm asking this because elsewhere in my DB I'm going to have a lot of references to an entity_id (that could be any kind of entity) and I don't want to run an initial query to just to check its type first.
Working in MySQL, no preference to engine.
Don't do this unless you really know what you are doing (and then probably don't do it!). This fits into the entity attribute value anitpattern. Better to model out the entities which relate to each person/animal/building separately.
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2314483800346542969
Either that or you could consider having multiple fk columns from entity type to the linked entities. How many entities are you expecting to reference entity_type?
I am building an application using MySQL 5.0.77 that contains
a) different user types (e.g. carpenter, mechanic, ..., plumber)
b) different input fields for each user type, e.g. user selects carpenter and is presented with fields pertaining to that profession, where the fields for each profession are different
My thinking is along the lines of:
Table: users
user_id
user_name
Table: carpentry
user_id
woodwork_rating
metalwork_rating
Table: plumbing
user_id
central_heating_rating
bathroom_rating
And so on...
This does not seem very good though since I could potentially end up with lots of tables and users existing in multiple tables with different fields.
I quite like the idea of a metatags table (like we see in Wordpress) so that each users field entry is stored, e.g.
Table: user_info
user_id
field
value
So we would have for example
1 | woodwork_rating | intermediate
1 | metalwork_rating | advanced
2 | woodowork_rating | advanced
My question is, how would you structure a database that has multiple fields for multiple users for which each user only fills in one category of the available fields?
Thanks
Table Users:
UserID: Autoinc PRIMARY KEY
(More user data columns here)
UserType: CHAR(5)
Table UserTypes
UserType: CHAR(5) PRIMARY KEY
Description: VARCHAR(50)
Table UserRatingList
UserRatingCode: CHAR(5) PRIMARY KEY
UserType: CHAR(5) REFERENCES UserTypes
Description: VARCHAR(50)
Table UserRatings
UserID: INTEGER PRIMARY KEY / REFERENCES Users
UserRatingCode: CHAR(5) PRIMARY KEY / REFERENCES UserRatingList
Rating: INTEGER (or whatever you prefer)
The table UserRatingList establishes the pattern of ratings that can be applied to each user type. UserRatings contains the actual ratings. I use CHAR(5) to provider readable codes without having to join in the Description fields, but you can change them to INTEGER if you want.
This structure can also be adapted to allow each user to have multiple types; simply create an addition UserTypeLinks table with UserID and UserType.
i would like to rely on 'mike' s last answer.
i am facing this exact problem. i am creating a network with the following types
Enterprise {name, mail, adress, etc ...}
Employee {name, mail, branch, jobdescription, etc ...}
Individual {name, mail, surname, age, town, etc...}
how about:
Table Users
userID (autoincr)
userType
mailadress
password
...
Table Usertypes
typeID
typeName
typeDescr
Table Enterprises
entID (autoincr)
userID
field 1
field 2
...
Table Employee
empID (autoincr)
userID
...
Table Individuals
indID (autoincr)
userID
...
does this make sense to you?
Are all the fields going to be "ratings" with the same datatype? If so I like Larry Lustig's solution.
Or could there be unrelated fields with different data types? dates, strings, decimals, integers? If so, the first solution of having 1 to 1 related tables that join with Users is OK. As long as you don't need to dynamically add new fields at run time.