top of page
Writer's picturecompnomics

Updating Records Table using SQL


Introduction


In Oracle SQL, updating columns in a table involves modifying the existing data within those columns. This is a common operation used for data maintenance and correction. In this blog post, we'll explore various update statements, their syntax, and examples using a sample table.


Our Sample Table: STUDENTS

Let's assume we have a table named STUDENTS with the following columns:

Column Name

Data Type

STUDENT_ID

NUMBER(10)

STUDENT_NAME

VARCHAR2(50)

ADDRESS

VARCHAR2(100)

PHONE

VARCHAR2(20)

CITY

VARCHAR2(50)

DISTRICT

VARCHAR2(50)

STATE

VARCHAR2(50)

Basic Update Statement

The most fundamental update statement is used to modify a single column for all rows in a table:

SQL

UPDATE table_name
SET column_name = new_value
WHERE condition;
  • table_name: The name of the table you want to update.

  • column_name: The name of the column you want to modify.

  • new_value: The new value you want to assign to the column.

  • WHERE condition: An optional condition that specifies which rows to update. If omitted, all rows will be updated.


Example:

Let's update the CITY of all students to 'Chandigarh':

SQL

UPDATE STUDENTS
SET CITY = 'Chandigarh'
WHERE 1 = 1; -- Updates all rows

Updating Multiple Columns

To update multiple columns in a single statement, you can list them separated by commas:

SQL

UPDATE table_name
SET column1 = new_value1,
    column2 = new_value2,
    ...
WHERE condition;

Example:

Let's update both the CITY and DISTRICT for students with a STUDENT_ID of 101:

SQL

UPDATE STUDENTS
SET CITY = 'Mohali',
    DISTRICT = 'SAS Nagar'
WHERE STUDENT_ID = 101;

Updating Based on Conditions

You can use the WHERE clause to specify conditions that must be met for a row to be updated. For example, you might want to update only the phone numbers of students in a particular state:

SQL

UPDATE STUDENTS
SET PHONE = '1234567890'
WHERE STATE = 'Punjab';

Updating a Specific Row

To update a single row, you can use a WHERE clause that uniquely identifies the row. For instance, you could update the address of a student with a specific STUDENT_ID:

SQL

UPDATE STUDENTS
SET ADDRESS = 'Sector 45, Chandigarh'
WHERE STUDENT_ID = 200;

Precautions

  • Backup: Always create a backup of your data before performing updates, especially if you're dealing with large datasets or critical information.

  • Testing: Test your update statements on a test database or a subset of your data to ensure they produce the desired results.

  • Commit and Rollback: Use COMMIT to save your changes and ROLLBACK to undo them if necessary.


By understanding these update statements and following best practices, you can effectively modify data in your Oracle tables to meet your specific needs.

9 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page