How to select column twice from the same mysql table? - mysql

I've been doing quite a bit of MySql lately for uni, and i cant seem to figure out how to get a field from a table twice in the same statement.
My database is this:
drop database if exists AIRLINE;
create database AIRLINE;
use AIRLINE;
CREATE TABLE AIRCRAFT
(
AircraftNo INT(20) NOT NULL,
AircraftType VARCHAR(100) NOT NULL,
FuelBurn VARCHAR(100) NOT NULL,
Airspeed VARCHAR(100) NULL,
LastInspection DATE NULL,
TotalFlyingTime INT(50) NOT NULL,
TotalTimeLeftEngine INT(50) NULL,
TotalTimeRightEngine INT(50) NULL,
PRIMARY KEY (AircraftNo)
);
CREATE TABLE PILOT
(
PilotCode INT(20) NOT NULL,
LastName VARCHAR(100) NOT NULL,
FirstName VARCHAR(100) NOT NULL,
MiddleInitial VARCHAR(50) NULL,
HiredDate DATE NULL,
BasePay VARCHAR(50) NULL,
Dependents VARCHAR(100) NULL,
License INT(50) NOT NULL,
TotalHours INT(50) NOT NULL,
PRIMARY KEY (PilotCode)
);
CREATE TABLE CUSTOMER
(
CustomerNo INT(20) NOT NULL,
Name VARCHAR(100) NOT NULL,
Contact INT(50) NOT NULL,
Phone INT(50) NOT NULL,
Street VARCHAR(100) NULL,
Suburb VARCHAR(100) NULL,
State VARCHAR(100) NULL,
Postcode INT(20) NULL,
Balance INT(50) NULL,
PRIMARY KEY (CustomerNo)
);
CREATE TABLE CHARTER
(
TripTicket INT(50) NOT NULL AUTO_INCREMENT,
CharterDate DATE NOT NULL,
PilotCode INT(20) NOT NULL,
CopilotCode INT(20) NULL,
AircraftNo INT(20) NOT NULL,
Destination VARCHAR(100) NOT NULL,
Distance INT(20) NULL,
HoursFlow INT(20) NULL,
HoursWating INT(20) NULL,
Fuel INT(20) NULL,
Oil INT(20) NULL,
CustomerNo INT(20) NOT NULL,
PRIMARY KEY (TripTicket),
FOREIGN KEY(PilotCode) REFERENCES PILOT(PilotCode),
FOREIGN KEY(CopilotCode) REFERENCES PILOT(PilotCode),
FOREIGN KEY(AircraftNo) REFERENCES AIRCRAFT(AircraftNo),
FOREIGN KEY(CustomerNo) REFERENCES CUSTOMER(CustomerNo)
);
My goal is to list the charterdate, destination, customer details (name, customerNo, address, phone), and pilot names (firstname, middleinitial, lastname) of all charters.
I have managed to get everything, but only with one pilot. I need to list both pilot names however.
I have googled my problem, but i cant seem to find anything.
If someone could please point me in the right direction, i would be hugely grateful.
Thanks
Cheers
Corey

You just need to JOIN the table twice with different aliases.
Something like:
SELECT p1.lastname, p2.lastname, /* other fields */
FROM CHARTER c
JOIN PILOT p1 ON p1.PilotCode = c.PilotCode
JOIN PILOT p2 on p2.PilotCode = c.CoPilotCode

Give alias name as
SELECT a.columname1 AS 1, a.columname1 AS 2
FROM tablename a

You have to use table aliases in your join:
SELECT MainPilot.LastName, CoPilot.LastName FROM CHARTER
LEFT JOIN PILOT MainPilot ON MainPilot.PilotCode=CHARTER.PilotCode
LEFT JOIN PILOT CoPilot ON CoPilot.PilotCode=CHARTER.CoPilotCode

You need to join the pilot table twice in your query. to do that you will have to use an alias for each Pilot table you join.

You can simply use the same column multiple times and add for each of them and as and use different name
SELECT column1 as c1, column1 as c2, column1 as c3 FROM TABLE1 WHERE ....

Related

SQL JOIN query issue provides inaccurate return

