Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 COUNT TABLE DEPENDENCIES fFOREIGN KEYS

Author  Topic 

SQLapprentice
Starting Member

29 Posts

Posted - 2012-01-18 : 20:52:54
Hi all!

I'm building a simple database which has several books, and each book has several versions.

I created a database which has a table 'book' that in turn has one column 'bookName' that is a primary key. I also created a table 'version' which has 2 columns: versionName(Primary key) and bookName(foreign key that refers to books).

I would like to ask you can I query the database how many versions has each book. I know I have to use count but how can I find out which tables are referenced by book?

Thanks in advance!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-18 : 21:14:56
I suspect this is a homework question, but I'll bite.

select b.bookname, count(*)
from book b
join version v
on b.bookname = v.bookname
group by b.bookname

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

SQLapprentice
Starting Member

29 Posts

Posted - 2012-01-18 : 21:24:11
Thank you! it's not homework!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-18 : 22:05:49
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -