mysql – 如何使用SQLite正确地做CREATE INDEX

我试图将我的MySQL create table语句转换成SQLite create table语句。我已经做了大部分工作,但是我不知道如何将MySQL的独特索引更改为Sqlites CREATE INDEX(我认为这些大致相同,如果我错了,请更正我)。

所以我有以下的MySQL表(它改变了一点:

-- -----------------------------------------------------
-- 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;

而且我根据http://www.sqlite.org/lang_createindex.html改变了索引。我也改变了一些从MySQL到SQLite的其他事情,但我测试了它,他们工作。
所以这是我的SQLite代码:

-- -----------------------------------------------------
-- 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);

这不行。当我删除I​​NDEX行它可以工作。就我看到的INDEX行符合这个描述http://www.sqlite.org/lang_createindex.html,我看不出哪里出错了。那么我该怎么改两行呢?

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

使他们的语法正确吗?

CREATE UNIQUE INDEX是其自己的语句,不能在CREATE TABLE语句中使用。

将索引语句移出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);
http://stackoverflow.com/questions/9843973/how-to-correctly-do-create-index-with-sqlite

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:mysql – 如何使用SQLite正确地做CREATE INDEX