1

I have a table named 'users' with the follwing columns

  • id

  • email

  • password

  • pin

    I want the 'pin' to be auto-filled with a unique 6 digit number every time a new record is added. It would be of a great help if anyone could suggest a good approach for this problem.

2

2 Answers 2

1

Define the PIN column as

pin MEDIUMINT NOT NULL AUTO_INCREMENT
Sign up to request clarification or add additional context in comments.

1 Comment

Why MEDIUMINT? That's needlessly specific.
-1

Use the zerofill option for your int field. the Mysql fills it for your.

CREATE TABLE `int6` (
  `id` int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

sample

 MariaDB [l]> drop table int6;
 Query OK, 0 rows affected (0.00 sec)

MariaDB [l]> CREATE TABLE `int6` (
    ->   `id` int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

MariaDB [l]> insert into int6 VALUES (1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [l]> select * from int6;
+--------+
| id     |
+--------+
| 000001 |
| 000002 |
| 000003 |
+--------+
3 rows in set (0.00 sec)

MariaDB [l]>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.