Error: Duplicate entry '' for key 'email' - mysql

I have seen this error with people running php scripts before but this is happending to me in phpmyadmin ??
Error
SQL query:
UPDATE `cl56-goldeng`.`users` SET `email` = '' WHERE `users`.`id` =118
MySQL said: Documentation
#1062 - Duplicate entry '' for key 'email'
It works fine if I give the field another value, but if I clear the field and press enter I get the above error.
The table itself looks like this :

On your table cl56-goldeng.users, the field email was specified on creation to not allow more than 1 of the same value to be allowed into it. This is done using the UNIQUE identifier on table creation in MySQL. You can see more on the UNIQUE identifier at this link.
You have 2 options that you could go about doing.
First would be to remove the unique constraint on the email field. This entirely depends on your logic in your code, but seeing as emails should almost always be unique, this is not suggested.
You can drop a unique key by running the command:
alter table [table-name] drop index [unique-key-index-name];
Second, would be to use NULL instead of an empty string. My assumption is that you are setting an empty string when the users email does not exist. In this scenario, it would be better to use NULL, and then check for that when retrieving data from the database.
You can insert a NULL value by using the NULL identifier in your MySQL statement, like such:
INSERT INTO users (firstName,lastName,email)
VALUES ('Bob','Ross',NULL);
And then check for a NULL value in whatever language you are accessing this data from.

You have a unique constraint on your email field. Either rethink your logic or drop the unique constraint.

Thats because you may have declare the email as unique key, and once you enter one row of empty email, it wont except another empty email

Related

Delete ID field in MySQL

I am new to MySQL databases and I have just imported my Access database into MySQL. When I did so, my primary key (ID) filed for one particular table is a bit messed up.
I would like all primary keys to be a number, but there is on that is showing up as a the height of a person. This is not present in the Access database.
I would add a picture but I cannot because I do not have the reputation yet... There is a 5' 7" in the ID place of an actual ID. When I attempt to delete this entry using the command
DELETE FROM actor_demographics WHERE ID=5' 7";
I am not able to delete the ID key.
Any ideas?
DELETE FROM actor_demographics WHERE ID=5' 7";
Is MySQL incorrect, it should be
DELETE FROM actor_demographics WHERE ID="5' 7\"";
It seems likely your ID column has a stringlike, not numberlike, data type.
Try using a string literal in your DELETE statement, thus
DELETE FROM actor_demographics WHERE ID='5'' 7"';
Because you need a ' character in your string constant, double it to escape it.

Why does MySQL think that I'm not using a WHERE statement?

I'm trying to update a table called rep in a database called premier_products. The table's primary key is rep_num.
When I run the following statement:
update rep
set last_name = "Perry"
where rep_num = 85;
I get an error that says "You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column."
I Googled that error message and most of the responses were along the lines of "You have to use a where clause or turn off safe mode". But as you can see, I am using a where clause. Why is the error appearing if I have a where clause?
MySQL server version 5.6.20.
This image shows that rep_num is definitely my primary key:
This image shows the current rep table:
Although you save only numbers, your primary-key type is char(2) and not tinyint(2) and when you update the record you are giving numerical value instead char value in your where condition. I think thats where the indexing mechanism triggers the error and tells you, your where condition is unsafe or might yield wrong results.
in your case try
update rep
set last_name = "Perry"
where rep_num = '85';
PS: why don't you name your tables with a prefix? like tbl_rep? just a thought.

Not getting concept of null

