Before we learn how to update data in SQL tables, let’s create two tables we will use: customers and orders.
- customers table
|
|
- orders table
|
|
Inserting Rows Updating Rows
New rows may be inserted into a table one at a time using the SQL INSERT statement. A table name and column list are specified in the INTO clause, and a value list, enclosed in parentheses, is entered in the VALUES clause.
- Adding data into the customers table
|
|
- Adding rows into orders table.
|
|
Deleting Rows in SQL
To remove one or more rows from a SQL table, you can use the DELETE
statement. The DELETE
statement allows you to specify a condition that identifies the rows to be deleted. Be cautious when using the DELETE
statement, as it can permanently remove data from your table.
Deleting Rows with a Condition
To delete rows that meet specific criteria, you can use the DELETE
statement with a WHERE
clause. Here’s an example:
|
|
Deleting All Rows
To delete all rows from a table, you can use the DELETE
statement without a WHERE
clause. Be careful when using this, as it will remove all data from the table.
|
|
Updating Rows in SQL
To modify existing data in SQL tables, you can use the UPDATE
statement. The UPDATE
statement allows you to change the values of one or more columns in existing rows.
Updating Rows with a Condition
To update rows that meet specific criteria, you can use the UPDATE
statement with a WHERE
clause. Here’s an example:
|
|
Updating All Rows
To update all rows in a table, you can use the UPDATE
statement without a WHERE
clause. Be cautious when using this, as it will modify all rows in the table.
|
|
These are basic examples of how to delete and update rows in SQL. Always exercise caution when modifying data, especially when using DELETE
without a WHERE
clause or when updating multiple rows.