DEFAULT constraint not working in SQL Oracle?

Viewed 248

I'm trying to get a constraint (this is just test code) but when I add the data (complete the whole table) and run it with leaving out Apptime data in the values it doesn't default to hello in the table it's just '-'. I'm using Oracle Live SQL. Any clue as to how this is done? Would be good if I could do it in the schema and not a constraint but if it has to be a constraint outside that's okay.

Thank you and apologies if I did anything wrong in this question, I'm new haha.

    DROP TABLE Bok;

    CREATE TABLE Bok (
    BokID number(3) NOT NULL PRIMARY KEY,
    Appdate varchar2(4),
    Apptime varchar2(5) DEFAULT 'hello'
     );

    
 
1 Answers

In order to default to work the insert query has to use DEFAULT keyword or skip the column

INSERT INTO Bok(BokID, Appdate, appTime)
VALUES (1, 'a', DEFAULT);

INSERT INTO Bok(BokID, Appdate)
VALUES (1, 'a');

One more option when DEFAULT ON NULL is defined:

CREATE TABLE Bok (
    BokID number(3) NOT NULL PRIMARY KEY,
    Appdate varchar2(4),
    Apptime varchar2(5)  DEFAULT ON NULL  'hello'
 );

INSERT INTO Bok(BokID, Appdate, appTime)
VALUES (1, 'a', NULL);
Related