iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >Oracle系列:(24)序列
  • 322
分享到

Oracle系列:(24)序列

2024-04-02 19:04:59 322人浏览 八月长安
摘要

什么是序列【Sequence】(1)类似于Mysql中的auto_increment自动增长机制,但oracle中无auto_increment机制(2)是oracle提供的一个产生唯一数值型值的机制(3)


什么是序列【Sequence】

(1)类似于Mysql中的auto_increment自动增长机制,但oracle中无auto_increment机制

(2)是oracle提供的一个产生唯一数值型值的机制

(3)通常用于表的主健值

(4)序列只能保证唯一,不能保证连续

     声明:oracle中,只有rownum永远保持从1开始,且继续

(5)序列值,可放于内存,取之较快

 

题问:为什么oracle不直接用rownum做主健呢?

rownum=1这条记录不能永远唯一表示SMITH这个用户

但主键=1确可以永远唯一表示SMITH这个用户


主键的目的就是为了唯一地标识一条记录,而rownum无法实现唯一标识某一条记录。



为什么要用序列

(1)以前我们为主健设置值,需要人工设置值,容易出错

(2)以前每张表的主健值,是独立的,不能共享


为emp表的empno字段,创建序列emp_empno_seq,

create sequence 序列名
create sequence emp_empno_seq;


删除序列emp_empno_seq,drop sequence 序列名

drop sequence emp_empno_seq;


查询emp_empno_seq序列的当前值currval和下一个值nextval,第一次使用序列时,必须选用:序列名.nextval

select emp_empno_seq.nextval from dual;
select emp_empno_seq.currval from dual;

Oracle系列:(24)序列


使用序列,向emp表插入记录,empno字段使用序列值

insert into emp(empno) values(emp_empno_seq.nextval);
insert into emp(empno) values(emp_empno_seq.nextval);
insert into emp(empno) values(emp_empno_seq.nextval);

Oracle系列:(24)序列


修改emp_empno_seq序列的increment by属性为20,默认start with是1,alter sequence 序列名

alter sequence emp_empno_seq
increment by 20;

Oracle系列:(24)序列


修改修改emp_empno_seq序列的的increment by属性为5

alter sequence emp_empno_seq
increment by 5;


修改emp_empno_seq序列的start with属性,行吗

alter sequence emp_empno_seq
start with 100;

不行,会报错

Oracle系列:(24)序列

但是可以在创建序列的时候 ,指定起始值和增长值

Oracle系列:(24)序列



有了序列后,还能为主健手工设置值吗?

insert into emp(empno) values(9999);
insert into emp(empno) values(7900);

可以

Oracle系列:(24)序列


(讲课内容)

删除表,会影响序列吗?

你无法做insert操作


删除序列,会影响表吗?

表真正亡,序列亡

【存疑:我做了试验,彻底删除emp表之后,还能继续使用emp_empno_seq的nextval和currval】




在hibernate中,如果是访问oracle数据库服务器,那么User.hbm.xml映射文件中关于<id>标签如何配置呢?

<id name="id" column="id">
   <generator class="increment/identity/uuid/【sequence】/【native】"/>
</id>


(1)是否需要底层数据库支持

identity需要底层数据库支持auto_increment。在mysql数据库中,需要设置表的主键字段为自增长。

而increment和uuid不需要底层数据库支持、不需要设置主键字段为自增长。

(2)是否支持多线程并发操作?

increment只能单线程访问,多线程不行。

identity和uuid都支持并发操作。

(3)适用场景

如果只使用(专用)oracle数据库,可以使用sequence,Hibernate会自动在Oracle数据库中创建一个序列。

如果不确定使用oracle、mysql、sqlserver,可以使用native,它是一个通用的变量。一般都会使用native。



Hibernate帮助文档


Various additional generators

