表不存在,但确实存在

时间:2016-04-04 22:45:56

标签: mysql database

我是SQL的新手,我不断收到错误#1146表不存在,但确实存在。有人能指出我的代码中问题所在的方向。

CREATE TABLE IF NOT EXISTS `Donut`
(
`DonutID` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`Description` VARCHAR(10) NOT NULL,
`price` DECIMAL(5,2) NOT NULL,
PRIMARY KEY (`DonutID`));

CREATE TABLE IF NOT EXISTS `Customer`
(
`CustomerID` INT NOT NULL AUTO_INCREMENT, 
`FirstName` VARCHAR(15) NOT NULL, 
`LastName` VARCHAR(15) NOT NULL, 
`Street` VARCHAR(20) NOT NULL, 
`Apartment` VARCHAR(5) NULL DEFAULT NULL,
`City` VARCHAR(20) NOT NULL, 
`State` VARCHAR(2) NOT NULL, 
`zip` INT(5) NULL, 
`Homephone`BIGINT(12) NULL,
`Cellphone`BIGINT(12) NULL,
`Otherphone`BIGINT (12)NUll,
PRIMARY KEY (`CustomerID`)
);
CREATE TABLE IF NOT EXISTS `SalesOrder` 
( 
`DonutOrderID` INT NOT NULL AUTO_INCREMENT, 
`SpecialNotes` VARCHAR(35) NULL DEFAULT NULL, 
`date` DATETIME,
`CustomerID` INT NOT NULL, 
 Primary KEY(`DonutOrderID`,`CustomerID`),
 FOREIGN KEY (`CustomerID`) REFERENCES `Customer` (`CustomerID`)
 );
CREATE TABLE IF NOT EXISTS `SalesOrderDetails`
(
 `DetailsID` INT,
`qty` INT,
`DonutOrderID` INT,
`DonutID` INT,
PRIMARY KEY (`DetailsID`,`DonutOrderID`,`DonutID`),
FOREIGN KEY (`DonutOrderID`) REFERENCES `SalesOrder` (`DonutOrderID`) 
ON DELETE NO ACTION ON UPDATE NO ACTION, 
FOREIGN KEY (`DonutID`) REFERENCES `Donut` (`DonutID`) 

);

CREATE OR REPLACE VIEW `CustomerFullName`
 AS SELECT *, CONCAT('firstName',' ','lastName') AS customerFullName FROM    Customer;

CREATE INDEX `Donut_name_idx` ON `Donut` (`name` ASC);

INSERT INTO Customer (CustomerID, FirstName, LastName, Street, apartment,     City, State, Zip,Homephone,Cellphone,Otherphone)
VALUES (1, 'John', 'Smith', '12 Main street', 'A', 'Lumberton', 'NJ',   '08048','6092651212',null,null);
INSERT INTO Donut (DonutID, Name, Description, price) 
VALUES (1,'Chocalte Donut','Choclate',1.25);
INSERT INTO `SalesOrder` (DonutOrderID,CustomerID,SpecialNotes) 
VALUES (1,1, 'Make sure they are fresh!');
 INSERT INTO `SalesOrderDetails`(DonutOrderID, DonutID,qty)
 VALUES (1,1,3);

以下是我的查询代码

SELECT Customer.FirstName, Customer.LastName, Customer.street,     Customer.apartment, 
Customer.city, Customer.state, Customer.zip,   Customer.Homephone,Customer.Cellphone,Customer.Otherphone, Donut.Name, Donut.Description, Donut.price,  
 SalesOrder.DonutOrderID, 
 Donut.DonutID, 
 SalesOrder.specialNotes,
 Customer.CustomerID,
 date 
 FROM `SalesOrder`,`SalesOrderDetails`
 INNER JOIN SalesOrderDetails.DonutOrderID ON SalesOrder=     SalesOrder.DonutOrderID
 INNER JOIN Customer  ON SalesOrder.CustomerID= Customer.CustomerID 
 INNER JOIN Donut ON `DonutID` = DonutID

由于

1 个答案:

答案 0 :(得分:0)

您的查询有多个语法错误,包括错误的JOIN和使用/不使用反引号访问您的列不一致。

我试图改进它,现在是:

SELECT 
  c.`FirstName`, c.`LastName`, c.`Street`, c.`Apartment`, c.`City`, 
  c.`State`, c.`zip`, c.`Homephone`, c.`Cellphone`, c.`Otherphone`, 
  d.`name`, d.`Description`, d.`price`,  
  so.`DonutOrderID`, d.`DonutID`, so.`specialNotes`, c.`CustomerID`, o.`date` 
FROM `SalesOrder` so
INNER JOIN `SalesOrderDetails` sod ON so.`DonutOrderID` = sod.`DonutOrderID`
INNER JOIN `Customer` c ON so.`CustomerID` = c.`CustomerID` 
INNER JOIN `Donut` d ON d.`DonutID` = sod.`DonutID`