DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 12th, 2003, 09:49 AM
Media19 Media19 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 11 Media19 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Db2

These are just a few more questions I've been working on. I know my way around db2 fairly well, but I'm still having a few troubles with some of its features tough. Any advice would be greatly appreciated. Thanks!

Emp(eid:integer primary, ename:string, age:integer, salary:integer)
Dept(did:integer primary, butget:integer, managerid:integer)
Works(eid:integer primary, did:integer primary, work_time:integer)

Write SQL statements to create the above tables with appropriate primary keys and foreign keys enforced.

I know how to create the above tables, but I can not figure out in db2 how to make a table with a double primary key. I keep getting errors when I try to do so. Also I'm failing to find any documentation on how to enforce the proper foreign keys.

Create a VIEW, called RichDeptStat, that has the schema:
RichDeptStat (did:integer, avgSal:real)
where, DeptID is the ID of *rich* departments (i.e., a department is called "rich" if the total sum of all employees' salaries is greater than $10,000), and avgSal is the average salary of all employees working for that rich department.
I know the syntax would be like this but I'm having trouble with the last condition.
Create view RichDeptStat as select DID, sum(emp.salary) as AVGSAL from emp, works join dept on dept.did=works.did where sum(emp.salary)>10,000
How do you enforce the condition that "all departments must have a manager"?
I was thinking there must be a way in DB2 to declare that the attribute manager in dept must be equal to one or greater than or equal to one?

Thanks again.

Last edited by Media19 : April 12th, 2003 at 09:54 AM.

Reply With Quote
  #2  
Old April 12th, 2003, 02:37 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,727 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 19 h 22 m 42 sec
Reputation Power: 848
sounds very suspiciously like homework, but you seem to be trying, instead of just asking for the answer, so here goes...

create table Works
( eid integer not null
, did integer not null
, work_time integer
, primary key (eid, did)
, foreign key eid references Emp (eid)
, foreign key did references Dept (did)
)

as far as enforcing the foreign keys is concerned, you do not have to do that, the database takes care of it for you -- that's why it's called declarative referential integrity

to find rich departments,

select did, sum(salary)/count(eid) as avgSal
from Dept
inner join Works on Dept.did=Works.did
inner join Emp on Works.eid=Emp.eid
group by did having sum(salary)>10000

the condition that "all departments must have a manager" is a rather sneaky way, typical of homework questions, of finding out whether you understand foreign keys

if you declare the manager column a NOT NULL foreign key, then each row in the Dept table must have a value in the manager column which can be found in an existing row of Emp


rudy
http://r937.com/

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Db2


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway