PostgreSQL create table syntax - postgresql

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);

Related

can't import files csv in pgAdmin 4

i will import data csv to postgresql via pgAdmin 4. But, there are problem
ERROR: invalid input syntax for type integer: ""
CONTEXT: COPY films, line 1, column gross: ""
i understand about the error that is line 1 column gross there is null value and in some other columns there are also null values. My questions, how to import file csv but in the that file there is null value. I've been search in google but not found similar my case.
CREATE TABLE public.films
(
id int,
title varchar,
release_year float4,
country varchar,
duration float4,
language varchar,
certification varchar,
gross int,
budget int
);
And i try in this code below, but failed
CREATE TABLE public.films
(
id int,
title varchar,
release_year float4 null,
country varchar null,
duration float4 null,
language varchar null,
certification varchar null,
gross float4 null,
budget float4 null
);
error message in image
I've searched on google and on the stackoverflow forums. I hope that someone will help solve my problem
There is no difference between the two table definitions. A column accepts NULL by default.
The issue is not a NULL value but an empty string:
select ''::integer;
ERROR: invalid input syntax for type integer: ""
LINE 1: select ''::integer;
select null::integer;
int4
------
NULL
Create a staging table that has data type of varchar for the fields that are now integer. Load the data into that table. Then modify the empty string data that will be integer using something like:
update table set gross = nullif(trim(gross), '');
Then move the data to the production table.
This is not a pgAdmin4 issue it is a data issue. Working in psql because it is easier to follow:
CREATE TABLE public.films_text
(
id varchar,
title varchar,
release_year varchar,
country varchar,
duration varchar,
language varchar,
certification varchar,
gross varchar,
budget varchar
);
\copy films_text from '~/Downloads/films.csv' with csv
COPY 4968
CREATE TABLE public.films
(
id int,
title varchar,
release_year float4,
country varchar,
duration float4,
language varchar,
certification varchar,
gross int,
budget int
);
-- Below done because of this value 12215500000 in budget column
alter table films alter COLUMN budget type int8;
INSERT INTO films
SELECT
id::int,
title,
nullif (trim(release_year), '')::real, country, nullif(trim(duration), '')::real,
LANGUAGE,
certification,
nullif (trim(gross), '')::float, nullif(trim(budget), '')::float
FROM
films_text;
INSERT 0 4968
It worked for me:
https://learnsql.com/blog/how-to-import-csv-to-postgresql/
a small workaround but it works
I created a table
I added headers to csv file
Right click on the newly created table-> Import/export data, select csv file to upload, go to tab2 - select Header and it should work

CREATE TABLE DB2 SQLSTATE: 42601, SQLCODE: -104): DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601