Hy Guys,
Beginning with mysql. I am not able to grasp the concept of NULL. Check screen-shot (*declare_not_null, link*). In it when I specifically declared 'name' field to be NOT NULL. When i run the 'desc test' table command, the table description shows default value for name field to be NULL.Why is that so?
From what I have read about NULL, it connotes a missing or information that is not applicable. So when I declare a field to be NOT NULL it implies (as per my understanding) that user must enter a value for the name field else the DB engine should generate an error i.e. record will not be entered in DB. However when i run 'insert into test value();' the DB engine enters the record in table. Check screen-shot(*empty_value, link*).
FLICKR LINKS
*declare_not_null*
http://www.flickr.com/photos/55097319#N03/5302758813/
*empty_values*
Check the second screenshot on flickr
Q.2 what would be sql statemetn to drop a primary key from a table's field.
If I use 'ALTER TABLE test drop key id;' it gives the following:
ERROR:
Incorrect table definition; there can be only one auto column and it must be defined as a key.
Thanks for your help..
You are looking at the default value column. The database won't let you update or insert that column with null.
I'll take the first question:
When i run the 'desc test' table command, the table description shows default value for name field to be NULL.Why is that so?
The default being NULL means either:
You have specified that the default is NULL or
You haven't specified a default value for that column.
In this case it is the second option. It does not mean that it is possible to insert a NULL.
Q.1. I don't have access to Flickr so I can't see your screenshots, anyway, if you declare a column NOT NULL, there may be a default value for this column set, and that is why you can add a record.
Q.2. Looks like you cannot have an auto increment column that is not a Primary Key. So if you want to drop the Primary Key, you need to drop the auto increment first.

Unique, Non-Required Column in SQL Server 2008

Using SQL Server 2008, I have a requirement that email addresses in my user table must be unique, but email addresses are not required in my app. I've tried to make email addresses required, but I just cannot get the customer to budge on this one. What is the best method to create this constraint? If I add a constraint, what does the constraint look like? If I add a before insert trigger, what does that look like?
This application will be running on a multi-server web farm, and there are multiple points of entry. An application-level lock() { } is not a great option. I'm doing a select and verifying that the email doesn't exist from code right before performing the insert operation to give immediate feedback to the user. However, I would really like a database-level solution here. Just in case.
I'm using an ORM (NHibernate with AutoMapping), so I'd rather not insert or update records with a stored procedure.
Use an unique filtered index:
create table Foo (
Id int not null identity(1,1) primary key
, Name varchar(256) null
, Address varchar(max) null
, Email varchar(256) null);
create index ndxFooEmail unique on Foo(Email)
where Email is not null;
This is a sure-shot 100% bullet proof way to guarantee uniqueness of an optional value. The uniqueness will be enforced in the database server, your ORM/DAL does not need to worry with it, simply handle the error if the unique constraint is violated rather than try to duplicate the constraint in the ORM/DAL (is not really possible to do it correctly under concurrency).
See Filtered Index Design Guidelines for more details about SQL Server 2008 filtered indexes.

violation of primary key constraint .Cannot insert duplicate key in object using ADO

we are working on a users apllication using Access2003(VBA) as software language and SQL Server 2005 as database.
We are using ADO method and we encounter a problem.
when users create new record in a ADO Screen and they want to save the record after implementing it they receive this error :
error -2147217873 violation of primary key constraint 'PK_ '.Cannot insert duplicate key in object 'Pk_...'
Any help will be appreciated
Thanks in advance
The problem occures since you can't have two primary keys with the same value.
If you are using Ints as primary key, remember to put auto-increment on it.
If you are using GUID as primary key, you might forget to set the guid to sometheing else than the default empty guid, and there by trying to insert and empty guid twice.
Are you trying to insert a new record with the primary key field having a value that is already in the database. The primary key field must always contain unique values.
Check witch columnt is your PrimaryKey. If you are trying to insert value that already exist then you are getting that error.
you should create your PK value either from your code or on the SQL side. On the SQL side, when creating your database, you have to indicate that default value for "myPrimaryKey" field is uniqueIdentifier, while from code, you could have something like
myRecordset.fields("myPrimaryKey") = stGuidGen()
(check here for the stGuidGen function)
There are some pros and cons to each method. By implementing the SQL method, you do not have to care about generating PKs anymore. By doing it through code, you can store the newly generated value without having to requery the database, and reuse it immediatly in your code, and this can be very useful.
get the property of your primary key column and set the identity property to YES