0

Say I have the following relational schema:

+-------+------------+
| Name  |   Subject  |
+--------------------+
| 1     |   A        |
| 1     |   B        |
| 1     |   C        |
| 2     |   D        |
| 2     |   E        |
| 3     |   F        |
| 4     |   G        |
| 5     |   H        |
| 5     |   I        |
+-------+------------+

and I seek these tuples:

3  
4

How to get unique names for people only doing 1 subject?

philipxy
  • 14,867
  • 6
  • 39
  • 83
Choc Boo
  • 21
  • 1
  • 2
    Welcome to Stack Overflow! It's unclear exactly how you're attempting to solve this: when you say "relational" - are you talking about a relational *database*? If so, which one, as each has its own variant of SQL. Also, please edit to show what you've done and where you're stuck. As written, it's both unclear (since we don't know which database you're using) and too broad (potentially many ways to solve this). – David Makogon Jun 18 '18 at 00:35
  • Hi David, Apologies for being noobish. I am not using any relations database, I am from a different field and am trying to teach relational algebra to myself. This is more from just a theoretical point of view. – Choc Boo Jun 18 '18 at 00:58
  • 1
    Hi. Please read & act on 'stackexchange homework' & [ask]. Reference your chosen textbook(s) for a definition of the RA you are using (there are many) & show your work & say how you are stuck. Read & use [this](https://stackoverflow.com/a/24425914/3404097). Google 'execute relational algebra online' & read & act on [mcve]. – philipxy Jun 18 '18 at 19:25
  • 1
    PS This is a (too-broad) faq you would find if you forced yourself to clearly express yourself. ("Basically" unpreceded by clear full details means "not".) Please always google many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using use one variant search for your title & keywords for your tags. (I found an answer in the second hit of my first google. It began 'stackoverflow.com relational algebra'.) – philipxy Jun 18 '18 at 19:37
  • 1
    Possible duplicate of [How to find all tuples in a table if and only if the tuple appears once?](https://stackoverflow.com/questions/19482046/how-to-find-all-tuples-in-a-table-if-and-only-if-the-tuple-appears-once) – philipxy Jul 03 '19 at 22:12

2 Answers2

2

Here's an example based on what AntC described:

NS={
name:string, subject:string
'1'        , 'A'           
'1'        , 'B'           
'1'        , 'C'           
'2'        , 'D'           
'2'        , 'E'           
'3'        , 'F'           
'4'        , 'G'           
'5'        , 'H'           
'5'        , 'I'           
}

(π name NS) - π name (σ s!=subject ((ρ s←subject NS) ⨝ NS))

If you want to test this out you can try it at:

https://dbis-uibk.github.io/relax/calc.htm

nvogel
  • 24,981
  • 1
  • 44
  • 82
1

Choc Boo, welcome to stackoverflow [relational-algebra]. I strongly suggest you find yourself a textbook or online learning resource that will take you step-by-step -- preferably something you can share with other learners.

Are you sure it's RA you want to learn? Or SQL? They're very different: as a noob, learning one will probably completely confuse you wrt the other. (The connection between them won't become clear until you're much deeper in.)

There are places you can run RA online. But beware they use a specific dialect of RA, which might not be the one you're trying to learn.

Your query could perhaps be expressed as:

"Return all Names that appear in a single tuple."

Note I didn't mention "only doing 1 subject": a relation is a set; if any Name appears more than once it must be that it appears with multiple Subjects.

There's a 'quick and dirty' way to an answer: for each Name count how many tuples; restrict the result to return only those with count == 1. (That way will need using an advanced RA operator for counting.)

There's a longer-winded way that uses only more primitive operators: take two versions of your relation; pair each tuple with each from the other version; the Names that appear in more than one tuple will pair up in multiple ways same-Name-different-Subject; those are the Names you don't want.

What do I mean by "two versions"? Find out about the Rename operation (ρ). What do I mean by "pair ... with ..."? Find out about the Join operation (). What do I mean by "don't want"? Find out about the set difference operator (-): I said relations are sets.

AntC
  • 2,623
  • 1
  • 13
  • 20