SQLite - alter a table's column type?

对着背影说爱祢 提交于 2019-11-29 04:48:27

问题


I am starting a project involving a small database and I am considering to use SQLite. If I create a table and I defined one of the columns as text, but all the values stored are integers - is there a way to change the data type of a column? I am using SQLite Manager and I can't find a function that allows me to do that. Can I do it using a SQL command, or is it a restriction of SQLite? Probably, even with this restriction, one can find some way around and create a new table with the required column types and then import data into it.

Regards, Nick


回答1:


SQLite does not fully support ALTER TABLE statements. The only thing you can do is rename a table and/or add columns. If you want to rename a column, your best option is to create a new table with the new columns datatype/names, and to drop the old table in order to rename the new one.

Lets say, you have a table and need to rename "field-1" to "field-2": First ==>> rename the old table:

    ALTER TABLE original RENAME TO tmp;

Now create the new table based on the old table but with the updated column name:
==>> create a table with the updated columns

    CREATE TABLE original(
    field_a INT
   , field_b INT
    );

Then copy the contents across from the original table.

   INSERT INTO origignal(field_a, field_b)
   SELECT field_a, field_b
   FROM tmp;

Lastly, drop the old table.

   DROP TABLE tmp;



回答2:


No, there is no "quick" way to do this with SQLite. Not like you can with MySQL. You will have to drop the table, then re-add it.

Another thing to keep in mind is that SQLite is pretty flexible about the type of data that you can put into each section. Here is a link that describes the datatypes, and how they work: http://www.sqlite.org/datatype3.html

Another option might be to use MySQL if the feature you are speaking of is a big deal to you. It's still free and is great for small projects.




回答3:


Alter table in SqliteManger

Steps

  1. Open database in sqlite manage
  2. double click on table name
  3. will popup alter window.



回答4:


With sqllite manager 3.5 double clic on the table that you need to modify



来源:https://stackoverflow.com/questions/21199398/sqlite-alter-a-tables-column-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!