insert into problem
Moderator: General Moderators
-
playwright
- Forum Newbie
- Posts: 20
- Joined: Wed Jun 02, 2010 6:11 pm
insert into problem
Hello.I have created a table with some rows. Then, i want to add more rows in the table using insert into. However, i need to check if any of the rows that i add is identical to a row that already exists, so as to avoid adding identical rows in the same table. I dont want to create a new table. I tried using distinct or unique but i dont think that they are approriate. Any ideas???
Re: insert into problem
How are you defining "identical"? You mean that EVERY column contains identical data? Is there a primary key? Is the primary key an auto-increment field?playwright wrote:Hello.I have created a table with some rows. Then, i want to add more rows in the table using insert into. However, i need to check if any of the rows that i add is identical to a row that already exists, so as to avoid adding identical rows in the same table. I dont want to create a new table. I tried using distinct or unique but i dont think that they are approriate. Any ideas???
In general, you need to perform a query that searches for a row in the table that has the same data as is proposed to be added. If it finds such a record, don't insert a new row. If it doesn't find such a record, insert a new row.
-
playwright
- Forum Newbie
- Posts: 20
- Joined: Wed Jun 02, 2010 6:11 pm
Re: insert into problem
Unfortunately, i realised that rows aren't exactly identical.
I have entries that are like these:
First column:user_id (primary key) Second column:book_name
(1,'Things Fall Apart')
(2,'Things Fall Apart')
(3,'The Famished Road')
I dont want to insert entries that have the same book name for example. i cant do it by using insert ignore since the two rows are not identical.
I have entries that are like these:
First column:user_id (primary key) Second column:book_name
(1,'Things Fall Apart')
(2,'Things Fall Apart')
(3,'The Famished Road')
I dont want to insert entries that have the same book name for example. i cant do it by using insert ignore since the two rows are not identical.
Re: insert into problem
That's why I asked. Go back and read my previous post for the answer to your question.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: insert into problem
So if you are only defining unique rows by a single column (ignore the primary key in this case), then set that column as unique and use the insert ignore statement.playwright wrote: I dont want to insert entries that have the same book name for example. i cant do it by using insert ignore since the two rows are not identical.