MySQL insert fails - mysql

I am trying to create and insert into 2 tables on sqlfiddle.
CREATE TABLE IF NOT EXISTS `Submissions` (
`sub_id` int(6) unsigned NOT NULL,
`hacker_id` int(3) unsigned NOT NULL,
`score` int(3) NOT NULL,
`sub_date` Date NOT NULL,
PRIMARY KEY (`sub_id`,`hacker_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Submissions` (`sub_id`, `hacker_id`, `score`, `sub_date`) VALUES
('18833', '962', '12', '2019-12-07'),
('35892', '962', '45', '2019-12-07');
CREATE TABLE IF NOT EXISTS `Hacker` (
`hacker_id` int(10) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY(`hacker_id`, `name`)
)
INSERT INTO `Hacker` (`hacker_id`, `name`) VALUES
('123','Bob');
The insert into Submissions works fine. I do not understand what is wrong with my Insert into Hacker which fails.
Fiddle is here: http://sqlfiddle.com/#!9/d4d786 but refreshing drops the second insert, I'm guessing because it fails to compile/build/whatever the term is in SQL.

You are missing a ; after your second CREATE TABLE statement. It should look like this:
CREATE TABLE IF NOT EXISTS `Submissions` (
`sub_id` int(6) unsigned NOT NULL,
`hacker_id` int(3) unsigned NOT NULL,
`score` int(3) NOT NULL,
`sub_date` Date NOT NULL,
PRIMARY KEY (`sub_id`,`hacker_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Submissions` (`sub_id`, `hacker_id`, `score`, `sub_date`) VALUES
('18833', '962', '12', '2019-12-07'),
('35892', '962', '45', '2019-12-07');
CREATE TABLE IF NOT EXISTS `Hacker` (
`hacker_id` int(10) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY(`hacker_id`, `name`)
);
INSERT INTO `Hacker` (`hacker_id`, `name`) VALUES
('123','Bob');

Related

Multiple Joins With Condition

I have the following schema
CREATE TABLE IF NOT EXISTS `params` (
`id` int(6) unsigned NOT NULL,
`parameter` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `entry` (
`id` int(6) unsigned NOT NULL,
`param_id` int(6) unsigned NOT NULL,
`value` int(6) unsigned NOT NULL,
`access_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `enabled` (
`id` int(6) unsigned NOT NULL,
`param_id` int(6) unsigned NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `params` (`id`, `parameter`) VALUES
('1','Height'),
('2', 'Weight'),
('3', 'Texture'),
('4', 'Colour');
INSERT INTO `entry` (`id`, `param_id`,`value`,`access_id`) VALUES
('1','1','5.2','1'),
('2','2','80','2'),
('3','1','6','2');
INSERT INTO `enabled` (`id`, `param_id`) VALUES
('1','1'),('2','2');
I am trying to get all parameter values with access_id of 1 from the entry table
OR
All Parameters that are enabled which (DOES NOT exist in the entry table with access_idof 1)
So the output should be
id parameter value access_id
1 Height 5 1
2 Weight NULL NULL
The second entry here has a value of null because although it is enabled it does not have an access_id of 1 in the entry table
The schema here http://sqlfiddle.com/#!9/103016
select p.*, e.value, e.access_id from params p
inner join entry e on p.id= e.param_id and e.access_id = 1
union
select p.*, null,null from params p
inner join enabled enb on p.id=enb.param_id
where enb.param_id not in (select param_id from entry where access_id = 1)

Why don't I have all permissions when I am logged on as root?

This is a GCP MySQL Cloud instance, MySQL 2nd Gen 5.7. Google installed and configured it.
I am logged in as root:
When I run a script to create schemas, tables, and views, I get this error:
Error Code: 1227. Access denied; you need (at least one of) the SUPER privilege(s) for this operation
Here are the permissions assigned to root:
Here is the SQL that fails, in the form of a stored procedure:
CREATE DEFINER=`root`#`%` PROCEDURE `CreateFullCoverageTestCase`()
BEGIN
-- Create all the elements of the Full Coverage Test Case
-- *********************
-- Schema Store
-- *********************
Begin
DROP Schema if exists `store`;
END;
BEGIN
CREATE SCHEMA `store` ;
CREATE TABLE `store`.`city` (
`CityID` int(11) NOT NULL AUTO_INCREMENT,
`City` varchar(45) NOT NULL,
PRIMARY KEY (`CityID`),
UNIQUE KEY `City_UNIQUE` (`City`));
INSERT INTO store.city (City) VALUES('Cincinnati');
INSERT INTO store.city (City) VALUES('Columbus');
INSERT INTO store.city (City) VALUES('Kokomo');
INSERT INTO store.city (City) VALUES('Hillsdale');
INSERT INTO store.city (City) VALUES('Lexington');
INSERT INTO store.city (City) VALUES('Oxford');
CREATE TABLE `store`.`state` (
`StateID` int(11) NOT NULL AUTO_INCREMENT,
`State` varchar(45) NOT NULL,
`StateAbbreviation` varchar(2) NOT NULL,
PRIMARY KEY (`StateID`),
UNIQUE KEY `StateAbbreviation_UNIQUE` (`StateAbbreviation`),
UNIQUE KEY `State_UNIQUE` (`State`),
UNIQUE KEY `StateID_UNIQUE` (`StateID`));
INSERT INTO store.state (State, StateAbbreviation) VALUES('Indiana', 'IN');
INSERT INTO store.state (State, StateAbbreviation) VALUES('Kentucky', 'KY');
INSERT INTO store.state (State, StateAbbreviation) VALUES('Michigan', 'MI');
INSERT INTO store.state (State, StateAbbreviation) VALUES('Ohio', 'OH');
CREATE TABLE `store`.`store` (
`StoreID` int(11) NOT NULL AUTO_INCREMENT,
`StoreNumber` varchar(10) NOT NULL,
`AddressLine1` varchar(45) NOT NULL,
`AddressLine2` varchar(45) DEFAULT NULL,
`CityID` int(11) NOT NULL,
`StateID` int(11) NOT NULL,
`ZipCode` varchar(10) NOT NULL,
PRIMARY KEY (`StoreID`),
UNIQUE KEY `StoreNumber_UNIQUE` (`StoreNumber`));
INSERT INTO store.store (StoreNumber, AddressLine1, CityID, StateID, ZipCode) VALUES('S000000001', '111 Main', 1, 4, '45255-1321');
INSERT INTO store.store (StoreNumber, AddressLine1, CityID, StateID, ZipCode) VALUES('S000000002', '817 Nordyke', 1, 4, '45103-0000');
INSERT INTO store.store (StoreNumber, AddressLine1, CityID, StateID, ZipCode) VALUES('S000000003', '123 Green', 6, 4, '45056-0000');
End;
-- *********************
-- Schema HR (Human Resources)
-- *********************
BEGIN
DROP Schema if exists `hr`;
END;
BEGIN
CREATE SCHEMA `hr` ;
CREATE TABLE `hr`.`employee` (
`EmployeeID` INT NOT NULL AUTO_INCREMENT,
`LastName` VARCHAR(45) NOT NULL,
`FirstName` VARCHAR(45) NOT NULL,
`EmployeeNumber` VARCHAR(10) NOT NULL,
`AddressLine1` VARCHAR(45) NOT NULL,
`AddressLine2` VARCHAR(45) NULL,
`City` VARCHAR(45) NOT NULL,
`State` VARCHAR(2) NOT NULL,
`ZipCode` VARCHAR(10) NOT NULL,
PRIMARY KEY (`EmployeeID`),
UNIQUE INDEX `EmployeeNumber_UNIQUE` (`EmployeeNumber` ASC));
INSERT INTO hr.employee (lastName, firstName, EmployeeNumber, AddressLine1, City, State, ZipCode) VALUES('Cheaney', 'Calbert', 'E000000001', '17th Street', 'Bloomington', 'IN', '47405');
INSERT INTO hr.employee (lastName, firstName, EmployeeNumber, AddressLine1, City, State, ZipCode) VALUES('Reynolds', 'Chris', 'E000000002', 'Fee Street', 'Bloomington', 'IN', '47405');
END;
-- *********************
-- Products for sale
-- *********************
BEGIN
DROP Schema if exists `product`;
END;
BEGIN
CREATE SCHEMA `product` ;
CREATE TABLE `product`.`manufacturer` (
`ManufacturerID` int(11) NOT NULL AUTO_INCREMENT,
`Manufacturer` varchar(45) NOT NULL,
`Comment` varchar(1000) DEFAULT NULL,
`AddressLine01` varchar(45) DEFAULT NULL,
`AddressLine02` varchar(45) DEFAULT NULL,
`City` varchar(45) DEFAULT NULL,
`State` varchar(45) DEFAULT NULL,
`ZipCode` varchar(10) DEFAULT NULL,
`Phone` varchar(13) DEFAULT NULL,
`Manufacturercol` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ManufacturerID`),
UNIQUE KEY `Manufacturer_UNIQUE` (`Manufacturer`));
INSERT INTO product.manufacturer (Manufacturer) VALUES('Smuckers');
INSERT INTO product.manufacturer (Manufacturer) VALUES('Dannon');
INSERT INTO product.manufacturer (Manufacturer) VALUES('Kellogs');
INSERT INTO product.manufacturer (Manufacturer) VALUES('Pepsi');
CREATE TABLE `product`.`unit` (
`UnitID` INT NOT NULL AUTO_INCREMENT,
`Unit` VARCHAR(45) NOT NULL,
PRIMARY KEY (`UnitID`),
UNIQUE INDEX `Unit_UNIQUE` (`Unit` ASC));
INSERT INTO product.unit(Unit) VALUES('Box');
INSERT INTO product.unit(Unit) VALUES('Bag');
INSERT INTO product.unit(Unit) VALUES('Carton');
INSERT INTO product.unit(Unit) VALUES('Jar');
INSERT INTO product.unit(Unit) VALUES('Tube');
INSERT INTO product.unit(Unit) VALUES('Case');
INSERT INTO product.unit(Unit) VALUES('Cup');
CREATE TABLE `product`.`product` (
`ProductID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`Description` varchar(45) NOT NULL,
`ManufacturerID` int(11) NOT NULL,
`UnitCost` decimal(10,3) DEFAULT '0.000',
`UnitPrice` decimal(10,3) DEFAULT '0.000',
`UnitID` int(11) DEFAULT NULL,
PRIMARY KEY (`ProductID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`),
UNIQUE KEY `Description_UNIQUE` (`Description`),
KEY `ManufacturerID_idx` (`ManufacturerID`),
KEY `UnitID_idx` (`UnitID`),
CONSTRAINT `ManufacturerID` FOREIGN KEY (`ManufacturerID`) REFERENCES `manufacturer` (`ManufacturerID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `UnitID` FOREIGN KEY (`UnitID`) REFERENCES `unit` (`UnitID`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000001', 'Blueberry Jam', 1, .50, 1.00, 4);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000002', 'Coffee Yogurt', 2, 1.00, 2.00, 7);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000003', '12 Individual Boxes Corn Flakes', 3, 1.00, 5.00, 6);
INSERT INTO product.product (SKU, Description, ManufacturerID, UnitCost, UnitPrice, UnitID) VALUES('P000000004', 'Box Corn Flakes', 3, 1.00, 5.00, 1);
END;
-- *********************
-- Loyalty (Customers)
-- *********************
BEGIN
DROP schema if exists `loyalty`;
END;
BEGIN
CREATE SCHEMA `loyalty`;
CREATE TABLE `loyalty`.`loyalty` (
`LoyaltyID` INT NOT NULL AUTO_INCREMENT,
`LoyaltyNumber` VARCHAR(10) NOT NULL,
`LastName` VARCHAR(45) NULL,
`FirstName` VARCHAR(45) NULL,
`AddressLine1` VARCHAR(45) NULL,
`AddressLine2` VARCHAR(45) NULL,
`City` VARCHAR(45) NULL,
`State` VARCHAR(2) NULL,
`ZipCode` INT NULL,
PRIMARY KEY (`LoyaltyID`),
UNIQUE INDEX `LoyaltyNumber_UNIQUE` (`LoyaltyNumber` ASC));
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber, LastName) VALUES(1, 'L000000001', 'Smith');
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber) VALUES(2, 'L000000002');
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber) VALUES(3, 'L000000003');
INSERT INTO loyalty.loyalty (LoyaltyID, LoyaltyNumber, LastName, FirstName) VALUES(4, 'L000000004', 'Knight', 'Robert');
END;
-- *********************
-- Reconciled Schema
-- *********************
BEGIN
DROP Schema if exists `reconciled`;
END;
BEGIN
CREATE SCHEMA `reconciled` ;
CREATE TABLE `reconciled`.`sale` (
`saleID` int(11) NOT NULL AUTO_INCREMENT,
`EmployeeFirstName` varchar(45) DEFAULT NULL,
`EmployeeLastName` varchar(45) DEFAULT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Unit` varchar(45) DEFAULT NULL,
`SKU` varchar(45) DEFAULT NULL,
`Qty` int(11) DEFAULT NULL,
`UnitCost` decimal(10,3) DEFAULT NULL,
`UnitPrice` decimal(10,3) DEFAULT NULL,
`Manufacturer` varchar(45) DEFAULT NULL,
`EmployeeNumber` varchar(45) DEFAULT NULL COMMENT 'The employee who handled the transaction',
`LoyaltyNumber` varchar(45) DEFAULT NULL,
`StoreNumber` varchar(45) DEFAULT NULL,
`DateOfTransaction` date DEFAULT NULL,
`TimeOfTransaction` time DEFAULT NULL,
`DateOfTransactionString` varchar(45) DEFAULT NULL,
`TimeOfTransactionString` varchar(45) DEFAULT NULL,
`WeekdayOfTransaction` int(11) DEFAULT NULL,
`MonthOfTransaction` int(11) DEFAULT NULL,
`YearOfTransaction` int(11) DEFAULT NULL,
`WeekdayNameOfTransaction` varchar(45) DEFAULT NULL,
`MonthNameOfTransaction` varchar(45) DEFAULT NULL,
`TotalPrice` decimal(10,3) DEFAULT NULL,
`ZipCode` varchar(10) DEFAULT NULL,
PRIMARY KEY (`saleID`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
CREATE TABLE `reconciled`.`weather` (
`WeatherID` int(11) NOT NULL AUTO_INCREMENT,
`DateStamp` date DEFAULT NULL,
`ZipCode` varchar(10) DEFAULT NULL,
`Temperature` int(11) DEFAULT NULL,
`MonthName` varchar(45) DEFAULT NULL,
`YearNumber` int(11) DEFAULT NULL,
`DayName` varchar(45) DEFAULT NULL,
`MonthNumber` int(11) DEFAULT NULL,
`DayNumber` int(11) DEFAULT NULL,
PRIMARY KEY (`WeatherID`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
END;
-- *********************
-- Aggregated schema for the data warehouse
-- *********************
BEGIN
DROP Schema if exists dw;
CREATE SCHEMA `dw` ;
CREATE TABLE `dw`.`salesqtybyproductandstore` (
`SalesByProductAndStoreID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`StoreNumber` varchar(45) NOT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Monday` int(11) DEFAULT '0',
`Tuesday` int(11) DEFAULT '0',
`Wednesday` int(11) DEFAULT '0',
`Thursday` int(11) DEFAULT '0',
`Friday` int(11) DEFAULT '0',
`Saturday` int(11) DEFAULT '0',
`Sunday` int(11) DEFAULT '0',
PRIMARY KEY (`SalesByProductAndStoreID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`,`StoreNumber`)
);
END;
BEGIN
CREATE TABLE `dw`.`monthlysalesqtybyproductandstore` (
`monthlysalesqtybyproductandstoreID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`StoreNumber` varchar(45) NOT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`January` int(11) DEFAULT '0',
`February` int(11) DEFAULT '0',
`March` int(11) DEFAULT '0',
`April` int(11) DEFAULT '0',
`May` int(11) DEFAULT '0',
`June` int(11) DEFAULT '0',
`July` int(11) DEFAULT '0',
`August` int(11) DEFAULT '0',
`September` int(11) DEFAULT '0',
`October` int(11) DEFAULT '0',
`November` int(11) DEFAULT '0',
`December` int(11) DEFAULT '0',
PRIMARY KEY (`monthlysalesqtybyproductandstoreID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`,`StoreNumber`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `dw`.`weeklysalesqtybyproductandstore` (
`WeeklysalesqtybyproductandstoreID` int(11) NOT NULL AUTO_INCREMENT,
`SKU` varchar(45) NOT NULL,
`StoreNumber` varchar(45) NOT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Monday` int(11) DEFAULT '0',
`Tuesday` int(11) DEFAULT '0',
`Wednesday` int(11) DEFAULT '0',
`Thursday` int(11) DEFAULT '0',
`Friday` int(11) DEFAULT '0',
`Saturday` int(11) DEFAULT '0',
`Sunday` int(11) DEFAULT '0',
PRIMARY KEY (`WeeklysalesqtybyproductandstoreID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`,`StoreNumber`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `dw`.`storesalesbytemperature` (
`StoreSalesByTemperatureID` INT NOT NULL AUTO_INCREMENT,
`StoreNumber` VARCHAR(45) NULL,
`Under0` DECIMAL(10,2) NULL,
`0To20` DECIMAL(10,2) NULL,
`21To30` DECIMAL(10,2) NULL,
`31To40` DECIMAL(10,2) NULL,
`41To50` DECIMAL(10,2) NULL,
`51To60` DECIMAL(10,2) NULL,
`61To70` DECIMAL(10,2) NULL,
`71To80` DECIMAL(10,2) NULL,
`81To90` DECIMAL(10,2) NULL,
`Over90` DECIMAL(10,2) NULL,
`StoreSalesByTemperaturecol` VARCHAR(45) NULL,
PRIMARY KEY (`StoreSalesByTemperatureID`),
INDEX `Unique` (`StoreNumber` ASC));
END;
BEGIN
DROP Schema if exists icecream;
CREATE SCHEMA `icecream`;
CREATE TABLE `icecream`.`flavor` (
`FlavorID` INT NOT NULL AUTO_INCREMENT,
`Flavor` VARCHAR(45) NULL,
PRIMARY KEY (`FlavorID`),
UNIQUE INDEX `Flavor_UNIQUE` (`Flavor` ASC));
CREATE TABLE `icecream`.`container` (
`ContainerID` INT NOT NULL AUTO_INCREMENT,
`Container` VARCHAR(45) NULL,
PRIMARY KEY (`ContainerID`),
UNIQUE INDEX `Container_UNIQUE` (`Container` ASC));
CREATE TABLE `icecream`.`icecream` (
`IceCreamID` int(11) NOT NULL AUTO_INCREMENT,
`FlavorID` int(11) DEFAULT NULL,
`ContainerID` int(11) DEFAULT NULL,
`SKU` varchar(45) DEFAULT NULL,
PRIMARY KEY (`IceCreamID`),
UNIQUE KEY `Unique` (`ContainerID`,`FlavorID`),
UNIQUE KEY `SKU_UNIQUE` (`SKU`),
CONSTRAINT `Container` FOREIGN KEY (`ContainerID`) REFERENCES `icecream`.`container` (`ContainerID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `Flavor` FOREIGN KEY (`FlavorID`) REFERENCES `icecream`.`flavor` (`FlavorID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO icecream.flavor (flavor) VALUES('Chocolate');
INSERT INTO icecream.flavor (flavor) VALUES('Vanilla');
INSERT INTO icecream.flavor (flavor) VALUES('Strawberry');
INSERT INTO icecream.flavor (flavor) VALUES('Raspberry Chocolate Chip');
INSERT INTO icecream.flavor (flavor) VALUES('Blueberry');
INSERT INTO icecream.container (Container) VALUES('cup');
INSERT INTO icecream.container (Container) VALUES('pint');
INSERT INTO icecream.container (Container) VALUES('quart');
INSERT INTO icecream.container (Container) VALUES('half gallon');
INSERT INTO icecream.container (Container) VALUES('gallon');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(1, 2, 'I000000001');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(2, 2, 'I000000002');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(3, 2, 'I000000003');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(1, 3, 'I000000004');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(2, 3, 'I000000005');
INSERT INTO icecream.icecream (flavorID, containerID, SKU) VALUES(3, 3, 'I000000006');
INSERT INTO icecream.icecream (flavorID, containerID) VALUES(4, 1);
END;
-- *********************
-- Schema Sales
-- *********************
BEGIN
DROP Schema if exists `weather`;
END;
BEGIN
CREATE SCHEMA `weather` ;
CREATE TABLE `weather`.`weather` (
`weatherText` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO weather.weather (weatherText) VALUES('2018/01/01 00:00:00.0000,45255,10');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/02 00:00:00.0000,45255,12');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/03 00:00:00.0000,45255,14');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/04 00:00:00.0000,45255,16');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/01 00:00:00.0000,45103,15');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/02 00:00:00.0000,45103,17');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/03 00:00:00.0000,45103,19');
INSERT INTO weather.weather (weatherText) VALUES('2018/01/04 00:00:00.0000,45103,21');
END;
-- *********************
-- Schema Sales
-- *********************
BEGIN
DROP Schema if exists `sales`;
END;
BEGIN
CREATE SCHEMA `sales` ;
CREATE TABLE `sales`.`transaction` (
`TransactionID` int(11) NOT NULL AUTO_INCREMENT,
`DateTimeOfTransaction` datetime NOT NULL,
`LoyaltyNumber` varchar(10) DEFAULT NULL,
`StoreNumber` varchar(10) NOT NULL,
`EmployeeNumber` varchar(10) NOT NULL,
PRIMARY KEY (`TransactionID`),
UNIQUE KEY `NaturalKey` (`DateTimeOfTransaction`,`LoyaltyNumber`,`StoreNumber`,`EmployeeNumber`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
END;
BEGIN
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(1, '2018-01-01 15:00:00', 'L000000001', 'S000000001', 'E000000001');
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(2, '2018-02-02 15:00:00', 'L000000002', 'S000000002', 'E000000002');
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(3, '2018-02-07 16:00:00', 'L000000002', 'S000000002', 'E000000002');
INSERT INTO `sales`.`transaction` (TransactionID, DateTimeOfTransaction, LoyaltyNumber, StoreNumber, EmployeeNumber) VALUES(4, '2018-02-09 16:16:00', 'L000000002', 'S000000002', 'E000000002');
END;
BEGIN
-- YYYY-MM-DD HH:MM:SS
CREATE TABLE `sales`.`transactiondetail` (
`TransactionDetailID` int(11) NOT NULL AUTO_INCREMENT,
`TransactionID` int(11) NOT NULL,
`SKU` varchar(45) NOT NULL,
`Qty` int(11) NOT NULL,
`TotalPrice` decimal(10,3) NOT NULL,
`Comment` varchar(1000) DEFAULT NULL,
PRIMARY KEY (`TransactionDetailID`),
UNIQUE KEY `NaturalKey` (`TransactionID`,`SKU`),
KEY `TransactionID_idx` (`TransactionID`),
CONSTRAINT `TransactionID` FOREIGN KEY (`TransactionID`) REFERENCES `sales`.`transaction` (`TransactionID`) ON DELETE NO ACTION ON UPDATE NO ACTION);
END;
BEGIN
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(1, 'P000000001', 10, 100);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(1, 'P000000002', 5, 25);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(2, 'P000000001', 1, 100);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(2, 'P000000002', 2, 25);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(3, 'P000000002', 40, 40);
INSERT INTO sales.transactiondetail (TransactionID, SKU, Qty, TotalPrice) VALUES(4, 'P000000002', 44, 44);
END;
BEGIN
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`localhost`
SQL SECURITY DEFINER
VIEW `reconciled`.`mapping` AS
SELECT
`hr`.`employee`.`FirstName` AS `EmployeeFirstName`,
`hr`.`employee`.`LastName` AS `EmployeeLastName`,
`product`.`product`.`Description` AS `ProductDescription`,
`product`.`unit`.`Unit` AS `Unit`,
`product`.`product`.`SKU` AS `SKU`,
`sales`.`transactiondetail`.`Qty` AS `Qty`,
`product`.`product`.`UnitCost` AS `UnitCost`,
`product`.`product`.`UnitPrice` AS `UnitPrice`,
`product`.`manufacturer`.`Manufacturer` AS `Manufacturer`,
`sales`.`transaction`.`EmployeeNumber` AS `EmployeeNumber`,
`sales`.`transaction`.`LoyaltyNumber` AS `LoyaltyNumber`,
`sales`.`transaction`.`StoreNumber` AS `StoreNumber`,
`store`.`store`.`zipCode` as `ZipCode`,
CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS DATE) AS `DateOfTransaction`,
CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS TIME) AS `TimeOfTransaction`,
CAST(CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS DATE)
AS CHAR CHARSET UTF8) AS `DateOfTransactionString`,
CAST(CAST(`sales`.`transaction`.`DateTimeOfTransaction`
AS TIME)
AS CHAR CHARSET UTF8) AS `TimeOfTransactionString`,
WEEKDAY(`sales`.`transaction`.`DateTimeOfTransaction`) AS `WeekdayOfTransaction`,
MONTH(`sales`.`transaction`.`DateTimeOfTransaction`) AS `MonthOfTransaction`,
YEAR(`sales`.`transaction`.`DateTimeOfTransaction`) AS `YearOfTransaction`,
DAYNAME(`sales`.`transaction`.`DateTimeOfTransaction`) AS `WeekdayNameOfTransaction`,
MONTHNAME(`sales`.`transaction`.`DateTimeOfTransaction`) AS `MonthNameOfTransaction`,
`sales`.`transactiondetail`.`TotalPrice` AS `TotalPrice`
FROM
((((((`sales`.`transaction`
JOIN `sales`.`transactiondetail` ON ((`sales`.`transaction`.`TransactionID` = `sales`.`transactiondetail`.`TransactionID`)))
JOIN `hr`.`employee` ON ((`hr`.`employee`.`EmployeeNumber` = `sales`.`transaction`.`EmployeeNumber`)))
JOIN `product`.`product` ON ((`product`.`product`.`SKU` = `sales`.`transactiondetail`.`SKU`)))
JOIN `product`.`unit` ON ((`product`.`product`.`UnitID` = `product`.`unit`.`UnitID`)))
JOIN `store`.`store` ON ((`store`.`store`.`StoreNumber` = `sales`.`transaction`.`StoreNumber`)))
JOIN `product`.`manufacturer` ON ((`product`.`product`.`ManufacturerID` = `product`.`manufacturer`.`ManufacturerID`))) ;
CREATE TABLE `reconciled`.`icecream` (
`icecreamid` int(11) NOT NULL AUTO_INCREMENT,
`flavor` varchar(45) DEFAULT NULL,
`container` varchar(45) DEFAULT NULL,
`sku` varchar(45) DEFAULT NULL,
PRIMARY KEY (`icecreamid`),
UNIQUE KEY `Unique` (`flavor`,`container`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
END;
BEGIN
CREATE VIEW `reconciled`.`totalSalesByStoreAndTemperature` AS
SELECT StoreNumber, temperature, SUM(TotalPrice) as `SumOfTotalPrice`
FROM reconciled.sale inner join reconciled.weather on Substring(reconciled.sale.zipCode,1,5) = substring(reconciled.weather.zipCode,1,5)
Group By StoreNumber, temperature
ORDER BY StoreNumber, Temperature;
END;
END
Just want to add the root cause of this issue.
Cloud SQL currently is limited to grant SUPER and FILE privileges according to GCP documentation, so they can't be assigned to any user including root.
As per MySQL 5.7 documentation, the DEFINER attribute requires the user to have the SUPER privilege.
The situation above was the cause of your error and it worked when you took out the DEFINER statement.
In summary any other action that requires SUPER o FILE privileges most probably will fail with the error "Access denied; you need (at least one of) the SUPER privilege(s) for this operation".
I took out the DEFINER clause in the creation of the view reconciled.mapping.
That eliminated the error when I executed the SP.
It wasn't the DEFINER clause of the SP creation script, it was the DEFINER clause in the SP itself.
with my experience in AWS RDS, you don't get SUPER privileges as those are used by the provisioner engine. I am pretty sure that is how it is done in GCP. It could be that something in query make MySQL think it needs SUPER privileges. Posting your query which is giving the error will be helpful.
-------Update after OP posted the query----------
this is the problem. DEFINER. try CREATE PROCEDURE CreateFullCoverageTestCase()

Error executing stored procedure fails

I created a stored procedure in mysql database, which fires multiple insert statemets.
As shown below:
DELIMITER //
DROP PROCEDURE IF EXISTS insert_user_group_info
//
CREATE PROCEDURE insert_group_user_info(groupname varchar(50), groupdesc varchar(100),
createddate varchar(50), createdby varchar(100))
BEGIN
DECLARE RETURN_VAL INT UNSIGNED DEFAULT 0;
DECLARE NEWGROUPID INT UNSIGNED DEFAULT 0;
START TRANSACTION;
Insert into group_tbl (`groupname`,
`groupdesc`,
`groupusers`,
`createdon`,
`createdby`,
`groupstatus`)
values
(groupname,
groupdesc,
'1',
createddate,
createdby,
'1');
SET NEWGROUPID = LAST_INSERT_ID();
INSERT INTO useringroup_tbl
( groupid,
username,
regdate,
joindate,
userstatus,
roleid)
VALUES
( NEWGROUPID,
createdby,
createddate,
createddate,
'1',
'1');
INSERT INTO userinrole_tbl
( username,
groupid,
roleid)
VALUES
(createdby,
NEWGROUPID,
'1');
SET RETURN_VAL = LAST_INSERT_ID();
SELECT RETURN_VAL;
COMMIT;
END//
DELIMITER ;
Schema for tables are as follows
Table 1 group_tbl
CREATE TABLE `group_tbl`
(
`groupid` int(11) NOT NULL auto_increment,
`groupname` varchar(50) NOT NULL,
`groupdesc` varchar(100) NOT NULL,
`groupusers` int(11) NOT NULL,
`createdon` datetime NOT NULL,
`createdby` varchar(50) NOT NULL,
`groupstatus` tinyint(4) NOT NULL,
PRIMARY KEY (`groupid`)
);
Table 2 useringroup_tbl
CREATE TABLE IF NOT EXISTS `useringroup_tbl`
(
`groupid` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
`regdate` datetime NOT NULL,
`joindate` datetime NOT NULL default '0000-00-00 00:00:00',
`userstatus` tinyint(4) NOT NULL,
`closingdate` datetime NOT NULL default '0000-00-00 00:00:00',
`roleid` int(11) NOT NULL default '0',
PRIMARY KEY (`groupid`,`username`)
);
Table 3 userinrole_tbl
CREATE TABLE IF NOT EXISTS `userinrole_tbl`
(
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`groupid` int(11) NOT NULL,
`roleid` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
While executing this procedure by this statement
call insert_group_user_info('test name','test description','2015-05-10 12:10:12','XYZuser')
I get an error
1062 - Duplicate entry '1-XYZuser' for key 1
and each time it the number appended to the username gets incremented
like this
1062 - Duplicate entry '2-XYZuser' for key 1
So please if anybody can give me direction what I am doing wrong,
Thanks in advance.
According to the error you have pasted,
1062 - Duplicate entry '1-XYZuser' - PRIMARY KEY (groupid,username) - It doesn't matter if you truncate the tables or not. You might be inserting the same data again.
You might be using last_insert_id() incorrectly.
Why is there no auto_increment in useringroup_tbl?

Issues in import sql in phpmyadmin

While importing the sql data, it shows syntax error:
Here is my code:
CREATE TABLE IF NOT EXISTS `tblproduct` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`image` text NOT NULL,
`price` double(10,2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `product_code` (`code`)
)
INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, '3D Camera', '3DcAM01', 'product-images/camera.jpg', 1500.00),
(2, 'External Hard Drive', 'USB02', 'product-images/external-hard-drive.jpg', 800.00),
(3, 'Wrist Watch', 'wristWear03', 'product-images/watch.jpg', 300.00);
I just confused with this, what is the exact error.
Can anyone help me to fix this? thanks in advance.
Looks like it's whole generated so there should be no errors. Try to add semicolon after create table syntax so each query is separated. Like this:
CREATE TABLE IF NOT EXISTS `tblproduct` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`image` text NOT NULL,
`price` double(10,2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `product_code` (`code`)
);
INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, '3D Camera', '3DcAM01', 'product-images/camera.jpg', 1500.00),
(2, 'External Hard Drive', 'USB02', 'product-images/external-hard-drive.jpg', 800.00),
(3, 'Wrist Watch', 'wristWear03', 'product-images/watch.jpg', 300.00);

selecting multiple rows issue in mysql

I am beginner in sql.
I have two tables users and installments
CREATE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(20) NOT NULL,
`password` varchar(10) NOT NULL,
`father_name` varchar(20) NOT NULL,
`phone` varchar(20) NOT NULL,
`cnic` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
`introducer` varchar(100) NOT NULL,
`date` date DEFAULT NULL,
`reg_number` varchar(100) DEFAULT NULL,
`installment` int(100) NOT NULL,
`user_level` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cnic` (`cnic`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
INSERT INTO `users` (`id`, `user_name`, `password`, `father_name`, `phone`, `cnic`, `email`, `address`, `introducer`, `date`, `reg_number`, `installment`, `user_level`) VALUES
(2, 'qaser', 'Qaser1', 'zamarrud', '0312546879', '37406-3140185-1', 'tariq_kareem#yahoo.com', 'street # 6', 'rizwan', '2014-08-20', 'E-002', 3000, 0);
and
CREATE `installments` (
`installment_id` int(11) NOT NULL AUTO_INCREMENT,
`month` date DEFAULT NULL,
`prv_arrear` int(100) NOT NULL,
`amount` int(100) NOT NULL,
`total` int(100) NOT NULL,
`receive` int(100) NOT NULL,
`arrear` int(100) NOT NULL,
`fk_users_id` int(11) NOT NULL,
PRIMARY KEY (`installment_id`),
KEY `fk_users_id` (`fk_users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
INSERT INTO `installments` (`installment_id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`) VALUES
(2, '2014-08-20', 2000, 2500, 4500, 3000, 1500);
when I run the following query I get the first table's record correctly but the second table's record showing NULL values
SELECT * FROM users
LEFT JOIN installments
ON users.id=installments.installment_id
WHERE users.cnic='37406-3140185-1';
Is there anything missing in the above query or is there another way to get the record from both tables simultaneously
id user_name password father_name phone cnic email address introducer date reg_number installment user_level installment_id month prv_arrear amount total receive arrear fk_users_id
2 qaser Qaser1 zamarrud 0312546879 37406-3140185-1 tariq_kareem#yahoo.com street # 6 rizwan 2014-08-20 s-001 3000 0 NULL NULL NULL NULL NULL NULL NULL NULL
I am also using the following query to inserting the record for getting primary key value and insert into foreign key
INSERT INTO `installments`(`id`, `month`, `prv_arrear`, `amount`, `total`, `receive`, `arrear`, fk_users_id)
SELECT NULL,now(),1000,2500,3500,3000,500, id
FROM users
WHERE cnic = '37406-3140190-1'
please help me thanks in advance and sorry if there is something wrong in my question because I am new in sql.
I suspect the right join condition is on fk_users_id. So this might do what you want:
SELECT *
FROM users u LEFT JOIN
installments i
ON u.id = i.fk_users_id
WHERE u.cnic = '37406-3140185-1';