Scaler Topics LogoScaler Topics Logo
Search for Articles, Topics

What is ROLLBACK in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Start Learning View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

ROLLBACK leads the current transaction to traverse back, i.e., it reverts all the updates performed by the SQL statements and restores the database to the way it was before the first statement of the transaction process. ROLL BACK IN SQL

These are a few factors on which ROLLBACK is usually decided on:

  • Duration for which the transaction has been running
  • Already updated data by transaction
  • Data yet to be updated by the transaction process.

Note: Data changes captured in the log files get discarded and are not reflected in the database. Old values can be restored by ROLLBACK using the log entries. That is why this process is also known as "Undoing".

Syntax:

How to Rollback Deleted Data in SQL?

Let's see now how to restore deleted data using SQL.

Say we have a table chocolate_storedb

Prod_IDbrandchocolatesales/day
055NestleMilky Bar10000000
056AmulDark Chocolate500000
057GandourSafari200000
058CadburyDairy Milk1000000
059NestleKitkat500000

Let's delete all the contents from this table.

All contents are now deleted from the table.

Let us now use ROLLBACK and then select command to see the table.

Output:

Prod_IDbrandchocolatesales/day
055NestleMilky Bar10000000
056AmulDark Chocolate500000
057GandourSafari200000
058CadburyDairy Milk1000000
059NestleKitkat500000

Hence, we see that the data is restored after using the ROLLBACK command in SQL.

How to Rollback Updated Query in SQL?

Let us take the same table, chocolate_storedb, and update the sales value.

Prod_IDbrandchocolatesales/day
055NestleMilky Bar100000
056AmulDark Chocolate5000
057GandourSafari2000
058CadburyDairy Milk10000
059NestleKitkat5000

We see the output here as an updated table.

Let's now use ROLLBACK to restore the table.

Output:

Prod_IDbrandchocolatesales/day
055NestleMilky Bar10000000
056AmulDark Chocolate500000
057GandourSafari200000
058CadburyDairy Milk1000000
059NestleKitkat500000

Hence, here we see that the ROLLBACK command restored the table after getting updated.

Example of ROLLBACK in SQL

Let's say we create the following students_table.

student_idstudent_nameagemarks_scored
207Sylvia Plath2294
208John Keats2182
209Rudyard Kipling2287
210W.B Yeats2176
211T.S Eliot2098

This deletes the row, and the table appears as:

student_idstudent_nameagemarks_scored
207Sylvia Plath2294
208John Keats2182
209Rudyard Kipling2287
211T.S Eliot2098

Now we use ROLLBACK

and on viewing the table again using the select command, we see the table back to its original form.

Final output:

student_idstudent_nameagemarks_scored
207Sylvia Plath2294
208John Keats2182
209Rudyard Kipling2287
210W.B Yeats2176
211T.S Eliot2098

We see an undo of the changes made.

Learn More

To learn more about SQL in detail click here.

Conclusion

  • We learned that ROLLBACK in SQL is a method of undoing the changes made to the database.
  • The old values are therefore restored, and the database disk changes are discarded. Now that we know how to revert back the changes in a database, it's time to play around with some SQL codes on your editor.

玻璃钢生产厂家党建文化玻璃钢卡通雕塑规定户外大型玻璃钢雕塑制作陕西玻璃钢造型龙泰雕塑深圳坂田玻璃钢花盆哪里有承德玻璃钢仿铜雕塑定制专业玻璃钢卡通雕塑定制厂家连云港玻璃钢仿真水果雕塑宁德玻璃钢仿真水果雕塑厂家清丰玻璃钢雕塑价格安阳景观玻璃钢雕塑厂家广场玻璃钢雕塑低价批发郑州玻璃钢商场美陈上海主题商场美陈制造福州玻璃钢仿真雕塑玻璃钢蘑菇雕塑定制商场美陈安装验收单模板泸州玻璃钢雕塑制作青岛彩绘玻璃钢雕塑榆树玻璃钢雕塑工程报价广东海洋生物玻璃钢雕塑厂四川商场美陈采购大型玻璃钢雕塑哪家正规沈阳玻璃钢雕塑修复北海玻璃钢座椅雕塑报价浙江雕塑玻璃钢景区玻璃钢雕塑工程户外玻璃钢雕塑批发设计玻璃钢雕塑来宾玻璃钢雕塑定制商场美陈 武汉香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声单亲妈妈陷入热恋 14岁儿子报警汪小菲曝离婚始末遭遇山火的松茸之乡雅江山火三名扑火人员牺牲系谣言何赛飞追着代拍打萧美琴窜访捷克 外交部回应卫健委通报少年有偿捐血浆16次猝死手机成瘾是影响睡眠质量重要因素高校汽车撞人致3死16伤 司机系学生315晚会后胖东来又人满为患了小米汽车超级工厂正式揭幕中国拥有亿元资产的家庭达13.3万户周杰伦一审败诉网易男孩8年未见母亲被告知被遗忘许家印被限制高消费饲养员用铁锨驱打大熊猫被辞退男子被猫抓伤后确诊“猫抓病”特朗普无法缴纳4.54亿美元罚金倪萍分享减重40斤方法联合利华开始重组张家界的山上“长”满了韩国人?张立群任西安交通大学校长杨倩无缘巴黎奥运“重生之我在北大当嫡校长”黑马情侣提车了专访95后高颜值猪保姆考生莫言也上北大硕士复试名单了网友洛杉矶偶遇贾玲专家建议不必谈骨泥色变沉迷短剧的人就像掉进了杀猪盘奥巴马现身唐宁街 黑色着装引猜测七年后宇文玥被薅头发捞上岸事业单位女子向同事水杯投不明物质凯特王妃现身!外出购物视频曝光河南驻马店通报西平中学跳楼事件王树国卸任西安交大校长 师生送别恒大被罚41.75亿到底怎么缴男子被流浪猫绊倒 投喂者赔24万房客欠租失踪 房东直发愁西双版纳热带植物园回应蜉蝣大爆发钱人豪晒法院裁定实锤抄袭外国人感慨凌晨的中国很安全胖东来员工每周单休无小长假白宫:哈马斯三号人物被杀测试车高速逃费 小米:已补缴老人退休金被冒领16年 金额超20万

玻璃钢生产厂家 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化