INSERT语句给出了不同的错误

时间:2013-10-31 11:24:12

标签: sql insert oracle11g

我正在尝试运行包含INSERT语句的文件。这些语句会给我not enough values错误或unique constraint错误。

以下是我的create table语句:

CREATE TABLE PART 
    (PNum VARCHAR(25) NOT NULL,
     PName VARCHAR(75) NOT NULL,
     PUnitPrice NUMBER(7,2) NOT NULL,
     ComponentOf VARCHAR(25),
            CONSTRAINT PART_PKEY PRIMARY KEY(PNum),
            CONSTRAINT PART_FKEY FOREIGN KEY(ComponentOf)
            REFERENCES PART(PNum)
    };
CREATE TABLE MANUFACTURER
    (MName VARCHAR(50) NOT NULL,
     MAddress VARCHAR(100) NOT NULL,
     MPhone VARCHAR(25) NOT NULL,
            CONSTRAINT MANUFACTURER_PKEY PRIMARY KEY(MName),
    );
CREATE TABLE PART-MANUFACTURED 
    (MDate DATE,not null
     PNum VARCHAR(25) NOT NULL,
     MName VARCHAR(50) NOT NULL,
     Quantity NUMBER(10) NOT NULL,
            CONSTRAINT PART-MANUFACTURED_PKEY PRIMARY KEY(MName,PNum,MDate),
            CONSTRAINT PART-MANUFACTURED_FKEY1  FOREIGN KEY(PNum)
                REFERENCES PART(PNum),
            CONSTRAINT PART-MANUFACTURED_FKEY2 FOREIGN KEY(MName)
                REFERENCES MANUFACTURER(MName)
    );
CREATE TABLE CUSTOMER
    (CNum VARCHAR(25) NOT NULL,
     CName VARCHAR(75) NOT NULL,
     CType VARCHAR(20) NOT NULL,
            CONSTRAINT CUSTOMER_PKEY PRIMARY KEY(CNum),
            CONSTRAINT CHECK_CType 
                CHECK(CType IN (‘INDIVIDUAL,’INSTITUITION’))
    );
CREATE TABLE ORDERS
    (CNum VARCHAR(25) NOT NULL,
     PNum VARCHAR(25) NOT NULL,
     OrderDate DATE NOT NULL,
     OrderQuantity NUMBER(7,2) NOT NULL,
            CONSTRAINT ORDERS_PKEY PRIMARY KEY(CNum,PNum,OrderDate),
            CONSTRAINT ORDERS_FKEY1 FOREIGN KEY(CNum)
                REFERENCES CUSTOMER(CNum),
            CONSTRAINT ORDERS_FKEY2 FOREIGN KEY(PNum)
                REFERENCES PART(PNum)
    );

以下是给出not enough values错误的声明:

INSERT INTO PART
VALUES('S001','System-Economy',1100,null);
INSERT INTO PART
VALUES('M001','Monitor-17 inch',250,'S001');

对于unique constraints错误,我认为我必须按照主要和引用键的顺序插入数据吗?

那么我需要更改什么才能使insert语句有效?

1 个答案:

答案 0 :(得分:1)

我在插入时没有看到任何错误。问题出在你的create table上。很多要纠正。试试这个

CREATE TABLE PART ( PNUM VARCHAR ( 25 ) NOT NULL,
                PNAME VARCHAR ( 75 ) NOT NULL,
                PUNITPRICE NUMBER ( 7, 2 ) NOT NULL,
                COMPONENTOF VARCHAR ( 25 ),
                CONSTRAINT PART_PKEY PRIMARY KEY ( PNUM ),
                CONSTRAINT PART_FKEY FOREIGN KEY
                    ( COMPONENTOF )
                     REFERENCES PART ( PNUM ) );

CREATE TABLE MANUFACTURER ( MNAME VARCHAR ( 50 ) NOT NULL,
                       MADDRESS VARCHAR ( 100 ) NOT NULL,
                       MPHONE VARCHAR ( 25 ) NOT NULL,
                       CONSTRAINT MANUFACTURER_PKEY PRIMARY KEY ( MNAME ) );

CREATE TABLE PART_MANUFACTURED ( MDATE DATE NOT NULL,
                           PNUM VARCHAR ( 25 ) NOT NULL,
                           MNAME VARCHAR ( 50 ) NOT NULL,
                           QUANTITY NUMBER ( 10 ) NOT NULL,
                           CONSTRAINT PART_MANUFACTURED_PKEY PRIMARY KEY
                               ( MNAME, PNUM, MDATE ),
                           CONSTRAINT PART_MANUFACTURED_FKEY1 FOREIGN KEY
                               ( PNUM )
                                REFERENCES PART ( PNUM ),
                           CONSTRAINT PART_MANUFACTURED_FKEY2 FOREIGN KEY
                               ( MNAME )
                                REFERENCES MANUFACTURER ( MNAME ) );

CREATE TABLE CUSTOMER ( CNUM VARCHAR ( 25 ) NOT NULL,
                    CNAME VARCHAR ( 75 ) NOT NULL,
                    CTYPE VARCHAR ( 20 ) NOT NULL,
                    CONSTRAINT CUSTOMER_PKEY PRIMARY KEY ( CNUM ),
                    CONSTRAINT CHECK_CTYPE CHECK
                        ( CTYPE IN ('INDIVIDUAL', 'INSTITUITION') ) );

CREATE TABLE ORDERS ( CNUM VARCHAR ( 25 ) NOT NULL,
                  PNUM VARCHAR ( 25 ) NOT NULL,
                  ORDERDATE DATE NOT NULL,
                  ORDERQUANTITY NUMBER ( 7, 2 ) NOT NULL,
                  CONSTRAINT ORDERS_PKEY PRIMARY KEY
                      ( CNUM, PNUM, ORDERDATE ),
                  CONSTRAINT ORDERS_FKEY1 FOREIGN KEY
                      ( CNUM )
                       REFERENCES CUSTOMER ( CNUM ),
                  CONSTRAINT ORDERS_FKEY2 FOREIGN KEY
                      ( PNUM )
                       REFERENCES PART ( PNUM ) );

INSERT INTO
      PART
VALUES
      ( 'S001',
        'System-Economy',
        1100,
        NULL );


INSERT INTO
      PART
VALUES
      ( 'M001',
        'Monitor-17 inch',
        250,
        'S001' );