💻
notes
  • Initial page
  • sql
    • date-and-time
    • Ordering Columns
    • Replacing Portions of Text
    • count-rows
    • Changing the Case of Strings
    • Create Excerpts with Substring
    • Transactions
    • removing-data
    • Finding Length of Strings
    • add-row-to-a-table
    • limit-and-paginate-results
    • Concatenating Strings
    • SQL JOINs
    • basic-math
    • updating-rows-in-a-table
    • Subqueries
    • Set Operations
    • SQL Basics Cheatsheet
  • ruby
    • gems
      • Auto use Ruby version with gemset
      • Must Have Gems
      • Create Devise user without any validation
    • rails
      • What are the differences between #where and #find?
  • postgresql
    • Export database dump from Heroku and import to local database
  • glossary
  • vim
    • Edit Recorded Macros
    • Sort Multiple Lines
    • Search and Replace
    • Folding
  • iTerm
  • git
    • Git
  • Command Line Utilities
  • How To Use Notes
  • Terminal Cheatsheet for Mac
Powered by GitBook
On this page
  • Updating All Rows in a Table
  • Updating Specific Rows
  • Removing Data from All Rows in a Table

Was this helpful?

  1. sql

updating-rows-in-a-table

Updating All Rows in a Table

An update statement for all rows:

UPDATE <table> SET <column> = <value>;

The = sign is different from an equality operator from a WHERE condition. It's an assignment operator because you're assigning a new value to something.

Examples:

UPDATE users SET password = "thisisabadidea";
UPDATE products SET price = 2.99;

Update multiple columns in all rows:

UDPATE <table> SET <column 1> = <value 1>, <column 2> = <value 2>;

Examples:

UPDATE users SET first_name = "Anony", last_name = "Moose";
UPDATE products SET stock_count = 0, price = 0;

Updating Specific Rows

An update statement for specific rows:

UPDATE <table> SET <column> = <value> WHERE <condition>;

Examples:

UPDATE users SET password = "thisisabadidea" WHERE id = 3;
UPDATE blog_posts SET view_count = 1923 WHERE title = "SQL is Awesome";

Update multiple columns for specific rows:

UDPATE <table> SET <column 1> = <value 1>, <column 2> = <value 2> WHERE <condition>;

Examples:

UPDATE users SET entry_url = "/home", last_login = "2016-01-05" WHERE id = 329;
UPDATE products SET status = "SOLD OUT", availability = "In 1 Week" WHERE stock_count = 0;

Removing Data from All Rows in a Table

To delete all rows from a table:

DELETE FROM <table>;

Examples:

DELETE FROM logs;
DELETE FROM users;
DELETE FROM products;
Previousbasic-mathNextSubqueries

Last updated 5 years ago

Was this helpful?