Oracle modify the value of multiple fields, oracle de-duplication

One: Create a simple student table:

create table STUDENT
(
  sid       NUMBER,
  sname     VARCHAR2(40),
  sage      NUMBER,
  sbirthday DATE,
  saddress  VARCHAR2(200)
);
comment on column student.sid  is '学号(主键)';
comment on column student.sname is '学生姓名';
comment on column student.sage is '学生年龄';
comment on column student.sbirthday is '学生出生年月日';
comment on column student.saddress is '学生地址';

Increase data:

insert into student (sid, sname, sage, sbirthday, saddress)
values (1, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');
insert into student (sid, sname, sage, sbirthday, saddress)
values (2, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');
insert into student (sid, sname, sage, sbirthday, saddress)
values (3, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');
insert into student (sid, sname, sage, sbirthday, saddress)
values (4, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');
insert into student (sid, sname, sage, sbirthday, saddress)
values (5, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');
insert into student (sid, sname, sage, sbirthday, saddress)
values (6, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');
insert into student (sid, sname, sage, sbirthday, saddress)
values (7, '张三', 10, to_date('1994-01-01', 'yyyy-mm-dd'), '上海市浦东新区');

delete data:

delete student where sid=7;

change the data:

update student set sname='赵四' where sid=1;

Modify the values ​​of multiple fields:

update student
  set sname = '李四',
      sage = 20,
      sbirthday = to_date('2010-01-01', 'yyyy-mm-dd'),
      saddress = '广州市越秀区'
where sid = 2;

If there are too many fields, writing in this way is a bit more troublesome, because the fields to be modified and the data to be modified are not separated.

There is another way of writing (it is convenient to write when there are many fields, and the writing efficiency is higher; when there are few fields, it will not be felt):

update student
  set (sname, sage, sbirthday, saddress) =
  (select '王五', 21, to_date('2010-01-01', 'yyyy-mm-dd'), '北京海淀区' from dual)
where sid = 3;

Modify the value of the field to be empty:

update student
  set (sname, sage, sbirthday, saddress) =
  (select '', '', to_date('', ''), '' from dual)
where sid = 4;

Modify the value of the field to be empty Method 2:

update student
  set (sname, sage, sbirthday, saddress) =
  (select '', '', to_date('', ''), '' from dual)
where sid = 5;

Modify multiple field time to be empty:

方法一:
update student
  set sname = '',
      sage = '',
      sbirthday = to_date('', ''),
      saddress = '',
      createdate = to_date('', '')
where sid = 7;
方法二 :
update student
  set (sname, sage, sbirthday, saddress,createdate) =
  (select '', '', to_date('', ''), '',to_date('', '') from dual)
where sid = 7;

oracle de-duplication query:

查询表中多余的重复记录(多个字段) 
select * from student a
 where (a.saddress, a.sname) in 
 (select saddress, sname from student group by saddress, sname having count(*) > 1)
select distinct sname from student;--去掉张三重复的名称查询
select distinct t1.sname,t1.sage,t1.saddress from  student t1;--去重查询
去重留下一条需要的:
--语法格式
delete from student(表名) t
 where t.rowid not in
       (select max(rowid) from 表名(temp) group by temp.字段名1,字段名2);

delete from student t
 where t.rowid not in 
 (select max(rowid)from student temp group by temp.sname, temp.saddress);

Removal:

delete from student t
 where t.rowid not in 
 (select max(rowid)from student temp group by temp.sname, temp.saddress);

Add a column:

Alter table student add createDate DATE;--添加创建时间的字段

Delete a column:

alter table student drop column createdate;--删除创建时间

 

 

Guess you like

Origin blog.csdn.net/qq_30764991/article/details/97702608