Mysql insert only if records do not exist

I want to insert multiple records inside a table again and again.But i want to stop the rows getting inserted which are already present in that table.Basically i want to stop duplicacy of rows…I would also like to mention that i am not allowed to create unique index or primary key on any column of my table

It would help if you could supply your current script.

Meanwhile take a look at the online manual and if you still have problems then please feel free to ask further questions and also your attempts at solving your problem.

http://itecsoftware.com/insert-if-not-exists-in-mysql

http://php.net/manual/en/ref.mysql.php

Are you aware that MySql is soon to be redundant and MySqli is recommended but preferably PDO?

Can you please elaborate,what do u mean by saying “MySql is soon to be redundant”

I have read the manual as well as the other link…But my problem is not solved because the script contained in the link works only if the column inside the table contains a unique id or a primary key

http://php.net/manual/en/mysqlinfo.api.choosing.php

Recommended API

It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development, as it has been deprecated as of PHP 5.5.0 and will be removed in the future. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.

Please supply further information regarding the fields that require inserting.

link works only if the column inside the table contains a unique id or a primary key

I believe it is possible to search on any field including memos, it just means the search will take a longer without any indexes.

Ok fine.Thanks for the info…

I am not allowed to change the definition of the fields of table.I have rights only to execute manipulation commands…What i know about the fields is that there is no primary key or unique index on any column…So,when i will insert records, I want only new records to get inserted and stop duplicacy.

Is it not your database? You can’t run an ALTER query?
Sorry if I seem a pest, but your situation as stated sounds an extremely rare one and I have never heard of such a limitation before.

Ya it is my database.But i wanna do it without a primary key or a unique id

Ah, OK, got it thanks. There is a difference between “not allowed” and “don’t want to”

As John said

we’d need to see the table(s) schema(s) to be able to proceed.

The table schema is simplest.It simply contains two columns with data type varchar and size(255)…The table has 3 rows inserted…what i want is that when user inserts more rows from front,only new one get inserted…As much as i have googled,the solution must be in the some query like insert into tablename (column1,column2) Values (‘val1’,val2’) where NOT EXISTS (Select column1,column2 from tablename)

you’re using php?

do a SELECT first, then INSERT only if SELECT failed

Ya,I am using php,Ok i will give it a try

I used this query-INSERT into 1004_tesco(Username)Values(‘jai’) WHERE NOT EXISTS(Select Username from 1004_tesco); on phpmyadmin.

I want to know why is it showing this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE NOT EXiSTS(Select Username from 1004_tesco)’ at line 1 .

I used this query-INSERT into 1004_tesco(Username)Values(‘jai’) WHERE NOT EXISTS(Select Username from 1004_tesco); on phpmyadmin.

I want to know why is it showing this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE NOT EXiSTS(Select Username from 1004_tesco)’ at line 1 .

[quote=“arpit1221, post:16, topic:102723, full:true”]I want to know why is it showing this error [/quote]because INSERT does not allow a WHERE clause

But i have seen on google that where works with insert because select is present as a subquery with insert

[quote=“arpit1221, post:18, topic:102723, full:true”]
But i have seen on google that where works with insert because select is present as a subquery with insert
[/quote]please show the example you found

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.