Изменение типа столбца SQLite и добавление ограничения PK

Использовать Center объект

    appBar: AppBar(
      title: Center(
        child: const Text('Title Centered')
      )
    )
13
задан Pål Brattberg 11 October 2012 в 18:28
поделиться

3 ответа

Поскольку СУБД не указана, это запросы DB2:

  1. Сделать ID первичным ключом:

     ALTER TABLE table
     ДОБАВИТЬ ОГРАНИЧЕНИЕ pk_id
     PRIMARY KEY (id) 
  2. Сделать соль не УНИКАЛЬНОЙ:

     ALTER TABLE table
     DROP UNIQUE  
  3. Сделать соль допускающей значение NULL:

     ALTER TABLE table
    ALTER COLUMN salt DROP NOT NULL 

Вам нужно будет выполнить реорганизацию после того, как drop not null. Это нужно сделать из командной строки.

reorg table <tableName>
1
ответ дан 1 December 2019 в 20:57
поделиться

В этом случае вы можете сделать соль на обнуляемой и удалить ограничение уникальности. . Также, если столбец id не содержит нулевых или повторяющихся значений, вы можете безопасно сделать его первичным ключом с помощью sql server management studio. ниже снимок экрана. надеюсь, что это проясняет: alt text http://img265.imageshack.us/img265/7418/91573473.png[1260 visibleили используйте следующий sql:

alter table <TableName> modify salt text null
alter table <TableName> drop constraint <Unique Constraint Name>
alter table <TableName> modify id int not null
alter table <TableName> add constraint pk<Table>d primary key (id)
0
ответ дан 1 December 2019 в 20:57
поделиться

Ниже приводится отрывок из Руководство по SQLite, в котором обсуждается команда ALTER TABLE (см. URL: SQLite Alter Table ):

SQLite поддерживает ограниченное подмножество ИЗМЕНИТЬ ТАБЛИЦУ. Команда ALTER TABLE в SQLite позволяет пользователю переименовать таблицу или добавить новый столбец в существующая таблица. Невозможно переименовать столбец, удалить столбец или добавить или удалить ограничения из table.

As the manual states, it is not possible to modify a column's type or constraints, such as converting NULL to NOT NULL. However, there is a work around by

  1. copying the old table to a temporary table,
  2. creating a new table defined as desired, and
  3. copying the data from the temporary table to the new table.

To give credit where credit is due, I learned this from the discussion on Issue #1 of hakanw's django-email-usernames project on bitbucket.org.

CREATE TABLE test_table(
    id INTEGER,
    salt TEXT NOT NULL UNIQUE,
    step INT,
    insert_date TIMESTAMP
);

ALTER TABLE test_table RENAME TO test_table_temp;

CREATE TABLE test_table(
    id INTEGER PRIMARY KEY,
    salt TEXT,
    step INT,
    insert_date TIMESTAMP
);

INSERT INTO test_table SELECT * FROM test_table_temp;

DROP TABLE test_table_temp;

Notes

  1. I used the table name test_table since SQLite will generate an error if you try to name a table as table.
  2. The INSERT INTO command will fail if your data does not conform to the new table constraints. For instance, if the original test_table contains two id fields with the same integer, you will receive an "SQL error: PRIMARY KEY must be unique" when you execute the "INSERT INTO test_table SELECT * FROM test_table_temp;" command.
  3. For all testing, I used SQLite version 3.4.0 as included as part of Python 2.6.2 running on my 13" Unibody MacBook with Mac OS X 10.5.7.
27
ответ дан 1 December 2019 в 20:57
поделиться
Другие вопросы по тегам:

Похожие вопросы: