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

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

Related

Can we use foreign key as primary key along with another attribute as below?

I have to create this project for one of my assignments and I have to use Transact-SQL to create this database. I have changed this below code many times to avoid this error but its still keep coming and can't think of any reason for this.
CREATE TABLE Job
(
Job_ID INT NOT NULL IDENTITY (500, 1),
Pickup_Address VARCHAR(255),
Destination_Address VARCHAR(255),
Crew_Name VARCHAR(255),
Customer_Name VARCHAR(255),
PRIMARY KEY(Job_ID)
)
GO
INSERT INTO Job(Pickup_Address,Destination_Address,Crew_Name,Customer_Name)
VALUES ('Ceylinco Centre Building, 3rd Floor, Nawam Mawatha','NO.50, Sumanagala Road,Rathmalana','Maharagama Crew 1','Dilan'),
('3/82, ST.JUDE LANE,Dalugama','19A, 4th Lane,Koswatthe Rd','Maharagama Crew 2','Kasun'),
('19 Saunders Place,Colombo 12','54/3, Elapitiwala,Ragama','Kottawa Crew 1','Kelly'),
('381 Prince of Wales Avenue,Colombo 14','51, SEA STREET,Colombo','Kottawa Crew 2','Kasun'),
('250 Galle Road,Colombo 03','34, Pepiliyana Road,Nugegoda','Nugegoda Crew 2','Alan')
GO
CREATE TABLE Loads
(
Load_ID INT NOT NULL IDENTITY (1, 1),
Job_ID INT NOT NULL FOREIGN KEY REFERENCES Job(Job_ID),
Load_Type VARCHAR(255),
Product_Name VARCHAR(255),
Loaded_Time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY (Load_ID, Job_ID),
)
GO
I have executed the above SQL code, previously I couldn't create the Loads table and got an error
Invalid referencing table
After I tried to avoid this now it can be executed although there is an error and it's in below. what I need to know is the reason for this and will it be a problem if I keep this as it is. Thank you
Foreign key 'FK_Loads_c6b32eef601ef719ed3' References invalid table 'job'
The code works, you may have the case sensitive collation. To know the collation you can run this code:
SELECT SERVERPROPERTY('collation');
If this is the case, you can change it (2008 or higher, except azure) with code like the following (adding the desired collation):
ALTER DATABASE YOURS_DATABASE
COLLATE Modern_Spanish_CS_AS ;
more information here here

spark sql how to create tables in databricks

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
)

Creating a table in SQL using Oracle 11G

