-1

I use SQLite 3 as my database management system.

I want to add a CONSTRAINT FOREIGN KEY to my table:

ALTER TABLE SocialMediaAccount
ADD CONSTRAINT FOREIGN KEY (memberFK) REFERENCES Member(id);

It gives me this error:

[18:09:09] Error while executing SQL query on database 'company': near "CONSTRAINT": syntax error

My table names and column names are right. I'm connected to the database.

philipxy
  • 14,867
  • 6
  • 39
  • 83

1 Answers1

0

sqlite doesn't implement all possible alter table subcommand variants. It is mostly restricted to add, removing simple column and renaming table. Adding constraint to an existing column or adding a column with a constraint is not possible. If you need to do something more elaborate than that, the common practice is to:

  1. create a new table with the new wanted schema and a temporary name
  2. then to copy data from the former table to the new one
  3. drop the former table
  4. and then rename the new table to the wanted name

For more information you may want to read the sections 7 and 8 of this documentation page https://www.sqlite.org/lang_altertable.html

davidriod
  • 937
  • 5
  • 14