Oracle table column name with space

后端 未结 6 1260
北荒
北荒 2020-12-06 09:24

Is it possible to create a table with column name containing space? If so how can I create and use it?

相关标签:
6条回答
  • 2020-12-06 09:45

    This is the columns rule defined for oracle

    Columns (for tables)

    1. 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.
      1. 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.
      2. 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 ""

    0 讨论(0)
  • 2020-12-06 09:48

    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.

    0 讨论(0)
  • 2020-12-06 09:50

    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...

    0 讨论(0)
  • 2020-12-06 09:53

    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.

    0 讨论(0)
  • 2020-12-06 09:57

    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
    
    0 讨论(0)
  • 2020-12-06 10:07

    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.

    0 讨论(0)
提交回复
热议问题