I have a field area_code in mysql table by php form. I need the validation & alert when typing the same area code which is already entered and stored in database.
The best way you do is to define a UNIQUE constraint on field area_code on the table.
ALTER TABLE tableName ADD CONSTRAINT tb_UQ UNIQUE (area_code)
if the code was executed and successful, the server will generate an error if you try to enter area_code that is already present on the table.
You could make a SELECT count statement and check if the returned rows. If so, it means that the record already exists.
SELECT COUNT(id) AS count FROM area_codes WHERE area_code = 'ABC'
If returned row is greater than 1, than the record you are trying to insert already exists.
Related
Is there any way to stop duplicate entries only if two columns are repeating the same value.
I am using MySQL.
for example
I have one table "voting" and fields are
id
vote
user_id
message_id
In this user can enter up or down vote for a message.
I don't want the user to add multiple vote for the same message
i.e if user_id 1 votes up to message_id 1
then if the same user votes up again for same message , i don't want to allow this repeating process .
I mean is there any way to set unique constraints for fields user_id and message_id and don't allow to insert a row if user_id and message_id is repeating.
I know we can stop it using logical code using php.
I am expecting answers is there any way to do this using mysql only.?
This should work:
ALTER TABLE `table`
ADD CONSTRAINT uc_user_message UNIQUE (user_id ,message_id )
This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.
i have a contactnumber column in mysql database. In contactnumber column there are more than 20,000 entries. Now when i upload new numbers through .csv file, i dont want duplicate numbers in database.
How can i avoid duplicate numbers while inserting in database.
I initially implemented logic that checks each number in .csv file with each of the number in database.
this works but takes lot of time to upload .csv file containing 1000 numbers.
Pleae suggest how to minimize time required to upload .csv file while not uploading duplicate values.
Simply add a UNIQUE constraint to the contactnumber column:
ALTER TABLE `mytable` ADD UNIQUE (`contactnumber`);
From there you can use the IGNORE option to ignore the error you'd usually be shown when inserting a duplicate:
INSERT IGNORE INTO `mytable` VALUES ('0123456789');
Alternatively, you could use the ON DUPLICATE KEY UPDATE to do something with the dupe, as detailed in this question: MySQL - ignore insert error: duplicate entry
If your contactnumber should not be repeated then make it PRIMARY or at least a UNIQUE key. That way when a value is being inserted as a duplicate, insert will fail automatically and you won't have to check beforehand.
The way I would do it is to create a temporary table.
create table my_dateasyyyymmddhhiiss as select * from mytable where 1=0;
Do your inserts into that table.
and then query out the orphans on the between mytable and the temp table based on contactnumber
then run an inner join query between the two tables and fetch out the duplicate for your telecaller tracking.
finally drop the temporary table.
Thing that this does not address are duplicates within the supplied file (don't know if that would be an issue in this problem)
Hope this help
If you don't want to insert duplicate values in table and rather wants to keep that value in different table.
You can create trigger on table.
like this:
DELIMITER $$
CREATE TRIGGER unique_key BEFORE INSERT ON table1
FOR EACH ROW BEGIN
DECLARE c INT;
SELECT COUNT(*) INTO c FROM table1 WHERE itemid = NEW.itemid;
IF (c > 0) THEN
insert into table2 (column_name) values (NEW.itemid);
END IF;
END$$
DELIMITER ;
I would recommend this way
Alter the contactnumber column as UNIQUE KEY
Using phpmyadmin import the .csv file and check the option 'Do not abort on INSERT error' under Format-Specific Options before submitting
I am running a insert statement to insert data, but I want to check for any duplicate entries based on date and then do an entry.
All I want is if today a user enters product_name='x', 'x' is unique so that no one can enter product name x again today. But of course the next day they can.
I do not want to run a select before the insert to do the checking. Is there an alternative?
You can either use
1. Insert into... on duplicate update
2. insert.. ignore
This post will answer your question
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
You can use the mysql insert into... on duplicate update syntax which will basically enter in a new row if one isn't there, or if the new row would have caused a key constraint to kick in, then it can be used to update instead.
Lets say you have the following table:
MyTable
ID | Name
1 | Fluffeh
2 | Bobby
3 | Tables
And ID is set as the primary key in the database (meaning it CANNOT have two rows with the same value in it) you would normally try to insert like this:
insert into myTable
values (1, 'Fluffster');
But this would generate an error as there is already a row with ID of 1 in it.
By using the insert on duplicate update the query now looks like this:
insert into myTable
values (1, 'Fluffster')
on duplicate key update Name='Fluffster';
Now, rather than returning an error, it updates the row with the new name instead.
Edit: You can add a unique index across two columns with the following syntax:
ALTER TABLE myTable
ADD UNIQUE INDEX (ID, `name`);
This will now let you use the syntax above to insert rows while having the same ID as other rows, but only if the name is different - or in your case, add the constraint on the varchar and date fields.
Lastly, please do add this sort of information into your question to start with, would have saved everyone a bit of time :)
I have an form, where is field called team.
<input type="text" name="team" id="team" />
I would like to inser data from that field IF that same team isn't in database yet.
Basically if user writes 'Chelsea' and that is already in database table then nothing basically happends but if it's not there yet, then it's inserted in to database table tt_clubs.
Can I check that if it's already there somehow? I'm rookie with SQL still :/
EDIT also it shouldn't matter if users writes 'chelsea' or 'Chelsea' or 'chElsea'.. all those should be same.
EDIT table structure is just, 'id' <- automatic and 'Team name'
The easy way
I believe that INSERT IGNORE would solve this problem:
INSERT IGNORE INTO tt_clubs (team) VALUES ('Chelsea');
If the name already exists in the table then the insert will simply be ignored.
Also if you have not already done so set the team field to be a unique key:
ALTER TABLE tt_clubs ADD UNIQUE(team);
Another way
Attempt to select the value first:
SELECT id
FROM tt_clubs
WHERE team LIKE 'Chelsea'
Then in PHP you can check how many rows have been returned. If there is one then don't run the insert statement otherwise insert the team name.
You can use solution like this:
Use ci collation for you table. CI means case-insensitive
Add Unique KEY on this field which is store CHELSEA.
Use Insert IGNORE INTO teams(field1) VALUES('Chelsea')
Another way:
Select row from db by this field - select id from teams where field1 = 'Chelsea'.
If row empty Insert a new row.
It totally depends on the DBMS. Oracle, doesn't have the INSERT IGNORE clause. I'd do a MERGE:
MERGE INTO my_table
USING ( SELECT 1 from dual )
ON UPPER( team_name ) = UPPER( user_input )
WHEN NOT MATCHED THEN INSERT
VALUES ( user_input )