首页 > 其他分享 >Sequence Trigger

Sequence Trigger

时间:2023-05-22 11:31:54浏览次数:38  
标签:insert Sequence bcd into Trigger values SQL TEST


SQL> 
SQL> create sequence sq1
  2  start with 1
  3  increment by 1
  4  minvalue 1
  5  maxvalue 9999999
  6  nocycle
  7  nocache
  8  noorder;
 
Sequence created
 
SQL> 
SQL> create or replace trigger pn_trigger
  2  before insert on users
  3  for each row
  4  begin
  5      select sq1.nextval into:new.id from sys.dual;
  6  end;
  7  /
 
Trigger created
 
SQL> insert into users( name) values('zhsan');
 
1 row inserted
 
SQL> select * from users;
 
     ID NAME
------- ------------------------------
      1 zhsan
 
SQL> insert into users values('lisi');
 
insert into users values('lisi')
 
ORA-00947: not enough values
 
SQL> insert into users( name) values('lisi');
 
1 row inserted
 
SQL> select * from users;
 
     ID NAME
------- ------------------------------
      1 zhsan
      2 lisi
 
SQL> drop table users;
 
Table dropped
 
SQL> 
SQL> create table 表名(
  2             userid number(10) primary key,
  3             username varchar2(20)
  4             );
 
Table created
 
SQL> select * from 表名
  2  /
 
     USERID USERNAME
----------- --------------------
 
SQL> 
SQL> CREATE SEQUENCE 序列名
  2   INCREMENT BY 1   -- 每次加几个
  3       START WITH 1     -- 从1开始计数
  4       NOMAXVALUE       -- 不设置最大值
  5       NOCYCLE          -- 一直累加,不循环
  6       CACHE 10;
 
Sequence created
 
SQL> 
SQL> CREATE TRIGGER 触发器名 BEFORE
  2  insert ON 表名 FOR EACH ROW
  3  begin
  4  select 序列名.nextval into:New.userid from dual;
  5  end;
  6  /
 
Trigger created
 
SQL> commit;
 
Commit complete
 
SQL> insert into 表名(Username) values('test');
 
1 row inserted
 
SQL> insert into 表名(Username) values('test');
 
1 row inserted
 
SQL> insert into 表名(Username) values('test');
 
1 row inserted
 
SQL> select * from 表名
  2  /
 
     USERID USERNAME
----------- --------------------
          1 test
          2 test
          3 test
 
SQL> insert into 表名  values('test');
 
insert into 表名  values('test')
 
ORA-00947: not enough values
 
SQL> select * from 表名
  2  /
 
     USERID USERNAME
----------- --------------------
          1 test
          2 test
          3 test
 
SQL> 
SQL> create sequence SEQ_TEST
  2  increment by 1
  3  start with 1
  4  minvalue 1
  5  nomaxvalue
  6  nocycle
  7  /
 
Sequence created
 
SQL> create table TEST(TEST_ID number primary key,symbol varchar2(10))
  2  /
 
Table created
 
SQL> 
SQL> create trigger TRG_TEST before insert on TEST
  2  for each row
  3  begin
  4  select SEQ_TEST.nextval into :new.TEST_ID from dual;
  5  end;
  6  /
 
Trigger created
 
SQL> insert into TEST(symbol) values('abc');
 
1 row inserted
 
SQL> insert into TEST(symbol) values('abc');
 
1 row inserted
 
SQL> select * from Test
  2  /
 
   TEST_ID SYMBOL
---------- ----------
         1 abc
         2 abc
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> select * from Test
  2  /
 
   TEST_ID SYMBOL
---------- ----------
         1 abc
         2 abc
         4 bcd
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> select * from Test
  2  /
 
   TEST_ID SYMBOL
---------- ----------
         1 abc
         2 abc
         4 bcd
         6 bcd
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> select * from Test;
 
   TEST_ID SYMBOL
---------- ----------
         1 abc
         2 abc
         4 bcd
         6 bcd
         8 bcd
 
SQL> insert into TEST(symbol) values('abc');
 
1 row inserted
 
SQL> select * from Test;
 
   TEST_ID SYMBOL
---------- ----------
         1 abc
         2 abc
         4 bcd
         6 bcd
         8 bcd
         9 abc
 
6 rows selected
  tri
SQL> drop  trigger TRG_TEST;
 
Trigger dropped
 
SQL> insert into TEST(symbol) values('abc');
 
insert into TEST(symbol) values('abc')
 
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."TEST_ID")
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> insert into TEST values(SEQ_TEST.nextval,'bcd');
 
1 row inserted
 
