Not sure what i'm doing wrong here. Im trying to understand the relationships between tables? Please help thank you
DROP DATABASE IF EXISTS `sql_retail`;
CREATE DATABASE `sql_retail`;
USE `sql_retail`;
SET NAMES utf8 ;
SET character_set_client = utf8mb4 ;
CREATE TABLE `RETAIL_ORDER` (
`OrderNumber` int(4) NOT NULL,
`StoreNumber` int(2) NOT NULL,
`StoreZip` char(9) NOT NULL,
`OrderMonth` char(12) NOT NULL,
`OrderYear` int(4) NOT NULL,
`OrderTotal` float(10) NOT NULL,
PRIMARY KEY (`OrderNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `RETAIL_ORDER` VALUES (1000, 10, 98110, 'December', 2017, 445.00);
INSERT INTO `RETAIL_ORDER` VALUES (2000, 20, 02335, 'December', 2017, 310.00);
INSERT INTO `RETAIL_ORDER` VALUES (3000, 10, 98110, 'January', 2018, 480.00);
CREATE TABLE `ORDER_ITEM` (
`OrderNumber` int(11) NOT NULL,
`SKU` int(7) NOT NULL,
`Quantity` int(6) NOT NULL,
`Price` float(10) NOT NULL,
`Extended Price` float(10) NOT NULL,
PRIMARY KEY (`OrderNumber`),FOREIGN KEY(`OrderNumber`)REFERENCES RETAIL_ORDER(`OrderNumber`),
FOREIGN KEY(`SKU`)REFERENCES SKU_DATA(`SKU`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `ORDER_ITEM` VALUES (1000, 201000, 1, 300.00, 300.00);
INSERT INTO `ORDER_ITEM` VALUES (1000, 202000, 1, 130.00, 130.00);
INSERT INTO `ORDER_ITEM` VALUES (2000, 101100, 4, 50.00, 200.00);
INSERT INTO `ORDER_ITEM` VALUES (2000, 101200, 2, 50.00, 100.00);
INSERT INTO `ORDER_ITEM` VALUES (3000, 100200, 1, 300.00, 300.00);
INSERT INTO `ORDER_ITEM` VALUES (3000, 101100, 2, 50.00, 100.00);
INSERT INTO `ORDER_ITEM` VALUES (3000, 101200, 1, 50.00, 50.00);
CREATE TABLE `SKU_DATA` (
`SKU` int(11) NOT NULL,
`SKU_Description` varchar(50) NOT NULL,
`Department` varchar(20) NOT NULL,
`Buyer` varchar(15) NOT NULL,
PRIMARY KEY (`SKU`),FOREIGN KEY(`Buyer`)REFERENCES BUYER(`BuyerName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `SKU_DATA` VALUES (100100, 'Std. Scuba Tank, Yellow', 'Water Sports', 'Peter Hansen');
INSERT INTO `SKU_DATA` VALUES (100200, 'Std. Scuba Tank, Magenta', 'Water Sports', 'Peter Hansen');
INSERT INTO `SKU_DATA` VALUES (100300, 'Std. Scuba Tank, Light Blue', 'Water Sports', 'Peter Hansen');
INSERT INTO `SKU_DATA` VALUES (100400, 'Std. Scuba Tank, Dark Blue', 'Water Sports', 'Peter Hansen');
INSERT INTO `SKU_DATA` VALUES (100500, 'Std. Scuba Tank, Light Green', 'Water Sports', 'Peter Hansen');
INSERT INTO `SKU_DATA` VALUES (100600, 'Std. Scuba Tank, Dark Green', 'Water Sports', 'Peter Hansen');
INSERT INTO `SKU_DATA` VALUES (101100, 'Dive Mask, Small Clear', 'Water Sports', 'Nancy Meyers');
INSERT INTO `SKU_DATA` VALUES (101200, 'Dive Mask, Med Clear', 'Water Sports', 'Nancy Meyers');
INSERT INTO `SKU_DATA` VALUES (201000, 'Half dome tent', 'camping', 'Cindy Lo');
INSERT INTO `SKU_DATA` VALUES (202000, 'Half dome tent vestibule', 'camping', 'Cindy Lo');
INSERT INTO `SKU_DATA` VALUES (203000, 'Half dome tent vestibule wide', 'camping', 'Cindy Lo');
INSERT INTO `SKU_DATA` VALUES (301000, 'Light fly climbing harness', 'climbing', 'Jerry Martin');
INSERT INTO `SKU_DATA` VALUES (302000, 'Locking Carabiner, Oval', 'climbing', 'Jerry Martin');
CREATE TABLE `BUYER` (
`BuyerName` varchar(15) NOT NULL,
`Department` varchar(15) NOT NULL,
`Position` varchar(15) NOT NULL,
`Supervisor` varchar(15) NOT NULL,
PRIMARY KEY (`BuyerName`),FOREIGN KEY(`Supervisor`)REFERENCES BUYER(`BuyerName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `SKU_DATA` VALUES ('Cindy Lo', 'Purchasing', 'Buyer 2', 'Mary Smith');
INSERT INTO `SKU_DATA` VALUES ('Jerry Martin', 'Purchasing', 'Buyer 1', 'Cindo Lo');
INSERT INTO `SKU_DATA` VALUES ('Mary Smith', 'Purchasing', 'Manager','' );
INSERT INTO `SKU_DATA` VALUES ('Nancy Meyers', 'Purchasing', 'Buyer 1', 'Pete Hansen');
INSERT INTO `SKU_DATA` VALUES ('Pete Hansen', 'Purchasing', 'Buyer 3', 'Mary Smith');
You have some errors.
First you assign a foreign key to a table that don't exists yet and second you have duplicate values into your inserts for your primary keys.
I have fixed your table creation, check your insert datas.
Follow below approach to create your tables and then do the inserts:
DROP DATABASE IF EXISTS sql_retail ;
CREATE DATABASE sql_retail ;
USE sql_retail ;
SET NAMES utf8 ;
SET character_set_client = utf8mb4 ;
CREATE TABLE `RETAIL_ORDER` (
`OrderNumber` int(4) NOT NULL,
`StoreNumber` int(2) NOT NULL,
`StoreZip` char(9) NOT NULL,
`OrderMonth` char(12) NOT NULL,
`OrderYear` int(4) NOT NULL,
`OrderTotal` float(10) NOT NULL,
PRIMARY KEY (`OrderNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `ORDER_ITEM` (
`OrderNumber` int(11) NOT NULL,
`SKU` int(7) NOT NULL,
`Quantity` int(6) NOT NULL,
`Price` float(10) NOT NULL,
`Extended Price` float(10) NOT NULL,
PRIMARY KEY (`OrderNumber`),
FOREIGN KEY(`OrderNumber`)REFERENCES RETAIL_ORDER(`OrderNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `SKU_DATA` (
`SKU` int(11) NOT NULL,
`SKU_Description` varchar(50) NOT NULL,
`Department` varchar(20) NOT NULL,
`Buyer` varchar(15) NOT NULL,
PRIMARY KEY (`SKU`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE ORDER_ITEM ADD FOREIGN KEY(`SKU`)REFERENCES SKU_DATA(`SKU`);
CREATE TABLE `BUYER` (
`BuyerName` varchar(15) NOT NULL,
`Department` varchar(15) NOT NULL,
`Position` varchar(15) NOT NULL,
`Supervisor` varchar(15) NOT NULL,
PRIMARY KEY (`BuyerName`),
FOREIGN KEY(`Supervisor`)REFERENCES BUYER(`BuyerName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE SKU_DATA ADD FOREIGN KEY(`Buyer`)REFERENCES BUYER(`BuyerName`);
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/28
Related
This question already has answers here:
MySQL Error 1215: Cannot add foreign key constraint
(29 answers)
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 2 years ago.
I want to add foreign key like this
and this is my table user_idea
CREATE TABLE IF NOT EXISTS `user_idea` (
`idea_id` int(5) NOT NULL AUTO_INCREMENT,
`user_id` varchar(30) DEFAULT NULL,
`title` varchar(40) DEFAULT NULL,
`innovators` varchar(30) DEFAULT NULL,
`idea_categories` varchar(30) DEFAULT NULL,
`status` int(5) DEFAULT NULL,
`description` varchar(50) DEFAULT NULL,
PRIMARY KEY (`idea_id`),
KEY `innovators` (`innovators`),
KEY `idea_categories` (`idea_categories`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
--
-- Dumping data untuk tabel `user_idea`
--
INSERT INTO `user_idea` (`idea_id`, `user_id`, `title`, `innovators`, `idea_categories`, `status`, `description`) VALUES
(1, 'jack', 'Video Annotations', 'jack', '1;2', 1, 'Video Annotations Description'),
(2, 'jack', 'Optimize waterfall model', 'jack;jackson', '3', 0, 'Optimize waterfall model Description'),
(3, 'jackson', 'Automation', 'jackson', '1', 1, 'Automation Description'),
(4, 'jackson', 'Design Patterns', 'jackson', '1', 0, 'Design Patterns Description'),
(5, 'alice', 'Identify Video Objects', 'alice;jack', '2', 1, 'Identify Video Objects Description'),
(6, 'bob', 'Tin Can LMS', 'bob', '1', 1, 'Tin Can LMS Description'),
(7, 'bob', 'Text Summarization', 'bob', '2;3', 0, 'Text Summarization Description');
--
-- Ketidakleluasaan untuk tabel pelimpahan (Dumped Tables)
--
--
-- Ketidakleluasaan untuk tabel `user_idea`
--
ALTER TABLE `user_idea`
ADD CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`id`);
my table idea_category
enter image description here
CREATE TABLE IF NOT EXISTS `idea_categories` (
`category_id` int(5) NOT NULL AUTO_INCREMENT,
`category_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `idea_categories` (`category_id`, `category_name`) VALUES
(1, 'Project Lifecycle'),
(2, 'Video'),
(3, 'Language Analysis');
My table user_info:
enter image description here
CREATE TABLE IF NOT EXISTS `user_info` (
`id` varchar(30) NOT NULL,
`full_name` varchar(30) DEFAULT NULL,
`dep_id` int(5) DEFAULT NULL,
`point` int(5) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `dep_id` (`dep_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `user_info` (`id`, `full_name`, `dep_id`, `point`) VALUES
('alice', 'Alice W', 2, 1),
('bob', 'Bob S.', 2, 2),
('jack', 'JACK D.', 2, 2),
('jackson', 'M.S.Jackson', 3, 3);
but I got an error cannot add foreign key? Please help
This question already has answers here:
How can I add a foreign key when creating a new table?
(5 answers)
Closed 4 years ago.
I am trying creating two tables, aluno = student and usuario = user. MySQL Workbench keep showing me that codigo in table usario doesn´t exist. Anyone can help me plz?
drop database `web2`;
CREATE DATABASE `web2` DEFAULT CHARSET latin1;
USE `web2`;
CREATE TABLE `aluno` (
`id_aluno` bigint(20) not null auto_increment,
`nome` varchar(100) not null,
`cpf` varchar(20) not null,
`rg` varchar(20) not null,
`dataDeNascimento` date not null,
`endereco` varchar(50),
`cidade` varchar(150),
`telefoneFixo` varchar(14),
`telefoneCelular` varchar(14),
`email` varchar(30) not null,
PRIMARY KEY (`id_aluno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
`telefoneCelular`,`email`) VALUES ('1', 'Marcela', '255665696363', '2153263699',
'1985-07-08', 'Rua Hum', 'Belo Horizonte', '(35)54321-9876', '(35)54321-9876','marcela#bh.mg');
INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
`telefoneCelular`,`email`) VALUES ('2', 'Paulo', '275865696361', '2183255599','1983-02-05', 'Rua Dois', 'Bela Vista', '(11)12345-6789',
'(11)12345-6789', 'paulo#saopaulo.sp');
INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
`telefoneCelular`,`email`) VALUES ('3', 'Marcos', '275812656361', '2183255599','1983-02-12', 'Rua Dois', 'Bela Vista', '(11)12345-6790',
'(11)12345-6789', 'paulo#saopaulo.sp');
INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
`telefoneCelular`,`email`) VALUES ('4', 'Rodolfo', '569865696361', '2183255599','1983-05-28', 'Rua Dois', 'Bela Vista', '(11)12345-6791',
'(11)12345-6789', 'paulo#saopaulo.sp');
INSERT INTO `aluno` (`id_aluno`, `nome`, `cpf`, `rg`,`dataDeNascimento`, `endereco`, `cidade`, `telefoneFixo`,
`telefoneCelular`,`email`) VALUES ('5', 'Larissa', '275865696361', '2183255599','1983-02-01', 'Rua Dois', 'Bela Vista', '(11)12345-6792',
'(11)12345-6789', 'paulo#saopaulo.sp');
CREATE TABLE `usuario` (
`id` bigint(20) not null auto_increment,
`login` varchar(10) not null,
`senha` varchar(10) not null,
PRIMARY KEY (`id`),
FOREIGN KEY (`codigo`) REFERENCES aluno (`id_aluno`),
UNIQUE KEY (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('1', 'marcela', '54321', '1');
INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('2', 'paulo', '12345', '2');
INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('3', 'gustavo', '52321', '3');
INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('4', 'leandro', '19315', '4');
INSERT INTO `usuario` (`id`, `login`, `senha`, `codigo` ) VALUES ('5', 'bruna', '14045', '5');
Are you missing the "codigo" column name in "CREATE TABLE usuario"
...
`codigo` bigint(20)
...
You need codigo defined in create table statement:
CREATE TABLE `usuario` (
`id` bigint(20) not null auto_increment,
`login` varchar(10) not null,
`senha` varchar(10) not null,
`codigo` bigint(20)
PRIMARY KEY (`id`),
FOREIGN KEY (`codigo`) REFERENCES aluno (`id_aluno`),
UNIQUE KEY (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
The reason for this is that foreign keys are constraints applied to columns, so the table first needs the column to which the foreign key constraint is applied.
I have a database of the following relations:
Bar(Name, Address, Licence)
Beer(Name,Manufacture)
Drinker(Name,Address)
Frequents(DrinkerName,BarName)
Likes(DrinkerName,BeerName)
Sells(BarName,BeerName,Amount)
Serves(BarName,BeerName)
The Sample DDL Statements:
CREATE TABLE `Bar` (
`Name` varchar(255) NOT NULL,
`Address` varchar(255) DEFAULT NULL,
`Licence` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Bar` (`Name`, `Address`, `Licence`) VALUES
('Deluxe', 'Luxvagen 2', 'Yes'),
('Grace', 'Gracevagen 2', 'Yes'),
('KrogBar', 'Barvagen 2', 'Yes');
CREATE TABLE `Beer` (
`Name` varchar(255) NOT NULL,
`Manufacture` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Beer` (`Name`, `Manufacture`) VALUES
('Carlsberg', 'Coppers'),
('Heiniken', 'Spendrups'),
('Miller', 'DaMill');
CREATE TABLE `Boor` (
`Name` varchar(255) NOT NULL,
`Age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Drinker` (
`Name` varchar(255) NOT NULL,
`Address` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Drinker` (`Name`, `Address`) VALUES
('Alex', 'Överbar 2'),
('Bam', 'Påbar 2'),
('Emil', 'Mittibar 2'),
('Max', 'Ibar 2'),
('Petra', 'Förebar 2'),
('Rebecca', 'Efterbar 2'),
('Sam', 'Underbar 2');
CREATE TABLE `Frequents` (
`DrinkerName` varchar(255) NOT NULL DEFAULT '',
`BarName` varchar(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Frequents` (`DrinkerName`, `BarName`) VALUES
('Emil', 'Deluxe'),
('Max', 'Deluxe'),
('Rebecca', 'Deluxe'),
('Alex', 'Grace'),
('Petra', 'Grace'),
('Bam', 'KrogBar'),
('Sam', 'KrogBar');
CREATE TABLE `Likes` (
`DrinkerName` varchar(255) NOT NULL DEFAULT '',
`BeerName` varchar(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Likes` (`DrinkerName`, `BeerName`) VALUES
('Bam', 'Carlsberg'),
('Emil', 'Carlsberg'),
('Rebecca', 'Carlsberg'),
('Emil', 'Heiniken'),
('Max', 'Heiniken'),
('Petra', 'Heiniken'),
('Sam', 'Heiniken'),
('Alex', 'Miller');
CREATE TABLE `Sells` (
`BarName` varchar(100) DEFAULT NULL,
`BeerName` varchar(100) DEFAULT NULL,
`Amount` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Sells` (`BarName`, `BeerName`, `Amount`) VALUES
('KrogBar', 'Miller', 3),
('KrogBar', 'Carlsberg', 2),
('KrogBar', 'Heiniken', 1),
('Deluxe', 'Heiniken', 1);
CREATE TABLE `Serves` (
`BarName` varchar(255) NOT NULL DEFAULT '',
`BeerName` varchar(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Serves` (`BarName`, `BeerName`) VALUES
('Grace', 'Carlsberg'),
('KrogBar', 'Carlsberg'),
('Deluxe', 'Heiniken'),
('Grace', 'Heiniken'),
('KrogBar', 'Heiniken'),
('KrogBar', 'Miller');
I want to find drinkers that frequent only bars that serve beer that they like (the assumption is that each drinker frequents at least one bar).
How can I construct such a query? I know that I have to use Joins and Sub-queries, I am not new to either of them but all my implementations have not yielded the correct results.
Is this what you looking for;
Select DISTINCT bar.Name,bar.Address,frequents.DrinkerName, likes.BeerName
From
Bar
Join frequents on bar.Name = frequents.BarName
join sells on bar.name = sells.BarName
join likes on frequents.DrinkerName = likes.DrinkerName
ALTER TABLE `charter`
ADD CONSTRAINT `charter_ibfk_3` FOREIGN KEY (`CHAR_DESTINATION`)
REFERENCES `airport` (`AIRPORT_CODE`);
I am attempting to create this relationship:
Charter and Airport ERD Diagram
CREATE TABLE `antonellacammarota` (
`CUS_CODE` int(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`CUS_CODE`),
FOREIGN KEY (`CUS_CODE`) REFERENCES `CUSTOMER` (`CUS_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert into customer values (10076, "Cammarota", "Antonella", "T", "805", "555-1212", 0);
insert into antonellacammarota(select cus_code from customer where cus_lname = 'Cammarota');
--
-- Table structure for table `airport`
--
CREATE TABLE IF NOT EXISTS `airport` (
`AIRPORT_CODE` varchar(3) NOT NULL DEFAULT '0',
`AIRPORT_NAME` varchar(40) DEFAULT NULL,
`AIRPORT_ADDRESS` varchar(45) DEFAULT NULL,
`AIRPORT_CITY` varchar(25) DEFAULT NULL,
`AIRPORT_STATE` char(2) DEFAULT NULL,
`AIRPORT_ZIP` varchar(10) DEFAULT NULL,
`AIRPORT_COUNTRY` varchar(25) DEFAULT NULL,
`AIRPORT_AREACODE` varchar(3) DEFAULT NULL,
`AIRPORT_PHONE` varchar(8) DEFAULT NULL,
PRIMARY KEY (`AIRPORT_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Constraints for table `airport`
--
ALTER TABLE `charter`
ADD CONSTRAINT `charter_ibfk_3` FOREIGN KEY (`CHAR_DESTINATION`) REFERENCES `airport` (`AIRPORT_CODE`);
--
-- Dumping data for table `airport`
--
INSERT INTO `airport` (`AIRPORT_CODE`, `AIRPORT_NAME`, `AIRPORT_ADDRESS`, `AIRPORT_CITY`, `AIRPORT_STATE`,
`AIRPORT_ZIP`, `AIRPORT_COUNTRY`, `AIRPORT_AREACODE`, `AIRPORT_PHONE`) VALUES
('ATL', 'Hartsfield-Jackson Atlanta Int', '6000 N Terminal Pkwy.', 'Atlanta', 'GA', '30320', 'US', '808', '897-1910'),
('BNA', 'Nashville Int', '1 Terminal Dr.', 'Nashville', 'TN', '37214', 'US', '615', '275-1675'),
('GNV', 'Gainesville Regional', '3880 NE 39th Ave.', 'Gainesville', 'FL', '32609', 'US', '352', '373-0249'),
('MOB', 'Mobile Regional', '8400 Airport Blvd.', 'Mobile', 'AL', '36608', 'US', '800', '357-5373'),
('MOY', 'Monterrey', '', 'Monterrey', '', '', 'Columbia', '', ''),
('STL', 'St. Louis Lambert Int', '10701 Lambert International Blvd.', 'St. Louis', 'MO', '63145', 'US', '314', '426-8000'),
('TYS', 'McGhee Tyson', '2055 Alcoa Hwy', 'Alcoa', 'TN', '37701', 'US', '865', '345-3000');
Use this to find out what entry is screwing things up
SELECT CHAR_TRIP, CHAR_DESTINATION
FROM charter
WHERE CHAR_DESTINATION NOT IN (
SELECT AIRPORT_CODE FROM airport);
I am using the following sql statement to sum values from two columns from two different tables. The statement can output but not the desired result.
SELECT
SUM(`_income`.rate) AS Income,
SUM(`_expense`.rate) AS Expense,
SUM(_income.rate)-SUM(_expense.rate) AS Balance
FROM `_expense`, `_income`
My table is here:
CREATE TABLE IF NOT EXISTS `_expense` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`qnty` int(11) NOT NULL,
`rate` int(11) NOT NULL,
`date` date NOT NULL,
`CreatedByPHPRunner` int(11) NOT NULL,
`remarks` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table _expense
INSERT INTO `_expense` (`id`, `item`, `qnty`, `rate`, `date`, `CreatedByPHPRunner`, `remarks`) VALUES
(2, 'Maian', 2, 20, '2013-08-15', 0, 'A tui kher mai'),
(3, 'Battery', 1, 2100, '2013-08-15', 0, 'A lian chi');
--
-- Table structure for table _income
CREATE TABLE IF NOT EXISTS `_income` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`items` varchar(100) DEFAULT NULL,
`qnty` int(11) DEFAULT NULL,
`rate` int(11) DEFAULT NULL,
`date` date DEFAULT NULL,
`remarks` varchar(255) DEFAULT NULL,
`CreatedByPHPRunner` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table _income
INSERT INTO `_income` (`id`, `items`, `qnty`, `rate`, `date`, `remarks`, `CreatedByPHPRunner`) VALUES
(1, 'TV chhe siam', 1, 1500, '2013-08-15', 'Ka hniam hrep', NULL),
(2, 'First Star', 1, 25, '2013-08-15', 'A loose-in aw', NULL),
(3, 'Mobile Chhe siam', 2, 200, '2013-08-13', 'Nokia chhuak ho a nia', NULL),
(4, 'Internet hman man', 1, 1500, '2013-08-14', 'Ka net min hman sak a', NULL);
This should do it:
select income, expense, income-expense balance
from (select sum(rate) income
from _income) i
JOIN (select sum(rate) expense
from _expense) e