将数据插入表时,MySQL错误1062和1452

时间:2016-08-27 03:06:41

标签: mysql sql-insert mysql-error-1062 mysql-error-1452

尝试按照视频和文字进行mysql课程。根据我的想法,我一直在完美地遵循它,但是当我尝试将数据插入表格时,我遇到了很多错误。

这是我的代码:

-- MyExercises

CREATE DATABASE MyExercises;

USE MyExercises;

CREATE TABLE Categories 
        (CategoryID INT NOT NULL, 
        CategoryName VARCHAR(20) NOT NULL,
        Description TEXT NULL,
PRIMARY KEY (CategoryID))
ENGINE = InnoDB;

SHOW COLUMNS FROM Categories;   
DESC Categories;   

CREATE TABLE Suppliers 
        (City VARCHAR(20) NULL, 
        CompanyName VARCHAR(30) NOT NULL,
        SupplierID INT NOT NULL,
PRIMARY KEY (SupplierID))
ENGINE = InnoDB;

    CREATE TABLE Products 
        (ProductID INT NOT NULL, 
        ProductName VARCHAR(40) NOT NULL,
        CategoryID INT NULL,
        SupplierID INT NULL,
        UnitPrice DECIMAL(5,2) NULL,
        UnitsInStock SMALLINT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)) 
ENGINE = INNODB;

INSERT INTO Categories (CategoryID, CategoryName, Description)
VALUES (1, 'Beverages', 'Soft drinks, coffees, teas, beers, and ales');

INSERT INTO Categories (CategoryID, CategoryName, Description)
VALUES (2, 'Condiments', 'Sweet and savory sauces');

INSERT INTO Categories (CategoryID, CategoryName, Description)
VALUES (3, 'Confections', 'Desserts, candies, and sweet breads');

INSERT INTO Categories (CategoryID, CategoryName, Description)
VALUES (4, 'Dairy Products', 'Cheese, Milk, Cream');

INSERT INTO Suppliers (SupplierID, CompanyName, City)
VALUES (1, 'Exotic Liquids', 'London');

INSERT INTO Suppliers (SupplierID, CompanyName)
VALUES (2, 'New Orleans Cajun Delights');

INSERT INTO Suppliers (SupplierID, CompanyName, City)
VALUES (3, 'Grandma Kelly''s Homestead', 'Adelaide');

INSERT INTO Suppliers (SupplierID, CompanyName)
VALUES (4, 'Tokyo Traders');

INSERT INTO Products (ProductID, ProductName, SupplierID, CategoryID, UnitPrice, UnitsInStock)
VALUES (1, 'Chai', 1, 1, 18, 39);

INSERT INTO Products (ProductID, ProductName, SupplierID, UnitPrice, UnitsInStock)
VALUES (2, 'Chang', 1, 19, 17);

INSERT INTO Products (ProductID, ProductName, CategoryID, UnitPrice, UnitsInStock)
VALUES (3, 'Ani Seed Syrup', 2, 10, 13);

INSERT INTO Products (ProductID, ProductName, SupplierID, CategoryID, UnitPrice)
VALUES (4, 'Chef Anton''s Cajun Seasoning', 2, 2, 22);

INSERT INTO Products (ProductID, ProductName, SupplierID, CategoryID, UnitsInStock)
VALUES (5, 'Chef Anton''s Gumbo Mix', 2, 2, 0);

INSERT INTO Products (ProductID, ProductName, SupplierID, CategoryID)
VALUES (6, 'Grandma''s Boysenberry Spread', 3, 2);

INSERT INTO Products (ProductID, ProductName, SupplierID, CategoryID, UnitPrice, UnitsInStock)
VALUES (7, 'Uncle Bob''s Organic Dried Pears', 5, 4, 30, 15);

INSERT INTO Products (ProductID, ProductName, SupplierID, CategoryID, UnitPrice, UnitsInStock)
VALUES (8, 'Northwood''s Cranberry Sauce', 4, 5, 40, 6);

对于为什么我应该在插入数据时遇到错误,是否有任何突出的显而易见的事实?

这是我特意得到错误的地方:

enter image description here 很多错误都是用于复制的错误代码1062 ,以及错误代码1452

1 个答案:

答案 0 :(得分:1)

您应该一次发出一个查询 - 这样您就会看到错误出现的位置。

错误#1062可能是由于主键的唯一性造成的。您尝试插入的ID的值已存在于表中。尝试截断整个表并再次运行脚本。

尝试插入Products表时可能会出现错误#1452。这是因为您有一个外键约束,并且在Categories表上发出的先前插入语句失败。

注意:您不需要为每一行发出insert语句。相反,您可以将它们分组为:

INSERT INTO Categories (CategoryID, CategoryName, Description)
  VALUES 
    (1, 'Beverages', 'Soft drinks, coffees, teas, beers, and ales'),
    (2, 'Condiments', 'Sweet and savory sauces'),
    (3, 'Confections', 'Desserts, candies, and sweet breads'),
    (4, 'Dairy Products', 'Cheese, Milk, Cream');

在创建Suppliers(SupplierID)表时,您还错过了Products的外键约束,我猜这应该存在。

对您而言,一个好的建议是使用AUTO_INCREMENT作为可为您生成值的主键。有关此主题的更多信息,请查看here。这将要求您从insert语句中删除主键列和值,从而缩短它们并且不必担心每次手动递增它们。