2016-04-05 118 views
0

這應該是一個非常簡單的問題,但我只是看不出什麼是錯的。我試圖創建一個表,有幾個檢查,而不是空表約束,但是當我試圖執行命令時,我得到一個缺少右括號錯誤。Oracle表創建缺少右括號

CREATE TABLE students (
    studentID NUMBER(5) PRIMARY KEY, 
    forename VARCHAR2(15) NOT NULL, 
    surname VARCHAR2(15) NOT NULL, 
    street VARCHAR2(20), 
    city VARCHAR2(15), 
    postcode VARCHAR2(10) NOT NULL, 
    dateOfBirth DATE() NOT NULL CHECK(dateOfBirth BETWEEN DATE '1999-01-01' AND SYSDATE), 
    gender VARCHAR2(10) CHECK(gender = 'male' OR gender = 'female' OR gender = 'both'), 
    category VARCHAR2(15) NOT NULL CHECK(category = 'first year undergraduate' OR caregory = 'postgraduate'), 
    fulltimeStudent VARCHAR(5) NOT NULL CHECK(fulltimeStudent = 'yes' OR fulltimeStudent = 'no'), 
    nationality VARCHAR(25) NOT NULL, 
    smoker VARCHAR(5) CHECK(smoker = 'yes' OR smoker = 'no') AND NOT NULL, 
    specialNeeds VARCHAR(30), 
    additionalComments VARCHAR(50), 
    status VARCHAR(15) NOT NULL CHECK(status = 'placed' or status = 'waiting') 
    ); 

全部低於錯誤 - enter image description here

+0

'dateOfBirth DATE()'將其更改爲'DATE' –

回答

2

沒有必要爲括號在 「DATE()」。 另外,您不能在檢查約束中使用SYSDATE,您可能希望使用觸發器,並且在「CAREGORY」中存在拼寫錯誤。在檢查約束

CREATE TABLE students (
    studentID NUMBER(5) PRIMARY KEY, 
    forename VARCHAR2(15) NOT NULL, 
    surname VARCHAR2(15) NOT NULL, 
    street VARCHAR2(20), 
    city VARCHAR2(15), 
    postcode VARCHAR2(10) NOT NULL, 
    dateOfBirth DATE NOT NULL, 
    gender VARCHAR2(10) CHECK(gender = 'male' OR gender = 'female' OR gender = 'both'), 
    category VARCHAR2(15) NOT NULL CHECK(category = 'first year undergraduate' OR category = 'postgraduate'), 
    fulltimeStudent VARCHAR(5) NOT NULL CHECK(fulltimeStudent = 'yes' OR fulltimeStudent = 'no'), 
    nationality VARCHAR(25) NOT NULL, 
    smoker VARCHAR(5) NOT NULL CHECK(smoker = 'yes' OR smoker = 'no'), 
    specialNeeds VARCHAR(30), 
    additionalComments VARCHAR(50), 
    status VARCHAR(15) NOT NULL CHECK(status = 'placed' or status = 'waiting') 
    ); 

上SYSDATE編輯 - 添加文檔參考:

您可以像這樣運行它的檢查約束 https://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm

條件不能包含以下 結構:

Subqueries and scalar subquery expressions 

Calls to the functions that are not deterministic (CURRENT_DATE, CURRENT_TIMESTAMP, DBTIMEZONE, LOCALTIMESTAMP, SESSIONTIMEZONE, SYSDATE, SYSTIMESTAMP, UID, USER, and USERENV) 

Calls to user-defined functions 

Dereferencing of REF columns (for example, using the DEREF function) 

Nested table columns or attributes 

The pseudocolumns CURRVAL, NEXTVAL, LEVEL, or ROWNUM 

Date constants that are not fully specified 
+0

我總是假設你可以在檢查約束中使用SYSDATE,但每天都會學到新的東西!謝謝。 – DaveDavidson

+0

@DaveDavidson - 有道理的,如果你從一開始就決定一個約束是真的還是假的,答案就不應該「取決於」。如果今天的約束是真的,那麼你可以改變記錄(添加,更新,刪除),並且明天約束不再滿足?這將是在數據庫中混亂不堪的好方法。 – mathguy