Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly do CREATE INDEX with SQLite

I'm trying to convert my MySQL create table statements to SQLite create table statements. Most of it I've done, however I don't know how to change MySQL's UNIQUE INDEX to Sqlites CREATE INDEX (I thought that these were roughly the same, please correct me if I'm wrong).

So I have the following MySQL table (it's changed a bit from the :

-- ----------------------------------------------------- -- Table `pyMS`.`feature` -- ----------------------------------------------------- CREATE  TABLE IF NOT EXISTS `pyMS`.`feature` (   `feature_id` VARCHAR(40) NOT NULL ,   `intensity` DOUBLE NOT NULL ,   `overallquality` DOUBLE NOT NULL ,   `quality` DOUBLE NOT NULL ,   `charge` INT NOT NULL ,   `content` VARCHAR(45) NOT NULL ,   `msrun_msrun_id` INT NOT NULL ,   PRIMARY KEY (`feature_id`, `msrun_msrun_id`) ,   UNIQUE INDEX `id_UNIQUE` (`feature_id` ASC) ,   INDEX `fk_feature_msrun1` (`msrun_msrun_id` ASC) ,   CONSTRAINT `fk_feature_msrun1`     FOREIGN KEY (`msrun_msrun_id` )     REFERENCES `pyMS`.`msrun` (`msrun_id` )     ON DELETE NO ACTION     ON UPDATE NO ACTION) ENGINE = InnoDB; 

And I changed the index according to http://www.sqlite.org/lang_createindex.html. I did also change some other things to go from MySQL to SQLite but I tested it and they work. So this is my SQLite code:

-- ----------------------------------------------------- -- Table `feature` -- ----------------------------------------------------- CREATE  TABLE IF NOT EXISTS `feature` (   `feature_id` VARCHAR(40) NOT NULL ,   `intensity` DOUBLE NOT NULL ,   `overallquality` DOUBLE NOT NULL ,   `quality` DOUBLE NOT NULL ,   `charge` INT NOT NULL ,   `content` VARCHAR(45) NOT NULL ,   `msrun_msrun_id` INT NOT NULL ,   CREATE UNIQUE INDEX `id_UNIQUE` ON `feature` (`feature_id` ASC) ,   CREATE INDEX `fk_feature_msrun1` ON `msrun` (`msrun_msrun_id` ASC) ,   CONSTRAINT `fk_feature_msrun1`     FOREIGN KEY (`msrun_msrun_id` )     REFERENCES `msrun` (`msrun_id` )     ON DELETE NO ACTION     ON UPDATE NO ACTION); 

This does not work. When I remove the INDEX lines it does work. As far as I can see the INDEX lines comply to this description http://www.sqlite.org/lang_createindex.html, I don't see where it goes wrong. So how can I change the two lines

CREATE UNIQUE INDEX `id_UNIQUE` ON `feature` (`feature_id` ASC) , CREATE INDEX `fk_feature_msrun1` ON `msrun` (`msrun_msrun_id` ASC) , 

to make their syntax correct?

like image 689
Niek de Klein Avatar asked Mar 23 '12 17:03

Niek de Klein


People also ask

How do you create an index properly?

To properly create an optimal set of indexes requires a list of the SQL to be used, an estimate of the frequency that each SQL statement will be executed, and the importance of each query. Only then can the delicate balancing act of creating the right indexes to optimize the right queries most of the time be made.

Does SQLite automatically CREATE INDEX?

By the same token, you should also omit explicit declaration of indexes on columns declared unique or unique constraints in the table definition. SQLite3 will create these indexes automatically.

What is CREATE INDEX in SQLite?

The CREATE INDEX command consists of the keywords "CREATE INDEX" followed by the name of the new index, the keyword "ON", the name of a previously created table that is to be indexed, and a parenthesized list of table column names and/or expressions that are used for the index key.

Does SQLite have an index?

A table may have multiple indexes. Whenever you create an index, SQLite creates a B-tree structure to hold the index data. The index contains data from the columns that you specify in the index and the corresponding rowid value. This helps SQLite quickly locate the row based on the values of the indexed columns.


1 Answers

CREATE UNIQUE INDEX is its own statement and cannot be used within a CREATE TABLE statement.

Move the index statements out of CREATE TABLE:

CREATE  TABLE IF NOT EXISTS `feature` (   `feature_id` VARCHAR(40) NOT NULL ,   `intensity` DOUBLE NOT NULL ,   `overallquality` DOUBLE NOT NULL ,   `quality` DOUBLE NOT NULL ,   `charge` INT NOT NULL ,   `content` VARCHAR(45) NOT NULL ,   `msrun_msrun_id` INT NOT NULL,   CONSTRAINT `fk_feature_msrun1`     FOREIGN KEY (`msrun_msrun_id` )     REFERENCES `msrun` (`msrun_id` )     ON DELETE NO ACTION     ON UPDATE NO ACTION); CREATE UNIQUE INDEX `id_UNIQUE` ON `feature` (`feature_id` ASC); CREATE INDEX `fk_feature_msrun1` ON `feature` (`msrun_msrun_id` ASC); 
like image 125
Justin ᚅᚔᚈᚄᚒᚔ Avatar answered Oct 01 '22 01:10

Justin ᚅᚔᚈᚄᚒᚔ