I have spend hours searching for this specific issue on the site though many are close not address it completely. Please link it if it exists and I will happily take down this question.
I have two tables wb_states and wb_cities you can see by the keys:
composite state_code+country_code & city_id are distinctive for their respective tables.
OBJECTIVE: run a JOIN query to pull in the state_name from the wb_states table (in place of the state_code)
I always get the sql_mode=only_full_group_by ERROR unless I use the following
SELECT ANY_VALUE(c.city_id) as id,
ANY_VALUE(c.city_ascii) as ci,
ANY_VALUE(s.state_name_ascii) as ps,
ANY_VALUE(c.country_code) as co
FROM wb_cities AS c
JOIN wb_states AS s ON
s.state_code=c.state_code
WHERE c.city_ascii like 'aa%' GROUP BY id ORDER BY co,ps,ci ASC
Of course the query works but the moment we employ ANY_VALUE it fails.
Unique id 2959927 - city of Aalen belongs to only one province Baden-Wurttemberg. How do you construct this query? Thank you.
CREATE TABLE `wb_cities` (
`city_id` int(11) NOT NULL,
`city_name` varchar(65) DEFAULT NULL,
`city_ascii` varchar(65) DEFAULT NULL,
`state_code` varchar(55) DEFAULT NULL,
`state_name_ascii` varchar(45) DEFAULT NULL,
`country_code` varchar(45) DEFAULT NULL,
`country_full` varchar(55) DEFAULT NULL,
`lat` varchar(45) DEFAULT NULL,
`lon` varchar(45) DEFAULT NULL,
PRIMARY KEY (`city_id`)
)
CREATE TABLE `wb_states` (
`id` int(11) DEFAULT NULL,
`state_code` varchar(10) NOT NULL,
`state_name` varchar(45) DEFAULT NULL,
`state_name_ascii` varchar(45) DEFAULT NULL,
`country_code` varchar(45) NOT NULL,
PRIMARY KEY (`state_code`,`country_code`)
)
INSERT INTO `wb_cities` VALUES (14256,'Āzādshahr','Azadshahr','09','Hamadan','IR','Iran','34.79049','48.57011'),(18918,'Protaras','Protaras','01','Famagusta','CY','Cyprus','35.0125','34.05833'),(23814,'Kahrīz','Kahriz','13','Kermanshah','IR','Iran','34.3838','47.0553'),(24851,'Nūrābād','Nurabad','23','Lorestan','IR','Iran','34.0734','47.9725'),(32723,'Īstgāh-e Rāh Āhan-e Garmsār','Istgah-e Rah Ahan-e Garmsar','25','Semnan','IR','Iran','35.23455','52.30942'),(32767,'Qarchak','Qarchak','26','Tehran','IR','Iran','35.42867','51.57544'),(32909,'Shahre Jadide Andisheh','Shahre Jadide Andisheh','26','Tehran','IR','Iran','35.6803','51.0193'),(41210,'Khorramdarreh','Khorramdarreh','36','Zanjan Province','IR','Iran','36.20898','49.19152'),(50672,'Wanlaweyn','Wanlaweyn','14','Shabeellaha Hoose','SO','Somalia','2.6185','44.8938'),(52867,'Qoryooley','Qoryooley','14','Shabeellaha Hoose','SO','Somalia','1.78784','44.52999');
INSERT INTO `wb_states` VALUES (29,'00','Armenia','Armenia','AM'),(11,'00','Aruba','Aruba','AW'),(15,'00','Bosnia and Herzegovina','Bosnia and Herzegovina','BA'),(31,'00','Botswana','Botswana','BW'),(7,'00','Belarus','Belarus','BY'),(23,'00','Cook Islands','Cook Islands','CK'),(39,'00','Christmas Island','Christmas Island','CX'),(4,'00','Czech Republic','Czech Republic','CZ'),(17,'00','Western Sahara','Western Sahara','EH'),(10,'00','Falkland Islands (Islas Malvinas)','Falkland Islands (Islas Malvinas)','FK'),(26,'00','Abkhazia','Abkhazia','GE'),(27,'00','Ghana','Ghana','GH'),(37,'00','Gibraltar','Gibraltar','GI'),(3,'00','Equatorial Guinea','Equatorial Guinea','GQ'),(16,'00','South Georgia and The South Sandwich Islands','South Georgia and The South Sandwich Islands','GS'),(28,'00','Hong Kong','Hong Kong','HK'),(6,'00','Indonesia','Indonesia','ID'),(8,'00','Ireland','Ireland','IE'),(32,'00','Kyrgyzstan','Kyrgyzstan','KG'),(36,'00','Comoros','Comoros','KM'),(34,'00','Muḩāfaz̧atalWafrah','MuhafazatalWafrah','KW'),(19,'00','Zhezqazghan Oblysy','Zhezqazghan Oblysy','KZ'),(21,'00','Lithuania','Lithuania','LT'),(40,'00','Monaco','Monaco','MC'),(13,'00','Montenegro','Montenegro','ME'),(9,'00','Mali','Mali','ML'),(5,'00','Macedonia','Macedonia','MO'),(35,'00','Mauritius','Mauritius','MU'),(25,'00','Malaysia','Malaysia','MY'),(41,'00','Norfolk Island','Norfolk Island','NF'),(30,'00','Bāgmatī Zone','Bagmati Zone','NP'),(2,'00','Niue','Niue','NU'),(14,'00','Panama','Panama','PA'),(22,'00','Philippines','Philippines','PH'),(42,'00','Palestine','Palestine','PS'),(43,'00','Serbia','Serbia','RS'),(24,'00','Swaziland','Swaziland','SZ'),(20,'00','Turks and Caicos Islands','Turks and Caicos Islands','TC'),(33,'00','Turkmenistan','Turkmenistan','TM'),(38,'00','Turkey','Turkey','TR'),(12,'00','British Virgin Islands','British Virgin Islands','VG'),(18,'00','Zimbabwe','Zimbabwe','ZW'),(164,'01','Abū Z̧aby','Abu Zaby','AE'),(90,'01','Badakhshan','Badakhshan','AF'),(112,'01','Barbuda','Barbuda','AG'),(118,'01','Aragatsotn','Aragatsotn','AM'),(124,'01','Benguela','Benguela','AO'),(45,'01','Buenos Aires','Buenos Aires','AR'),(146,'01','Burgenland','Burgenland','AT');
You join wb_cities to wb_states only on state_code but you should also use country_code, because state_code is not unique in wb_states.
Also I don't see any reason for aggregation. You need only a join of the tables:
SELECT c.city_id id,
c.city_ascii ci,
s.state_name_ascii ps,
c.country_code co
FROM wb_cities AS c JOIN wb_states AS s
ON s.state_code=c.state_code AND s.country_code = c.country_code
WHERE c.city_ascii like 'aa%'
ORDER BY co, ps, ci
Your data model is broken. Here are two definitions of state_code:
`state_code` varchar(55)
`state_code` varchar(10)
And yet this is part of an undeclared foreign key relationship.
I think this is the root problem:
PRIMARY KEY (`state_code`, `country_code`)
Presumably, the ID is unique, so you should have:
CREATE TABLE `wb_states` (
`state_id` int(11) DEFAULT PRIMARY KEY,
`state_code` varchar(10) NOT NULL,
`state_name` varchar(45) DEFAULT NULL,
`state_name_ascii` varchar(45) DEFAULT NULL,
`country_code` varchar(45) NOT NULL,
`country_full` varchar(55) DEFAULT NULL,
UNIQUE (`state_code`,`country_code`)
);
Then, your wb_cities table can be appropriately defined as:
CREATE TABLE `wb_cities` (
`city_id` int(11) NOT NULL PRIMARY KEY,
`city_name` varchar(65) DEFAULT NULL,
`city_ascii` varchar(65) DEFAULT NULL,
`state_id` int DEFAULT NULL,
`lat` varchar(45) DEFAULT NULL,
`lon` varchar(45) DEFAULT NULL,
PRIMARY KEY (`city_id`),
FOREIGN KEY (state_id) REFERENCES wb_states(state_id)
);
You should probably have a "countries" table as well, but at least the country can go in the "states" table rather than the "cities" table.
Then, the "state" information can be looked up using the state_id.
After you have fixed the data model, you can think about fixing the query.

