🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.delphi.bazy-danych
4 messages
4 total messages Started by Borneq Sun, 16 Aug 2015 08:30
Wiele do wielu
#82554
Author: Borneq
Date: Sun, 16 Aug 2015 08:30
10 lines
1141 bytes
Jedna książka może mieć wielu autorów, jeden autor wiele książek. Oprócz 
tabel BOOKS i AUTHORS potrzebna tabela BOOKS_AUTHORS, ale jak to będzie 
wyglądało dla:
book1 - auth1
book2 - auth2,auth3
book3 - auth1,auth3
book4 - auth1,auth4
book5 - auth2,auth3,auth4

BOOKS ma 5 pozycji , AUTHORS 4 a BOOKS_AUTHORS?
Re: Wiele do wielu
#82555
Author: Borneq
Date: Sun, 16 Aug 2015 08:55
40 lines
1552 bytes
W dniu 16.08.2015 o 10:30, Borneq pisze:
> Jedna książka może mieć wielu autorów, jeden autor wiele książek. Oprócz
> tabel BOOKS i AUTHORS potrzebna tabela BOOKS_AUTHORS, ale jak to będzie
> wyglądało dla:
> book1 - auth1
> book2 - auth2,auth3
> book3 - auth1,auth3
> book4 - auth1,auth4
> book5 - auth2,auth3,auth4
>
> BOOKS ma 5 pozycji , AUTHORS 4 a BOOKS_AUTHORS?

BOOKS
1,book1
2,book2
3,book3
4,book4
5,book5

AUTHORS
1,auth1
2,auth2
3,auth3
4,auth4

BOOKS_AUTHORS
1,1
2,2
2,3
3,1
3,3
4,1
4,4
5,2
5,3
5,4

Coś takiego.
Jak wypełniać BOOKS_AUTHORS za pomocą komponentów DevArt i jak potem 
wyświetlać autorów dla książki?
Re: Wiele do wielu
#82556
Author: arivald
Date: Sun, 16 Aug 2015 09:45
79 lines
2715 bytes
On 2015-08-16 10:55, Borneq wrote:
> W dniu 16.08.2015 o 10:30, Borneq pisze:
>> Jedna książka może mieć wielu autorów, jeden autor wiele książek. Oprócz
>> tabel BOOKS i AUTHORS potrzebna tabela BOOKS_AUTHORS, ale jak to będzie
>> wyglądało dla:
>> book1 - auth1
>> book2 - auth2,auth3
>> book3 - auth1,auth3
>> book4 - auth1,auth4
>> book5 - auth2,auth3,auth4
>>
>> BOOKS ma 5 pozycji , AUTHORS 4 a BOOKS_AUTHORS?
>
> BOOKS
> 1,book1
> 2,book2
> 3,book3
> 4,book4
> 5,book5
>
> AUTHORS
> 1,auth1
> 2,auth2
> 3,auth3
> 4,auth4
>
> BOOKS_AUTHORS
> 1,1
> 2,2
> 2,3
> 3,1
> 3,3
> 4,1
> 4,4
> 5,2
> 5,3
> 5,4
>
> Coś takiego.
> Jak wypełniać BOOKS_AUTHORS za pomocą komponentów DevArt.

Co do komponentów UI, to są nieistotne.

Do wyświetlenia danych potrzebujesz JOINa, i jeśli komponenty bazodanowe 
(pochodne TDataset) tego same nie obsługują, to zawsze można w nich 
podać SQLe do insert/update. Albo użyć eventów żeby po 
insercie/update/delete do AUTHORS zmienić też zawartosć BOOKS_AUTHORS.

Mozesz też zrobić view, cross-join tych tabel, z triggerami na 
insert/update/remove, i robić update obu tablel w triggerach.


> i jak potem wyświetlać autorów dla książki?
>

można różnie, z bazą na BOOKS_AUTHORS z joinem do AUTHORS, lub na 
AUTHORS, np tak:

select * from AUTHORS
where authotID = (select authorId from BOOKS_AUTHORS where bookId = 'book');

lub:

select AUTHORS.* from AUTHORS
inner join BOOKS_AUTHORS on BOOKS_AUTHORS.autorId = AUTHORS.authorID
where BOOKS_AUTHORS.bookId = 'book';

lub:

select AUTHORS.* from BOOKS_AUTHORS
inner join AUTHORS on BOOKS_AUTHORS.autorId = AUTHORS.authorID
where BOOKS_AUTHORS.bookId = 'book';


Z tym że jak chcesz jeszcze dane książki w tym samym datasecie mieć, to 
zrób jeszcze join do BOOKS.

-- 
Arivald
Re: Wiele do wielu
#82557
Author: wloochacz
Date: Mon, 17 Aug 2015 17:23
36 lines
2161 bytes
W dniu 2015-08-16 o 10:30, Borneq pisze:
> Jedna książka może mieć wielu autorów, jeden autor wiele książek. Oprócz
> tabel BOOKS i AUTHORS potrzebna tabela BOOKS_AUTHORS, ale jak to będzie
> wyglądało dla:
> book1 - auth1
> book2 - auth2,auth3
> book3 - auth1,auth3
> book4 - auth1,auth4
> book5 - auth2,auth3,auth4
>
> BOOKS ma 5 pozycji , AUTHORS 4 a BOOKS_AUTHORS?
Tak:

Create Table "Book"  (
	"IdBook" Integer NOT NULL,
  Primary Key ("IdBook")
);

Create Table "Author"  (
	"IdAuthor" Integer NOT NULL,
  Primary Key ("IdAuthor")
);

Create Table "BookAuthor"  (
	"IdBook" Integer NOT NULL,
	"IdAuthor" Integer NOT NULL,
  Primary Key ("IdBook","IdAuthor")
);

Alter Table "BookAuthor" add Foreign Key ("IdBook") references "Book" 
("IdBook") on update no action on delete cascade ;
Alter Table "BookAuthor" add Foreign Key ("IdAuthor") references 
"Author" ("IdAuthor") on update no action on delete cascade ;

-- 
wloochacz
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads