Oracle基础知识语法

oracle支持五种类型的完整性约束

  1. not null (非空)--防止null值进入指定的列,在单列基础上定义,默认情况下,oracle允许在任何列中有null值.
  2. check (检查)--检查在约束中指定的条件是否得到了满足.
  3. unique (唯一)--保证在指定的列中没有重复值.在该表中每一个值或者每一组值都将是唯一的.
  4. primary key (主键)--用来唯一的标识出表的每一行,并且防止出现null值,一个表只能有一个主键约束.
  5. poreign key (外部键)--通过使用公共列在表之间建立一种父子(parent-child)关系,在表上定义的外部键可以指向主键或者其他表的唯一键.oracle支持五种类型的完整性约束
  6. not null (非空)--防止null值进入指定的列,在单列基础上定义,默认情况下,oracle允许在任何列中有null值.
  7. check (检查)--检查在约束中指定的条件是否得到了满足.
  8. unique (唯一)--保证在指定的列中没有重复值.在该表中每一个值或者每一组值都将是唯一的.
  9. primary key (主键)--用来唯一的标识出表的每一行,并且防止出现null值,一个表只能有一个主键约束.
  10. poreign key (外部键)--通过使用公共列在表之间建立一种父子(parent-child)关系,在表上定义的外部键可以指向主键或者其他表的唯一键.

