
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inserting random numbers into a table in MySQL?
To insert random numbers, use RAND() function from MySQL. Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.46 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(30); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 20 | | 30 | +-------+ 3 rows in set (0.00 sec)
Let us insert random numbers into a table in MySQL −
mysql> update DemoTable set Value=FLOOR(@number * rand()) + 1; Query OK, 3 rows affected (0.17 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 2 | | 2 | | 1 | +-------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- Inserting records into a MySQL table using PreparedStatement in Java?
- Inserting Records into Table using Node
- Inserting data into a new column of an already existing table in MySQL?
- Split number into 4 random numbers in JavaScript
- Display random row from a MySQL table
- Inserting multiple parameter values into a single column with MySQL?
- Can we skip column when inserting into MySQL?
- Insert JSON into a MySQL table?
- Inserting a key into a tree in Javascript
- Display an error while inserting duplicate records in a MySQL table
- Fetch random rows from a table with MySQL
- Write an JDBC example for inserting value for Blob datatype into a table?
- Select random row that exists in a MySQL table?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- Inserting an Element into Deaps
Advertisements