Jump to content

DB structure foreign key


cedtech23

Recommended Posts

I am a newbie to database design and working of a database that consist of five tables

 

1. appointments

2. interpreters

3. patients

4. providers

5. requesters

 

the structure of the tables is shown below. should I add the primary keys from interpreters(intpre_id),

patients(pt_id), providers (prov_id), requesters(pt_id) to the appointment tables as foreign keys or regular int data type?  ???

 

 

mysql> show tables;

+-----------------+

| Tables_in_forms |

+-----------------+

| appointments    |

| interpreters    |

| patients        |

| providers      |

| requesters      |

+-----------------+

5 rows in set (0.00 sec)

 

 

mysql> describe appointments;

+-----------+---------+------+-----+---------+----------------+

| Field    | Type      | Null  | Key | Default | Extra          |

+-----------+---------+------+-----+---------+----------------+

| appt_id  | int(11)  | NO  | PRI | NULL    | auto_increment |

| appt_date | date  | YES  |      | NULL    |                |

| appt_time | time    | YES  |      | NULL    |                |

+-----------+---------+------+-----+---------+----------------+

3 rows in set (0.01 sec)

 

mysql> describe interpreters;

+-----------+------------------+------+-----+---------+----------------+

| Field    | Type                  | Null | Key | Default | Extra          |

+-----------+------------------+------+-----+---------+----------------+

| f_name    | varchar(50)      | NO  |      |        |                |

| l_name    | varchar(50)      | NO  |      |        |                |

| intpre_id | int(10) unsigned  | NO  | PRI | NULL    | auto_increment |

+-----------+------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

 

mysql> describe patients;

+-----------+------------------+------+-----+---------+----------------+

| Field        | Type                | Null | Key | Default | Extra          |

+-----------+------------------+------+-----+---------+----------------+

| f_name    | varchar(50)      | NO  |    |        |                |

| l_name    | varchar(50)      | NO  |    |        |                |

| phone_num | varchar(11)  | YES  |    | NULL    |                |

| pt_id    | int(10) unsigned  | NO  | PRI | NULL    | auto_increment |

+-----------+------------------+------+-----+---------+----------------+

4 rows in set (0.00 sec)

 

mysql> describe providers;

+---------+-------------+------+-----+---------+----------------+

| Field  | Type        | Null | Key | Default | Extra          |

+---------+-------------+------+-----+---------+----------------+

| prov_id | int(11)    | NO  | PRI | NULL    | auto_increment |

| f_name  | varchar(50) | NO  |    |        |                |

| l_name  | varchar(50) | NO  |    |        |                |

+---------+-------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

 

mysql> describe requesters;

+--------+------------------+------+-----+---------+----------------+

| Field  | Type            | Null | Key | Default | Extra          |

+--------+------------------+------+-----+---------+----------------+

| f_name | varchar(50)      | NO  |    |        |                |

| l_name | varchar(50)      | NO  |    |        |                |

| req_id | int(10) unsigned | NO  | PRI | NULL    | auto_increment |

+--------+------------------+------+-----+---------+----------------+

3 rows in set (0.00 sec)

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/79740-db-structure-foreign-key/
Share on other sites

I can see that an an appointment will involve a patient and may involve an interpreter (but the interpreter may be null if not required).

 

I don't know how providers and requesters are related to appointments as I have no idea what these definitions are.

 

As to INT or FK - if tables use myisam engine you have to use int. If innodb then use int but you can also define them as foreign keys. Either way you can still use them for relational joins.

The appoinments table will be used to capture information from the user. All fields are required and the provider, interpreters

must be in the providers, interpreters tables. I made the all fields NOT NUll.

 

Basically ever appointment will have a provider, patient, interpreter, requester, date and time.

 

 

 

 

 

mysql> describe appointments;

+-----------+---------+------+-----+---------+----------------+

| Field    | Type    | Null | Key | Default | Extra          |

+-----------+---------+------+-----+---------+----------------+

| appt_id  | int(11) | NO  | PRI | NULL    | auto_increment |

| appt_date | date    | NO  |    |        |                |

| appt_time | time    | NO  |    |        |                |

| pt_id    | int(11) | NO  |    |        |                |

| prov_id  | int(11) | NO  |    |        |                |

| intpre_id | int(11) | NO  |    |        |                |

| req_id    | int(11) | NO  |    |        |                |

+-----------+---------+------+-----+---------+----------------+

7 rows in set (0.00 sec)

Just join the tables on the keys

SELECT a.appt_date, a.appt_time, p.f_name, p.l_name, i.f_name, i.l_name
FROM appointments a
INNER JOIN patients p ON a.pt_id = p.pt_id
INNER JOIN interpreters i ON a.intpre_id = i.intpre_id

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.