SQL> select * from Test;
 
   TEST_ID SYMBOL
---------- ----------
         1 abc
         2 abc
         4 bcd
         6 bcd
         8 bcd
         9 abc
        10 bcd
        11 bcd
        12 bcd
        13 bcd
 
10 rows selected
 
SQL>



标签:insert,Sequence,bcd,into,Trigger,values,SQL,TEST
From: https://blog.51cto.com/gjwrxz/6322310

相关文章

  • subsequence1 (牛客多校) (2个串比大小, DP, 组合数)
    题面大意:给定2个字符串,问有多少个子字符串S,是大于t的 思路数据范围很小,因此考虑n^2做法分2步,位数s>位数t的时候然后位数相等的时候利用DP,处理,分别就是枚举前k个数和s相同,然后k+1个数比t大就可以. 具体思路自己想想,和那个比较像   cons......
  • 【P4331 [BalticOI 2004]】Sequence 数字序列 题解(左偏树维护动态区间中位数)
    左偏树维护动态区间中位数。传送门P4331BalticOI2004Sequence数字序列。Solution1我的思路和题解前半部分完全重合了((如果按照单调不增去分割\(a\)序列的话,对于每一段我们能很简单地得出它的最佳答案:中位数。发现严格单调很难做,很难拿捏,考虑对\(a\)序列的每一项都进......
  • [AGC049D] Convex Sequence
    [AGC049D]ConvexSequence给定整数\(n\)和\(m\),问有多少个长为\(n\)的非负整数数列\(A\),满足以下条件:\(A_1+A_2+\ldots+A_n=m\)对任意\(i(2\leqi\leqN-1)\),都有\(2A_i\leqA_{i-1}+A_{i+1}\)答案对\(10^9+7\)取模。\(\texttt{datarange}\):\(n,m\le......
  • MySQL触发器Trigger加载以及目前局限
    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。作者:亮文章来源:GreatSQL社区原创概念介绍首先需要知道MySQL中触发器特点,以及表table相关触发器加载方式MySQL中单个trigger仅支持单事件触发即单......
  • CronTrigger时间配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年]
    CronTrigger配置格式:格式:[秒][分][小时][日][月][周][年]序号说明是否必填允许填写的值允许的通配符1秒是0-59,-*/2分是0-59,-*/3小时是0-23,-*/4日是1-31,-*?/LW5月是1-12orJAN-DEC,-*......
  • CF1794C Scoring Subsequences题解
    文中\(a\)为题目中给的\(a\)。如果我们要求\(a_1,a_2,a_3,\dots,a_m\)的结果,那么我们可以把\(a\)数组从后往前依次除以\(i\),\(i\)从\(1\)到\(n\),即为\(\frac{a_1}{m},\frac{a_2}{m-1},\frac{a_3}{m-2},\dots,\frac{a_{m-1}}{2},\frac{a_m}{1}\),并将其保......
  • 【题解】P4331 [BalticOI 2004]Sequence 数字序列
    以各种方式出现被玩烂的题目,算是小trick题?cpeditor意外地好用思路可并堆。平行时空同位体:CF13CP4331P4597CF713CP2893已知做法:\(O(n^2)\)dp:令\(f[i][j]\)为前\(i\)个数不超过\(j\)的最小代价优化:使用堆维护dp产生的折线(P4597题解区)\(O(n\logn......
  • Personalized Top-N Sequential Recommendation via Convolutional Sequence Embeddin
    目录概符号说明Caser代码TangJ.andWangK.Personalizedtop-nsequentialrecommendationviaconvolutionalsequenceembedding.WSDM,2018.概序列推荐的经典之作,将卷积用在序列推荐之上.符号说明\(\mathcal{U}=\{u_1,u_2,\cdots,u_{|\mathcal{U}|}\}\),us......
  • 「USACO2016JAN」Subsequences Summing to Sevens
    [USACO16JAN]SubsequencesSummingtoSevensS题目描述FarmerJohn's\(N\)cowsarestandinginarow,astheyhaveatendencytodofromtimetotime.EachcowislabeledwithadistinctintegerIDnumbersoFJcantellthemapart.FJwouldliketota......
  • ABC262Ex Max Limited Sequence 题解
    题意:给定\(m\)个限制\((l_i,r_i,p_i)\)及\(n,k\),求满足以下条件的长度为\(n\)的不同序列\(a=(a_1,a_2,\cdots,a_n)\)的数目。\(\foralli\in[1,n],0\leqa_i\leqk\)\(\foralli\in[1,m],\max\limits_{j\in[l_i,r_i]}a_j=p_i\)同P4229,但数据更强,目测只允......