oracle sql基础语句

创建表:
create table emp (eno number(2),ename char(14));

数据删除:
delete emp where empno=123;

数据修改:
update emp set ename =’akdi’,job=’maker’ where eno =4;

插入数据:
insert into dept(dname,deptno) values(‘autoo’,10);

从其他表中选择插入数据:
insert into emp (empno, ename) select id,name from old_emp where department in (10,20,30,40);

使用参数:
insert into dept values(&eno,&ename);

查询表:
select * from dept;

修改表名:
alter table 表名 rename to 新表名;

添加数据:
update student set tname =’网站’ where sno =2;

改变字段类型:
alter table T1 modify id char (10);

创建自增字段:(主键)
create table menu( menuId number(10) not null primary key,
/primary key 主键 /
name varchar2(40) not null,
id_parent number(10) not null,
url varchar2(300) null);

创建视图:
create view new as select ename ,job , sal from emp where job =’manager’;

拷贝视图或者表:
create table new2 as select * from emp;

查询不从复值,倒序:
select distinct id,name,adress from T3 order by tid desc;

–绝对值:abs()
select abs (id) value from emp;

–取整函数(大):ceil()
select ceil(-2.1)value from emp;

–取整函数(小):floor()
select floor (-2.1) value from emp;

–四舍五入:round()
select round (1.235) value from emp ;

–取整函数(截取):trunc()
select trunc(-2.11) value from emp;

–取平方根:sqrt()
select sqrt() value from emp;

–取随机数: dbms_random (minvalue,maxvalue )
select dbms_random.value (2,7) value from emp;

–取符号:sign()
select sign (-3) value from emp;
select sign (2) value from emp;

–取集合的最大值:greatest(value)
select greatest(-1,3,5,7,9) value from dual; –(9)

–取集合的最小值:least(value)
select least(-1,3,5,7,9) value from dual; –(-1)

–处理Null值:nvl(空值,代替值)
select nvl(null,10) value from dual; –(10) 
select nvl(score,10) score from student;

–rownum小于某个数时可以直接作为查询条件(注意oracle不支持select top)
select * from student where rownum <3;

–区间查询
select from (select rownum rn, student. from student) where rn >3 and rn<6;

–排序+前n条
select from (select rownum rn, t. from ( select d.* from DJDRUVER d order by drivernumber)t )p where p.rn<10;

–排序+区间查询1
select from (select rownum rn, t. from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t )p where p.rn<9 and="" p.rn="">6;

–排序+区间查询2
select from (select rownum rn, t. from ( select d.* from DJDRIVER d order by DJDRIVER_DRIVERTIMES)t where rownum<9 )p="" where="" p.rn="">6;–效率远高于方式一

–分页查询效率高
select from (select rownum rn, d. from DJDRIVER d where rownum<=20 )p

–在查询的字段后追加符号
select t.id||’%’, t.rowid from table1 t

并一列
select SS a from a1
union
select SS b from b1

to_char(‘xx’,’YYYY’ )变年
substr(F_xada,1,length(F_xada)-4)缩减字段

把数据库授权给yx,授权链接把资源给yx;
grant dba to yx;
grant connect,resource to yx;

NVL条件
NVL(E1, E2)的功能为:如果E1为NULL,则函数返回E2,否则返回E1本身。但此函数有一定局限,所以就有了NVL2函数。
拓展:NVL2函数:Oracle/PLSQL中的一个函数,Oracle在NVL函数的功能上扩展,提供了NVL2函数。NVL2(E1, E2, E3)的功能为:如果E1为NULL,则函数返回E3,若E1不为null,则返回E2。string1和replace_with必须为同一数据类型,除非显式的使用TO_CHAR函数。
select nvl(sum(t.dwxhl),1)
from tb_jhde t

获取当前时间,并赋值:
select
F_PROVINCE,F_CITY,F_COUNTY,F_PROVINCEID,F_CITYID,F_COUNTYID,F_ADRESS,F_TITLE,F_DATAFROM,F_SJLY,F_FSSJ,F_RAINFALL,F_WATERDEPTH,F_WATERTYPE,F_BZ,F_TENANTID
from
W_CSLQFSXX t,
(select trunc(sysdate, ‘yyyy’) as bn from dual) bn
where
to_char(F_DATATIME,’yyyy’) = to_char(bn.bn,’yyyy’)

/创建表空间/
create tablespace yxh
logging
datafile ‘F:\03software\oracle11g\win64_11gR2_database_1of2\oradata\eyv\lwz01.dbf’
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
/删除表空间/
drop tablespace yxh;

=============================================================================

文章目录
,