i have a simple project which project give person information to database (sqlite) now i want to avoid from insert duplicate value(by name) what i cant do about it ? thank you guys
Asked
Active
Viewed 146 times
0
-
2create unique keys – Scary Wombat Jul 12 '17 at 07:20
-
1set name to be the primary key? – Bill F Jul 12 '17 at 07:20
-
2Please assign Unique Key on name column. – Jaykumar Gondaliya Jul 12 '17 at 07:21
-
1As mentioned above creating unique constraint in first step. Next important step is having a validation in app code to tell the user that name already exists in database before even inserting. – Pரதீப் Jul 12 '17 at 07:22
-
i found it making a index like this : "CREATE UNIQUE INDEX name_index ON Course(CourseName);"; – White Druid Jul 12 '17 at 08:50
2 Answers
1
Just assign UNIQUE to name column. But my suggestion is never set UNIQUE on name column because there are plenty of chances to have same name for more people. Set UNIQUE constraint to another column like ID, email, phone etc.

blb007
- 15
- 8
0
You have two options add Name as primary key or check if the name exist before each insert.
SELECT count(*) FROM "table_name" WHERE name = "name"
If it return > 0 don't make the insert.

solamente
- 266
- 2
- 15