SQL Server MSSQL insert not working

kjthegm6  于 2023-03-07  发布在  其他
关注(0)|答案(3)|浏览(168)

I'm doing excercises from the db kroenke book. Anyone can see why this insert:

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

doesn't work on the following table:

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
        );

The format looks fine but getting this error:

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

Thanks!

hi3rlvi2

hi3rlvi21#

Just used the column names in the Insert statement and it has worked.

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

-- Added column names in the insert statement and it worked.

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

gkn4icbw2#

I tried the same query in my mysql the insert query seems to work fine! Is the table altered?

voj3qocg

voj3qocg3#

Did you created this table before with different columns or column order?

Can you try

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);

相关问题