คำสั่ง Update คือคำสั่งการจัดการข้อมูลที่ใช้ในการแก้ไขระเบียนของตาราง อาจใช้เพื่ออัปเดตแถวเดียวตามเงื่อนไข ทุกแถวหรือชุดของแถวตามเงื่อนไขที่กำหนดโดยผู้ใช้
ใช้ร่วมกับคำสั่ง SET ในการปฏิบัติงาน คำสั่ง WHERE อาจใช้เพื่อให้ตรงกับเงื่อนไข -
ตัวอย่างที่ 1
ตัวอย่างการใช้คำสั่ง update ด้านล่างนี้ -
update table student set name=’sneha’ where branch=’CSE’;
ตัวอย่างที่ 2
รับด้านล่างเป็นอีกตัวอย่างหนึ่งของการใช้คำสั่ง update -
create table employee(ename varchar(30),department varchar(20));
insert into employee values('pinky','CSE');
insert into employee values('priya','ECE');
insert into employee values('hari','EEE');
select * from employee;
update employee set ename='sneha' where department='CSE';
select * from employee; ผลลัพธ์
คุณจะได้ผลลัพธ์ดังต่อไปนี้ -
pinky|CSE priya|ECE hari|EEE sneha|CSE priya|ECE hari|EEE
อัปเดตค่าของคอลัมน์
รับด้านล่างเป็นตัวอย่างในการปรับปรุงชุดพนักงานตาราง age=age+1:
create table employee(ename varchar(30),department varchar(20), age number(30));
insert into employee values('ram','projectmanager',40);
insert into employee values('priya','assistant director',45);
insert into employee values('hari','developer',46);
select * from employee;
update employee set age=age+2;
select * from employee; ผลลัพธ์
คุณจะได้ผลลัพธ์ดังต่อไปนี้ -
ram|projectmanager|40 priya|assistant director|45 hari|developer|46 ram|projectmanager|42 priya|assistant director|47 hari|developer|48
อัปเดตหลายคอลัมน์ในคำสั่งเดียว
รับด้านล่างเป็นตัวอย่างการปรับปรุงชุดเงินเดือนตาราง -
ที่นี่
- โบนัส=โบนัส+5000
- พื้นฐาน=พื้นฐาน+(โบนัส 0.2*)
ตัวอย่าง
create table employee(ename varchar(30),department varchar(20), age number(30), salary number(20));
insert into employee values('ram','projectmanager',40,50000);
insert into employee values('priya','assistant director',45,45000);
insert into employee values('hari','developer',46,30000);
select * from employee;
update employee set age=age+2,
salary= salary+5000;
select * from employee; ผลลัพธ์
คุณจะได้ผลลัพธ์ดังต่อไปนี้ -
ram |projectmanager |40|50000 priya|assistant director|45|45000 hari |developer |46|30000 ram |projectmanager |42|55000 priya|assistant director|47|50000 hari |developer |48|35000