mithlesk
mithlesk

Reputation: 9

Can we make email as unique key in mysql?

Can we make email as the unique key in a database, so that the same user cannot register again and again?

here is my code

$sql = "CREATE TABLE IF NOT EXISTS $table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
ymi text NOT NULL,
country text NOT NULL,
Matrix text NOT NULL,
name text NOT NULL,
email text NOT NULL,
number text NOT NULL,
 Dob text NOT NULL,
 fileToUpload text NOT NULL,
UNIQUE (`id`)
)

Upvotes: 1

Views: 8551

Answers (2)

Sunil Kashyap
Sunil Kashyap

Reputation: 2984

Yes, you can create an email as a unique key

CREATE TABLE table_name(
...
   UNIQUE KEY(index_column_,index_column_2,...) 
);


$sql = "CREATE TABLE IF NOT EXISTS $table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    ymi text NOT NULL,
    country text NOT NULL,
    Matrix text NOT NULL,
    name text NOT NULL,
    email varchar(190) NOT NULL,
    number text NOT NULL,
    Dob text NOT NULL,
    fileToUpload text NOT NULL,
    UNIQUE KEY unique_email (email)
)

Upvotes: 1

PHP Geek
PHP Geek

Reputation: 4033

Yes you can create the email as unique key. And auto increment ID must be primary key..

CREATE TABLE table_name (
    id int primary key autoincrement,
    name varchar(255) NULL,
    email varchar(190) UNIQUE)

And other columns can be added in same fashion.

Upvotes: 4

Related Questions