Partition table created using 'CREATE TABLE AS' - postgresql

How do we partition a table created using the CREATE TABLE AS command in PostgreSQL. I tried the following:
CREATE TABLE schema.table2 AS TABLE schema.table1 PARTITION BY LIST(col1)
but it gives this error:
ERROR: syntax error at or near "PARTITION"

That's not possible.
CREATE TABLE AS is different from CREATE TABLE and only supports a subset of features for the latter.

Related

ibm db2 create index in tablespace does not work

I created a database on db2 11.5, then tablespace, then create a table. Everything okay so far.
However, when i tried to create index in the newly created TABLESPACE, it complains syntax error:
CREATE INDEX SCH.TBL_CAT_ERR_NIX01 ON SCH.TBL_CAT_ERR (CAT_NO ASC, CAT_ERR_ID ASC) in TBS_CAT_SINDEX;
with error:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0109N The statement or command was not processed because the following
clause is not supported in the context where it is used: "IN". SQLSTATE=42601
i tried these 2 but still they don't work, complaining the "PARTITION" clause is not supported
CREATE INDEX SCH.TBL_CAT_ERR_NIX01 ON SCH.TBL_CAT_ERR (CAT_NO ASC, CAT_ERR_ID ASC) partitioned in TBS_CAT_SINDEX;
CREATE INDEX SCH.TBL_CAT_ERR_NIX01 ON SCH.TBL_CAT_ERR (CAT_NO ASC, CAT_ERR_ID ASC) not partitioned in TBS_CAT_SINDEX;
Could you help me pointing out what I'm missing?
When you run your create table statement, you can optionally specify the index in clause at that time to allow indexes to use a specific tablespace that you pre-created. Additional capabilities are available for PARTITIONED tables.
The key detail you omitted from your question was whether or not your table is itself partitioned.
The documentation for create index states that the IN tablespapace-name clause can only be specified or a nonpartitioned index on a partitioned table. So if your table is not itself partitioned then you cannot use this IN clause when creating an index, and you should consider using the create table statement to identify an index tablespace for that table instead.

Is it possible to create partitioned table with 'create table as' in PostgreSQL?

I am trying to create a partitioned table as follows:
create table archive.table1
as table work1.table1 with no data
partition by range (wk_date)
and I am getting the following error:
SQL Error [42601]: ERROR: syntax error at or near "partition"
Position: 98
You can run the following, which is simpler and will work:
CREATE TABLE archive.table1 (LIKE work1.table1) PARTITION BY RANGE (wk_date);
No, this is not possible.
You need to first create the partitioned table, then you need to create the partitions. Once that is done you can do an insert into partitioned_table select * from old_table

Drop temporary tables created by Amazon Redshift

I’m trying to drop temporary tables created by Redshift.
I use the following query to find all the temp tables in the cluster:
select name, count(distinct id)
from stv_tbl_perm
where temp = 1
group by 1
The table i'm trying to drop called $stg_inappshourly.
I've tried to drop it in both of the following methods:
drop table $stg_inappshourly
drop table stg_inappshourly
The first one returns a syntax error. The second one drops the actual table.
Any ideas how to drop it?
Solved.
The reason this table kept existing is because its session had an error and it didn't close as expected.
The only way I found to remove this table was rebooting the Redshift instance.

Issue with basic create statements in postgreSQL

I've used the psql module to create a new database using the following syntax:
CREATE DATABASE fish
I can open the database. However, when I try to create tables or columns it gives me a syntax error for the following message.
CREATE TABLE salmon;
this is the error message:
ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE species;
I've checked a lot of online postgreSQL resources and they haven't been of much help. To the best of my knowledge, I haven't messed up the syntax. Thanks.
you can use this syntax for empty table:
create table salmon();
You must create atleast one column in a table:
CREATE TABLE salmon ( column_name data_type ...........);
Postgres create table link: https://www.postgresql.org/docs/9.1/static/sql-createtable.html
You can't create an empty table - it must have at least one column. E.g.:
CREATE TABLE salmon (name VARCHAR(10));
psql is not a module. Please read https://www.postgresql.org/docs/current/static/app-psql.html
you odn't open a database - you connect to it.
Establishes a new connection to a PostgreSQL server
https://www.postgresql.org/docs/current/static/sql-createtable.html
{ column_name | ( expression ) }
Either column list (create table a (a int);) or expression (create table b as select now() time_column) is obligatory part.

Delete column in hive table

I am working with hive version 0.9 and I need delete columns of a hive table. I have searched in several manuals of hive commands but I only I have found commands to version 0.14. Is possible to delete a column of a hive table in hive version 0.9? What is the command?
Thanks.
We can’t simply drop a table column from a hive table using the below statement like sql.
ALTER TABLE tbl_name drop column column_name ---- it will not work.
So there is a shortcut to drop columns from a hive table.
Let’s say we have a hive table.
From this table I want to drop the column Dob. You can use the ALTER TABLE REPLACE statement to drop a column.
ALTER TABLE test_tbl REPLACE COLUMNS(ID STRING,NAME STRING,AGE STRING); you have to give the column names which you want to keep in the table
There isn't a drop column or delete column in Hive.
A SELECT statement can take regex-based column specification in Hive releases prior to 0.13.0, or in 0.13.0 and later releases if the configuration property hive.support.quoted.identifiers is set to none.
That being said you could create a new table or view using the following:
drop table if exists database.table_name;
create table if not exists database.table_name as
select `(column_to_remove_1|...|column_to_remove_N)?+.+`
from database.some_table
where
...
;
This will create a table that has all the columns from some_table except the columns named column_to_remove_1, ... , to column_to_remove_N. You can also choose to create a view instead.
ALTER TABLE table_name REPLACE COLUMNS ( c1 int, c2 String);
NOTE: eliminate column from column list. It will keep matched columns and removed unmentioned columns from table schema.
we can not delete column from hive table . But droping a table(if its external) in hive and the recreating table(with column excluded) ,wont delete ur data .
so what can u do is(if u dont have table structure) run this command :
show create table database_name.table_name;
Then you can copy it and edit it (with column eliminated).Afterwards you can do as per invoke the shell
table details are empid,name,dept,salary ,address. i want remove address column. Just write REPLACE COLUMNS like below query
jdbc:hive2://> alter table employee replace columns(empid int, name string,dept string,salary int);
As mentioned before, you can't drop table using an alter statement.
Alter - replace is not guaranteed to work in all the cases.
I found the best answer for this here:
https://stackoverflow.com/a/48921280/4385453