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.
Related
For some reason my code won't let me create the projects table due to the foreign key error. I have tried a few different things and just cant seem to get it to work, ive tried looking on here for solutions but cant seem to do it. Any help would be much appreciated.
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
FOREIGN KEY(PROJECT_NAME) REFERENCES PEOPLE(PROJECT)
);
Presumably, you want the foreign key the other way around. You would expect people to reference projects instead of projects to reference people.
This means that you need to create the projects table first, and then the people table. Also, you need a proper primary key on projects so you can reference it in people (I assumed project_name).
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
PRIMARY KEY (PROJECT_NAME)
);
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT),
FOREIGN KEY(PROJECT) REFERENCES PROJECTS(PROJECT_NAME)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
Demo on DB Fiddle
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.
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;
I am trying TPCH DDL queries on memsql. I am new to memsql. I am not able to convert 5 TPCH ddl sql queries to memsql queries. Not able to achieve foreign key relationship using memsql's FOREIGH SHARD KEY concept. Please help me to covert below 5 out of 8 table creation queries into memsql queries. Tried hard but face lot of different issues.
CREATE TABLE REGION ( R_REGIONKEY INTEGER NOT NULL PRIMARY KEY,
R_NAME CHAR(25) NOT NULL,
R_COMMENT VARCHAR(152)
);
CREATE TABLE NATION ( N_NATIONKEY INTEGER NOT NULL PRIMARY KEY,
N_NAME CHAR(25) NOT NULL,
N_REGIONKEY INTEGER NOT NULL REFERENCES REGION(R_REGIONKEY),
N_COMMENT VARCHAR(152)
);
CREATE TABLE PART ( P_PARTKEY INTEGER NOT NULL PRIMARY KEY,
P_NAME VARCHAR(55) NOT NULL,
P_MFGR CHAR(25) NOT NULL,
P_BRAND CHAR(10) NOT NULL,
P_TYPE VARCHAR(25) NOT NULL,
P_SIZE INTEGER NOT NULL,
P_CONTAINER CHAR(10) NOT NULL,
P_RETAILPRICE DECIMAL(15,2) NOT NULL,
P_COMMENT VARCHAR(23) NOT NULL
);
CREATE TABLE SUPPLIER ( S_SUPPKEY INTEGER NOT NULL PRIMARY KEY,
S_NAME CHAR(25) NOT NULL,
S_ADDRESS VARCHAR(40) NOT NULL,
S_NATIONKEY INTEGER NOT NULL REFERENCES NATION(N_NATIONKEY),
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL
);
CREATE TABLE PARTSUPP ( PS_PARTKEY INTEGER NOT NULL REFERENCES PART(P_PARTKEY),
PS_SUPPKEY INTEGER NOT NULL REFERENCES SUPPLIER(S_SUPPKEY),
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY, PS_SUPPKEY)
);
CREATE TABLE CUSTOMER ( C_CUSTKEY INTEGER NOT NULL PRIMARY KEY,
C_NAME VARCHAR(25) NOT NULL,
C_ADDRESS VARCHAR(40) NOT NULL,
C_NATIONKEY INTEGER NOT NULL REFERENCES NATION(N_NATIONKEY),
C_PHONE CHAR(15) NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL
);
CREATE TABLE ORDERS ( O_ORDERKEY INTEGER NOT NULL PRIMARY KEY,
O_CUSTKEY INTEGER NOT NULL REFERENCES CUSTOMER(C_CUSTKEY),
O_ORDERSTATUS CHAR(1) NOT NULL,
O_TOTALPRICE DECIMAL(15,2) NOT NULL,
O_ORDERDATE DATE NOT NULL,
O_ORDERPRIORITY CHAR(15) NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL
);
CREATE TABLE LINEITEM ( L_ORDERKEY INTEGER NOT NULL REFERENCES ORDERS(O_ORDERKEY),
L_PARTKEY INTEGER NOT NULL REFERENCES PART(P_PARTKEY),
L_SUPPKEY INTEGER NOT NULL REFERENCES SUPPLIER(S_SUPPKEY),
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY DECIMAL(15,2) NOT NULL,
L_EXTENDEDPRICE DECIMAL(15,2) NOT NULL,
L_DISCOUNT DECIMAL(15,2) NOT NULL,
L_TAX DECIMAL(15,2) NOT NULL,
L_RETURNFLAG CHAR(1) NOT NULL,
L_LINESTATUS CHAR(1) NOT NULL,
L_SHIPDATE DATE NOT NULL,
L_COMMITDATE DATE NOT NULL,
L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
FOREIGN KEY (L_PARTKEY,L_SUPPKEY) REFERENCES PARTSUPP(PS_PARTKEY, PS_SUPPKEY)
);
I am able to create first 3 tables in memsql but not able to remaining tables.1st and 3rd queries are very simple and worked as it is. I am able to create 2nd table but again not sure whether this is right way to achieve.
CREATE TABLE NATION ( N_NATIONKEY INTEGER NOT NULL,
N_NAME CHAR(25) NOT NULL,
N_REGIONKEY INTEGER NOT NULL,
N_COMMENT VARCHAR(152),
FOREIGN SHARD KEY (N_REGIONKEY) REFERENCES REGION (R_REGIONKEY),
PRIMARY KEY (N_NATIONKEY, N_REGIONKEY)
);
Is it possible to create only Replicate table and not partition in memsql? and how?
Since MemSQL does not support referential integrity, foreign shard keys are an optimization aid and not necessary. Foreign shard keys though do allow you to know at table creation time that two tables may be joined locally (no network traffic) on that key. However, the optimizer does not require foreign shard keys to take advantage of this data locality.
Starting with the ORDERS and LINEITEM tables:
CREATE TABLE ORDERS ( O_ORDERKEY INTEGER NOT NULL PRIMARY KEY,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS CHAR(1) NOT NULL,
O_TOTALPRICE DECIMAL(15,2) NOT NULL,
O_ORDERDATE DATE NOT NULL,
O_ORDERPRIORITY CHAR(15) NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
KEY (O_CUSTKEY)
);
CREATE TABLE LINEITEM ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY DECIMAL(15,2) NOT NULL,
L_EXTENDEDPRICE DECIMAL(15,2) NOT NULL,
L_DISCOUNT DECIMAL(15,2) NOT NULL,
L_TAX DECIMAL(15,2) NOT NULL,
L_RETURNFLAG CHAR(1) NOT NULL,
L_LINESTATUS CHAR(1) NOT NULL,
L_SHIPDATE DATE NOT NULL,
L_COMMITDATE DATE NOT NULL,
L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
FOREIGN SHARD KEY (L_ORDERKEY) REFERENCES ORDERS (O_ORDERKEY),
KEY (L_PARTKEY),
KEY (L_SUPPKEY)
);
In this case we know we can take advantage of a local join between ORDERS and LINEITEM, because they are both sharded on ORDERKEY. ORDERS and LINEITEM are the two biggest tables in TPCH, so we want to ensure that they can be joined locally. Since ORDERS' primary key is O_ORDERKEY, I do not need to specify a shard key for ORDERS. MemSQL will shard by O_ORDERKEY automatically.
I've also put secondary indexes on the remaining foreign key columns. This is useful since there will be joins on the foreign keys.
Applying these concepts to PART, PARTSUPP, SUPPLIER, and CUSTOMER:
CREATE TABLE CUSTOMER ( C_CUSTKEY INTEGER NOT NULL PRIMARY KEY,
C_NAME VARCHAR(25) NOT NULL,
C_ADDRESS VARCHAR(40) NOT NULL,
C_NATIONKEY INTEGER NOT NULL,
C_PHONE CHAR(15) NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL,
KEY(C_NATIONKEY)
);
CREATE TABLE SUPPLIER ( S_SUPPKEY INTEGER NOT NULL PRIMARY KEY,
S_NAME CHAR(25) NOT NULL,
S_ADDRESS VARCHAR(40) NOT NULL,
S_NATIONKEY INTEGER NOT NULL,
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL,
KEY(S_NATIONKEY)
);
CREATE TABLE PART ( P_PARTKEY INTEGER NOT NULL PRIMARY KEY,
P_NAME VARCHAR(55) NOT NULL,
P_MFGR CHAR(25) NOT NULL,
P_BRAND CHAR(10) NOT NULL,
P_TYPE VARCHAR(25) NOT NULL,
P_SIZE INTEGER NOT NULL,
P_CONTAINER CHAR(10) NOT NULL,
P_RETAILPRICE DECIMAL(15,2) NOT NULL,
P_COMMENT VARCHAR(23) NOT NULL
);
CREATE TABLE PARTSUPP ( PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY, PS_SUPPKEY),
SHARD KEY(PS_PARTKEY),
KEY(PS_SUPPKEY)
);
Although PARTSUPP and PART are both sharded on the same key (PARTKEY), I do not need to specify a foreign shard key in order to take advantage of a local join between them on that key; the optimizer will pick that up automatically.
In response to your final question, MemSQL does allow you create a replicated table instead of a partitioned table. This is called a reference table and will be useful for the NATION and REGION tables since they are very small. Reference tables are not necessary to run distributed queries, but are a useful optimization.
CREATE REFERENCE TABLE REGION ( R_REGIONKEY INTEGER NOT NULL PRIMARY KEY,
R_NAME CHAR(25) NOT NULL,
R_COMMENT VARCHAR(152)
);
CREATE REFERENCE TABLE NATION ( N_NATIONKEY INTEGER NOT NULL PRIMARY KEY,
N_NAME CHAR(25) NOT NULL,
N_REGIONKEY INTEGER NOT NULL,
N_COMMENT VARCHAR(152)
);
For more documentation on everything described, check out:
http://docs.memsql.com/latest/concepts/distributed_sql/
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 ....