一.入门部分
  1. 创建表空间
    create tablespace schooltbs datafile ‘D:oracledatasourceschooltbs.dbf’ size 10M autoextend on;

  2. 删除表空间
    drop tablespace schooltbs[including contents and datafiles;

  3. 查询表空间基本信息
    select *||tablespace_name from DBA_TABLESPACES;

  4. 创建用户
    create user lihua
    identified by lihua
    default tablespace schooltbs
    temporary tablespace temp;

  5. 更改用户
    alter user lihua
    identified by 123
    default tablespace users;

  6. 锁定用户
    alter user lihua account lock|unlock;

  7. 删除用户
    drop user lihua cascade;--删除用户模式

  8. oracle数据库中的角色
    connect,

    dba,

    select_catalog_role,

    delete_catalog_role,

    execute_catalog_role,

    exp_full_database,

    imp_full_database,

    resource

  9. 授予连接服务器的角色
    grant connect to lihua;

  10.授予使用表空间的角色
    grant resource to lihua with grant option;--该用户也有授权的权限

  11.授予操作表的权限
    grant select,insert on user_tbl to scott;--当前用户
    grant delete,update on lihua.user_tbl to scott;--系统管理员

  12.修改表的结构(alter)
    Alter table 表名 add(列的名称,列的类型);

二.sql查询和sql函数
  1.sql支持的命令:
    数据定义语言(ddl):create,alter,drop
    数据操纵语言(dml):insert,delete,update,select
    数据控制语言(dcl):grant,revoke
    事务控制语言(tcl):commit,savepoint,rollback

  2.oracle数据类型
    字符,数值,日期,raw,lob

      字符型

        char:1-2000字节的定长字符

        varchar2:1-4000字节的变长字符
        long:2gb的变长字符
          注意:一个表中最多可有一列为long型
        long列不能定义唯一约束或主键约束
        long列上不能创建索引
          过程或存储过程不能接受long类型的参数。

      数值型
        number:最高精度38位
        日期时间型
        date:精确到ss
        timestamp:秒值精确到小数点后6位

      函数
        sysdate,systimestamp返回系统当前日期,时间和时区。
        更改时间的显示
          alter session set nls_date_language=’american’;
          alter session set nls_date_format=’yyyy-mm-dd’;
        oracle中的伪列
          像一个表列,但没有存储在表中
          伪列可以查询,但不能插入、更新和修改它们的值
        常用的伪列:rowid和rownum
          rowid:表中行的存储地址,可唯一标示数据库中的某一行,可以使用该列快速定位表中的行。
          rownum:查询返回结果集中的行的序号,可以使用它来限制查询返回的行数。

  3.数据定义语言
    用于操作表的命令
      create table
      alter table
      truncate table
      drop table

    修改表的命令
      alter table stu_table rename to stu_tbl;--修改表名
      alter table stu_tbl rename column stu_sex to sex;--修改列名
      alter table stu_tbl add (stu_age number);--添加新列
      alter table stu_tbl drop(sex);--删除列
      alter table stu_tbl modify(stu_sex varchar2(2));--更改列的数据类型
      alter table stu_tbl add constraint pk_stu_tbl primary key(id);--添加约束

  4.数据操纵语言
    select,update,delete,insert

      选择无重复的行:select distinct stu_name from stu_tbl;
      利用现有的表创建表:create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;
      插入来自其他表中的记录:insert into stu_tbl_log select id,stu_name,stu_age from stu_tbl;

  5.数据控制语言
    grant,revoke

  6.事务控制语言
    commit,savepoint,rollback

  7.sql操作符
    算术操作符: l+-*/
    比较操作符: l=,!=,<>,>,<,>=,<=,between-and,in,like,is null等
    逻辑操作符: land,or,not
    集合操作符: lunion,union all,intersect,minus
    连接操作符: ||

  8.sql函数
    单行函数:从表中查询的每一行只返回一个值,可出现在select子句,where子句中
    日期函数
    数字函数
    字符函数
    转换函数:tochar(),todate(),tonumber()
    其他函数:
      nvl(exp1,exp2):表达式一为null时,返回表达式二
      nvl2(exp1,exp2,exp3):表达式一为null时返回表达式三,否则返回表达式二
      nullif(exp1,exp2):两表达式相等时,返回null,否则返回表达式一
    分组函数:基于一组行来返回
      avg,min,max,sum,count
      group by,having
    分析函数
      row_number,rank,dense_rank
    示例:
      select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl
      oi,user_tbl u,order_tbl o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;

三.锁和数据库对象

  1.锁:数据库用来控制共享资源并发访问的机制。

    锁的类型:行级锁,表级锁
      行级锁:对正在被修改的行进行锁定。行级锁也被称之为排他锁。
          在使用下列语句时,oracle会自动应用行级锁:
          insert,update,delete,select…… for update
          select……for update允许用户一次锁定多条记录进行更新。
          使用commit or rollback释放锁。
      表级锁:
        lock table user_tbl in mode mode;
      表级锁类型:
        行共享 row share
        行排他 row exclusive
        共享 share
        共享行排他 share row exclusive
        排他 exclusive
        死锁:两个或两个以上的事务相互等待对方释放资源,从而形成死锁
  2.数据库对象
    oracle数据库对象又称模式对象
    数据库对象是逻辑结构的集合,最基本的数据库对象是表
    数据库对象:
      表,序列,视图,索引

    序列
      用于生成唯一,连续序号的对象。
    创建语法:
      create sequence user_id_seq
      start with 1000
      increment by 1
      maxvalue 2000
      minvalue 1000
      nocycle
      cache 1000;--指定内存中预先分配的序号
    访问序列:
      select user_id_seq.currval from dual;
      select user_id-seq.nextval from dual;
    更改删除序列:
      alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值
      drop sequence user_id_seq;
      在hibernate中访问序列:user_id_seq

    视图
      以经过定制的方式显示来自一个或多个表的数据
    创建视图:
      create or replace view
      user_tbl_view (vid,vname,vage)
      as select id,user_name,age from user_tbl
      [with check option]|[with read only];
    创建带有错误的视图:
      create force view user_tbl_force_view as
      select * from user_table;--此时user_table可以不存在
    创建外联接视图:
      create view user_stu_view as
      select u.id,u.user_name,u.password,s.ddress
      from user_tbl u,stu_tbl s
      where u.s_id(+)=s.id;--哪一方带有(+),哪一方就是次要的
    删除视图:drop user_stu_view;

    索引:用于提高sql语句执行的性能
    索引类型:唯一索引,位图索引,组合索引,基于函数的索引,反向键索引
    创建标准索引:create index user_id_index on user_tbl(id) tablespace schooltbs;
    重建索引:alter index user_id_index rebuild;
    删除索引:drop index user_id_index;
    创建唯一索引:create unique index user_id_index on user_tbl(id);
    创建组合索引:create index name_pass_index on user_tbl(user_name,password);
    创建反向键索引:create index user_id_index on user_tbl(id) reverse;

原文地址:https://www.cnblogs.com/hdjty/p/12943019.html