MySQL: making a column unique? - mysql

I have a table that is in production. I realize that some of the columns should be unique. Is it safe to go into phpMyAdmin and change those columns to make it unique?
ALTER TABLE `foo` ADD UNIQUE ( `bar` )

Follow the below steps to apply unique column value from phpmyadmin panel:
Go to the table structure. Click on the unique keyword as like below -
Click on the ok from confirmation box -
Unique value constraint for column will apply.
Or you can run mysql query:
ALTER TABLE user ADD UNIQUE(email);

You do not have duplicates -> will apply the key without issues
You do have duplicates -> will give an error message, nothing happened to your data
All is unique, except several rows with NULL in them, unique constraint is still applied, as NULL is not checked when checking for unique values (you can have the entire table have a NULL value in a unique field without any error message).
One more thing, if you have a prod DB, you must also have a dev DB which you can test on without fear, right?

If there are already some duplicate values in those columns, then this will generate an error. If there aren't any duplicate values in those columns, then you will be fine.

It will only be a problem if the pre-existing values on the table are not unique, otherwise I don't think there will be any problem.

I had this problem and my values were not unique. I also couldn't find an easy way to edit this problem in PHPMyAdmin. Here's how I solved it:
I clicked into the table I needed to update
I exported the table, changing it to be a CSV export and then edited
it manually to update the non-unique values.
Making sure I was still in the table I had exported (because I
wanted to keep the headers intact), I imported my newly saved CSV
Hope that saves someone some time in the future.

Related

Importing SQL file "Duplicate entry '0' for key 'PRIMARY'"

I know there is similar questions that are answered but please try to help me out.
I'm importing a backup of my MyBB database into a new host using PHPMyAdmin. When trying to import the .sql file, I get the following error:
SQL query:
INSERT INTO mybb_datacache( `title` , `cache` )
VALUES (
'internal_settings', 'a:1:{s:14:\"encryption_key\";s:32:\"rrvohvVATtOauucNTmEXAmvNvbw9ujvb\";}'
);
MySQL said:
#1062 - Duplicate entry 'internal_settings' for key 'PRIMARY'
I can't figure out the problem. I emptied all tables and clean install of the database four times already. I've been trying to figure this out all day and it's very frustrating.
(link removed)
Please help me out. How do I fix this problem?
When you export your sql from php admin
Select "custom" as export method"
then, instead of 'insert', choose "update"
This will perform update-statements and prevent duplicated inserts.
Make sure the column set as your PRIMARY KEY is set to AUTO_INCREMENT
From the current version of the question, it appears that the title column of the mybb_datacache table is the Primary Key and already has a record with the value 'internal_settings' in it. If this is indeed the case, then the problem is that all records must have unique values for their Primary Key.
I had this problem, and i found what the problem is.
The thing is that, the field cannot be empty. ie, if you are altering a table to add primary key, make sure you have some values in that field. or if u are importing a sql file, make sure that that field in the file has some values....
this solved my problem...

Check for the respective data in a specific column and if not detected, then insert. Otherwise update

I need to insert new row into table foo. But before insert those data, I need to check there was already inserted a row for the respective user name. If there has been already inserted, then I need to update the current data with the new data.
I know to do this using PHP if condition. But I love to do this using MySQL functions/statements by just a one line. Please can anyone help me?
For the example, kindly use the following statement. It should be updated.
$in = "insert into foo(username, text) values('user-x', 'user-x-text')";
Mysql_query($in);
When searching for similar questions, I got this post: Similar question with an answer. But I was struggle to use that solution since I don't know, the process occur by that code snippet will get down the server resources like speed etc. Because this script will run about 20 times per user.
Thank you.
I think INSERT ... ON DUPLICATE KEY UPDATE should be able to work
Make username a UNIQUE index, it doesn't have to be a primary key
If I'm not mistaken, DUPLICATE KEY will run only when you have a collision in any of the columns you supply that is either a primary key or unique index. In your case, text column is neither so it will be ignored for collisions.
INSERT... ON DUPLICATE KEY UPDATE works on unique indexes as confirmed by the MySql docs

mysql with duplicate key

I tried searching online for this...I had a row that I inserted into the database. I removed it. The table has a Unique Key on a particular column. When I try to insert a new row with the same value for the unique key, it fails saying duplicate entry. However, there is no duplicate entry since the row is not there! Is there a way to reset this?
I would like the table to accept values that are unique to what is there right now. I tried to remove the unique key constraint from the table to see if that would work, however, when I added it back, it was having the same issue.
Maybe you perform dirty reading? and the deletion did not commit? try use the read commit option, I think it's called isolation level.

How to alter MySQL table without losing data?

In my application, I make some changes and upload them to a testing server. Because I have no access to the server database I run ALTER commands to make changes on it.
Using a method I ran the following command on server:
ALTER TABLE `blahblahtable` ADD COLUMN `newcolumn` INT(12) NOT NULL
After that, I found that the all the data of the table has been removed. Now the table is blank.
So I need to alter the table without removing his data. Is there any way to do that?
Your question is quite obvious. You're adding a new column to the table, and setting it to NOT NULL.
To make things clearer, I will explain the reaction of the server when you run the command:
You add a new column, so every row of the table has to set a value for that column.
As you don't declare any default value, all the rows set null for this new column.
The server notices that the rows of the table have a null value on a column that doesn't allow nulls. This is illegal.
To solve the conflict, the invalid rows are deleted.
There are some good fixes for this issue:
Set a default value (recommended) for the column you're creating.
Create the column without the NOT NULL, set the appropiate values, and then make the column NOT NULL.
You can create a temp table, pass all the information from the table you want to alter, and then return the info to the altered table.

MySql Query Browser Updating row issue

I am unable to edit my table in MySqL Query Browser.
I have the primary key defined, but I am still unable to edit.
Is there anything else that I need?
Thanks
Things to check:
does your user have write access to this table?
does the primary key column is shown in your query?
does your query is showing fields present in only one table?