home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / database / oracle / 1441 < prev    next >
Encoding:
Internet Message Format  |  1992-08-31  |  3.6 KB

  1. Path: sparky!uunet!decwrl!bu.edu!dartvax!kip-sn-49.dartmouth.edu!user
  2. From: carl.pedersen@dartmouth.edu (L. Carl Pedersen)
  3. Newsgroups: comp.databases.oracle
  4. Subject: Re: How to do this in SQL ?
  5. Message-ID: <carl.pedersen-310892180039@kip-sn-49.dartmouth.edu>
  6. Date: 31 Aug 92 22:15:52 GMT
  7. References: <4067@ncrsoph.Sophia.NCR.COM>
  8. Sender: news@dartvax.dartmouth.edu (The News Manager)
  9. Followup-To: comp.databases.oracle
  10. Organization: Dartmouth College
  11. Lines: 85
  12.  
  13. In article <4067@ncrsoph.Sophia.NCR.COM>, sstasuke@ncrsoph.Sophia.NCR.COM
  14. (Steve Stasukewicz) wrote:
  15. > I'm new to SQL, ( and Oracle for that matter) so I'm relying on a little 
  16. > help from the net. ( Actually, there's probably a very easy solution to 
  17. > this. )
  18. > I have the following four tables.  Column names are the same to show
  19. > relationships.  In the 'likes' table, a unique row is identified by a
  20. > combination of all 3 columns.  In the 'item_type' table, a unique row
  21. > is identified by a combination of f1 and f2.  In the other two tables,
  22. > each field is unique to it's column.
  23. >        likes               item            item_type             employee
  24. >  emp_no   f1   f2       f1   txt1        f1   f2  txt2        emp_no   name
  25. >  ------  ---- ----     ---- ------      ---- ---- ----        ------  -----
  26. >   999     10   01       10  apples       10   01  green        999     john
  27. >   999     10   02       20  bananas      10   02  red          123     sue
  28. >   123     30   02       30  beer         20   01  yellow       848     lisa
  29. >   848     20   10                        20   02  brown
  30. >                                          30   01  bud
  31. >                                          30   02  miller
  32. > From the  rows in table 'likes', I should be able to determine that john
  33. > likes green and red apples, sue likes miller beer, and lisa likes
  34. > yellow bananas. (Of course I don't know how to do this yet, but I should
  35. > be able to figure this one out).  My other question is this:
  36. > 1) How can I select all employees who like green apples, but do NOT like
  37. >    miller beer. ( In this case john.) You can tell someone does not like 
  38. >    something by it's absence in the 'likes' table.  To take this a step
  39. >    further, I would like to select all employees who like green apples,
  40. >    miller beer, yellow bananas, and dislikes bud beer and red apples.
  41.  
  42. I haven't tested the following, but it "ought" to work:
  43.  
  44. create view likes_v as
  45. select e.emp_no, e.name, i.txt1 thing, t.txt2 kind
  46.   from employee e, 
  47.        likes l,
  48.        item i,
  49.        item_type t
  50.  where e.emp_no = l.emp_no and
  51.        i.f1 = l.f1 and
  52.        t.f1 = l.f1 and t.f2 = l.f2;
  53.  
  54. select emp_no, name from likes_v
  55.  where kind = 'green' and thing = 'apples'
  56. minus
  57. select emp_no, name from likes_v
  58.  where kind = 'miller' and thing = 'beer';
  59.  
  60. select emp_no, name from likes_v
  61.  where kind = 'green' and thing = 'apples'
  62. intersect
  63. select emp_no, name from likes_v
  64.  where kind = 'miller' and thing = 'beer'
  65. intersect
  66. select emp_no, name from likes_v
  67.  where kind = 'yellow' and thing = 'bananas'
  68. minus
  69. select emp_no, name from likes_v
  70.  where kind = 'bud' and thing = 'beer'
  71. intersect
  72. select emp_no, name from likes_v
  73.  where kind = 'red' and thing = 'apples';
  74.  
  75. > Are these tables structured correctly for what I want?  I was also 
  76. > thinking of combining the 'item' and 'item_type' tables into 1 table,
  77. > and having 1 less column in the 'likes' table.  For SQL, is one approach
  78. > more efficient than the other? 
  79.  
  80. That is a deep question.  Somehow, I have the impression that what you are
  81. giving us is not your real application.  In order to express an intelligent
  82. opinion on your table design, I think I'd need to know more about your
  83. application.
  84.