I'm making progress on mid-term. [message #109589] |
Sat, 26 February 2005 13:00 ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
I've created a table 'cards' with two columns NBR NUMBER(22) and CARD VARCHAR2(2). There are 52 rows represent one row for every card in a deck. I'm using this to generate random numbers
select r
from (select r
from (select rownum r
from all_objects
where rownum < 52)
order by dbms_random.value)
where rownum <= 52;
I need to figure how to assign my NBR to the corresponding random number so I can "shuffle" the deck. Then show all the numbers <20 as the card value as for each hand. 4 players 5 cards each. Thanks I hope this worded correctly.
-
Attachment: MIDTERM.doc
(Size: 55.50KB, Downloaded 1288 times)
|
|
|
|
Re: I'm making progress on mid-term. [message #109593 is a reply to message #109589] |
Sat, 26 February 2005 18:57 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
Thanks. Here is what I've come up with.
select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52;
I can't get this cursor to work to insert the values into the shuffle1 column.
DECLARE
CURSOR c_deal IS select C.nbr, c.card
from cards c
v_DEAL c_DEAL%rowtype;
V_SHUFFLE NUMBER(22);
BEGIN
FOR V_DEAL IN C_DEAL LOOP
select
case r
when 1 then '2D'
WHEN 2 THEN '3D'
WHEN 3 THEN '4D'
WHEN 4 THEN '5D'
WHEN 5 THEN '6D'
WHEN 6 THEN '7D'
WHEN 7 THEN '8D'
WHEN 8 THEN '9D'
WHEN 9 THEN '10D'
WHEN 10 THEN 'JD'
WHEN 11 THEN 'QD'
WHEN 12 THEN 'KD'
ELSE 'AD'
END into v_SHUFFLE1 from (select r
from (select rownum r
from all_objects
where rownum <= 13)
order by dbms_random.value)
where rownum <= 1;
INSERT INTO FACES VALUES (V_ST.NBR, V_ST.FACES, V_SHUFFLE1);
END LOOP;
END;
I know there is only 13 random's just started small and work up. Thanks for any help in advance. I need it.
|
|
|
|
Re: I'm making progress on mid-term. [message #109599 is a reply to message #109589] |
Sat, 26 February 2005 23:26 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
How did you get it to run the shuffle in number 4? I've been fooling with it all day. I think I can get the deal part(#5) to work but i need to get 4 first. Did you use a cursor/loop or just a cursor? I have figured out #3 using a straight insert statement. Ala, insert into cards values (1, '2D',NULL,NULL,NULL,NULL); but I can get the shuffle. Thank you very much for all of the help you have offered. This is more than my 35 yr old mind can figure. Thanks again, I do appriecaite all your help.
|
|
|
|
Re: I'm making progress on mid-term. [message #109621 is a reply to message #109589] |
Sun, 27 February 2005 07:07 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
Would you be willing share the script? You don't have to give me the exact code. Do I put the for loop inside the implicit cursor and use the random number generator or the other way around? I'm just not sure of the structure. We have used loop and cursor but never together and inserting values into tables. We always used an dmbs_output for queries. I understand what i need to do but not how. Thank you again for responding. This is incredibly hard, at least I think so. Thanks again. This what we were told to reconfigure:
DECLARE
CURSOR c_st IS select s.s_id, cs.c_sec_id
from course_section cs, student s
minus
select s_id, c_sec_id
from enrollment;
v_st c_st%rowtype;
v_grade char(1);
BEGIN
FOR V_ST IN C_ST LOOP
select
case r
when 1 then 'A'
WHEN 2 THEN 'B'
WHEN 3 THEN 'C'
WHEN 4 THEN 'D'
ELSE 'F'
END into v_grade from (select r
from (select rownum r
from all_objects
where rownum <= 5)
order by dbms_random.value)
where rownum <= 1;
INSERT INTO ENROLLMENT VALUES (V_ST.S_ID, V_ST.C_SEC_ID, V_GRADE);
END LOOP;
END;
But i'm kind of stuck. Once i get the shuffle I think I will have it. Thank you again.
[Updated on: Sun, 27 February 2005 08:00] Report message to a moderator
|
|
|
Re: I'm making progress on mid-term. [message #109627 is a reply to message #109589] |
Sun, 27 February 2005 11:02 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
Here's what I've been working on
DECLARE
CURSOR c_deal IS select c.nbr
from cards c;
v_DEAL c_DEAL%rowtype;
V_SHUFFLE NUMBER(22);
BEGIN
FOR V_DEAL IN C_DEAL LOOP
select
case r
when 1 then '2D'
WHEN 2 THEN '3D'
WHEN 3 THEN '4D'
WHEN 4 THEN '5D'
WHEN 5 THEN '6D'
WHEN 6 THEN '7D'
WHEN 7 THEN '8D'
WHEN 8 THEN '9D'
WHEN 9 THEN '10D'
WHEN 10 THEN 'JD'
WHEN 11 THEN 'QD'
WHEN 12 THEN 'KD'
ELSE 'AD'
END into v_SHUFFLE1 from select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52;
INSERT INTO cards VALUES (V_ST.NBR,
V_SHUFFLE1);
END LOOP;
END;
|
|
|
Re: I'm making progress on mid-term. [message #109628 is a reply to message #109627] |
Sun, 27 February 2005 12:05 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
I believe it would be unethical for me to share the script that I have written, as it would amount to doing your midterm for you. I don't mind helping you learn though, so that you can do your midterm yourself. It is better if you post what you have and I make suggestions to point you in the right direction. I am repeating myself here, but which problem are you working on, 3 or 4? It would help if you would answer that question. You seem to be attempting to combine the two problems or something. In problem 3 you should be inserting values into the nbr and card columns of the cards table. All inserts into the other three tables should have been done in problems 1 and 2, so there should be no more inserts at all for all of the rest of the problems. Problem 3 also does not require any use of random ordering. When you get to problem 4, you should already have rows in your cards table with values in the nbr and card columns that were inserted in problem 3. In problems 4, you should therefore be updating, not inserting values into the shuffle1 , shuffle2, and shuffle3 columns only, and leaving the rest of the columns alone, neither inserting nor updating them. It would help if you post the results of:
SELECT * FROM faces
/
SELECT * FROM players
/
SELECT * FROM suits
/
SELECT * FROM cards
/
so that I can see that you have completed problems 1 and 2 properly, since if they are not done correctly, you will not be able to get the rest to work. I will wait for you to provide the results of that and tell me which problem you are working on, 3 or 4, before offering further suggestions, since I can't really tell you what to change in the code if I do not know what you are trying to make it do.
|
|
|
Re: I'm making progress on mid-term. [message #109629 is a reply to message #109589] |
Sun, 27 February 2005 12:13 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
I am on problem 4 updating the shuffles. All of my tables look as they are supposed to. I am having a problem getting the update to work with the shuffle:
UPDATE CARDS
SET shuffle1 = (select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52)
where NBR <= 52;
But as you can tell this is not working. I figure I would start small and see what happens. I'm getting an invalid character. I hope this is what you are looking for. Thanks again.
|
|
|
|
|
|
|
|
Re: I'm making progress on mid-term. [message #109637 is a reply to message #109589] |
Sun, 27 February 2005 12:54 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
It looks like the select statement that your instructor provided can be used as is within your code and the rest generally demonstrates how to create a procedure, how to create a function, an explicit cursor, an implicit cursor, and usage of the select statement within a procedure, however it does not do exactly what you need to do, and you should not expect it to do so, otherwise he would have written your code for you, and you would just be substituting the table names and column names, and not having to think or learn anything. Why don't you try doing as I suggested, one step at a time, and create a table using the select statement that you have been given, as demonstrated below, then try to update the cards table from that new table. I am curious. Where do you go to school and who is your instructor?
1 create table test_tab as
2 select r,ROWNUM "NBR"
3 from (select r, ROWNUM
4 from (select rownum r
5 from all_objects
6 where rownum <= 52)
7 order by dbms_random.value)
8* where rownum <= 52
scott@ORA92> /
Table created.
scott@ORA92> select * from test_tab;
R NBR
---------- ----------
5 1
12 2
44 3
7 4
8 5
42 6
40 7
38 8
31 9
41 10
46 11
17 12
16 13
14 14
37 15
2 16
48 17
21 18
43 19
20 20
26 21
22 22
10 23
1 24
30 25
9 26
32 27
51 28
24 29
33 30
28 31
13 32
34 33
50 34
4 35
19 36
35 37
25 38
52 39
18 40
23 41
47 42
3 43
6 44
29 45
11 46
49 47
39 48
36 49
27 50
45 51
15 52
52 rows selected.
scott@ORA92>
|
|
|
|
|
|
Re: I'm making progress on mid-term. [message #109643 is a reply to message #109640] |
Sun, 27 February 2005 13:26 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
Here is a link that no Oracle developer or DBA should be without. It is a menu that can be used to search the Oracle online documentation in various ways. It may require that you register on Oracle's website, with OTN (Oracle Technology Network) to view it, but it is free.
http://otn.oracle.com/pls/db92/db92.homepage
If, for example, you search for UPDATE, you will find various sections that include syntax, explanation, and examples. You will proably find most of what you need in the SQL reference manual. This should be the first place that you search. Here is another link to the section on UPDATE in the SQL reference manual.
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_108a.htm#SQLRF01708
Hopefully, this will help you figure out how to create your update statement.
Are you switching careers or are you a programmer learning a new language? Sometimes it helps if we know what other languages you have programmed in, so we can just tell you what to expect to be the same or similar and what is different.
|
|
|
Re: I'm making progress on mid-term. [message #109644 is a reply to message #109589] |
Sun, 27 February 2005 13:26 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
here's my update
update cards
set shuffle1 =
(select r
from test_tab)
where nbr <=52;
but getting single-row subquery returns more than one row.
I thought that I was updating shuffle1 with R from the test_tab
where nbr <= 52 in the cards table. Please tell me I'm getting warmer.
|
|
|
Re: I'm making progress on mid-term. [message #109645 is a reply to message #109644] |
Sun, 27 February 2005 13:42 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
If you use the link that I provided, there is an option within that link to search for error messages:
http://otn.oracle.com/pls/db92/db92.error_search?remark=homepage&prefill=ORA-
Any time that you receive an error, this is where you should go to find out more about that error. It helps if you provide the actual error number as shown below.
scott@ORA92> update cards
2 set shuffle1 =
3 (select r
4 from test_tab)
5 where nbr <=52;
(select r
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
So, if you use that menu to search for ORA-01427, you get:
"ORA-01427 single-row subquery returns more than one row
Cause: The outer query must use one of the keywords ANY, ALL, IN, or NOT IN to specify values to compare because the subquery returned more than one row.
Action: Use ANY, ALL, IN, or NOT IN to specify which values to compare or reword the query so only one row is retrieved."
I am tryng to teach you how to diagnose things. The text of this one may be somewhat confusing to you here. The problem is that you are trying to update each value of shuffle1 in each row with 52 values from the test_tab table. You can only update one value with one value. As I said before, you need to compare another column in the cards table to another column in the test_tab table to narrow those 52 values down to one. Otherwise, it is like saying to set the row of this column = to 1 and 2 and 3 and 4 and ... and 51 and 52, when you can only set it to one of those values. The key part of the message text above that applies is, "reword the query so only one row is retrieved." The rest of the text applies to other situations where you might want to compare a value to multiple values. Oracle has no way of knowing which you are trying to do, when it provides the error message.
|
|
|
|
|
|
|
|
|
Re: I'm making progress on mid-term. [message #109652 is a reply to message #109589] |
Sun, 27 February 2005 14:12 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
Now that I did that what do I compare when using my random generator? I understand what I did but not sure of what to do next. BTW you did in one afternoon which hadn't been able to done in six weeks of class. Thanks a lot. It make oracle more enjoyable when you understand what you are doing.
|
|
|
|
|
|
Re: I'm making progress on mid-term. [message #109656 is a reply to message #109589] |
Sun, 27 February 2005 14:28 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
from selesctr,ROWMUN "NBR"
ERRORat line 4
ORA-00903 Invalid table name
update cards
set shuffle1 =
(select R
from select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52
where test_tab.nbr = cards.nbr)
|
|
|
|
|
|
|
Re: I'm making progress on mid-term. [message #109661 is a reply to message #109660] |
Sun, 27 February 2005 14:41 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](/forum/images/custom_avatars/43710.gif) |
Barbara Boehmer
Messages: 9104 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
Before you move on to the dealing problem, you need to finish the shuffle problem, so take that update statement and put it in a procedure, make two additional copies of the update statment, one for shuffle2, and one for shuffle3, and add them to the procedure, then execute the procedure. When you are done with that, then post what you have tried on the dealing problem, complete with any errors received, but please try looking up the error messages and figuring it out for yourself first.
|
|
|
|
Re: I'm making progress on mid-term. [message #109663 is a reply to message #109589] |
Sun, 27 February 2005 14:49 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to previous message](/forum/theme/orafaq/images/down.png) |
craigier
Messages: 42 Registered: February 2005
|
Member |
|
|
Here's my procedure but it was created with errors;
CREATE OR REPLACE PROCEDURE shuffle_cards
AS
BEGIN
UPDATE CARDS
SET shuffle1 =
(select R
from select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52
where test_tab.nbr = cards.nbr);
UPDATE CARDS
SET shuffle2 =
(select R
from (select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52) test_tab
where test_tab.nbr = cards.nbr);
UPDATE CARDS
SET shufle3 =
(select R
from (select r,ROWNUM "NBR"
from (select r, ROWNUM
from (select rownum r
from all_objects
where rownum <= 52)
order by dbms_random.value)
where rownum <= 52) test_tab
where test_tab.nbr = cards.nbr);
END;
I haven't even begun to look at the dealing problem. Are you going to be around in an hour or so? I should be good and confused by then plus have done some work on it. Thanks again.
|
|
|