All generators implement the interface org.hibernate.id.IdentifierGenerator. This is a very simple interface. Some applications can choose to provide their own specialized implementations, however, Hibernate provides a range of built-in implementations. The shortcut names for the built-in generators are as follows:

  • increment

    generates identifiers of type longshort or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

  • identity

    supports identity columns in DB2, MySQL, MS SQLServer, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.

  • sequence

    uses a sequence in DB2, postgresql, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int

  • uuid

    Generates a 128-bit UUID based on a custom alGorithm. The value generated is represented as a string of 32 hexidecimal digits. Users can also configure it to use a separator (config parameter "separator") which separates the hexidecimal digits into 8{sep}8{sep}4{sep}8{sep}4. 

  • uuid2

    Generates a IETF RFC 4122 compliant (variant 2) 128-bit UUID. The exact "version" (the RFC term) generated depends on the pluggable "generation strategy" used. Capable of generating values as java.util.UUIDjava.lang.String or as a byte array of length 16 (byte[16]). The "generation strategy" is defined by the interface org.hibernate.id.UUIDGenerationStrategy.

  • guid

    uses a database-generated GUID string on MS SQL Server and MySQL.

  • native

    selects identity,sequence or hilo depending upon the capabilities of the underlying database.

  • assigned

    lets the application assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.

  • foreign

    uses the identifier of another associated object. It is usually used in conjunction with a <one-to-one> primary key  association.






您可能感兴趣的文档:

--结束END--

本文标题: Oracle系列:(24)序列

本文链接: https://www.lsjlt.com/news/44722.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • sql中外码怎么设置
    sql 中外码设置步骤:确定父表和子表。在子表中创建外码列,引用父表主键。使用 foreign key 约束将外码列链接到父表主键。指定引用动作,以处理父表数据更改时的子表数据操作。 ...
    99+
    2024-05-15
  • sql中having是什么
    having 子句用于过滤分组结果,应用于分组后的数据集。它与 where 子句类似,但基于分组结果而不是原始数据。用法:1. 过滤分组后的聚合值。2. 根据分组后的...
    99+
    2024-05-15
  • 在sql中空值用什么表示
    在 sql 中,空值表示未知或不存在的值,可使用 null、空字符串或特殊值表示。处理空值的方法包括使用操作符(is null/is not null)、coalesce 函数(返回第一...
    99+
    2024-05-15
    oracle
  • sql中number什么意思
    sql 中的 number 类型用于存储数值数据,包括小数和整数,特别适合货币、度量和科学数据。其精度由 scale(小数点位数)和 precision(整数字段和小数字段总位数)决定。...
    99+
    2024-05-15
  • sql中空值赋值为0怎么写
    可以通过使用 coalesce() 函数将 sql 中的空值替换为指定值(如 0)。coalesce() 的语法为 coalesce(expression, replacement),其...
    99+
    2024-05-15
  • sql中revoke语句的功能
    revoke 语句用于撤销指定用户或角色的权限或角色成员资格。可撤销的权限包括 select、insert、update、delete 等,撤销的对象类型包括表、视图、存储过程...
    99+
    2024-05-15
    敏感数据
  • sql中REVOKE是什么意思
    revoke 是 sql 中用于撤销用户或角色对数据库对象权限的命令。它通过撤销权限类型、对象级别和目标权限来实现:权限类型:撤销 select、insert、update、d...
    99+
    2024-05-15
  • sql中sp是什么意思
    sql中的sp是存储过程的缩写,它是一种预编译的、已命名的sql语句块,存储在数据库中,可以被用户通过简单命令调用。存储过程的特点有:可重用性、模块化、性能优化、安全性、事务支持。存储过...
    99+
    2024-05-15
    敏感数据
  • sql中references是什么意思
    sql 中的 references 关键字用于在外键约束中定义表之间的父-子关系。外键约束确保子表中的行都引用父表中存在的行,从而维护数据完整性。references 语法的格式为:fo...
    99+
    2024-05-15
  • sql中判断字段为空怎么写
    sql 中可通过 4 种方法判断字段是否为空:1)is null 运算符;2)is not null 运算符;3)coalesce() 函数;4)case 语句。例如,查询所有 colu...
    99+
    2024-05-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作