MSSQL插入不起作用

时间:2016-11-30 17:36:10

标签: sql sql-server tsql

我正在从db kroenke书中做练习。任何人都可以看到这个插入的原因:

INSERT INTO PRODUCT_SALES VALUES(41197, 3, 'VK001', 1, 14.95, 14.95);

不适用于下表:

CREATE TABLE PRODUCT_SALES(
        TimeID          Int                 NOT NULL,
        CustomerID      Int                 NOT NULL,
        ProductNumber   Char(35)            NOT NULL,
        Quantity        Int                 NOT NULL,
        UnitPrice       Numeric(9,2)        NOT NULL,
        Total           Numeric(9,2 )       NULL,
        CONSTRAINT      SALES_PK
        PRIMARY KEY     (TimeID, CustomerID, ProductNumber),
        CONSTRAINT      PS_TIMELINE_FK FOREIGN KEY(TimeID)
                                REFERENCES TIMELINE(TimeID)
                                        ON UPDATE NO ACTION
                                        ON DELETE NO ACTION,
        CONSTRAINT      PS_CUSTOMER_FK FOREIGN KEY(CustomerID)
                                REFERENCES CUSTOMER(CustomerID)
                                        ON UPDATE NO ACTION
                                        ON DELETE NO ACTION,
        CONSTRAINT      PS_PRODUCT_FK FOREIGN KEY(ProductNumber)
                                REFERENCES PRODUCT(ProductNumber)
                                        ON UPDATE NO ACTION
                                        ON DELETE NO ACTION
        );

格式看起来不错但收到此错误:

Column name or number of supplied values does not match table definition.

谢谢!

3 个答案:

答案 0 :(得分:1)

刚刚在Insert语句中使用了列名,它已经有效了。

CREATE TABLE TIMELINE (TimeID INT NOT NULL PRIMARY KEY)
GO
CREATE TABLE CUSTOMER (CUSTOMERID INT NOT NULL PRIMARY KEY)
GO
CREATE TABLE PRODUCT(ProductNumber Char(35) NOT NULL PRIMARY KEY)
GO

INSERT INTO TIMELINE VALUES (41197)
INSERT INTO CUSTOMER VALUES (3)
INSERT INTO PRODUCT VALUES ('VK001')
GO

CREATE TABLE PRODUCT_SALES(
        TimeID          Int                 NOT NULL,
        CustomerID      Int                 NOT NULL,
        ProductNumber   Char(35)            NOT NULL,
        Quantity        Int                 NOT NULL,
        UnitPrice       Numeric(9,2)        NOT NULL,
        Total           Numeric(9,2 )       NULL,
        CONSTRAINT      SALES_PK
        PRIMARY KEY     (TimeID, CustomerID, ProductNumber),
        CONSTRAINT      PS_TIMELINE_FK FOREIGN KEY(TimeID)
                                REFERENCES TIMELINE(TimeID)
                                        ON UPDATE NO ACTION
                                        ON DELETE NO ACTION,
        CONSTRAINT      PS_CUSTOMER_FK FOREIGN KEY(CustomerID)
                                REFERENCES CUSTOMER(CustomerID)
                                        ON UPDATE NO ACTION
                                        ON DELETE NO ACTION,
        CONSTRAINT      PS_PRODUCT_FK FOREIGN KEY(ProductNumber)
                                REFERENCES PRODUCT(ProductNumber)
                                        ON UPDATE NO ACTION
                                        ON DELETE NO ACTION
        );
GO

- 在insert语句中添加了列名并且它有效。

INSERT INTO PRODUCT_SALES (TimeID ,CustomerID, ProductNumber , Quantity, UnitPrice , Total)
VALUES(41197, 3, 'VK001', 1, 14.95, 14.95);

答案 1 :(得分:0)

The Query seems to work fine

我在我的mysql中尝试了相同的查询,插入查询似乎工作正常!桌子改变了吗?

答案 2 :(得分:0)

您之前是否使用不同的列或列顺序创建了此表?

你能试试吗

DROP TABLE PRODUCT_SALES
CREATE TABLE PRODUCT_SALES(
    TimeID          Int                 NOT NULL,
    CustomerID      Int                 NOT NULL,
    ProductNumber   Char(35)            NOT NULL,
    Quantity        Int                 NOT NULL,
    UnitPrice       Numeric(9,2)        NOT NULL,
    Total           Numeric(9,2 )       NULL,
    CONSTRAINT      SALES_PK
    PRIMARY KEY     (TimeID, CustomerID, ProductNumber),
    CONSTRAINT      PS_TIMELINE_FK FOREIGN KEY(TimeID)
                            REFERENCES TIMELINE(TimeID)
                                    ON UPDATE NO ACTION
                                    ON DELETE NO ACTION,
    CONSTRAINT      PS_CUSTOMER_FK FOREIGN KEY(CustomerID)
                            REFERENCES CUSTOMER(CustomerID)
                                    ON UPDATE NO ACTION
                                    ON DELETE NO ACTION,
    CONSTRAINT      PS_PRODUCT_FK FOREIGN KEY(ProductNumber)
                            REFERENCES PRODUCT(ProductNumber)
                                    ON UPDATE NO ACTION
                                    ON DELETE NO ACTION
    );

INSERT INTO PRODUCT_SALES (TimeID,CustomerID,ProductNumber,Quantity,UnitPrice,Total )
VALUES(41197, 3, 'VK001', 1, 14.95, 14.95);