Drop a hive table named "union" - sql

I am trying to drop a table names "union" but I keep getting an error.
I am not sure who and how created that table, but nothing works on it, including describe or select.
Using "hdfs dfs -ls" outside of hive, I can see that table exists and there is data in it, but cannot drop the table.
I am assuming there may be a problem because the table is called "union" and the error I get is
"cannot recognize input near 'union'".
How can I drop the table?

to escape in hive you can use bakctick:
DROP TABLE IF EXISTS `union`;

Related

Databricks - is not empty but it's not a Delta table

I run a query on Databricks:
DROP TABLE IF EXISTS dublicates_hotels;
CREATE TABLE IF NOT EXISTS dublicates_hotels
...
I'm trying to understand why I receive the following error:
Error in SQL statement: AnalysisException: Cannot create table ('default.dublicates_hotels'). The associated location ('dbfs:/user/hive/warehouse/dublicates_hotels') is not empty but it's not a Delta table
I already found a way how to solve it (by removing it manually):
dbutils.fs.rm('.../dublicates_hotels',recurse=True)
But I can't understand why it's still keeping the table?
Even though that I created a new cluster (terminated the previous one) and I'm running this query with a new cluster attached.
Anyone can help me to understand that?
I also faced a similar problem, then tried the command line CREATE OR REPLACE TABLE and it solved my problem.
DROP TABLE & CREATE TABLE work with entries in the Metastore that is some kind of database that keeps the metadata about databases and tables. There could be the situation when entries in metastore don't exist so DROP TABLE IF EXISTS doesn't do anything. But when CREATE TABLE is executed, then it additionally check for location on DBFS, and fails if directory exists (maybe with data). This directory could be left from some previous experiments, when data were written without using the metastore.
if the table created with LOCATION specified - this means the table is EXTERNAL, so when you drop it - you drop only hive metadata for that table, directory contents remains as it is. You can restore the table by CREATE TABLE if you specify the same LOCATION (Delta keeps table structure along with it's data in the directory).
if LOCATION wasn't specified while table creation - it's a MANAGED table, DROP will destroy metadata and directory contents

Drop a View or Table, unknowing which it is, in sqlite3

I am in a situation where I want to drop a view or table, but can only know at run-time which it is (same identifier though). This does not work:
DROP VIEW IF EXISTS my_table_or_view;
Because if my_table_or_view is a table, it will throw:
android.database.sqlite.SQLiteException: use DROP TABLE to delete table my_table_or_view
Likewise, I cannot use DROP TABLE, because it tells me to use DROP VIEW if i have a view at hand. I could catch the error, of course, but since this is part of a larger transaction, I would definitely prefer an answer that works using pure SQL (as understood by sqlite3). Any ideas?
You can get information about items in your sqlite database by SELECTing from a pseudo-table called sqlite_master.
In this case, you would do:
SELECT type FROM sqlite_master WHERE name = 'my_table_or_view'
and the resulting information will tell you whether you're dealing with a table or a view.
More info: http://www.sqlite.org/faq.html#q7

How to delete a mysql-enum-like column in sql server?

in orther to get a column similar to the mysql ENUM type, I wrote a sql query as follows
ALTER TABLE [DbName].[dbo].[MediaContent]
ADD MediaType nvarchar(50)
check(MediaType in ('audio','video','song','other'))
this worked as wished(for test): But now I want to delete this column without success. It seems like there no way to directly delete a column which has a constraint up on it.
How can I solve this issue? I want to delete this column and create another one.
here is the error message I get while the deletion
The object 'CK__MediaCont__Media__14270015' is dependent on column 'MediaType'.
ALTER TABLE DROP COLUMN MediaType failed
because one or more objects access this
column. (Microsoft SQL Server, Error: 5074)
The object referenced in the error message is the name of the constraint. You should be able to use the follow:
ALTER TABLE [DbName].[dbo].[MediaContent]
DROP CONSTRAINT CK__MediaCont__Media__14270015
You need to first drop the check constraint mentioned in the error message since that's stopping you from dropping the column. Following that you may drop the column.
Drop the constrain first then drop the column ,it will work

How to deal with a Firebird table that's apparently both there and not there

I've just had something very strange happen to me with a Firebird database.
I was trying to create a table, and the CREATE TABLE failed for some reason. But now it's stuck in a very strange state:
If I try to CREATE TABLE again with the same table name, it gives an error: the table already exists. But if I try to DROP TABLE that table, it gives an error: the table does not exist. Trying to SELECT * FROM that table gives the "table does not exist" error, and the name does not show up in the metadata query:
SELECT RDB$RELATION_NAME
FROM RDB$RELATIONS
WHERE RDB$SYSTEM_FLAG=0
So for some reason, the table really seems to not be there, but I can't create it because something somewhere indicates that it does exist.
Does anyone have any idea how to fix this? I've already tried closing all connections to that database, which has helped with inconsistency issues in the past, but this time it doesn't help.
You didn't give details about what was the error when you tried to create the table, so I cannot comment it. But RDB$RELATIONS is not the only system table affected when you create a table. Maybe you are now in an inconsistent situation where some information about that table exists in some system tables and doesn't exists in others.
Another option is corrupted indexes in the system tables, so the record is not there but the index think it still exists.
Try to do a backup/restore and see if it helps. It it doesnt work, try to search for records related to that "non created" table in the other system tables (RDB$RELATION_FIELDS, etc) and if you find any, try to delete them.
As a last option, you may create a new clean database with correct metadata and pump your data to it using IBDataPump.

There is already an object named 'tbltable1' in the database

I am trying to insert data from one table to another with same structure,
select * into tbltable1 from tbltable1_Link
I am getting the following error message:
There is already an object named 'tbltable1' in the database.
The SELECT INTO statement creates a new table of the name you provide and populates it with the results of the SELECT statement.
I think you should be using INSERT INTO since the table already exists. If your purpose is in fact to populate a temporary table, then you should provide a table name that does not already exist in the database.
See MSDN for more information on this.
If you are confident that tbltable1 is not required, you can drop the table first.
You may also want to consider using temporary tables...
Select * into ##MyTemporaryTable FROM tblTable1_Link
You can then use the temporary table in this session. (Ending the session should drop the temporary table automatically, if I remember correctly. It's been a while since I've worked with SQL Server).