2
votes

Suppose I have a table :

Table A:

Id Name     Movie   Comment 

1  Foo     Bar  anything
2  Foo     Bar  anything

Here I want to make sure that the user cannot insert Foo and Bar twice , but he is allowed to insert Foo , foobar . One solution to prevent this duplication was to add this statement ALTER TABLE table ADD UNIQUE KEY(Name , Movie). That was probably for sql not sqlite. I wrote the same command in python for sqlite3 as cur.execute("ALTER TABLE ADD UNIQUE KEY(Name,Movie)"). It gave me the error sqlite3.operationalerror: near "unique" syntax error. Is there anything that can be done while creating the table A , or is there any mistake in my sqlite query .

Help is appreciated .

1

1 Answers

1
votes
CREATE UNIQUE INDEX IDX_Movies ON table(Name, Movie)

This is standard SQL, and documented at http://sqlite.org/lang_createindex.html. (SQLite's on-line documentation is pretty comprehensive)