如何使用SQLite正确执行CREATE INDEX

时间:2021-05-08 23:02:26

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

我正在尝试将我的MySQL创建表语句转换为SQLite创建表语句。我已经完成了大部分工作,但是我不知道如何将MySQL的UNIQUE INDEX更改为Sqlites CREATE INDEX(我认为这些大致相同,如果我错了请纠正我)。

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

所以我有以下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;

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:

我根据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);

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

这不起作用。当我删除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) ,

to make their syntax correct?

使他们的语法正确吗?

2 个解决方案

#1


42  

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

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

Move the index statements out of 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);

#2


12  

You need to look at CREATE TABLE syntax, not CREATE INDEX. SQLite will accept this form of a UNIQUE constraint in a CREATE TABLE statement.

您需要查看CREATE TABLE语法,而不是CREATE INDEX。 SQLite将在CREATE TABLE语句中接受这种形式的UNIQUE约束。

CREATE  TABLE IF NOT EXISTS `feature` (
...
  CONSTRAINT `id_UNIQUE` UNIQUE (`feature_id`),
...
);

#1


42  

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

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

Move the index statements out of 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);

#2


12  

You need to look at CREATE TABLE syntax, not CREATE INDEX. SQLite will accept this form of a UNIQUE constraint in a CREATE TABLE statement.

您需要查看CREATE TABLE语法,而不是CREATE INDEX。 SQLite将在CREATE TABLE语句中接受这种形式的UNIQUE约束。

CREATE  TABLE IF NOT EXISTS `feature` (
...
  CONSTRAINT `id_UNIQUE` UNIQUE (`feature_id`),
...
);