VIEW created in MySQL giving wrong output with SELECT

I'm creating a database for a courtier company and I have 5 relations
CREATE TABLE Customer
(
cid int(7) NOT NULL,
cfname char(25) NOT NULL,
clname char(25) NOT NULL,
aptnum int(100) NOT NULL,
street char(50) NOT NULL,
pobox int(10) NOT NULL,
area char(50) NOT NULL,
country char(50) NOT NULL,
phone int(12) NOT NULL,
PRIMARY KEY (cid)
)ENGINE=INNODB;
CREATE TABLE Orderr
(
orderid int(8) NOT NULL,
origin char(100) NOT NULL,
destination char(100) NOT NULL,
eta date NOT NULL,
weight int(100) NOT NULL,
priority enum('F','R') NOT NULL,
task enum('P','D') NOT NULL,
odate date NOT NULL,
cnum int(12) NOT NULL,
cpin int(8) NOT NULL,
custid int(7) NOT NULL,
PRIMARY KEY (orderid),
FOREIGN KEY (custid) REFERENCES Customer(cid)
)ENGINE=INNODB;
CREATE TABLE History
(
histid int(6) NOT NULL,
orderid int(8) NOT NULL,
status enum('D','O','R') NOT NULL,
current_loc char(50) NOT NULL,
PRIMARY KEY(histid),
FOREIGN KEY (orderid) REFERENCES Orderr(orderid)
)ENGINE=INNODB;
CREATE TABLE Driver
(
driverid int(6) NOT NULL,
dfname varchar(25) NOT NULL,
dlname varchar(25) NOT NULL,
dob date NOT NULL,
phone int(10) NOT NULL,
vehicle int(6) NOT NULL,
PRIMARY KEY (driverid)
)ENGINE=INNODB;
CREATE TABLE Vehicle
(
vid int(6) NOT NULL,
num_plate varchar(6) NOT NULL,
vtype enum('T','B','P') NOT NULL,
driverr int(6) NOT NULL,
orders int(8) NOT NULL,
PRIMARY KEY (vid),
FOREIGN KEY (driverr) REFERENCES Driver(driverid),
FOREIGN KEY (orders) REFERENCES Orderr(orderid)
)ENGINE=INNODB;
I am trying to create a VIEW for the Customer giving access to specific columns, here is the query
CREATE VIEW CustView AS
SELECT cfname, clname, aptnum, street, pobox, area, country, origin, destination, weight, priority, task, eta, odate, dfname, dlname, driver.phone
FROM Customer, Orderr, Vehicle, Driver
WHERE Customer.cid=Orderr.custid AND Vehicle.driverr=Driver.driverid AND Orderr.orderid=Vehicle.orders;
When I run SELECT * FROM CustView I do not get the desired output. What changes if any should I make to my query or perhaps to my relations?
Thanks.
As the previous posters remarked, I can only guess. But from your table and view definition, I get the suspicion that you didn't model the relation between Vehicle and Order properly. A Vehicle can be used for many Orders, right? In this case, the Vehicle id must be a foreign key in the Order table. Your design is just the other way round.
Not related to your question, I am suspicious of your History table. The history of orders fulfilled comprises just those orders which are in the past, right? So the history would be just a view that selects orders by past values of some of the dates contained therein.

