Error in sqlite3 Foreign or foreigin

Asked

Viewed 42 times

2

I created a database on sqlite3 in version 3.9.2 and had problems with Foreign key.

alter table table_01 add column tab_2 integer foreign key references table_02(id);
    // Esse deu erro de sintaxe no foreign key
    alter table table_01 add column tab_2 integer foreigin key references table_02(id);
    // Já esse, adicionou o campo com sucesso.

as it is in the code, gdb accepted foreigin instead of Foreign, someone might know to tell me something about this?

1 answer

2


In fact, as I pass foreigin key, if using Sqlitestudio, he understands that you are passing the type, along with the integer. See the print of the same query I’ve been running here:

Insert query (identical to the one you made):

inserir a descrição da imagem aqui

How was the column after insertion:

inserir a descrição da imagem aqui

One can notice that the column was added as foreign key, but the syntax used was ignored for this purpose.

As can be seen in this SOEN response, the correct syntax for this change is:

ALTER TABLE tabela ADD COLUMN coluna TIPO REFERENCES tabela_parente(coluna_parente); 

See below in print, the column added in tests I did, running the above command:

inserir a descrição da imagem aqui

References for reading:

https://www.sqlite.org/lang_altertable.html

https://www.sqlite.org/lang_altertable.html#otheralter (important reading)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.