Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Bagaimana cara membuat tabel dengan batasan saat menarik data dari tabel lain?

Tampaknya Anda perlu menggunakan kunci komposit untuk tabel WRITERS Anda. Contoh(diuji dengan Oracle 12c dan 11g, dbfiddle di sini ):

-- 4 authors
create table author ( authorid primary key, fname, lname )
as
select 1, 'fname_1', 'lname_1' from dual union all
select 2, 'fname_2', 'lname_2' from dual union all
select 3, 'fname_3', 'lname_3' from dual union all
select 4, 'fname_4', 'lname_4' from dual ;

-- 7 books
create table books ( isbn primary key, title )
as
select '978-1449324451', 'title_1' from dual union all
select '978-1449324452', 'title_2' from dual union all
select '978-1449324453', 'title_3' from dual union all
select '978-1449324454', 'title_1_4' from dual union all 
select '978-1449324455', 'title_2_4' from dual union all
select '978-1449324456', 'title_3_4' from dual union all
select '978-1449324457', 'title_4_4' from dual ;

-- suppose that 4 books are written by one and the same author
create table bookauthor( authorid, isbn )
as
select A.authorid, B.isbn
from author A 
  join books B on A.authorid = substr( B.title, length( B.title ), 1 ) ;

Tambahkan beberapa batasan ke tabel BOOKAUTHOR, dan periksa isinya:

-- authorid, isbn
alter table bookauthor 
add (
  constraint ba_fk1 foreign key( authorid ) references author( authorid )
, constraint ba_fk2 foreign key( isbn ) references books( isbn )
, constraint ba_pk primary key ( authorid, isbn )
) ;


SQL> select * from bookauthor;
  AUTHORID ISBN          
---------- --------------
         1 978-1449324451
         2 978-1449324452
         3 978-1449324453
         4 978-1449324454
         4 978-1449324455
         4 978-1449324456
         4 978-1449324457

Kode DDL "Asli" (dengan sedikit modifikasi) -> INSERT gagal

create table writers (
  authorid varchar2( 4 )
, lname varchar2( 10 )
, fname varchar2( 10 )
, isbn char( 14 )
, title varchar2( 30 ) constraint title_nn not null
, constraint wt_pk primary key ( authorid )
, constraint wt_fk foreign key( isbn ) references books( isbn )
);

INSERT INTO writers
SELECT authorid, fname, lname, isbn, title 
FROM author 
   JOIN bookauthor USING(authorid) 
   JOIN books USING(isbn);
-- ORA-00001: unique constraint (...WT_PK) violated 
-- author 4 with 4 books!

Kode DDL yang disarankan (dan pengujian):

create table writers2 (
  authorid varchar2( 4 )
, lname varchar2( 10 )
, fname varchar2( 10 )
, isbn char( 14 )
, title varchar2( 30 ) constraint title_nn2 not null
, constraint wt_pk2 primary key ( authorid, isbn )
, constraint wt_fk2 foreign key( isbn ) references books( isbn )
);

INSERT INTO writers2
SELECT authorid, fname, lname, isbn, title 
FROM author 
   JOIN bookauthor USING(authorid) 
   JOIN books USING(isbn);
-- 7 rows inserted.

MEMILIH dari WRITERS2:

SQL> select * from writers2 ;

AUTH LNAME      FNAME      ISBN           TITLE                         
---- ---------- ---------- -------------- ------------------------------
1    fname_1    lname_1    978-1449324451 title_1                       
2    fname_2    lname_2    978-1449324452 title_2                       
3    fname_3    lname_3    978-1449324453 title_3                       
4    fname_4    lname_4    978-1449324454 title_1_4                     
4    fname_4    lname_4    978-1449324455 title_2_4                     
4    fname_4    lname_4    978-1449324456 title_3_4                     
4    fname_4    lname_4    978-1449324457 title_4_4 

Namun, tidak yakin mengapa Anda memerlukan tabel WRITERS - karena Anda dapat menghasilkan datanya dengan menjalankan kueri.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Perbedaan antara MySQL dan Oracle DB

  2. Bagaimana saya bisa menggunakan regex untuk membagi string, menggunakan string sebagai pembatas?

  3. pilih dari satu tabel, masukkan ke tabel lain kueri Oracle sql

  4. Nilai SQL MIN dari dua tabel

  5. hsqldb Oracle mode pilih untuk pembaruan SEKARANG