mysql workbench duplicate key error

I have a data set with a list of country names, and the country names are repeated once for "Males" and then again for "Females".
For example:
c_name gender
China M
Greece M
Algeria M
China F
Greece F
Algeria F
When I create table and import data from a csv file, I get a 'duplicate key' error. I am wondering if this has anything to do with the engine settings? Any ideas how this can be resolved? (I know it works because my friend got it to work on her Mac, and she did not have the option to choose 'Collate' or 'Engine' when creating her tables, but I'm on Windows)
EDIT: Here's how I'm creating the table:
CREATE TABLE dbs.enrollment (
e_id INT NOT NULL,
c_name VARCHAR(45) NOT NULL,
gender VARCHAR(45) NULL,
2001 INT NULL,
2002 INT NULL,
2003 INT NULL,
2004 INT NULL,
2005 INT NULL,
2006 INT NULL,
2007 INT NULL,
2008 INT NULL,
2009 INT NULL,
2010 INT NULL,
PRIMARY KEY (e_id, c_name));
Your friend might set a primary key with c_name in this table ,if you want to have a change ,you can cancel the primary key。
It would be better to answer if you shared the query which is used for the above. I agree with Walker Li.
If your query is something like this..
CREATE TABLE enrollment (
c_name VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL,
PRIMARY KEY (c_name)
);
You can change it by removing the primary id line as
CREATE TABLE enrollment (
c_name VARCHAR(255) NOT NULL,
gender VARCHAR(255) NOT NULL
);
Hope this solves your problem.
If your csv data contains any unique column as ID you could use it as primary key as,
CREATE TABLE enrollment (
e_id INT NOT NULL,
gender VARCHAR(255) NOT NULL,
PRIMARY KEY (e_id)
);
A table can have only one primary key. Remove c_name in the last line and only have e_id as shown below
PRIMARY KEY (e_id);
The combination of e_id and c_name must be unique. If not you can create an additional artifical primary key as work around.
CREATE TABLE `enrollment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`e_id` int(11) NOT NULL,
`c_name` varchar(45) NOT NULL,
`gender` varchar(45) DEFAULT NULL,
`2001` int(11) DEFAULT NULL,
`2002` int(11) DEFAULT NULL,
`2003` int(11) DEFAULT NULL,
`2004` int(11) DEFAULT NULL,
`2005` int(11) DEFAULT NULL,
`2006` int(11) DEFAULT NULL,
`2007` int(11) DEFAULT NULL,
`2008` int(11) DEFAULT NULL,
`2009` int(11) DEFAULT NULL,
`2010` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Split non-relational table into multiple relational tables

I have the following table of 60000 rows, in a MySQL 5 database, which is derived from a CSV file:
CREATE TABLE `smts_import` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`AscCode` varchar(5) NOT NULL,
`AscName` varchar(50) NOT NULL,
`RgnCode` varchar(5) NOT NULL,
`RgnName` varchar(100) NOT NULL,
`SCode` varchar(30) NOT NULL,
`SName` varchar(100) NOT NULL,
`AM` int(11) NOT NULL,
`AF` int(11) NOT NULL,
`LG` decimal(10,4) NOT NULL,
`LT` decimal(10,4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ars_codes` (`AscCode`,`RgnCode`,`SCode`),
KEY `s_code` (`SCode`)
);
and which contains much repeated data. SCodes are unique, and the relationship between the RgnCode and SCode fields is many-to-one, as is that between the AscCode and RgnCode fields.
I want to split up (normalize) the data into three separate tables in a relational manner, such that no data are repeated:
CREATE TABLE `ascs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a_code` varchar(6) NOT NULL,
`a_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a_code` (`a_code`)
);
CREATE TABLE `rgns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a_id` int(11) NOT NULL,
`r_code` varchar(6) NOT NULL,
`r_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a_id`+`r_code` (`a_id`, `r_code`),
KEY `r_code` (`r_code`)
);
CREATE TABLE `sms` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`r_id` int(11) NOT NULL,
`s_code` varchar(16) NOT NULL,
`s_name` varchar(100) NOT NULL,
`a_m` int(11) NOT NULL,
`a_f` int(11) NOT NULL,
`lg` decimal(10,4) NOT NULL,
`lt` decimal(10,4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `r_id+s_code` (`r_id`,`s_code`),
KEY `s_code` (`s_code`)
);
where rgns.a_id is a foreign key on ascs.id, and sms.r_id is a foreign key on rgns.id.
I've created the three tables and successfully populated the first two, ascs and rgns, with unique data from the smts_import table. My problem comes when I try to populate the third table sms with just the SCode, SName, AM, AF, LG and LT fields from the smts_import table, PLUS the appropriate id from the rgns table. And here I just get lost, I've tried many variations on the following:
INSERT INTO sms (r_id, s_code, s_name, a_m, a_f, lg, lt)
SELECT DISTINCT sr.id, SCode, SName, AM, AF, LG, LT
FROM sms_import AS si, rgns AS sr, ascs AS sa
WHERE (sr.r_code = si.RgnCode)
AND (sr.a_id = sa.id)
AND (sa.a_code = si.AscCode)
ORDER BY SCode
but I just end up with too many records. How do I write this insert statement to get the appropriate fields from all records from the sms_import table, plus the correct values in the sms.r_id field from the rgns.id field?
Thanks for your help
"the relationship between the RgnCode and SCode fields is many-to-one"
So you also get many rgns.id per SCode.
If you select distinct rgns.id, SCode, etc. you will get each SCode more than once depending on how many RgnCode you have for it.
I would suggest adding a id column to the sms table and create a separate table sms2rgns which contains the one-to-many relations sms.id -> rgns.id

