spark sql how to create tables in databricks - apache-spark-sql

hey guys i am new to databricks and i have been trying to create tables using spark sql but i was unable to do so due to the error : Error in SQL statement: ParseException:
no viable alternative at input 'Create table offices(\n\tAreaCode VARCHAR(10) NOT NULL PRIMARY'(line 2, pos 31)
As i do not know what is wrong with the code as i am able to create the exact table using the below code. Do let me know if there is any suggestion or alternatives!.
Create table offices(
AreaCode VARCHAR(10) NOT NULL PRIMARY KEY,
city VARCHAR(50) NOT NULL
)

Take the primary key out
Create table offices(
AreaCode VARCHAR(10) NOT NULL,
city VARCHAR(50) NOT NULL
)

Related

Presto Hive SQL Error: mismatched input 'PARTITIONED'. Expecting: 'COMMENT', 'WITH', <EOF>

I am trying to create a Hive table with partitions but getting the above error. What am I doing wrong?
CREATE TABLE IF NOT EXISTS schema.table_name
(
ID varchar(20),
name varchar(50)
)
PARTITIONED BY (part_dt varchar(8), system varchar(5));
The code works without the partitioning clause. Something gives up during partitioning.
Statement is working in hive. Pls find below screenshot.
Its possible that some of column names are reserved keywords and that is throwing error. if yes, you can use below SQL too.
CREATE TABLE IF NOT EXISTS schema.table_name
(
`ID` varchar(20),
`name` varchar(50)
)
PARTITIONED BY (`part_dt` varchar(8), `system` varchar(5));

sql creating table issue

unexpected beginning of statement
the coding
If you are using SQL then the issue is with create query where you passes int(10)
you can not pass any size to int type of fields
create table acc
(acc_id int not null primary key,
username_acc varchar(30) not null,
....)
Try with above
Thanks

i want to create a table but bot able to create it

i want to create a table in hive with NOT NULL property but i am not able to create it . it will says "ParseException line 1:44 mismatched input 'NOT' expecting ) near ')' in create table statement".
i have tried with primary key option but it will give same error
create table AGENTS(agent_code varchar(10) NOT NULL ,
agent_name varchar(40),
working_area varchar(30),
commission double(10,5),
phone_no int(15),
country varchar(25));
I don't think Hive supports double with scale and precision arguments. And, I don't think there is a length for int Perhaps you intend numeric:
create table AGENTS (
agent_code varchar(10) NOT NULL ,
agent_name varchar(40),
working_area varchar(30),
commission decimal(10, 5),
phone_no decimal(15),
country varchar(25)
);
NOT NULL constraints are only enforced relatively recently, so that might also be a problem.
Check your Hive version. Older versions of Hive do not support NOT NULL constraint like most databases. NOT NULL constraint was introduced from 3.0.0 version onwards.
Reference: https://issues.apache.org/jira/browse/HIVE-16575

In H2 Database, add index while table creation in single query

I am trying to create table having different indexes with single query but H2 gives Error for example:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
but this gives error as
Unknown data type: "("; SQL statement:
[Error Code: 50004]
[SQL State: HY004]
Due to this I have to run 2 different queries to create table with Index. First query to create table and then second query to add index with
create INDEX c_fid on tbl_Cust(fid);
Is there something wrong in my query or H2 simply does not support this creation of table with index in single query?
Interesting question. The solution is even more interesting, as it involves MySQL compatibility mode.
It's actually possible to perform the exact same command you wrote without any modification, provided you just add to your jdbc url the MySQL mode.
Example URL like this: jdbc:h2:mem:;mode=mysql
SQL remains:
create table tbl_Cust
(
id int primary key auto_increment not null,
fid int,
c_name varchar(50),
INDEX (fid)
);
Update count: 0
(15 ms)
Too bad I did not see this question earlier... Hopefully the solution might become handy one day to someone :-)
I could resolve the problem. According to
http://www.h2database.com/html/grammar.html#create_index
I modified the query. It works fine with my H2 server.
CREATE TABLE subscription_validator (
application_id int(11) NOT NULL,
api_id int(11) NOT NULL,
validator_id int(11) NOT NULL,
PRIMARY KEY (application_id,api_id),
CONSTRAINT subscription_validator_ibfk_1 FOREIGN KEY (validator_id) REFERENCES validator (id) ON UPDATE CASCADE
);
CREATE INDEX validator_id ON subscription_validator(validator_id);

ORA-00907: missing right parenthesis error

CREATE TABLE dbo.PhotoLibrary (
PhotoLibraryID INT IDENTITY (1, 1) PRIMARY KEY,
ImageName VARCHAR(100),
Photo VARBINARY(MAX)
)
While using the above code in SQL I am getting the error help me
You are passing in SQL Server code to Oracle. Big problem.
CREATE TABLE PhotoLibrary (
PhotoLibraryID INT PRIMARY KEY,
ImageName VARCHAR2(100),
Photo VARBINARY(MAX)
);
Oracle doesn't understand identity. If you want an auto-incrementing column, then you need to use a sequence. Here is an example.