oracle

– oracle命令
– 创建一张学生表:学号 姓名 出生日期
create table student(sno number(6), sname varchar2(10), birthday date);
– 对表新增一列电话
alter table student add tele varchar2(11);
– 查看表结构
desc student;
– 对表中电话列长度修改20 注意:修改的长度比本身大,
– 可以随便修改,如果修改的长度比本身小,表中含有大的数据是修改不了的
alter table student modify tele varchar2(20);
– 删除表中的电话列,删除需要加column
alter table student drop column tele; – 没权限未验证

– 插入数据
– 插入date类型的默认数据,使用SQL语句查询时间的默认格式
select sysdate from dual;
– SYSDATE

– 30-APR-21

– 根据查询的格式进行相应的添加
insert into student values(1, ‘A’, ‘11-OCT-21’, ‘13312342306’);

– 修改本次会话插入的格式,只能用于本次会话,重新开的窗口不能使用
alter session set nls_date_format=‘yyyymmdd’;
– 设置会话之后插入数据
insert into student values(2, ‘sss’, ‘19950102’, ‘14412344321’);
– 对于不设置本次会话的插入所需的格式
insert into student values(3, ‘qqq’, to_date(‘20210908’, ‘yyyymmdd’), ‘18786133204’);

– 插入列空值的方式
insert into student values(9, null, to_date(‘20200908’, ‘yyyymmdd’), null);
– 或者
insert into student(sno, sname, birthday) values(8, ‘kkkk’, to_date(‘1998-09-07’, ‘yyyy-mm-dd’));

– 以某列为空值进行查询, 不能使用tele=null或者tele=’’
select * from student where tele is null;

– 疑问点
– to_char()与to_date()在不同数据库(SQLServer/ORACLE)的作用
– 能够跟Linux系统使用上下左右键 /为上一条命令,其他都是实现不了

– 拷贝一张表及表中的数据出来
create table student1 as select * from student;
– 将数据过滤掉,只拷贝表的结构
create table student1 as select * from student where 1>2;

– 删除表中的数据:truncate暗含commit操作,delete删除会进入日志,能够找回数据
truncate table student1;
delete from student1;

– 将表中数据查询出来插入到另外一个表中
insert into student1 select * from student;
– 拓展
insert into student1(sno, sname, birthday) select sno, sname, birthday from student;

– 更新一条数据
update student1 set sname=‘aa’, tele=‘13312344321’ where sno=2;
– 删除一行数据
delete student where sno=4;
delete from student where sno=4;
– 删除表
drop table student;

– 查询统计表中有多少条数据
select count(*) from student2;
– 查询数据对查询条件转为大写
select * from student where upper(sname) = ‘a’;
– 向数据库中修改一个字段带有单引号
update student set name=‘a’‘b’ where sno = 4;
– 模糊匹配查询数据:%匹配后面所有,匹配后面的一个字符
select * from student where sname like ‘A%’;
select * from student where sname like 'A
’;
– 查询数据查询条件的字符长度为2个长度的
select * from student where length(sname)=2;

– 排序查询数据:desc降序 1:表示第一列 2:表示第二列
select * from student order by sno desc;
select * from student order by sno desc, sname ;
select * from student order by 1 desc, 2 ;
– 查询数据进行起别名,注意的是别名若果有空格则需要加双引号抱起来,否则会报错
select sno 学号, sname “姓 名” from student;

– 创建一张成绩表
create table grades(sno number(5), subject varchar2(10), score number(5,2));
– 插入数据
insert into grades values(1, ‘语文’, 60);
insert into grades values(1, ‘数学’, 60);
insert into grades values(1, ‘英语’, 60);
insert into grades values(2, ‘语文’, 70);
insert into grades values(2, ‘数学’, 70);
insert into grades values(3, ‘语文’, 80);

– 要求1:查出每个学生的成绩总和
select sno, sum(score) from grades group by sno;
– 要求2:查询每门课程的平均成绩
select subject, avg(score) from grades group by subject;
– 要求3:查出 平均成绩 大于 60 的 学生的 学号 和平均成绩
select sno, avg(score) from grades group by sno having(avg(score) > 60);
– 要求4:查出 平均成绩 大于 所有学生的平均成绩 的学号 和平均成绩
select sno, avg(score) from grades group by sno having(avg(score) > (select avg(score) from grades));
– 要求5:查出 平均成绩 大于 60 的 学生的 学号 和平均成绩,并按照学号的降序排列(说明了 几个 谓语的顺序)
select sno, avg(score) from grades group by sno having(avg(score) > 60) order by sno desc;

– 将数据查询之后进行连接成字符串
select ‘学号是:’ || sno || ‘姓名是:’ || sname from student;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值