CHECK constraint to restrict the registration date to dates after August 26, 2005

后端 未结 2 1043
一向
一向 2021-01-28 04:32

Need some help with this:

Create a table called TEMP_STUDENT with the following columns and constraints: a column STUD_ID for the student ID and is the

相关标签:
2条回答
  • 2021-01-28 04:48

    Try this

    CREATE TABLE Temp_Student
    (STUD_ID NUMBER (8,0),
    FIRST_NAME VARCHAR2(25) NOT NULL,
    LAST_NAME VARCHAR2(25) NOT NULL,
    ZIP VARCHAR2(5),
    REGISTRATION_DATE DATE NOT NULL,
    CONSTRAINT STUD_ID_PK PRIMARY KEY(STUD_ID),
    CONSTRAINT ZIP_FK FOREIGN KEY (ZIP)
    REFERENCES ZIPCODE (ZIP),
    CONSTRAINT chk_REGISTRATION_DATE CHECK (REGISTRATION_DATE> TO_DATE('26-AUGUST-2005'))
    )
    
    0 讨论(0)
  • 2021-01-28 05:08

    You have several errors in your expression:

    1. It's to_date() not to date() (note the underscore)
    2. a function call must not be put into single quotes, so it's to_date(..), not 'to_date(...)'
    3. you repeated the column after the > operator which is also wrong.

    So the correct expression is this:

    CONSTRAINT chk_REGISTRATION_DATE 
       CHECK (REGISTRATION_DATE > TO_DATE('2005-08-26', 'yyyy-mm-dd'))
    

    Note that you should always specify a format when using to_date() otherwise the conversion is subject to the NLS setting of the server and the client and might produce strange errors.

    And even if you use a format mask you should not use a literal that depends on the current NLS language. AUGUST might not work for all languages as the month name. It's better to use the month number.

    0 讨论(0)
提交回复
热议问题