SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 COUNT TABLE DEPENDENCIES fFOREIGN KEYS
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SQLapprentice
Starting Member

29 Posts

Posted - 01/18/2012 :  20:52:54  Show Profile  Reply with Quote
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

USA
37133 Posts

Posted - 01/18/2012 :  21:14:56  Show Profile  Visit tkizer's Homepage  Reply with Quote
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 - 01/18/2012 :  21:24:11  Show Profile  Reply with Quote
Thank you! it's not homework!
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37133 Posts

Posted - 01/18/2012 :  22:05:49  Show Profile  Visit tkizer's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000