【问题标题】:PLSQL Adding new publisher in publisher table and then adding it to book table where publisher value is null and Displaying it in the given formatPLSQL 在发布者表中添加新发布者,然后将其添加到发布者值为空的图书表中并以给定格式显示
【发布时间】:2021-06-22 03:09:10
【问题描述】:

我正在尝试编写一个 PLSQL 块来显示书籍及其作者和出版商的详细信息。问题陈述如下:

  1. 将新发布者“ttk publisher”插入发布者表。
  2. 在 book 表中为出版商值为空的记录分配此新值。
  3. 以以下格式检索新更新的新记录(使用字符串函数仅显示“标题”列的前 10 个字母)

输出格式如下:

BOOKID.............TITLE.............publisher.............Author

639163050...........10 Years o...........Prentice Hall...........Paul Deitel    
330895717...........African Fo...........Prentice Hall...........Tem Nieto

架构详情如下

1.author- authorid, firstname, lastname

2.book- author_id, bookid, publisherid, title

3.publisher- publisherid, publishername

【问题讨论】:

    标签: sql oracle plsql


    【解决方案1】:

    应该是这样的。在代码中读取 cmets。

    declare
      l_publisherid   publisher.publisherid%type;
    begin
      -- 1. You didn't say how PUBLISHER.PUBLISHER_ID gets its value,
      -- so I'll presume that it is automatically generated, somehow
      -- (identity column or a database trigger)
      insert into publisher (publishername) values ('ttk publisher')
        returning publisherid into l_publisherid;
      
      -- 2. 
      update book b set
        b.publisherid = l_publisherid
        where b.publisherid is null;
    
      -- 3. Join all 3 tables on appropriate columns. Use a loop to
      -- display ALL books involved. WHERE clause is used to retrieve
      -- books whose PUBLISHERID has just been updated
      for cur_r in 
        (select b.bookid, 
                substr(b.title, 1, 10) title, 
                p.publishername publisher, 
                a.firstname ||' '|| a.lastname author
         from book b join publisher p on p.publisherid = b.publisherid
         join author a on a.authorid = b.author_id
         where p.publisherid = l_publisherid
        )
      loop
        dbms_output.put_line(cur_r.bookid    ||'...'||
                             cur_r.title     ||'...'||
                             cur_r.publisher ||'...'||
                             cur_r.author);
      end loop;                         
    end;
    /
    

    【讨论】:

    • 嗨,谢谢,我尝试运行相同的代码,但它给出了一个错误 ~ 在标准输出上没有响应 ~
    • 好吧,如果您发布了正确的测试用例(CREATE TABLE 和 INSERT INTO 语句),我/我们可以对其进行测试。另外,你用什么工具?您是否启用 dbms_output?
    • 我在hackerrank上运行它
    • 对不起,我不知道它是怎么操作的,没用过。
    【解决方案2】:

    这就是hackerrank的工作方式:

          set serveroutput on
        
        declare
        l_publisherid publisher.publisherid%type;
        begin
        
            insert into publisher values (123, 'TTK Publisher')
            returning publisherid into l_publisherid;
               
            update book b set b.publisherid=l_publisherid
            where b.publisherid is null;
        
          declare
        
          cursor c is
        
            select * from book b
        left join author on authorid=author_id
        left join publisher p on b.publisherid=p.publisherid
        where p.publisherid=l_publisherid;
        
        
         begin
            dbms_output.put_line('BOOKID'||'.............'||'TITLE'||'.............'||'publisher'||'.............'||'Author');
        
          for i in c loop
           dbms_output.put_line(i.bookid||'...........'||substr(i.title,1,10)||'...........'||i.publishername||'...........'||concat(i.firstname||' ',i.lastname));
            end loop;
            END;
    END;
    /
    

    【讨论】:

      猜你喜欢
      • 2014-09-11
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 2010-11-24
      • 2018-08-02
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多