I am trying to create a DB2 table with the following
CREATE TABLE ACQ_FAH_DEV.fah_balance_ledger
(
"ACTIVE" VARCHAR(10),
INPUT_BY VARCHAR(32),
INPUT_TIME DATE,
AMENDED_BY VARCHAR(32),
AMENDED_TIME DATE,
ENTITY VARCHAR(20),
ACCOUNT_CODE VARCHAR(20),
ACCOUNT_NAME VARCHAR(255),
PORTFOLIO_CODE VARCHAR(100),
OM_LOAD_RUN_ID VARCHAR(128) NOT NULL,    
OM_LOAD_TMST TIMESTAMP(6) NOT NULL,    
BM_BUSINESS_INTERVAL_TYP VARCHAR(20) NOT NULL,
BM_BUSINESS_INTERVAL_TMST TIMESTAMP(6) NOT NULL,    
BM_BUSINESS_INTERVAL_START_END_FLAG VARCHAR(10) NOT NULL,    
SM_SOURCE_SYSTEM_CD VARCHAR(16) NOT NULL,    
OM_UNIQUE_ROW_ID BIGINT NOT NULL,    
OM_USER_ID VARCHAR(100) NOT NULL,    
OM_VERSION_ID SMALLINT NOT NULL
)
ORGANIZE BY COLUMN IN ACQ_FAH_DEV
DISTRIBUTE BY HASH(
ACCOUNT_CODE,
OM_VERSION_ID,
BM_BUSINESS_INTERVAL_TYP
);
but running into this error
(SQLSTATE: 42601, SQLCODE: -104): DB2 SQL Error: SQLCODE=-104,
SQLSTATE=42601, SQLERRMC=(;LOAD_RUN_ID VARCHAR;BINARY, DRIVER=4.26.14
SQLSTATE 42601: A character, token, or clause is invalid or missing.
SQL0104N An unexpected token "(" was found following "LOAD_RUN_ID
VARCHAR". Expected tokens may include: "BINARY".
The error seems innocuous but I am not able to see why this is failing.
For LUW, the tablespace clause should be before the organized clause (I assume IN ACQ_FAH_DEV refers to which tablespace to put the table in). Try:
CREATE TABLE ACQ_FAH_DEV.fah_balance_ledger
(
"ACTIVE" VARCHAR(10),
INPUT_BY VARCHAR(32),
INPUT_TIME DATE,
AMENDED_BY VARCHAR(32),
AMENDED_TIME DATE,
ENTITY VARCHAR(20),
ACCOUNT_CODE VARCHAR(20),
ACCOUNT_NAME VARCHAR(255),
PORTFOLIO_CODE VARCHAR(100),
OM_LOAD_RUN_ID VARCHAR(128) NOT NULL,
OM_LOAD_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_TYP VARCHAR(20) NOT NULL,
BM_BUSINESS_INTERVAL_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_START_END_FLAG VARCHAR(10) NOT NULL,
SM_SOURCE_SYSTEM_CD VARCHAR(16) NOT NULL,
OM_UNIQUE_ROW_ID BIGINT NOT NULL,
OM_USER_ID VARCHAR(100) NOT NULL,
OM_VERSION_ID SMALLINT NOT NULL
)
IN ACQ_FAH_DEV
ORGANIZE BY COLUMN
DISTRIBUTE BY HASH(
ACCOUNT_CODE,
OM_VERSION_ID,
BM_BUSINESS_INTERVAL_TYP
);
Documentation for CREATE TABLE statement

Redshift - inserting into identity column

I have created a table as mentioned below.
create table employee (
surrogate_key bigint IDENTITY(1,1),
first_name varchar(200),
last_name varchar(200),
phone_number varchar(200),
creditcard_number bigint
)
insert into employee values
('gaurang', 'shah', '356-776-4456', '4716973408090483')
However, following code is giving error.
Error
[Code: 500310, SQL State: 0A000] [Amazon](500310) Invalid operation: cannot set an identity column to a value;
insert into employee(first_name,last_name,phone_number,creditcard_number)
values('gaurang', 'shah', '356-776-4456', 4716973408090483)
You have to specify column names when identity column present in the table
One more option is define surrogate key with default like this
surrogate_key bigint generated by default as IDENTITY(1,1),
Then run this query
insert into employee1
values(default,'gaurang', 'shah', '356-776-4456', 4716973408090483)

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.

create tables from sql file on schema PostgreSQL

I try to create tables from file on my schema, but i receive error message. Users table created on public schema was successfully, but other table i created on test1 schema was wrong. I don't know why, help me please.
Thank for advance.
CREATE SCHEMA test1
--Create sequence
CREATE SEQUENCE test1.table_id_seq START 1;
--Create function to auto generate ID
CREATE OR REPLACE FUNCTION test1.next_id(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 1;
BEGIN
SELECT nextval('test1.table_id_seq') % 1024 INTO seq_id;
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
result := (now_millis - our_epoch) << 23;
result := result | (shard_id << 10);
result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
--Talbe ----users----
CREATE TABLE users
(
id bigserial NOT NULL PRIMARY KEY,
username varchar(30) NOT NULL UNIQUE,
password varchar NOT NULL,
first_name varchar(10),
last_name varchar(10),
profile_picture varchar,
create_time timestamp (0) without time zone
);
--Table ----photos----
CREATE TABLE test.photos
(
id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
caption text,
low_resolution varchar,
hight_resolution varchar,
thumnail varchar,
user_id bigint NOT NULL REFERENCES users(id),
create_time timestamp (0) without time zone
--Table ----comments----
CREATE TABLE test1.comments
(
id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
create_time timestamp (0) without time zone,
text text,
user_id bigint REFERENCES users(id),
photo_id bigint REFERENCES test1.photos(id)
);
--Table ----likes----
CREATE TABLE test1.likes
(
photo_id bigint REFERENCES test1.photos(id),
user_id bigint REFERENCES users(id),
);
--Table ----follows----
CREATE TABLE test1.follows
(
user_id bigint NOT NULL REFERENCES users(id),
target_id bigint NOT NULL REFERENCES users(id),
);
CREATE TABLE teset1.feeds
(
user_id bigint NOT NULL REFERENCES users(id),
photo_id bigint NOT NULL REFERENCES test1.photos(id),
create_time timestamp (0) without time zone,
);
Well, it would have been helpful for you to have shown us what errors you were getting and what you tried to do to fix them. But here are some obvious problems with the SQL you posted:
Missing semicolon after "CREATE SCHEMA test1 "
Missing closing paren and semicolon at the end of CREATE TABLE test.photos ...
Dump neglects to create the "test" schema where the "photos" table wants to go.
Several foreign key references of test1.photos.id, but "photos" was created in the "test" schema, not "test1".
Extraneous trailing commas after the last column definitions for several tables
Typo: "teset1.feeds", should be "test.feeds"
TL;DR Whoever created this dump file wasn't paying very close attention.
After fixing all the problems above, I managed to load your SQL.