• oracle数据库基本操作


    --获取当前系统时间

    select systimestamp from dual ;

    --rowid

    select rowid,emp.* from emp ;

    --rownum

    select rownum,emp.* from emp ;

     --创建表

    creat table java0512(

    sid int primary key , --主键非空

    sname varchar(20) unique , --唯一约束

    birth date,

    ssex char(5) check(ssex in(' 男 ' , ' 女 ')),--检查约束 ,在check里面直接写条件,只能填条件以内的

    saddress varchar(30) default ' 山东淄博 ' , --默认约束

    idcard varchar(30) not null , --非空约束

    deptno number ,

    foreign key(deptno) references dept(deptno) --外键约束

    );

    drop table java0512 ; --删除表

    insert into java0512(sid,sname,birth,ssex,idcard,deptno)
    values(2,'海绵宝宝',to_date('1998-08-08','yyyy-MM-dd'),'男','370303xxx',20);

    --添加信息,date 类型按指定格式写

    --复制表!!!

    create table emp01 as select * from emp;

    --复制表结构

    create table emp01 as select * from emp where 1=2;

    --插入来自其他表中的数据

    insert into emp01 select * from emp where 条件

    --创建用户,赋密码

    create user 用户名 identified by 密码

    --连接数据库权限,赋值查询权限(新用户可以查询旧用户的信息)

    grant connect , resource to 新用户

    grant select on 旧用户 to 新用户 with grant option

    --新用户添加或删除别的用户的信息

    GRANT update,delete,insert ON scott.emp
    TO xiaoniu WITH GRANT OPTION;

    --撤销权限

    revoke select on scott.emp from xiaoniu 

    --给表起别名

    select * from emp e join dept d on e.depton=d.deptno

    --左外连接
    select * from dept d left join emp e on d.deptno=e.deptno
    --右外接
    select * from emp d right join dept e on d.deptno=e.deptno

    insert into emp(empno,ename,job,mgr,hiredate,sal,comm)
    values(7899,'TONY','hair',7369,to_date('1998-08-08','yyyy-mm-dd'),5000,200)
    --全外连接
    select * from emp e full join dept d on d.deptno=e.deptno

    --分页
    --第一页
    select * from(select rownum as n,emp.* from emp) where n>=1 and n<=3
    --er,n>(当前页-1) * 每页显示条数 and n<当前页*每页显示条数
    select * from(select rownum as n,emp.* from emp) where n>=3 and n<=5

    --连接操作符
    select '员工编号为'||empno||',姓名为'||ename||',的工作是'||job as info from emp
    --转大小写
    --小写
    select lower(ename),job,mgr from emp

    select upper(ename),job,mgr from emp
    --替换decode(多用于是否,性别,可直接替换0,1)
    select ename,decode(deptno,'10','开发部',20,'测试部',30,'产品部')as ename from emp
    --查询员工的工龄,当前系统时间-员工工龄
    select ename,extract(year from sysdate)-extract(year from hiredate)as age from emp;

    --把0.123转成$ 

    select to_char(0.123 , ' $0.9999 ') from dual

    --计算和

    select to_number(' 200 ')+100 as num from dual 

     --转换空值函数

    --num=sal+comm

    select ename,sal,comm,sal+comm as num from emp

     --如果comm为null就按0计算(空值转换函数nvl(字段,字段为空时转换为什么))

    select ename,sal,comm,sal+nvl(comm,0) as num from emp

    --nvl2(字段,字段不为空时的值,字段为空时的值)

    select ename,sal,comm,sal+nvl2(comm,comm,100) as num from emp

     --两数相同返回第一个值,一样返回null

    select nullif(100,200) from dual

     --并列函数

    --emp表工资排名

    select emp.*,row_number()over(order by sal desc)as num from emp

     --并列,两个第一就没有第二

    select emp . * , rank() over(order by sal desc) as num from emp

     --并列,两个第一有第二

    select emp . * ,dense_rank()over(order by sal desc) as num from emp

    --授权

    GRANT CREATE SYNONYM TO xiaoniu;
    GRANT SELECT ON SCOTT.EMP TO xiaoniu;
    GRANT DELETE ON SCOTT.EMP TO xiaoniu;
    GRANT UPDATE ON SCOTT.EMP TO xiaoniu;

    --同义词(相当于给表起一个永久性别名)

    --给表起别名,谁创建,谁使用(私有同义词)

    create synonym e for scott.emp;

    查表可以直接select * from e

    --删除私有同义词

    drop synonym e

    --创建共有的同义词,都可以调

    create public synonym e for scott.emp

    --删除共有的同义词

    drop public synonym e

    --创建序列,自增

    create sequence seq01

    start with 1

    increment by 1;

    --删除序列

    drop sequence seq01

    --清空表

    delete from java0512

    --查当前索引页的值是几

    select seq01.currval from dual

    --创建视图语句

    create view e_d
    as select emp.*,dept.deptno as dno,dname from emp join dept on emp.deptno=dept.deptno

    --设置权限

    grant create view to scott

    select * from e_d

    --创建表

    create table a_testseq
    (
    id number,
    name varchar2(10)
    );

    --创建序列
    create sequence aeq_value
    start with 1
    increment by 1;
    --添加数据

     文件-->新建-->命令窗口

    --查
    select * from a_testseq where id=78888
    create index in_test on a_testseq(id)

  • 相关阅读:
    函数之返回值
    函数之初识函数
    三元运算符
    枚举enumerate
    模块
    迭代器
    斐波那契
    leetcode155 最小栈
    leetcode94 二叉树的中序遍历
    leetcode20 有效的括号
  • 原文地址:https://www.cnblogs.com/111wdh/p/13625196.html
Copyright © 2020-2023  润新知