- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update records in a column with random numbers in MySQL?
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)
Advertisements