Keys Rename
# Redis Rename Command
[!(#) Redis Keys](#)
The Redis RENAME command is used to change the name of a key.
### Syntax
The basic syntax for the Redis RENAME command is as follows:
redis 127.0.0.1:6379> RENAME OLD_KEY_NAME NEW_KEY_NAME
### Available Since
>= 1.0.0
### Return Value
Returns OK if the rename is successful, otherwise returns an error.
Returns an error if OLD_KEY_NAME and NEW_KEY_NAME are the same, or if OLD_KEY_NAME does not exist. When NEW_KEY_NAME already exists, the RENAME command will overwrite the old value.
### Examples
## Examples
# Key exists and newkey does not exist
redis> SET message "hello world"
OK
redis> RENAME message greeting
OK
redis> EXISTS message # message no longer exists
(integer)0
redis> EXISTS greeting # greeting now exists
(integer)1
# When the key does not exist, an error is returned
redis> RENAME fake_key never_exists
(error) ERR no such key
# When newkey already exists, RENAME overwrites the old newkey
redis> SET pc "lenovo"
OK
redis> SET personal_computer "dell"
OK
redis> RENAME pc personal_computer
OK
redis> GET pc
(nil)
redis> GET personal_computer # The original value dell is overwritten
"lenovo"
[!(#) Redis Keys](#)
YouTip