Is it possible to create a table with column name containing space? If so how can I create and use it?
6 Answers
It is possible, but it is not advisable. You need to enclose the column name in double quotes.
create table my_table ("MY COLUMN" number);
But note the warning in the documentation:
Note: Oracle does not recommend using quoted identifiers for database object names. These quoted identifiers are accepted by SQL*Plus, but they may not be valid when using other tools that manage database objects.
The name will be case-sensitive, and you wil have to enclose the name in double quotes every time you reference it:
select "MY COLUMN" from my_table;
So... don't, would be my advice...
Yes, it's possible. You just have to be sure to quote the column name. For instance:
CREATE TABLE Fubar ("Foo Bar" INT);
INSERT INTO Fubar VALUES (1337);
SELECT "Foo Bar" FROM SpaceMonster
Even though it's possible, it doesn't make it a good idea. You'll probably save yourself from a lot of pain if just replace all you spaces with underscores.
-
In case of having double quotes outside, how do you enclose field name then? ie: d2rq:sqlExpression "TO_CHAR(factCloverAsphere.Time Data Recorded,'Dy Mon DD HH24:MI:SS YYYY')"– AngelinaAug 26, 2013 at 14:23
it is possible by naming the column between two "
example: "My columN" , the column name becomes case sensitive which means.
SELECT "my column" from table; --NOT OK
SELECT "My columN" from table; --OK
-
this was the fix i needed. the column was created with a mixed case name but my query was using all lower case. thanks.– 4mla1fnSep 15, 2020 at 14:28
You can (see the Oracle doc) if you quote these appropriately. However I suspect this is not a good idea, since you'll have to quote everything. Generally db naming standards / conventions (e.g. here or here) favour using underscores (which don't require quoting) over whitespace.
This is the columns rule defined for oracle
Columns (for tables)
- All columns are in form {alias}_{colname}. For example prs_id,
prs_name, prs_adr_id, adr_street_name. This guarantees that column
names are unique in a schema, except denormalized columns from
another table, which are populated using triggers or application
logic.
- All columns are in singular. If you think you need a column name in plural think twice whether it is the right design? Usually it means you are including multiple values in the same column and that should be avoided.
- All tables have surrogate primary key column in form {alias}_id, which is the first column in the table. For example, prs_id, mat_id, adr_id.
you can always have alias for column name bu using ""
-
-
5Those look like company or project naming conventions, not 'Oracle' rules. The object naming rules are here. Dec 10, 2012 at 9:32
-
There's no need for column names to be unique in a schema. Only in a table or view. Dec 12, 2012 at 6:04
Did you Google for this? I did - the 6th link was this, in which I find the following:
create table employee (
"First Name" varchar2(20),
"Last Name" varchar2(20),
Address varchar2(60),
Phone varchar2(15),
Salary number,
EmpID number,
DeptID number
);
... which works fine when I tried it in 10g.