我好菜呀呀呀呀 2022-03-27 19:22 采纳率: 66.7%
浏览 15
已结题

关于Oracle数据库的问题

问题遇到的现象和发生背景

在建立的数据表student中设置一个字段CreatedBy的默认值为另外一个字段sno

问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

1条回答 默认 最新

  • DarkAthena ORACLE应用及数据库设计方案咨询师 2022-03-27 21:00
    关注

    参考oracle官方文档 ,

    The DEFAULT expression can include any SQL function as long as the function does not return a literal argument, a column reference, or a nested function invocation
    The DEFAULT expression can include the sequence pseudocolumns CURRVAL and NEXTVAL, as long as the sequence exists and you have the privileges necessary to access it
    A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the pseudocolumns LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified
    The expression can be of any form except a scalar subquery expression

    简单来说,在create table的语法中,字段的默认值可以为很多东西,但是不能为另一个列(序列伪列除外)。
    因此,建议采用触发器的形式

    --测试表
    create table test_20220327 (sno varchar2(10),create_by varchar2(10) );
    --触发器
    create or replace trigger TEST_20220327_create_by_tri
      before insert on TEST_20220327
      for each row
    declare
    begin
      :new.create_by := :new.sno;
    end;
    /
    --插入数据
    insert into test_20220327(sno) values ('abc');
    --查询结果
    select * from test_20220327;
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月4日
  • 已采纳回答 3月27日
  • 创建了问题 3月27日

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: