1つ:簡単な学生テーブルを作成します。
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 '学生地址';
データを増やす:
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 student where sid=7;
データを変更します。
update student set sname='赵四' where sid=1;
複数のフィールドの値を変更します:
update student
set sname = '李四',
sage = 20,
sbirthday = to_date('2010-01-01', 'yyyy-mm-dd'),
saddress = '广州市越秀区'
where sid = 2;
フィールドが多すぎる場合、変更するフィールドと変更するデータが分離されていないため、この方法での書き込みは少し面倒です。
別の書き方もあります(フィールドが多いと書きやすく、書き込み効率が高くなります。フィールドが少ないと感じられません):
update student
set (sname, sage, sbirthday, saddress) =
(select '王五', 21, to_date('2010-01-01', 'yyyy-mm-dd'), '北京海淀区' from dual)
where sid = 3;
フィールドの値を空に変更します。
update student
set (sname, sage, sbirthday, saddress) =
(select '', '', to_date('', ''), '' from dual)
where sid = 4;
フィールドの値を空に変更します方法2:
update student
set (sname, sage, sbirthday, saddress) =
(select '', '', to_date('', ''), '' from dual)
where sid = 5;
複数のフィールド時間を空に変更します。
方法一:
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;
オラクルの重複排除クエリ:
查询表中多余的重复记录(多个字段)
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);
除去:
delete from student t
where t.rowid not in
(select max(rowid)from student temp group by temp.sname, temp.saddress);
列を追加します。
Alter table student add createDate DATE;--添加创建时间的字段
列を削除します。
alter table student drop column createdate;--删除创建时间