Copy table structure to new table in sqlite3

前端 未结 5 1497
梦谈多话
梦谈多话 2020-12-02 14:20

Is there an easy way to copy an existing table structure to a new one? (dont need the data, only the structure -> like id INTEGER, name varchar(20) ...)

Thx

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 14:57

    You could use a command like this:

    CREATE TABLE copied AS SELECT * FROM mytable WHERE 0
    

    but due to SQLite's dynamic typing, most type information would be lost.

    If you need just a table that behaves like the original, i.e., has the same number and names of columns, and can store the same values, this is enough.

    If you really need the type information exactly like the original, you can read the original SQL CREATE TABLE statement from the sqlite_master table, like this:

    SELECT sql FROM sqlite_master WHERE type='table' AND name='mytable'
    

提交回复
热议问题