Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Design: replace a boolean column with a timestamp column?

Earlier I have created tables this way:

create table workflow (
    id number primary key,
    name varchar2(100 char) not null,
    is_finished number(1) default 0 not null,
    date_finished date
);

Column is_finished indicates whether the workflow finished or not. Column date_finished is when the workflow was finished.

Then I had the idea "I don't need is_finished as I can just say: where data_finished is not null", and I designed without is_finished column:

create table workflow (
    id number primary key,
    name varchar2(100 char) not null,
    date_finished date
);

(We use Oracle 10)

Is it a good or bad idea? I've heard you can not have an index on a column with NULL values, so where data_finished is not null will be very slow on big tables.

like image 932
Igor Mukhin Avatar asked Oct 12 '10 16:10

Igor Mukhin


People also ask

How do you change a table with a boolean column?

ALTER TABLE table_name ALTER COLUMN col_name SET NOT NULL; Or you can put them all together in a single statement: ALTER TABLE table_name ADD COLUMN “col_name” BOOLEAN DEFAULT FALSE; This way it might take longer if the operation is huge.

How do I change a boolean to true in SQL?

Sql server does not expose a boolean data type which can be used in queries. Instead, it has a bit data type where the possible values are 0 or 1 . So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value. Save this answer.

What is the alternative of timestamp in SQL Server?

In SQL Server, ROWVERSION and TIMESTAMP data types represent automatically generated binary numbers, unique within the database. ROWVERSION and TIMESTAMP are synonyms, ROWVERSION is available since SQL Server 2005, while TIMESTAMP is deprecated and will be removed in a future version of SQL Server .

What is a column timestamp useful in solving?

You can use the timestamp column to just give return the rows that have changed since a point in time (by providing the previous timestamp).


1 Answers

Is it a good or bad idea?

Good idea.

You've eliminated space taken by a redundant column; the DATE column serves double duty--you know the work was finished, and when.

I've heard like you can't have an index on a column with NULL values, so "where data_finished is not null" will be very slow on big tables.

That's incorrect. Oracle indexes ignore NULL values.

You can create a function based index in order to get around the NULL values not being indexed, but most DBAs I've encountered really don't like them so be prepared for a fight.

like image 117
OMG Ponies Avatar answered Oct 22 '22 18:10

OMG Ponies