1

I'm kinda stuck in a tricky situation with the mySQL DB design for my webservice.The DB had initially this Structure:

   CREATE TABLE IF NOT EXISTS `Disease` (
   `Name` varchar(20) NOT NULL,
   `Age` int(10) unsigned NOT NULL,
   `Descriptin` text NOT NULL,
   `Sex` varchar(10) NOT NULL,
   `Ethnicity` varchar(20) NOT NULL,
    PRIMARY KEY (`Name`,`Sex`,`Ethnicity`),
    KEY `Sex` (`Sex`),
    KEY `Ethnicity` (`Ethnicity`)
     ) 

  ALTER TABLE `Disease`
   ADD CONSTRAINT `Disease_ibfk_1` FOREIGN KEY (`Sex`) REFERENCES `Sex` (`Sex`)     ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `Disease_ibfk_2` FOREIGN KEY (`Ethnicity`) REFERENCES `Ethnicity` (`Ethnicity`)     ON DELETE CASCADE ON UPDATE CASCADE;

So basically Disease(Name,Age,Description,Sex, Ethnicity ) Where Sex and Ethnicity are foreign keys to two tables named Sex and Ethnicity because they can have more than one value.

Now to the question I need to add another Column called Symptoms which will be multivalued but I cant declare it as a foreign key, what i need is this:

example of a row

Disease(Name="DiseaseName",Age="40",Description="is caused by...",Sex="male",Ethnicity="Caucasian",Symptoms"Headache,sorethroat,fever") 

So basically i need Symptoms to contain a String of Array but apparently I cant do it since its an RDBMS

Thanks all for you time and efforts!

4 Answers 4

3

Don't do that. Instead, normalize your data model: Make a new table "Symptoms", constrained with foreign key "Disease", and make one record for each symptom.

Whenever you start thinking about putting collections of data into a single field, you're effectively trying to build your own mini database inside the database. Thinking that you can outperform and outwit your RDBMS is optimistic at best and most likely leads to unmaintainable code later on.

By the way, does Sex really have to be looked up in a separate table? For micro-categories like that you might like to consider some sort of enum type.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes i have actually implemented two other columns Sex and Ethnicity the way you said, the issue is that they don't solve my need. Is there any other way around it?
@Rosa: Well, how about an enum?
Actually just adopted enum for Sex :) and it works thanks! But again for the original question it wont work, since using enum you can store one value per record
@Rosa: You mean for the symptoms? No, sure, you cannot use enums for symptoms, least of all because there isn't a bounded, predetermined set of symptoms. As I said, make a separate "Symptoms" table and refer each symptom to a disease.
0

You can accomplish the "array of strings" you're looking for by normalizing your data. Add a new key column to your 'Disease' table. Then create a child table called 'Symptom'. Insert a record for each string with a foreign key back to the 'Disease' table parent record.

1 Comment

Let me explain what i want to accomplaish: A query on table Disease will match the Sex,Ethnicity and age of a patient and the "list" of symptoms he has and then return the name of the disease.How could i do this? Is it possible?
0

In case you didn't notice you misspelled Description in your table creation query.

1 Comment

actually the original is in Italian, just changed it in English here for better comprehension, Thanks anyways.
0

You need a m:n relation:

  • Table: Disease
    • Name
    • Sex
    • Ethnicity
  • Table: Symptoms
    • ID
    • Name
  • Table: Disease_has_Symptoms
    • Name (FK to Disease)
    • Sex (FK to Disease)
    • Ethnicity (FK to Disease)
    • ID (FK to Symptoms)

(Maybe it's easier to add an ID-column to Disease and reference that inside Disease_has_Symptoms)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.