加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长百科 > 正文

Oracle数据库常用语法

发布时间:2020-12-26 05:14:35 所属栏目:站长百科 来源:网络整理
导读:基本 --新建表: create table table1( id varchar(300) primary key,name varchar(200) not null); --插入数据 insert into table1 (id,name) values (‘aa‘,‘bb‘); --更新数据 update table1 set id = ‘bb‘ where id=‘cc‘; --删除数据 delete from

select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) currenttime,
to_char(sysdate - interval ‘7‘ year,‘yyyy-mm-dd hh24:mi:ss‘) intervalyear,
to_char(sysdate - interval ‘7‘ month,‘yyyy-mm-dd hh24:mi:ss‘) intervalMonth,
to_char(sysdate - interval ‘7‘ day,‘yyyy-mm-dd hh24:mi:ss‘) intervalday,
to_char(sysdate - interval ‘7‘ hour,‘yyyy-mm-dd hh24:mi:ss‘) intervalHour,
to_char(sysdate - interval ‘7‘ minute,‘yyyy-mm-dd hh24:mi:ss‘) intervalMinute,
to_char(sysdate - interval ‘7‘ second,‘yyyy-mm-dd hh24:mi:ss‘) intervalSecond
from dual;

6)add_months

select add_months(sysdate,12) newtime from dual;
7)extract

select extract(month from sysdate) "This Month",
extract(year from add_months(sysdate,36)) " Years" from dual;


字符函数
--字符函数
select substr(‘abcdefg‘,1,5)substr,--字符串截取
instr(‘abcdefg‘,‘bc‘) instr,--查找子串

‘Hello‘||‘World‘ concat,--连接

trim(‘ wish ‘) trim,--去前后空格
rtrim(‘wish ‘) rtrim,--去后面空格
ltrim(‘ wish‘) ltrim,--去前面空格

trim(leading ‘w‘ from ‘wish‘) deleteprefix,--去前缀
trim(trailing ‘h‘ from ‘wish‘) deletetrailing,--去后缀
trim(‘w‘ from ‘wish‘) trim1,

ascii(‘A‘) A1,
ascii(‘a‘) A2,--ascii(转换为对应的十进制数)
chr(65) C1,
chr(97) C2,--chr(十进制转对应字符)

length(‘abcdefg‘) len,--length

lower(‘WISH‘)lower,
upper(‘wish‘)upper,
initcap(‘wish‘)initcap,--大小写变换

replace(‘wish1‘,‘1‘,‘youhappy‘) replace,--替换

translate(‘wish1‘,‘y‘)translate,--转换,对应一位(前面的位数大于等于后面的位数)
translate(‘wish1‘,‘sh1‘,‘hy‘)translate1,

concat(‘11‘,‘22‘) concat          --连接


from dual;


to_number
--to_number(expr)
--to_number(expr,format)
--to_number(expr,format,‘nls-param‘)

select to_number(‘0123‘)number1,--converts a string to number
trunc(to_number(‘0123.123‘),2) number2,
to_number(‘120.11‘,‘999.99‘) number3,
    to_number(‘0a‘,‘xx‘) number4,--converts a hex number to decimal
to_number(100000,‘xxxxxx‘) number5

from dual;


聚合函数
student表如下:


count:

--count (distinct|all)
select count(1) as count from student;--效率最高
select count(*) as count from student;
select count(distinct score) from student;
语句1结果:11

avg

--avg (distinct|all)
select avg(score) score from student;
select avg(distinct score) from student;
select classno,avg(score) score from student group by classno;
语句3输出结果:

?

max

--max (distinct|all)
select max(score) from student;
select classno,max(score) score from student group by classno;
min

--min (distinct|all)
select min(score) from student;
select classno,min(score) score from student group by classno;
stddev(standard deviation)标准差

--stddev
select stddev(score) from student;
select classno,stddev(score) score from student group by classno;
sum

--sum
select sum(score) from student;
select classno,sum(score) score from student group by classno;
median--中位数

--median
select median(score) from student;
select classno,median(score) score from student group by classno;
?案例1--学生选课
1. 创建表 stu(学生表),course(课程表),选课表(s_c)

--创建表

create table STU
(
id NUMBER not null,
name VARCHAR2(255)
) ;

create table COURSE
(
id NUMBER not null,
coursename VARCHAR2(255)
) ;

create table S_C
(
sid NUMBER,
cid NUMBER,
score NUMBER
);
2.插入数据

--插入数据
Insert into STU (ID,NAME) values (1,‘wish‘);
Insert into STU (ID,NAME) values (2,‘rain‘);
Insert into STU (ID,NAME) values (3,‘july‘);
Insert into STU (ID,NAME) values (4,‘joey‘);

Insert into COURSE (ID,COURSENAME) values (1,‘math‘);
Insert into COURSE (ID,COURSENAME) values (2,‘english‘);
Insert into COURSE (ID,COURSENAME) values (3,‘Japanese‘);
Insert into COURSE (ID,COURSENAME) values (4,‘chinese‘);

Insert into S_C (SID,CID,SCORE) values (1,80);
Insert into S_C (SID,2,90);
Insert into S_C (SID,SCORE) values (2,4,100);
Insert into S_C (SID,SCORE) values (4,SCORE) values (3,60);
3.查询学生选课情况

with vt as
(select s.id,s.name,c.coursename,sc.score from stu s,course c,s_c sc where s.id=sc.sid and c.id=sc.cid)
select * from vt order by id;
结果:

?

案例2--图书馆借阅
1.创建表: 图书(book),读者(reader),借阅(borrow)

--创建表 book
create table book(
bookId varchar2(30),--图书总编号
sortid varchar2(30),--分类号
bookname varchar2(100),--书名
author varchar2(30),--作者
publisher varchar2(100),--出版单位
price number(6,2) --价格
);

--创建表 reader
create table reader (
cardId varchar2(30),--借书证号
org varchar2(100),--单位
name varchar2(100),--姓名
gender varchar2(2),--性别
title varchar2(30),--职称
address varchar2(100) --地址
);

--创建表 borrow
create table borrow(
cardId varchar2(30),--借书证号
bookId varchar2(30),--图书总编号
borrowDate varchar2(30) --借阅时间
);
2.插入数据

--插入数据-book
insert into book (bookId,sortid,bookname,author,publisher,price)
values (‘aaa‘,‘a1‘,‘gone with the wind‘,‘CA‘,‘renmin‘,‘103‘);

insert into book (bookId,price)
values (‘bbb‘,‘a2‘,‘the little prince‘,‘CB‘,‘jixie‘,‘30‘);

insert into book (bookId,price)
values (‘ccc‘,‘a3‘,‘the ordinary world‘,‘CC‘,‘130‘);

insert into book (bookId,price)
values (‘ddd‘,‘a4‘,‘the little women‘,‘dianzi‘,‘110‘);

--插入数据-reader
insert into reader(cardid,org,name,gender,title,address)
values (‘xxx‘,‘A‘,‘wish‘,‘student‘,‘bupt‘);

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读