I am new to learning SQL and have been struggling to create a table for an assignment. These are the requirements:
Create a new table to track the Library location.
LIBRARY (lib_id, lib_name, lib_address, lib_city, lib_state, lib_zip)
LIB_ID is the library id – it is an auto generated number. (you should create a sequence number called lib_id_seq, start with 1001 and increment by 1.)
LIB_ID is the primary key.
LIB_NAME, LIB_ADDRESS, and LIB_CITY is between 1 and 35 characters.
LIB_STATE is 2 characters – default to TX.
LIB_ZIP is 5 numbers. Check for one of the following zip codes – 75081, 75080, 75082, 75079, 75078
And this is what I have written out so far:
CREATE TABLE LIBRARY
(
LIB_ID INT(4),
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT ‘TX’,
LIB_ZIP INT(5) CHECK (Frequency IN ('75078', ‘75079', '75080', '75081', ‘75082’))
PRIMARY KEY(LIB_ID)
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
I keep getting errors, but am not sure what I need to fix.
For oracle (Kid Tested unsure if SO approved)...
use varchar2 instead of varchar
use Number instead of int
added constraint syntax (named them)
adjusted apostrophe's (Removed) instead of whatever the heck you had in some of them :P (It's a numeric field shouldn't be using text apostrophes!)
personally I wouldn't name a table library as that's a reserved word
I woudln't use a numeric Zip code as we will never do math on a zipcode.
.
.
CREATE TABLE LIBRARY (
LIB_ID Number(4),
LIB_ADDRESS VARCHAR2(35),
LIB_CITY VARCHAR2(35),
LIB_STATE VARCHAR2(2) DEFAULT 'TX',
LIB_ZIP NUMBER(5),
CONSTRAINT Lib_ZIP_CON CHECK (LIB_ZIP IN (75078, 75079, 75080, 75081, 75082)),
CONSTRAINT LIB_ID_PK PRIMARY KEY(LIB_ID)
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
This works for SQL Server. You need to modify the syntax accordingly for the concerned db.
CREATE TABLE LIBRARY
(
LIB_ID INTEGER PRIMARY KEY,
LIB_ADDRESS VARCHAR(35),
LIB_CITY VARCHAR(35),
LIB_STATE VARCHAR(2) DEFAULT 'TX',
LIB_ZIP INTEGER,
CHECK( LIB_ZIP IN ('75078', '75079', '75080', '75081', '75082') )
);
CREATE SEQUENCE LIB_ID_SEQ
START WITH 1001
INCREMENT BY 1;
For learning how to create tables and constraints check this link on w3schools as you seem to be a beginner.
http://www.w3schools.com/sql/sql_primarykey.asp

Creating Databases in SQLite

We were asked to create a database in sqlite3 and then create a table in it. I used this command:
$sqlite3 me5.db
and tried to create a table with this statement:
CREATE TABLE me5.petID(pet id PRIMARY KEY int(3), pet name varchar(10), pet type varchar(10), pet age int(3));
but it says that:
ERROR: near "CREATE" : syntax error
What could I have possible done wrong? Thanks.
try this
CREATE TABLE petID(pet_id int(3) PRIMARY KEY, pet_name varchar(10), pet_type varchar(10), pet_age int(3));
you dont have to specify the database name because you're already using it after the command sqlite3 me5.db.
you have spaces in the names of the fields, which is not allowed. so i've put underscores instead of spaces.
use PRIMARY KEY after int(3)

auto_increment causing errors with CREATE TABLE

I have been using netbeans as a tool for my java, and i have a problem. I read this tutorial and then i tried to create a table using this SQL:
CREATE TABLE CUSTOMERS (
ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(30),
ADDRESS VARCHAR(30),
CITY VARCHAR(30),
STATE_ VARCHAR(30),
ZIP VARCHAR(15),
COUNTRY_ID INTEGER,
PHONE VARCHAR(15),
EMAIL_ADDRESS VARCHAR(50)
)ENGINE=INNODB;
When i tried to run it, I got this error message:
sql state 42X01 : Syntax error :
encountered "AUTO_INCREMENT" at line 2
column 29
and when i delete the AUTO_INCREMENT, another error:
detected ENGINE=INNODB;
can someone help me? Thanks.
You seem to be using MySQL syntax with another database engine. The parts it complained about are precisely the MySQL-specific ones.
my sugestion would be the following
CREATE TABLE CUSTOMERS
( ID INTEGER NOT NULL auto_increment,
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(30),
ADDRESS VARCHAR(30),
CITY VARCHAR(30),
STATE_ VARCHAR(30),
ZIP VARCHAR(15),
COUNTRY_ID INTEGER,
PHONE VARCHAR(15),
EMAIL_ADDRESS VARCHAR(50),
PRIMARY KEY (ID));
Dunno what the engine=innodb is for, have you tried without it?
The "engine=innodb" part specifies the database engine that gets used in the database. With MySQL you can specify different engines like "InnoDB", "MyISAM", etc. They have different properties and features - some allow foreign indexes, some do not. Some have different locking mechanisms, some have different atomicity/rollback properties. I don't know the details but if you need a really high-performance database setup you should investigate which engine is best for each type of table you're creating. Also, all my database experience has been with MySQL and I'm not sure if that's what you're using.
Been a long time but if anybody else stumbles on this like I did, a solution that worked for me is instead of using auto_increment, describe the ID column as
ID INTEGER GENERATED ALWAYS AS IDENTITY, WHATEVER VARCHAR(20), ETC ETC...