为什么我的sqlplus代码上的复合主键出现ORA-00936错误?

时间:2017-01-23 00:34:24

标签: sql oracle sqlplus composite-primary-key ora-00936

错误发生在此代码块的最后。

SQL> CREATE TABLE Hotel
2  (hotelNo          INTEGER       NOT NULL
3  ,hotelName        VARCHAR2(50)  NOT NULL
4  ,city             VARCHAR2(50)  NOT NULL
5  ,CONSTRAINT PKHotel PRIMARY KEY (hotelNo)
6  );

Table created.

SQL> CREATE TABLE Room
2  (roomNo           INTEGER       NOT NULL
3  ,hotelNo          INTEGER       NOT NULL
4  ,type             CHAR(6)       NOT NULL
5  ,price            DECIMAL (3,2) NOT NULL
6  ,CONSTRAINT PKRoom PRIMARY KEY (roomNo, hotelNo)
7  ,CONSTRAINT FKRoom FOREIGN KEY (hotelNo) REFERENCES Hotel (hotelNo)
8  ,CONSTRAINT chk_type CHECK (type IN ('Single', 'Double', 'Family'))
9  ,CONSTRAINT chk_roomNo CHECK(roomNo > 1 AND roomNo < 100)
10  ,CONSTRAINT chk_price CHECK(price > 10.00 AND price <100.00)
11  );

Table created.

SQL> -- Q3.
SQL> CREATE TABLE Guest
2  (guestNo         INTEGER        NOT NULL
3  ,guestName       VARCHAR(50)    NOT NULL
4  ,guessAddress    VARCHAR(50)    NOT NULL
5  ,CONSTRAINT PKGuest PRIMARY KEY (guestNo)
6  );

Table created.

SQL>
SQL> CREATE TABLE Booking
2  (hotelNo         INTEGER        NOT NULL
3  ,guestNo         INTEGER        NOT NULL
4  ,dateFrom        DATE           NOT NULL
5  ,dateTo          DATE           NOT NULL
6  ,roomNo          INTEGER        NOT NULL
7  ,discount     DECIMAL(2,2)DEFAULT
8  ,CONSTRAINT PKBooking PRIMARY KEY (hotelNo,guestNo,dateFrom)
9  ,CONSTRAINT FKBooking_1 FOREIGN KEY (hotelNo, roomNo) REFERENCES Room                (hotelNo, roomNo)
10  ,CONSTRAINT FKBooking_2 FOREIGN KEY (guestNo) REFERENCES Guest (guestNo)
11  );
,CONSTRAINT PKBooking PRIMARY KEY (hotelNo,guestNo,dateFrom)
*
ERROR at line 8:
ORA-00936: missing expression

我真的不明白为什么这不起作用,因为语法很好。我假设逻辑是错误的,但我在这方面比较新,所以我不确定。我真的很感激一些反馈。

2 个答案:

答案 0 :(得分:2)

那是因为你遗漏了DISCOUNT的DEFAULT值/表达式。

答案 1 :(得分:1)

嗯,错误就在这里:

,discount    DECIMAL(2,2)DEFAULT

如果您在列定义中启动了DEFAULT子句 - 您应该指定构成默认值的表达式 - 或者将DEFAULT关键字留下......

或者我错过了什么?