I have trouble understanding what happens in the oracle database after this sql is executed:
CREATE TABLE EMPTYSTRING
(
COLUMNA VARCHAR2(1)
);
INSERT INTO EMPTYSTRING (COLUMNA) VALUES('X');
ALTER TABLE EMPTYSTRING ADD
(
COLUMNB VARCHAR2(1) DEFAULT '' NOT NULL
);
As I read, oracle treats empty strings as nulls. But then why is adding a column with a default "null" value and a constraint that says that values cannot be null legal?
More importantly, since it is legal, how is this treated internaly? If we try
SELECT * FROM EMPTYSTRING WHERE COLUMNB='';
SELECT * FROM EMPTYSTRING WHERE COLUMNB IS NULL;
we get no results. On the other hand, if we try
SELECT * FROM EMPTYSTRING;
SELECT * FROM EMPTYSTRING WHERE TRIM(COLUMNB) IS NULL;
we get:
COLUMNA COLUMNB
------- -------
X
So what is really written in the database? Why does it behave this way?
And if we try to insert a row without specifying a value for COLUMNB
INSERT INTO EMPTYSTRING (COLUMNA) VALUES('Y');
we get "cannot insert NULL" error so defaulting to empty string doesn't really work except for the rows that were in the table before we added COLUMNB.