Hi,

> I don't understand the sqlite command "alter table add column". Please help 
> me!
> 
> I have the following 2 tables:
> 
> TABLEONE(columnA INTEGER PRIMARY KEY, columnB TEXT);
> 
> TABLETWO(columnX INTEGER PRIMARY KEY, columnY TEXT);
> 
> I want to add a new column "cloumnZ" to TABLETWO. The new column must be a 
> foreignkey and references to columnA!

If you look at the documentation (http://www.sqlite.org/lang_altertable.html), 
it says:

It is not possible to rename a column, remove a column, or add or remove 
constraints from a table.


The usual way to achieve this is to rename the existing table, recreate the 
table with the new column and constraint, and copy the data from the old table:

ALTER TABLE TABLETWO RENAME TO OLD_TABLETWO;
CREATE TABLE TABLETWO (...);
INSERT INTO TABLETWO (columnX, columnY, columnZ) SELECT (columnX, columnY, ...);

You may or may not do the final step of:

DROP TABLE OLD_TABLETWO;

I generally do this step a bit later, when I'm really really sure everything 
worked fine.

You need to decide how you want to populate ColumnZ in TABLETWO.

Hope this helps,
Neil

_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to