Hive- issue with Create Table with column have space - sql

Will need some advice. In HIVE DB is it possible to create table with column have space as below
CREATE TABLE TEST2("Kod ASS" String)
get an error as below
Error: Error while compiling statement: FAILED: ParseException line 1:19 cannot recognize input near '"Kod ASS"' 'String' ')' in column specification
SQLState: 42000
ErrorCode: 40000

show manual about column names:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL
In Hive 0.12 and earlier, only alphanumeric and underscore characters are allowed in table and column names.
In Hive 0.13 and later, column names can contain any Unicode character (see HIVE-6013). Any column name that is specified within
backticks (`) is treated literally. Within a backtick string, use
double backticks (``) to represent a backtick character. Backtick
quotation also enables the use of reserved keywords for table and
column identifiers.
To revert to pre-0.13.0 behavior and restrict column names to alphanumeric and underscore characters, set the configuration property
hive.support.quoted.identifiers to none. In this configuration,
backticked names are interpreted as regular expressions. For details,
see Supporting Quoted Identifiers in Column Names.

CREATE TABLE DB_Name.Table_name (
First name VARCHAR(64), Last name VARCHAR(64), Location id VARCHAR(64) , age INT, gpa DECIMAL(3,2)) CLUSTERED BY (age) INTO 2 BUCKETS STORED AS ORC;
OR
CREATE TABLE TEST2(Kod ASS String) STORED AS TEXTFILE;
You can use and put column name inside.
I hope both worked for you.

Related

code convert unique chars to '?' in postgreSQL queries

I have an automation code that runs a bunch of queries into Postgresql DB.
one of my queries is :
CREATE TABLE 行 (CustomerName int, City varchar(255),Country varchar(255))
when running it into the DB, I got this response:
Query response from db:
CREATE TABLE ? (CustomerName int, City varchar(255),Country varchar(255));
ERROR: syntax error at or near "?"
LINE 1: CREATE TABLE ? (CustomerName int, City varchar(255),Country ...
^
postgres=#
it seems that it converts the unique char to '?'.
any suggestion why this could happen?
I'm sure that before the query is executed the letters are encoded correctly.
(when running this query manually everything goes well)
I would avoid using keyword characters, or international characters.
If you really need to do that you can try to use " double quote
Quoted identifiers can contain any character, except the character with code zero. (To include a double quote, write two double-quotes.) This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies.
SQL-SYNTAX-IDENTIFIERS
CREATE TABLE "行" (CustomerName int, City varchar(255),Country varchar(255))

I am unable to drop a column from DB2 table

I am trying to drop a column from my DB2 table.
Table name = Instructor
Column name is Page
Command used is:
ALTER TABLE instructor
DROP COLUMN page;
I am getting this error
Column, attribute, or period "PAGE" is not defined in "GFQ70186.INSTRUCTOR".. SQLCODE=-205, SQLSTATE=42703, DRIVER=4.25.1301
Please help me to understand this error
If your column name is Page (i.e. with a capital P and lower case age) then you will need to use double quotes
ALTER TABLE INSTRUCTOR
DROP COLUMN "Page"
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000720.html
Ordinary identifier:
An ordinary identifier is an uppercase letter followed by zero or more characters, each of which is an uppercase letter, a digit, or the underscore character. Note that lowercase letters can be used when specifying an ordinary identifier, but they are converted to uppercase when processed
Delimited identifier:
A delimited identifier is a sequence of one or more characters enclosed by double quotation marks. Leading blanks in the sequence are significant. A delimited identifier can be used when the sequence of characters does not qualify as an ordinary identifier. In this way an identifier can include lowercase letter

Error trying to rename columns with space in oracle table. Error - SQL Error : ORA- 00946 : missing TO keyword

I have table named employee. In which , I wish to alter the column name from Employee Name to Employee_Name (note that the existing column name has a space!)
When I use the following command:
Alter table employee rename column Employee Name to Employee_Name
I am getting this error:
SQL Error : ORA- 00946 : missing TO keyword
How to overcome this issue?
Since Oracle doesn't know the space is part of the column name, you have to enclose it in quotes to make sure it recognizes it correctly:
Alter table employee rename column "Employee Name" to Employee_Name
I wish to alter the column name from 'Employee Name' to 'Employee_Name'
The problem lies in the fact that you used quoted identifier for column name while creating the table.
From the docs,
Database Object Naming Rules
Every database object has a name. In a SQL statement, you represent
the name of an object with a quoted identifier or a nonquoted
identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you
must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
You need to use double quotation marks around the existing column name to fix the problem:
SQL> CREATE TABLE employee("employee name" VARCHAR2(10));
Table created.
SQL> ALTER TABLE employee RENAME column "employee name" TO employee_name;
Table altered.

"Column ’x’ does not exist" error for string literal ’x’ in PostgreSQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Column ‘mary’ does not exist
I need to check the values that can be accepted to a column through a check constraint. I need to use the check constraint, because this is for a college assignment.
I use this code to create and add the constraint to the table.
CREATE TABLE Ereignis(
E_Id Serial PRIMARY KEY,
Typ varchar(15),
Zeitpunkt timestamp,
Ort varchar(32),
Anzahl_Pers int
);
ALTER TABLE Ereignis ADD
CONSTRAINT typ_ch CHECK (Typ in (’Verkehrsunfall’, ’Hochwasser’, ’Sonstiges’));
Here is the error I get:
ERROR: column "’verkehrsunfall’" does not exist
As I get from this error it tries to compare column typ with column verkehrsunfall, where as I try to check the values that column try can get is one of the (’Verkehrsunfall’, ’Hochwasser’, ’Sonstiges’) strings.
This is exactly the same syntax what our lecturer showed us at the lecture. I am not sure if it is possible to compare varchars with check? Or what am I doing wrong?
Here is the example from the lecture:
CREATE TABLE Professoren
(PersNr INTEGER PRIMARYKEY,
Name VARCHAR( 3 0 ) NOT NULL ,
Rang CHAR(2) CHECK (Rang in ('C2' ,'C3' ,'C4')) ,
Raum INTEGER UNIQUE) ;
Your text editor or word processor is using so-called smart quotes, like ’, not ordinary single quotes, like '. Use ordinary single quotes (actually apostrophes) ' for literals, or double quotes " for identifiers. You also have some odd commas in there which may cause syntax errors. See the PostgreSQL manual on SQL syntax, specicifically lexical structure.
Don't edit SQL (or any other source code) in a word processor. A decent text editor like Notepad++, BBEdit, vim, etc, won't mangle your SQL like this.
Corrected example:
CREATE TABLE Professoren
(PersNr INTEGER PRIMARYKEY,
Name VARCHAR(30) NOT NULL,
Rang CHAR(2) CHECK (Rang in ('C2' ,'C3' ,'C4')),
Raum INTEGER UNIQUE);
The reason it doesn't cause an outright syntax error - and instead gives you an odd error message about the column not existing - is because PostgreSQL accepts unicode column names and considers the ’ character a perfectly valid character for an identifier. Observe:
regress=> SELECT 'dummy text' AS won’t, 'dummy2' as ’alias’;
won’t | ’alias’
------------+---------
dummy text | dummy2
(1 row)
Thus, if you have a column named test and you ask for the column named ’test’, PostgreSQL will correctly tell you that there is no column named ’test’. In your case you're asking for a column named ’verkehrsunfall’ when you meant to use the literal string Verkehrsunfall instead, hence the error message saying that the column ’verkehrsunfall’ does not exit.
If it were a real single quote that'd be invalid syntax. The 1st wouldn't execute in psql at all because it'd have an unclosed single quote; the 2nd would fail with something like:
regress=> SELECT 'dummy2' as 'alias';
ERROR: syntax error at or near "'alias'"
LINE 1: SELECT 'dummy2' as 'alias';
... because in ANSI SQL, that's trying to use a literal as an identifier. The correct syntax would be with double-quotes for the identifier or no quotes at all:
regress=> SELECT 'dummy2' as "alias", 'dummy3' AS alias;
alias | alias
--------+--------
dummy2 | dummy3
(1 row)
You also have an unwanted space in the varchar typmod; varchar( 3 0 ) is invalid:
regress=> SELECT 'x'::varchar( 3 0 );
ERROR: syntax error at or near "0"
LINE 1: SELECT 'x'::varchar( 3 0 );
BTW, in PostgreSQL it's usually better to use a text column instead of varchar. If you want a length constraint for application or validation reasons, add a check constraint on length(colname).

What are valid table names in SQLite?

What are the combination of characters for a table name in SQLite to be valid? Are all combinations of alphanumerics (A-Z, a-z and 0-9) constitute a valid name?
Ex. CREATE TABLE 123abc(...);
What about a combination of alphanumerics with dashes "-" and periods ".", is that valid as well?
Ex. CREATE TABLE 123abc.txt(...);
Ex. CREATE TABLE 123abc-ABC.txt(...);
Thank you.
I haven't found a reference for it, but table names that are valid without using brackets around them should be any alphanumeric combination that doesn't start with a digit:
abc123 - valid
123abc - not valid
abc_123 - valid
_123abc - valid
abc-abc - not valid (looks like an expression)
abc.abc - not valid (looks like a database.table notation)
With brackets you should be able to use pretty much anything as a table name:
[This should-be a_valid.table+name!?]
All of these are allowed, but you may have to quote them in "".
sqlite> CREATE TABLE "123abc"(col);
sqlite> CREATE TABLE "123abc.txt"(col);
sqlite> CREATE TABLE "123abc-ABC.txt"(col);
sqlite> select tbl_name from sqlite_master;
123abc
123abc.txt
123abc-ABC.txt
In general, though, you should stick to the alphabet.
Per Clemens on the sqlite-users mailing list:
Everything is allowed, except names beginning with "sqlite_".
CREATE TABLE "TABLE"("#!#""'☺\", "");
You can use keywords ("TABLE"), special characters (""#!#""'☺\"), and even the empty string ("").
From SQLite documentation on CREATE TABLE, the only names forbidden are those that begin with sqlite_ :
Table names that begin with "sqlite_" are reserved for internal use. It is an error to attempt to create a table with a name that starts with "sqlite_".
If you use periods in the name you will have issues with your SQL Queries. So I would say avoid those.