I am inserting data into a table called users. It contains some basic fields - id,username,email,password,etc. The table is in a MySQL 5 DB and the only key field in the table is the ID field.
So when I insert a record with a duplicate EMAIL I get the following error:
Duplicate entry 'myemail@domain.com' for key 2
Now why would it yell at me for duplicating the email address when it doesn't know I don't want dupes? Now If I dump a SQL file of the table it starts out like this...
CREATE TABLE `tbl_users` (
`user_ID` int(11) NOT NULL auto_increment,
`user_FirstName` varchar(50) default NULL,
`user_LastName` varchar(50) default NULL,
`user_Email` varchar(50) default NULL,
Looking further down I see this:
PRIMARY KEY (`user_ID`),
UNIQUE KEY `user_Email` (`user_Email`),
KEY `user_ID` (`user_ID`)
Now where did that come from?
I don't remember putting it there? I don't even know how. Then it strikes me - I copied this user table from another database that was probably created by someone else ages ago and I simply manipulated it for my own needs.
So how do I check for unique keys like that in Navicat? Well look around I say and what you find might might surprise you - if you're not all that familiar to Navicat that is. I use it simply to create some tables, massage data once and a while, export data, copy application databases from one app to a new one. But once and a while I do something different - or try to - and find neat little tricks in Navicat.
So I looked around and what do you know - a tab called indexes. And there it was the email index type was - of all things - UNIQUE. Hot diggity!
Did you know that tab was there? Do you know where it is? It is in the table design view. Go ahead I dare you - select a table and click design. Then stop looking at all the fields in your table and look at the tabs just above.
Did you know you can set foreign keys right in Navicat - no SQL needed :) Did you know you can change the table type or set up triggers? I didn't - but now I do. And bet you a Moxie that by this time tomorrow - I will have forgotten all about it. That's why I write this blog anyway - so I can go look up what I forgot.
Good night