How to update records in a column with random numbers in MySQL?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable746 (
   Number int
);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable746 values(100);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable746 values(200);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable746 values(300);
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable746 values(400);
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable746;

This will produce the following output -

+--------+
| Number |
+--------+
| 100    |
| 200    |
| 300    |
| 400    |
+--------+
4 rows in set (0.00 sec)

Following is the query to update records with random number:

mysql> update DemoTable746 set Number=CAST(RAND() * 10000 AS UNSIGNED);
Query OK, 4 rows affected (0.13 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable746;

This will produce the following output -

+--------+
| Number |
+--------+
| 4422   |
| 1350   |
| 3485   |
| 3371   |
+--------+
4 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 07:27:50
Advertisements