1

I want to create a table that stores doctors. My problem is how i will store the specialities of a doctor as a doctor can have more than one (aphrodisiologist,dermatologist). So far i have thougth of using varchar type and storind the values in comma separated format.

CREATE TABLE IF NOT EXISTS `doctors` (
  `doctor_id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(50) NOT NULL,
  `password` varchar(30) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `speciality` varchar(254) NOT NULL,
  `lat` decimal(10,8) NOT NULL,
  `lng` decimal(10,8) NOT NULL,
  `phone` char(10) NOT NULL,
  `mobile` char(10) NOT NULL,
  PRIMARY KEY (`doctor_id`)
);

But my problem is when quering the table. If i want to find the doctors that have the speciality 'alfa' and i do this

select * from doctors where speciality='alfa'

the doctor that has two specialities (alfa,beta) will not be included in the result. The only thing i have thought is to use LIKE instead of WHERE but i think that there must be a better way. What is the best way to implement this (perhaps a different data type from varchar)?

4

3 回答 3

2

This is a many-to-many relationship. Each doctor can potentially have multiple specialties, and many doctors have each specialty.

Doctors >--------< Specialties

The way to represent a many-to-many relationship in a relational database is not with a comma-separated list, but with another table.

Doctors ----< HasSpecialty >---- Specialties 

Here's an example:

CREATE TABLE HasSpecialty (
  specialty_id INT,
  doctor_id INT,
  PRIMARY KEY (specialty_id, doctor_id),
  KEY (doctor_id, specialty_id),
  FOREIGN KEY (doctor_id) REFERENCES Doctors(doctor_id),
  FOREIGN KEY (specialty_id) REFERENCES Specialties(specialty_id)
);

Querying for doctors with a given specialty, do a JOIN:

SELECT d.* FROM Doctors AS d
INNER JOIN HasSpecialty AS hs USING (doctor_id)
INNER JOIN Specialties AS s USING (specialty_id)
WHERE s.specialty = 'alfa';

Some people dislike JOINs because they think they are slow. They go to great lengths to avoid JOIN, such as using dependent subqueries which can be worse. With proper indexes, joins are not slow.

于 2013-10-30T18:58:34.597 回答
0

You can use type SET http://dev.mysql.com/doc/refman/5.5/en/set.html Then you can find data even use bit field mappings. But be careful this type has some limitations. Take a look at documentation.

于 2013-10-30T18:49:46.670 回答
0

Create a table of doctor_specialties with these columns:

doctor_id int not null
specialty varchar(254) not null

The doctor_id should be a foreign key to doctors, and consider making a 2-column primary key on both columns.

Insert records in this table, referencing the doctor and their specialty. You would have multiple records if the doctor has multiple specialties.

Then, to query doctors that have a certain specialty, you could do:

select * from doctors d 
where exists (select * from doctor_specialties 
              where doctor_id = d.doctor_id 
                  and specialty = 'alfa');

There are a number of ways to write this query, including sub-selects or joins. I use EXISTS when I don't need to return the information in the other table.

于 2013-10-30T18:50:10.870 回答