I created a form on my website where the user can create an account. I save everything into a database connected to a website.
This is how I created a table:
CREATE TABLE Customer(
CID int(9) not null PRIMARY KEY AUTO_INCREMENT,
Email VARCHAR(100) not null,
CustomerName VARCHAR(42) not null,
Password VARCHAR(42) not null,
Phone VARCHAR(42)
);
Since users would use the email and password to later login to the website, I don't want duplicated email entries.
Now my question is should I make column email unique inside my table or should I check if the inputted email already exists in the database (with PHP)? Is there even any difference?
Making the email and username as unique for example
CREATE TABLE `Customer` (
`CID` INT(9) NOT NULL AUTO_INCREMENT,
`Email` VARCHAR(100) NOT NULL,
`CustomerName` VARCHAR(42) NOT NULL,
`Password` VARCHAR(42) NOT NULL,
`Phone` VARCHAR(42) NULL DEFAULT NULL,
PRIMARY KEY (`CID`),
UNIQUE INDEX `Email` (`Email`),
UNIQUE INDEX `CustomerName` (`CustomerName`)
);
of course you must also check it in your application, but this will prevent it.
Related
I have a table called participants:
CREATE TABLE IF NOT EXISTS `participants` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`email` varchar(30) NOT NULL,
`name` varchar(40) NOT NULL,
`birthdate` date NOT NULL,
`personal_number` varchar(16) DEFAULT NULL,
`phone` varchar(16) NOT NULL,
`photo` varchar(100) NOT NULL,
`address` text NOT NULL,
`city_id` int(3) DEFAULT NULL,
`tournament_id` int(8) NOT NULL,
`time_created` datetime NOT NULL,
`time_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `city_id` (`city_id`),
KEY `tournament_id` (`tournament_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The rule for creating participants is each registered email address can only participate in one tournament (foreign key tournament_id); therefore two records with the same email address but different tournament_id should be allowed.
Is is_unique form validation able to do this operation? Or should I create a callback or a helper?
is_unique is just a short name for value_does_not_already_exists_in_a_single_column.
So, no - it won't do the job; you'll need a callback.
I have a weird issue, well, weird in my eyes anyway.
I've got a database with ID, username, email, password etc...
The ID is the Primary key, and both the username and email have the UNIQUE key assigned.
Now the strange thing is, when I submit, lets say the following values;
username: ActionHank
email: ah#ah.com
it is added.
Now when I try adding the same values again, I get an error that it is not allowed since it would be a duplicate entry. This works great.
But, when I put in the following next;
username: ActionHank2
email: ah#ah.com
It just adds it again, so 2 users have the same e-mail address while I've given the email row the UNIQUE key. I could also just change the e-mail address instead of the username and it will be added. So somehow the MySQL only registers one of the UNIQUE keys or something, I'm confused.
Could someone help me on this matter? Thanks
Edit: I've used phpMyAdmin to create the table and exported it to SQL, here's how the create table looks:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(25) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`date_registered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip_address` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
So I think it has a composite as Michael and Kingkero suggested. How can I solve this within phpMyAdmin?
You have defined the combination of username and email to be unique. If you want each to be unique, you need to define separate unique constraints:
create table . . .
unique username,
unique email
)
Or, because these are single columns, you can just do:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(25) NOT NULL UNIQUE,
`email` varchar(255) NOT NULL UNIQUE,
`password` varchar(255) NOT NULL,
`date_registered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ip_address` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
Try this:
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
My server is on hostgator running on a linux centOS.
I'm simply trying to create a table within my database and I figured out how to get the table to get created. Although when I add the AUTO_INCREMENT setting the code doesn't execute and the table isn't created.
Why would this be and how can I correct it?
Here is my code:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
To use AUTO_INCREMENT you may have to assign the column as a primary key:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL,
PRIMARY KEY (id))";
Your query will give error
there can be only one auto column and it must be defines as a key, so add primary key to id field
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
I'm Using 3 tables here listed below:
CREATE TABLE `user` (
`uid` int(10) NOT NULL AUTO_INCREMENT,
`kid` int(3) NOT NULL,
`Email` varchar(255) DEFAULT NULL,
`del` tinyint(1) DEFAULT '0',
PRIMARY KEY (`uid`),
KEY `kid` (`kid`),
KEY `email` (`Email`)
) ENGINE=MyISA
and
CREATE TABLE `blacklist_global` (
`bgid` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`kid` int(3) DEFAULT NULL,
`stmp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`bgid`),
UNIQUE KEY `email` (`email`),
KEY `kid` (`kid`)
) ENGINE=MyISAM
and
CREATE TABLE `verteiler_user` (
`vuid` int(10) NOT NULL AUTO_INCREMENT,
`uid` int(3) NOT NULL,
`vid` int(3) NOT NULL,
`del` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`vuid`),
KEY `uid` (`uid`)
) ENGINE=MyISAM
every entry in table user is also in table verteiler_user. now i want to delete every entry from user where user.uid = verteiler_user.uid and verteiler_user.vid=XX and user.uid = XXX
so atm im doing it from php fetch all entrys from table user, all from blacklist_global.
the problem is, in the blacklist i could enter *#heloooo.de so i want to delete every email from the domain helooo.de but its very very slow.
is it possible to do it only in mysql? without php ? or any tip, of doing this faster ?
This might get what you need. Test it first before using it on production data.
DELETE FROM user WHERE uid = ? AND uid in (SELECT uid from verteiler_user WHERE vid = ?)
Edited based on comment:
DELETE FROM user WHERE uid IN (SELECT uid FROM blacklist_global WHERE email LIKE '%#somedomain.com')
Here is my USER table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`expiry` varchar(6) NOT NULL,
`contact_id` int(11) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(100) NOT NULL,
`level` int(3) NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And here is my contact_info table
CREATE TABLE IF NOT EXISTS `contact_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email_address` varchar(255) NOT NULL,
`company_name` varchar(255) NOT NULL,
`license_number` varchar(255) NOT NULL,
`phone` varchar(30) NOT NULL,
`fax` varchar(30) NOT NULL,
`mobile` varchar(30) NOT NULL,
`category` varchar(100) NOT NULL,
`country` varchar(20) NOT NULL,
`state` varchar(20) NOT NULL,
`city` varchar(100) NOT NULL,
`postcode` varchar(50) NOT NULL,
PRIMARY KEY (`id`,`email_address`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The system uses username to login users. I want to modify it in such a way that it uses email for login. But there is no email_address in users table.
I have added foreign key - email in user table(which is email_address in contact_info).
How should I query database?
No, no, no, no no. Seriously, no. Don't make me come over there :-)
You're breaking third normal form by storing the email address twice.
The relationship need only be a short one, that of id. Assuming you're not guaranteeing the IDs will be identical in the two tables (i.e., my users.id isn't necessarily equal to my contact_info.id), just add a ci_id to the users table to act as a foreign key to the contact_info table.
Then the query to get a user's username and email will be something like:
select u.username, ci.email
from users u, contact_info ci
where u.username = 'paxdiablo'
and u.ci_id = ci.id;