Unable to create a table and import the data from excel using SQL query in PostgreSQL - postgresql

I'm using the below SQL query to create a table and copy the values from an excel:
CREATE TABLE teacher (
DAILY_ALLOWANCE INTEGER
,DATE_OF_BIRTH DATE
,EMAIL VARCHAR(35)
,FIRST_NAME VARCHAR(35)
,FULL_NAME VARCHAR(35)
,ID VARCHAR(35)
,NAME VARCHAR(35)
,NUMBER_OF_STUDENTS INTEGER
,OWNERID VARCHAR(35)
,SALARY INTEGER
,TEACHER_UNIQUE_ID VARCHAR(15)
,YEARS_OF_EXPERIENCE INTEGER
) copy teacher (
DAILY_ALLOWANCE
,DATE_OF_BIRTH
,EMAIL
,FIRST_NAME
,FULL_NAME
,ID
,NAME
,NUMBER_OF_STUDENTS
,OWNERID
,SALARY
,TEACHER_UNIQUE_ID
,YEARS_OF_EXPERIENCE
)
FROM 'C:\Users\Surendra Anand R\Desktop\Note!\Files\Teacher.csv' csv header;
But I'm getting an error:
ERROR: syntax error at or near "copy"
LINE 14: ) copy teacher (
Could anyone please explain what I'm missing?.

As #a_horse_with_no_name clarified,
The query needs to be run one by one.

Related

insertion of nested array of custom in table for postgres

command = (
CREATE TYPE belongings AS (
item TEXT,
quantity INTEGER
)
CREATE TYPE student AS (
name TEXT,
id INTEGER,
bag belongings[]
)
CREATE TABLE studentclass(
date DATE NOT NULL,
time TIMESTAMPTZ NOT NULL,
PRIMARY KEY (date, time),
class student
)
)
Can i ask how to do insert for this in postgres psycog2? thank you.
When i put the insert as
insert_sql = "INSERT INTO studentclass (date, time, class) VALUES (%s,%s,%s)"
error output is
DETAIL: Cannot cast type text[] to belongings[] in column
I don't think i just cast it with "::belongings[]" in the INSERT statement as it is a nested.
My earlier asked question for a simpler table.
Unable to insert nested record in postgres

When using COPY FROM statement getting ERROR: null value in column "field_id" violates not-null constraint

I am using the COPY FROM command to load data from a file.
The table is defined with identity column, which is not part of the file.
CREATE TABLE APP2DBMAP (
FIELD_ID integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
I executed the following COPY FROM command, the file contains 3 values in 1 row.
copy app2dbmap (field_name, table_name, column_name) from '/opt/NetMgr/data/templ_db.txt' DELIMITER ',' ;
And I got the following error:
ERROR: null value in column "field_id" violates not-null constraint
DETAIL: Failing row contains (null, 'aaa', 'bbb', 'ccc').
CONTEXT: COPY app2dbmap, line 1: "'aaa','bbb','ccc'"
I tried to change the column description of field_id to serial, and it did work fine.
I don't understand why it doesn't work with the original table definition.
The problem is you have specified the field_id to be a not null value and hence when the file is passing null as a value, your error is there.
If you want an auto increment id, Use,
CREATE TABLE APP2DBMAP (
FIELD_ID smallserial NOT NULL,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
You can also use bigserial(int4) instead of smallint(int8)
or you can give a default value,
CREATE TABLE APP2DBMAP (
FIELD_ID integer NOT NULL default 0,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
You will have to pass something, you cannot pass null in a not null column

PostgreSQL Insert with failed unique constraint

CREATE TABLE s_etpta.sfphierg (
hierar VARCHAR(10) NOT NULL,
libelle VARCHAR(40),
typfct VARCHAR(1),
utilcre VARCHAR(10),
datcre DATE,
utilmod VARCHAR(10),
datmod DATE,
CONSTRAINT i_sfphierg PRIMARY KEY(hierar)
)
CREATE TABLE s_etpta.hopsech (
horsect VARCHAR(40) NOT NULL,
libelle VARCHAR(40),
libcourt VARCHAR(20),
horcode VARCHAR(10),
CONSTRAINT i_hopsech PRIMARY KEY(horsect)
)
BEGIN
delete from SFPHIERG;
INSERT INTO SFPHIERG ("hierar", "libelle", "typfct", "utilcre", "datcre",
"utilmod", "datmod")
select '01'||horcode, E'Hircuit standard'||horcode, E'1', E'HQS', E'2007-01-
29', E' ', E'1900-01-01'
FROM HOPSECH where HOPSECH.IJIGHSUPPM like '1'
and not exists (select hierar from SFPHIERG where hierar like '01'||horcode);
INSERT INTO SFPHIERG ("hierar", "libelle", "typfct", "utilcre", "datcre",
"utilmod", "datmod")
select '00'||horcode, E'Circuit cascade'||horcode, E'1', E'HQS', E'2007-01-
29', E' ', E'1900-01-01'
FROM HOPSECH where HOPSECH.IJIGHSUPPR like '1'
and not exists (select hierar from SFPHIERG where hierar like '00'||horcode);
END;
In my functions's body I have two insert queries executed after a delete query.
The rows are correctly inserted when my first column doesn't exist, when it does I have the failed unique constraint error, when this error occur nothing is added into my table.
Is there a way I can stop this error from blocking all the inserts and only the inserts where my first column exists?

Access database, Sql query , Error "Syntax error in DROP TABLE or DROP INDEX."

This is the query , running this in C#.
n getting above error
"DROP TABLE IF EXISTS `NATIONAL_ID_ISSUANCE_CENTER`;
CREATE TABLE `NATIONAL_ID_ISSUANCE_CENTER` (
`ID` INTEGER NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(100),
`APPLICATION_ID` INTEGER,
`STATUS` INTEGER,
`CREATED_BY` INTEGER,
`UPDATED_BY` INTEGER,
`CREATED_DATE` DATETIME,
`UPDATED_DATE` DATETIME,
`THIRD_PARTY_ID` INTEGER,
`PROVINCE_ID` INTEGER,
INDEX (`APPLICATION_ID`),
PRIMARY KEY (`ID`),
INDEX (`PROVINCE_ID`),
INDEX (`THIRD_PARTY_ID`)
)"
You can't put an IF statement inside Drop and Create statements. Anytime you want to drop a table that you're not sure exists, use the following:
IF(OBJECT_ID('[Database].[Schema].[TableName]') is not null)
BEGIN
DROP TABLE [Database].[Schema].[TableName];
END;
Please note you should replace [Database], [Schema], and [TableName] with the appropriate database, schema, and table names, respectively.

PostgreSQL create table syntax

I'm more a mysql person, but I have to do a db in pg and the following CREATE TABLE keeps generating syntax errors... I just get an error: ERROR: syntax error at or near "(" and error: ERROR: syntax error at or near ")" Googling around didn't give me much help... I'm sure that I'm doing something mysql-esque and that's causing problems... (Note: I did already create the mfseq successfully...)
CREATE TABLE master_file (
mfid INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('mfseq'),
prefix VARCHAR(4),
fname VARCHAR(30) NOT NULL,
lname VARCHAR(80) NOT NULL,
MI varchar(1) NULL,
address1 VARCHAR(200) NOT NULL,
address2 VARCHAR(200),
city VARCHAR(28),
state VARCHAR(2),
zip INT(5),
zip_plus4 INT(4),
mrn VARCHAR(30),
aID INT,
iID INT,
gID VARCHAR(1),
pphone VARCHAR(10);
);
Maybe int -> integer and without size (or numeric) and delete the delimiter at pphone field.
It should not be a semi-colon here: pphone VARCHAR(10);