0
create table ROOM

(NO_ROOM INT PRIMARY KEY, TYPE VARCHAR2(8) NOT NULL, SIZE VARCHAR2(8) NOT NULL)

;

I get ORA-00904, i think problem is from NO_ROOM. I try to use NUMBER, it's same.

PS. I make table for room that has no.room type and size.

  • `SIZE` is reserved word! So, it's not allowed to use them as a name of variables or fields. You can find here http://docs.oracle.com/cd/B28359_01/appdev.111/b31231/appb.htm list of reserved words for Oracle 11g. – RustamIS Aug 24 '13 at 14:15

4 Answers4

7

SIZE is a reserved keyword. That means we cannot use it as an identifier (unless we put it in double quotes, but double-quoted identifiers are Teh Suck! so don't do that). Change the column name to ROOMSIZE and your statement will run.

Note that TYPE is also a keyword but not reserved. So we are allowed to use it as a column identifier. Types weren't introduced until 8; making TYPE a reserved keyword would have broken code in Oracle applications all over the world, not least in its own data dictionary.

The documentation has a complete list of the reserved words. Find it here.


Why would using "SIZE" be such a bad idea? After all, as @JavaBeginner says, the SQL standard does permit it.

Once we choose to use double-quotes to get around Oracle's naming rules we are condemned to use them whenever we reference the column. Hence this would not be a valid query:

 select no_room, size
 from room
 where size > 10

We would have to write instead:

 select no_room, "SIZE"
 from room
 where "SIZE" > 10

And it always have to be "SIZE": "size" is a different identifier. so is "Size".

Best practice is the informed interpretation of what the standards permit. SQL allows us to do things which we shouldn't do if we want to build a robust and maintainable database. Not using double-quoted identifiers falls into that category.

Community
  • 1
  • 1
APC
  • 144,005
  • 19
  • 170
  • 281
2

Size is a keyword and it cannot be used as column name unless you use it with double quotes. My suggestion is to use some other name for column as room_size. If you still want to use SIZE as column name for some reason, you will need to use double quotes while creating the table and also take care of the same while doing any other queries using this column.

Here is the working fiddle with size used as column name http://sqlfiddle.com/#!4/7e746

I do want to add(same as above) that using reserved word for column name(using double quotes) is a bad idea.

rakeshjain
  • 1,791
  • 11
  • 11
  • 2
    Don't tell people to use double-quoted identifiers. They're a complete pain to work with, and there's no need to use them in this situation. – APC Aug 24 '13 at 05:57
  • @APC I have already suggested to use other column name such as room_size. Why have you down voted it? – rakeshjain Aug 24 '13 at 06:11
  • @APC rakeshjain has done a suggestion how to do.It depends on the OP to use or not.What to do if the OP needs to have the column name as SIZE – SpringLearner Aug 24 '13 at 06:20
  • @rakeshjain +1 for hinting the idea of double qoutes – SpringLearner Aug 24 '13 at 06:21
  • 2
    @javaBeginner - bad advice is bad advice. In the real world there is never any reason to use a reserved keyword as a column name. Even the users want to see *Size* on the screen we simply set a display header in the front end. – APC Aug 24 '13 at 08:27
  • @APC Yes what ever you said is also true but my doubt is if sql is allowing us to do then why cant we use this? – SpringLearner Aug 24 '13 at 08:28
  • @javaBeginner - the law of the land allows you to drive your own car into the side of your own garden wall. My advice to you would be to not do that but please feel free to make up your own mind. Anyway, I've edited my answer to explain why using `"SIZE"` is a bad idea. – APC Aug 24 '13 at 08:52
  • @APC I don't understand the reason for getting down voted multiple times for this. If you read my post, I have clearly written "My suggestion is to use some other name for column as room_size". I have not suggested him to use the reserved words/key words. I gave the solution for double quotes only if he is persisting to use that reserved word. So why am i being down voted? – rakeshjain Aug 24 '13 at 09:52
  • Actually you've been downvoted once not "multiple times". If you don't like that you have the option of editing your answer to explain your position more clearly. – APC Aug 24 '13 at 15:01
1

You can't use any of these reserved words as identifiers: http://docs.oracle.com/cd/B19306_01/server.102/b14200/ap_keywd.htm

Size is in the list. If you choose another name for the column you should be okay.

dcaswell
  • 3,137
  • 2
  • 26
  • 25
0

SIZE is a reserved word by Oracle! So, it's not allowed to use them as a name of variables or objects. You can find here http://docs.oracle.com/cd/B28359_01/appdev.111/b31231/appb.htm list of reserved words for Oracle 11g.

Here is the second moment, you can use it inside double quote like "SIZE" or "Size", but that will be case sensitive and not recommended.

RustamIS
  • 697
  • 8
  • 24
  • Precisely what do you think your answer adds? It simply recapitulates other answers in this thread. A better use of your time would be to answer one of the many [oracle] questions which have no answers. http://stackoverflow.com/questions/tagged/oracle?sort=unanswered&pagesize=30 Otherwise you should show your agreement with existing answers by upvoting them. – APC Aug 24 '13 at 15:21