• java 从零开始,学习笔记之基础入门<Oracle_基础>(三十三)


    Oracle

    数据库基本知识

     

    【训练1】 显示DEPT表的指定字段的查询。

                  输入并执行查询:

                  SELECTdeptno,dname FROM dept;

     

     

    显示结果如下:        

    DEPTNODNAME

                  --------------------------------       

                     10  ACCOUNTING

                     20  RESEARCH

                          30  SALES

                       40  OPERATIONS

     说明:结果只包含2列deptno和dname。在语句中给出要显示的列名,列名之间用“,”分隔。表头的显示默认为全部大写。对于日期和数值型数据,右对齐显示,如deptno列。对于字符型数据,左对齐显示,如dname列。

     

     

    4.判断空值NULL

                  在表中,字段值可以是空,表示该字段没有内容。如果不填写,或设置为空则我们说该字段的内容为NULL。NULL没有数据类型,也没有具体的值,但是使用特定运算可以判断出来。这个运算就是:

                  IS  [NOT] NULL

     

    【训练5】 显示经理编号没有填写的雇员。

                  输入并执行查询:

                  SELECT  ename, mgr FROM emp WHERE mgr IS NULL;

           执行结果为:

                  ENAME     MGR

                  ---------------------------

                  KING

                  注意:以下用法是错误的。

                  SELECT  ename, mgr FROM emp WHERE mgr=NULL;

    Oracle基本查询

    --创建表空间

    createtablespace java001

    datafile'E:oracleproduct10.2.0oradataorcljava001.dbf'

    size50m;

     

    --删除表空间

    droptablespace java001

     

    --创建临时表空间

    createtemporarytablespace java001_temp

    tempfile'E:oracleproduct10.2.0oradataorcljava001_temp.dbf'

    size30m;

     

    -- 创建用户账户

    createuser lu

      identifiedby lu

      defaulttablespace JAVA001

      temporarytablespace JAVA001_TEMP

      profileDEFAULT;

     

    --授予权限的时候,切换到管理员的身份,不能为自己授权

    --为用户授予权限,链接数据的权限以及创建数据库对象的权限

    grantconnectto lu

    --为用户授予访问数据库资源的权限

    grantresourceto lu

    --将管理员的权限授予用户lu

    grantdbato lu

     

    --修改用户密码为lu

    alteruser lu identifiedby lu

    --锁定和解锁

    alteruser lu accountlock;

    alteruser lu accountunlock;

    --删除用户

    drop user lu

    -- 创建序列IDSEQENCE

    createsequence IDSEQENCE

    minvalue1

    maxvalue1000

    startwith1

    incrementby1

    cache20;

     

    --dual数据中万能的表,很多信息可以在里面获取

    SELECT IDSEQENCE.NEXTVAL from dual;

     

    --orcale 数据库语言分为3大类

    --DDL:数据库定义语言--create table 创建数据

    --DML:数据库管理语言--insert ,delete

    --DCL:数据库控制语言--对于权限进行操作的grant

     

    --数据类型

     --字符型:varchar2(varchar),存储可变长度的字符

            -- char,存储固定长度

            --nvarchar 用于国际编码,多用来存储中文

     --数字型:number(),float

     --日期类型:

     selectsysdatefrom dual      

     

    --创建表

    createtable user_tb(

           userid number,

           username varchar2(30),

           userage char(3)

     

    )

     

    altertable user_tb addconstraint pk_id primarykey(userid)

     

     

     insertinto user_tb (userid,username,userage) values (2,'李四',25);

     insertinto user_tb (userid,username,userage) values (3,'王五',24);

     insertinto user_tb (userid,username,userage) values (4,'王五',24);

     insertinto user_tb (userid,username,userage) values (5,'王五',24);

     insertinto user_tb (userid,username,userage) values (6,'王五',24);

     

    --***级联删除有外键约束可以一起删除

     foreignkey(class_no) references tb_class(class_no) ondeletecascade

    --给表重命名

    rename student to tb_stu;

     

    select * from user_tb

    --采用||进行合并查询,as后面是查询结果列名

    select userid||'/'||username as ID和用户名from user_tb

    selectdistinct(username) from user_tb

     

    update user_tb set username='衣服'where userid=2;

     

    --对表进行更新操作的设置

    select * from user_tb forupdate

     

    --字符串的拼接

    select concat('027-','88888888') from user_tb

    --改成大小写

    select Upper('abc') from user_tb

     

    delete user_tb where userid='6'

    --首字母大写

    select initcap(sty_name) from tb_stu where stu_no=1

     

    --函数

    --instr(str1,str2):返回str2在str1中出现的位置,如果不存在返回0

    --相当于java中的str1.indexOf(str2);

    begin

      dbms_output.put_line(instr('abcde','b'));

    end;

     

    select * from scott.emp;

    select * from scott.dept;

    --想查询入职年份在198211号之后的员工

    --to_date转换成日期格式,yyyy-MM_dd日期标准形式

    select ename,hiredate from scott.emp  where    hiredate > to_date('1982-1-1','yyyy-MM-dd');

     

    --查询销售部的所有员工

    select ename from scott.emp where deptno=(

           select deptno from scott.dept where dname = 'SALES'

    )

     

    --查询comm字段不为空,不等于不能用"!=",等于不能用"=="

    select * from scott.emp where comm isnotnull;

    select * from scott.emp where comm isnull;

     

    --between and

    --查询工资在某范围内的员工

    select * from scott.emp where sal between3000and9000

     

    --rownum表中自带的虚拟列,用来指示结果的记录行数

    selectrownum,ename from scott.emp

     

    --采用运算符,即+,-,*,/查询结果会出现多的一列,此为计算后形成的,成为计算列

    select ename,sal,sal+2000as计算列from scott.emp;

     

    --as作为列名与别名的连接,as也可以省略,如果遇到特殊字符或空格,需要将别名用引号包起来

    select ename  员工,sal  工资from scott.emp

    select ename as "Name",sal*1.5as "员工工资(加年终奖)" from scott.emp;

     

    --使用||做连接两个字符串

    select ename||job as员工和职位from scott.emp

    select ename || ' is ' ||job as员工和职位from scott.emp;

     

    --消除重复的行

    selectdistinct job from scott.emp

    selectdistinct deptno from scott.emp

     

    --排序,order by 默认采用升序,如果要采用降序,必须制定

    select ename,sal from scott.emp orderby sal desc

     

    --多列排序,先按照第一列,然后依次往下

    select ename,sal,hiredate from scott.emp orderby sal,hiredate

     

    --对计算列进行排序,对其指定别名

    select empno,ename,sal*Months_between(sysdate,hiredate) as total from

    scott.emp orderby total;

     

    --条件查询,条件格式是放在where 关键字后面

    --中文日期格式为"DD-MM-yy"

    select ename,hiredate from scott.emp where hiredate >='1-1-1982';

     

    --多条件查询

    select * from scott.emp where (deptno=10or deptno=20) and sal<1500;

     

    --like 模糊查询,%代表0个或者多个任意的字符,下划线代表一个任意字符

    select ename from scott.emp where  ename like'A%'

     

    SELECT * FROM SCOTT.EMP WHERE ENAME LIKE'A____'

     

     

    select * from scott.dept;

  • 相关阅读:
    tiled工具使用
    shan
    随笔
    潘大神又一篇
    潘大神的文章
    最近用到这个强大的工具 PhysicsEditor (转)
    为什么要写博客?
    nyoj 998
    欧拉函数
    背包问题
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3471572.html
Copyright © 2020-2023  润新知