SQL |序列

序列是一组整数1,2,3,…,它们由一些数据库系统生成并支持,以便根据需要生成唯一的值。

null
  • 序列是用户定义的绑定到模式的对象,它生成一系列数值。
  • 序列在许多数据库中经常使用,因为许多应用程序要求表中的每一行都包含唯一的值,而序列提供了一种生成序列的简单方法。
  • 数值序列是在a中生成的 气味的或降序的 以规定的时间间隔,并可配置为在超过最大值时重新启动。

语法:

CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;

sequence_name: Name of the sequence.

initial_value: starting value from where the sequence starts. 
Initial_value should be greater than or equal 
to minimum value and less than equal to maximum value.

increment_value: Value by which sequence will increment itself. 
Increment_value can be positive or negative.

minimum_value: Minimum value of the sequence.
maximum_value: Maximum value of the sequence.

cycle: When sequence reaches its set_limit 
it starts from beginning.

nocycle: An exception will be thrown 
if sequence exceeds its max_value.

实例

下面是按升序创建序列的序列查询。

  • 例1:
    CREATE SEQUENCE sequence_1
    start with 1
    increment by 1
    minvalue 0
    maxvalue 100
    cycle;
    

    上面的查询将创建一个名为 序列_1 .序列将从1开始,并将递增1,最大值为100。超过100后,序列将从起始值开始重复。

  • 例2: 下面是按降序创建序列的序列查询。
    CREATE SEQUENCE sequence_2
    start with 100
    increment by -1
    minvalue 1
    maxvalue 100
    cycle;
    

    上面的查询将创建一个名为 序列2 .序列将从100开始,应小于或等于最大值,并将以-1递增,最小值为1。

  • 使用顺序的示例: 创建一个名为students的表,列为id和name。
    CREATE TABLE students
    ( 
    ID number(10),
    NAME char(20)
    );
    

    现在将值插入表中

    INSERT into students VALUES(sequence_1.nextval,'Ramesh');
    INSERT into students VALUES(sequence_1.nextval,'Suresh');
    

    哪里 序列1。nextval 将按顺序_1中定义的顺序在id列中插入id。 输出:

     ______________________
    | ID  |      NAME      |
    ------------------------
    |  1  |     Ramesh     |
    |  2  |     Suresh     |            
     ----------------------
    

本文由 阿尔什普里特·辛格 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享