Home > Database > Oracle > body text

How to modify column values ​​in Oracle database

PHPz
Release: 2023-04-04 09:28:03
Original
3224 people have browsed it

Oracle database is one of the most commonly used relational database management systems in the world. When operating and maintaining the Oracle database, it is often necessary to modify the columns in the table to meet the needs of business logic. This article will introduce in detail how to modify column values ​​in Oracle database for readers' reference.

1. Use the UPDATE statement to modify column values ​​

The UPDATE statement is one of the most commonly used ways to modify data in the Oracle database. The UPDATE statement can easily modify the column values ​​in the table. . The basic syntax is as follows:

UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;

Among them, table_name represents the name of the table that needs to be modified; column1, column2, etc. represent the column names that need to be modified; value1, value2, etc. represent the values ​​that need to be modified; condition represents the conditions for modifying the data.

The following is an example. Suppose we need to change the position in the record of the employee named "Zhang San" from "Programmer" to "Software Engineer" in the table. You can use the following SQL statement to achieve this:

UPDATE employee_table
SET position = 'Software Engineer'
WHERE name = 'Zhang San';

2. Use PL/SQL statement block to modify the column value

In addition to using the UPDATE statement, we can also use the PL/SQL statement block to modify the column values ​​in the table. PL/SQL is a procedural language dedicated to Oracle database. Its syntax is similar to other programming languages ​​and can be used to write stored procedures, triggers, functions, etc.

The following is an example. Suppose we need to increase all salaries in the employee table by 500 yuan. We can use the following PL/SQL statement block to achieve this:

DECLARE
v_increase_salary NUMBER(10 ,2) := 500;
BEGIN
FOR emp IN (SELECT * FROM employee_table)
LOOP

UPDATE employee_table
SET salary = emp.salary + v_increase_salary
WHERE id = emp.id;
Copy after login

END LOOP;
COMMIT;
END;

In the above example, we first define a v_increase_salary variable to store the salary value that needs to be increased. Then, use a FOR loop to traverse all employee records and increase their salary value by 500 yuan through the UPDATE statement. Finally, submit the modification results through the COMMIT statement.

3. Use TRIGGER to modify column values

TRIGGER is a very commonly used object in Oracle database and can be used to automatically perform specific operations. By creating TRIGGER on the table, we can automatically perform related operations when data operations occur, such as modifying the values ​​of columns. The specific operation method is as follows:

CREATE OR REPLACE TRIGGER trigger_name
AFTER INSERT OR UPDATE OR DELETE
ON table_name
FOR EACH ROW
BEGIN
-- trigger_body
- - Modify the value of the specified column in the table and other operations
END;

Among them, table_name represents the name of the table where TRIGGER needs to be created; trigger_name represents the name of TRIGGER, which must not conflict with other object names; AFTER INSERT OR UPDATE OR DELETE indicates that TRIGGER is triggered when executing INSERT, UPDATE or DELETE operations; FOR EACH ROW indicates that operations are performed on each row of records separately; trigger_body indicates the specific operations that need to be performed when triggering TRIGGER.

The following is an example. Suppose we need to add a trigger to the employee table so that when any person's salary is modified, the salary value before modification will be recorded into the employee salary history table, then You can use the following SQL statement to create the corresponding TRIGGER:

CREATE OR REPLACE TRIGGER trg_save_salary
AFTER UPDATE ON employee_table
FOR EACH ROW
BEGIN
INSERT INTO salary_history_table(emp_id, old_salary, new_salary , update_date)
VALUES(:OLD.id, :OLD.salary, :NEW.salary, SYSDATE);
END;

In the above example, we created a TRIGGER named trg_save_salary , which will be triggered when an UPDATE operation occurs in the employee_table table. Through the INSERT INTO statement, we record the salary before modification into the salary_history_table table, and record other related information (employee ID, old salary value, new salary value, modification date) together.

Summary:

Oracle database is one of the most commonly used relational database management systems in the world. By mastering the use of UPDATE statements, PL/SQL statement blocks and TRIGGER, you can realize the management of tables Modify column values ​​and meet business needs. In actual applications, different modification methods need to be selected according to different business scenarios to achieve optimal operation and maintenance effects.

The above is the detailed content of How to modify column values ​​in Oracle database. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template