Is there a way in a derby database to add a column after another just like mysql does?
I don't believe so. Column ordering isn't really a standard SQL feature. Well-written db applications shouldn't care about column ordering. You can specify the order of the columns in your output by naming the columns in your SQL statement as in:
create table t (a int, b int, c int );
select b, c, a from t;
Related
Is there any way to ALTER a table and change the column name in clickhouse?
I only found to change tha table name but not for an individual column in a straight forward way.
Thanks.
The feature has been introduced here into v20.4.
ALTER TABLE table1 RENAME COLUMN old_name TO new_name
You can also rename multiple columns at on:
ALTER TABLE table1
RENAME COLUMN old_name1 TO new_name1,
RENAME COLUMN old_name2 TO new_name2
Old answer:
ClickHouse doesn't have that feature yet.
Implementation is not trivial, because ALTERs that changing columns
are processed outside of usual replication queue, and adding rename
without reworking of ALTERs will introduce race conditions in
replicated tables.
https://github.com/yandex/ClickHouse/issues/146#issuecomment-255631384
As #Slash said, the solution for now is to create new table and
INSERT INTO `new_table` SELECT * FROM `old_table`
Do not forget that column aliasing won't work there (AS).
INSERT INTO `new_table` SELECT a, b AS c, c AS b FROM `old_table`
That will still insert a into first column, b into second column and c into third column. AS has no effect there.
You can try use CREATE TABLE new_table with another field name
and run INSERT INTO new_table SELECT old_field AS new_field FROM old_table
If you created the table using Engine=log, it won't allow you to alter or rename the column.
connection_string = f'clickhouse://{username}:{password}#{host}:{port}/{database}'
engine = create_engine(connection_string)
conn = engine.connect()
table = "table1"
schema = 'Parameter String, Key UInt8'
engine.execute("CREATE TABLE IF NOT EXISTS {}({}) ENGINE = Log".format(table,schema))
If you created table using the mergeTree engine, it’s allowed to rename the column:
engine.execute("CREATE TABLE IF NOT EXISTS {}({}) ENGINE =MergeTree ORDER BY Key".format(table,schema))
How can I make this work?
hive> desc temp;
OK
a int
b int
Time taken: 0.077 seconds, Fetched: 2 row(s)
I want to create t2 HIVE table with column names c and d but getting below error.
hive> create table t2(c int,d int) as select a,b from temp;
FAILED: SemanticException [Error 10065]: CREATE TABLE AS SELECT command cannot specify the list of columns for the target table
You need not to mention table schema again as you are specifying to take the schema from another table. So your table creation statement should be like
create table t2 as select a,b from temp;
I know this is old, but by definition, but in case anyone else has the same question, the answer is pretty straightforward.
By definition, CREATE TABLE AS SELECT is used to persist a resultset as a table. This means that if you want the new table to have different column names, you need to use a different resultset to create it. An easy way you do that is with column aliases:
create table t2 as select a as c, b as d from temp;
I know you said you didn't want to do this (although there was a small typo - your query would fail because it would try to name both columns c), but hopefully this explains why you should approach it this way: you need to build a dataset whose metadata matches the table you want to create.
How do we alter the datatype of multiple columns in Hive ?
CREATE TABLE test_change (a int, b int, c int);
ALTER TABLE test_change CHANGE a a string b b doube c c decimal(11,2);
As far as I know, you can't. In the Hive documentation you can find the following:
ALTER TABLE table_name [PARTITION partition_spec] CHANGE [COLUMN] col_old_name col_new_name column_type
[COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];
This command will allow users to change a column's name, data type, comment, or position, or an arbitrary combination of them. The PARTITION clause is available in Hive 0.14.0 and later; see Upgrading Pre-Hive 0.13.0 Decimal Columns for usage. A patch for Hive 0.13 is also available (see HIVE-7971).
The documentation is speaking about "a column".
The alternative would be to write multiple queries, one for each datatype you have to change.
Reference: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL
I have two columns Id and Name in Hive table, and I want to delete the Name column. I have used following command:
ALTER TABLE TableName REPLACE COLUMNS(id string);
The result was that the Name column values were assigned to the Id column.
How can I drop a specific column of the table and is there any other command in Hive to achieve my goal?
In addition to the existing answers to the question : Alter hive table add or drop column
As per Hive documentation,
REPLACE COLUMNS removes all existing columns and adds the new set of columns.
REPLACE COLUMNS can also be used to drop columns. For example, ALTER TABLE test_change REPLACE COLUMNS (a int, b int); will remove column c from test_change's schema.
The query you are using is right. But this will modify only schema i.e, the metastore. This will not modify anything on data side.
So, before you are dropping the column you should make sure that you hav correct data file.
In your case the data file should not contain name values.
If you don't want to modify the file then create another table with only specific column that you need.
Create table tablename as select id from already_existing_table
let me know if this helps.
i have a table t1 in database A and another table t2 in another database B.
how can we select data from them without using qualifiers directly..
just as database1.table1.something.
You should be able to query it with fully qualified names such as SCHEMA.TABLE#DBLINK.
If you don't want to use the #DBLINK notation while querying from database A you can mask the #DBLINK in a view (In database A) and query that view instead.
CREATE OR REPLACE VIEW remote_table [(column_list)]
AS select * FROM SCHEMA.TABLE#DBLINK;