はい、新しい列を追加しなくてもかまいません。あなたはこれをしようとする前に、完全にデータベースをバックアップする必要がありますので、データベースの破損を避けるためにそれを正しく行うには注意する必要があります:
pragma writable_schema=1;
// replace the entire table's SQL definition, where new_sql_definition contains the foreign key clause you want to add
UPDATE SQLITE_MASTER SET SQL = new_sql_definition where name = 'child' and type = 'table';
// alternatively, you might find it easier to use replace, if you can match the exact end of the sql definition
// for example, if the last column was my_last_column integer not null:
UPDATE SQLITE_MASTER SET SQL = replace(sql, 'my_last_column integer not null', 'my_last_column integer not null, foreign key (col1, col2) references other_table(col1, col2)') where name = 'child' and type = 'table';
pragma writable_schema=0;
いずれかの方法で、あなたはおそらく最初にどのようなSQLを見たいと思ってますあなたが変更を加える前に、定義は次のとおりです。
select sql from SQLITE_MASTER where name = 'child' and type = 'table';
あなたは置き換える()アプローチを使用する場合は、最初に実行して置き換える()コマンドをテストするために、実行する前に、それが役に立つかもしれません:
select update(sql, ...) from SQLITE_MASTER where name = 'child' and type = 'table';
私は古いテーブルの名前を変更し、新しいテーブルを作成し、データをコピーする方が簡単だと思います。その後、古いテーブルを削除することができます。 – tuinstoel
はい、それは簡単です。私はsqlite FAQを引用していました:http://www.sqlite.org/faq.html#q11。実際には、 'RENAME TO'は現在sqlite 3でサポートされているいくつかの' ALTER TABLE'亜種のうちの1つです。 –
それはそうでなければなりません: FOREIGN KEY(parent_id)REFERENCES parent(id) 本当に、Jonathanは「親テーブル」の名前を付けます。 実際には、テーブルはpersonという名前にする必要がありますが、... – igorludi