Should I create a parent table for shared attributes?

I am designing the database (MySQL) in which I have two tables Employees and Guests as following :
CREATE TABLE employee (
`EMP_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FIRST_NAME` VARCHAR(8) NOT NULL,
`MID_NAME` VARCHAR(11),
`LAST_NAME` VARCHAR(8) NOT NULL,
`BIRTHDAY` DATE,
`COUNTRY_ID` INT,
`NAT_ID` VARCHAR(8) NOT NULL,
`ID_EXP_DATE` DATE,
`ID_TYPE` VARCHAR(8) NOT NULL,
`Mobile` VARCHAR(8) NOT NULL,
`PHONE` VARCHAR(8) NOT NULL,
`EMAIL` VARCHAR(27) NOT NULL,
`DEPT_ID` TINYINT NOT NULL references DEPARTMENT (ID),
`POSITION` VARCHAR(20),
`EMP_TYPE` TINYINT NOT NULL references EMP_TYPES (type),
`JOINDATE` DATE,
`SALARY` MEDIUMINT DEFAULT 0 ,
`WORKEMAIL` VARCHAR(30),
`MARITALSTAT` VARCHAR(7),
`EMERGCONTACT` VARCHAR(22),
`EMERG_CONT_PHN` VARCHAR(11),
`GENDER` VARCHAR(6),
`RESUMEURL` VARCHAR(60),
`RELIGION` VARCHAR(11),
PRIMARY KEY (`EMP_ID`));
CREATE TABLE Guest (
`guest_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FIRST_NAME` VARCHAR(8) NOT NULL,
`MID_NAME` VARCHAR(11),
`LAST_NAME` VARCHAR(8) NOT NULL,
`BIRTHDAY` DATE,
`COUNTRY_ID` INT,
`NAT_ID` VARCHAR(8) NOT NULL,
`ID_EXP_DATE` DATE,
`ID_TYPE` VARCHAR(8) NOT NULL,
`Mobile` VARCHAR(8) NOT NULL,
`PHONE` VARCHAR(8) NOT NULL,
`EMAIL` VARCHAR(27) NOT NULL,
`WORKEMAIL` VARCHAR(30),
`MARITALSTAT` VARCHAR(7),
`EMERGCONTACT` VARCHAR(22),
`EMERG_CONT_PHN` VARCHAR(11),
`GENDER` VARCHAR(6),
`RELIGION` VARCHAR(11),
..................................// More attributes specific for guest table
PRIMARY KEY (`Guest_ID`));
Since both of the tables have auto generated primary keys and it would not be appropriate if they share primary key from person. would it be a good idea to create a table named person with all the common attributes and two child tables as Employee and Guest?
and what would be the best practice to implement this?
Thank You,
There are some things to consider:
Can an Employee be a Guest or vice versa?
How to compare them?
If this plays a role for your application, i´d consider a ParentTable, because so you can easily figure out, if an Employee is also a Guest or not. So you can easily compare them.
On the other hand, you could do a 3rd Option:
A Table called Personal Information.
An Employee has an Reference to this, Guests also.
If an Employee and an Guest both refeer to the same entry, you know they are the same person.
Schema:
yes the schema is like:
Table:
PersonalInformation
ID (Primary Key, auto inc)
And put all your information here
Table Employee
ID (Primary Key, auto inc int)
PersonalInformation_ID (ForeignKey to PersonalInformation.ID)
Add your Employee specific columns here
Table Guest
ID (Primary Key, auto inc int)
PersonalInformation_ID (ForeignKey to PersonalInformation.ID)
Add your Guest specific columns here
Hope this helps you implement it, otherwise ask again.