简体   繁体   中英

Allow Null values for indexed column or NOT NULL for indexed column in MYSQL? What is good?

I am using MYSQL as database. Check is this table definition

CREATE TABLE `test`.`header`
( 
`header_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, 
`title` VARCHAR(500) NOT NULL, 
`body` VARCHAR(5000) NOT NULL, 
`created_by_id_ref` BIGINT UNSIGNED NOT NULL, 
`created_date` DATETIME NOT NULL, 
`updated_date` DATETIME NULL DEFAULT NULL, 
`is_void` TINYINT(1) NULL DEFAULT NULL,
PRIMARY KEY (header_id`) ) ENGINE=INNODB CHARSET=latin1 COLLATE=latin1_swedish_ci; 

In my interface user can delete any of the record by simply selecting the record from a grid view. So in that case I am simply updating the "is_void" status to true.

I declared that column by this syntax. which shows above. Here it again.

`is_void` TINYINT(1) NULL DEFAULT NULL,

So If I add an index to this column is this column declaration is good? In that case records default have null values. For voided records it will be "1". So if I am going to filter any of those records for voided records I have to use

Select ........ where is_void=1;

If I want to filter non voided records I can use

Select ........ where is_void IS NULL;

So is this NULL declaration affect to my select query performance? (Remember I indexed this column)

Or Shall I declare my column as

`is_void` TINYINT(1) NOT NULL,

and then I have insert "0" for non voided records. Then if I want to filter non voided records I can use

Select ........ where is_void=0;

So What is the best? where is_void=0; or where is_void IS NULL;

Thank you very much.

In terms of performances, both approaches are equivalent * .

In terms of logic, NULL is widely regarded as meaning "unknown value" or "not applicable". Go with 1 or 0, as defined by the TRUE and FALSE constants .

Also, even though MySQL implements it as an alias for TINYINT(1) , I would advise using the ANSI-standard BOOLEAN type .


* Ok, this is not entirely true in theory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM