Skip to content
Advertisement

how to avoid duplicate entries

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement