I am unable to create many to one relation in PostgreSQL using spring-boot application.properties? - postgresql

GenerationTarget encountered exception accepting command :
Error executing DDL "
create table student (
id bigserial not null,
created_time timestamp,
updated_time timestamp,
admission_class int4,
age int8,
date_of_birth date,
gender int4,
name varchar(255),
parent_or_guardian_remark varchar(255),
previous_class int4,
previous_school varchar(255),
relationship varchar(255),
parent_details_id id not null,
teacher_id id not null,
primary key (id)
)"
via JDBC Statement

Related

whenever i try to create table in mysql

CREATE TABLE HRDATA( SRNO smallint NOT NULL auto_increment, BRANCHNAME varchar(255), EMPLOYEENAME varchar(255), EMPID bigint auto_increment NOT NULL, ADDRESS VARCHAR(255), CITY VARCHAR(255), primary key(SRNO);
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

relation tables inheritance modeling

I want to understand inheritance in postgresql, simple whitch columns in whitch tables.
CREATE TABLE users (
id serial PRIMARY KEY,
username VARCHAR UNIQUE NOT NULL,
email VARCHAR NOT NULL,
password_salt VARCHAR,
password_hash VARCHAR,
avatar serial
)
CREATE TABLE groups (
id serial PRIMARY KEY NOT NULL,
name VARCHAR,
email VARCHAR,
avatar serial,
)
CREATE TABLE accounts (
id serial PRIMARY KEY NOT NULL,
name VARCHAR,
avatar serial,
rating json NOT NULL,
);
CREATE TABLE users_to_accounts (
id serial PRIMARY KEY NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME,
)
CREATE TABLE account_subscryptions (
user_id serial NOT NULL,
account_id serial NOT NULL,
) INHERITS users_to_accounts
CREATE TABLE account_memberships (
user_id serial NOT NULL,
account_id serial NOT NULL,
) INHERITS users_to_accounts
CREATE TABLE users_to_groups (
id serial PRIMARY KEY NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME,
)
CREATE TABLE group_subscryptions (
user_id serial NOT NULL,
group_id serial NOT NULL,
) INHERITS users_to_groups
CREATE TABLE group_memberships (
user_id serial NOT NULL,
group_id serial NOT NULL,
) INHERITS users_to_groups
Now.
1. Is it good design to have foreign keys in child tables an all common data in "abstract" table?
2. Is there any traps in future changes of database with inherited relantion tables?
3. I am all wrong and there is a better way for this schema?
4. I want to create good database schema and generate graphql api in postgraphile, looking in google for half day, did not gave me any one good or best solution, so every link will by great.
It may be usefull for others, I think. Thanks

Postgresql Creating a Schema with tables in it

Here's my script.
CREATE SCHEMA testSchema
create table REF_PRODUCT (
id int8 not null,
created_date timestamp,
CODE varchar(255),
DESCRIPTION varchar(255),
LOCATION varchar(255),
MANUFACTURER varchar(255),
NAME varchar(255),
COST numeric(19, 2),
DEALERS_PRICE numeric(19, 2),
SUGGESTED_RETAIL_PRICE numeric(19, 2),
STOCK int4,
PRODUCT_TYPE varchar(255),
picture_id int8,
primary key (id)
)
create table TXN_LINE_ITEM (
id int8 not null,
created_date timestamp,
cancelled boolean,
cost numeric(19, 2),
dp numeric(19, 2),
itemCode varchar(255),
itemName varchar(255),
priceType varchar(255),
qty int4,
refunded boolean,
srp numeric(19, 2),
total numeric(19, 2),
transactionRecord_id int8,
primary key (id)
)
create table TXN_PRODUCT_PICTURE (
id int8 not null,
created_date timestamp,
picture bytea,
primary key (id)
)
create table TXN_TRANSACTION_RECORD (
id int8 not null,
created_date timestamp,
total numeric(19, 2),
transaction_date timestamp,
TRANSACTION_NUMBER varchar(255),
VALID boolean,
primary key (id)
)
alter table REF_PRODUCT
add constraint FK_sjugahpelk16qj5h3w8dli42l
foreign key (picture_id)
references TXN_PRODUCT_PICTURE;
alter table TXN_LINE_ITEM
add constraint FK_o5mslaahpil9d3g9rl2s22rpm
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD;
create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
);
The problem is the fk relationships specified in alter table statement is not being created
Because of this error.
[WARNING ] CREATE SCHEMA testSchema
create table REF_PRODUCT (
id int8 not null,
created_date timestamp,
CODE varchar(255),
DESCRIPTION varchar(255),
LOCATION varchar(255),
MANUFACTURER varchar(255),
NAME varchar(255),
COST numeric(19, 2),
DEALERS_PRICE numeric(19, 2),
SUGGESTED_RETAIL_PRICE numeric(19, 2),
STOCK int4,
PRODUCT_TYPE varchar(255),
picture_id int8,
primary key (id)
)
create table TXN_LINE_ITEM (
id int8 not null,
created_date timestamp,
cancelled boolean,
cost numeric(19, 2),
dp numeric(19, 2),
itemCode varchar(255),
itemName varchar(255),
priceType varchar(255),
qty int4,
refunded boolean,
srp numeric(19, 2),
total numeric(19, 2),
transactionRecord_id int8,
primary key (id)
)
create table TXN_PRODUCT_PICTURE (
id int8 not null,
created_date timestamp,
picture bytea,
primary key (id)
)
create table TXN_TRANSACTION_RECORD (
id int8 not null,
created_date timestamp,
total numeric(19, 2),
transaction_date timestamp,
TRANSACTION_NUMBER varchar(255),
VALID boolean,
primary key (id)
)
alter table REF_PRODUCT
add constraint FK_sjugahpelk16qj5h3w8dli42l
foreign key (picture_id)
references TXN_PRODUCT_PICTURE
ERROR: syntax error at or near "alter"
LINE 53: alter table REF_PRODUCT
^
[WARNING ] alter table TXN_LINE_ITEM
add constraint FK_o5mslaahpil9d3g9rl2s22rpm
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD
ERROR: relation "txn_line_item" does not exist
[WARNING ] create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
)
ERROR: relation "seq_entity_id" already exists
NOTE THAT: There's no existing tables on the said Schema I have made. What have I missed?
I think you have two ways of doing this.
The first one: don't run one create schema statement, but run individual statements for each part (this is what I prefer). You can still do that as a single transaction:
begin transaction;
CREATE SCHEMA testSchema; -- only create the namespace
-- make the new schema the default schema
-- so the the tables do not need to be full qualified
set search_path = testschema;
create table REF_PRODUCT (
...
);
create table TXN_LINE_ITEM (
...
);
create table TXN_PRODUCT_PICTURE (
...
);
create table TXN_TRANSACTION_RECORD (
...
);
alter table REF_PRODUCT
add constraint fk_product_picture
foreign key (picture_id)
references TXN_PRODUCT_PICTURE;
alter table TXN_LINE_ITEM
add constraint FK_line_item_trans_record
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD;
create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
);
commit;
The other option is to move the foreign keys into the table definition, but then you need to re-order your create table statements:
CREATE SCHEMA testSchema
create table TXN_PRODUCT_PICTURE (
...
)
create table TXN_TRANSACTION_RECORD (
...
)
create table REF_PRODUCT (
...,
constraint fk_product_picture
foreign key (picture_id)
references TXN_PRODUCT_PICTURE
)
create table TXN_LINE_ITEM (
...
constraint FK_line_item_trans_record
foreign key (transactionRecord_id)
references TXN_TRANSACTION_RECORD
)
create table SEQ_ENTITY_ID (
sequence_name varchar(255),
sequence_next_hi_value int4
)
;
Unrelated, but:
What is this SEQ_ENTITY_ID table for? Why don't you use native Postgres sequences. They will be much faster, more scalable and more robust than anything you can do in your application to generate unique numbers

Will My Table Be Acceptable For PostgreSQL?

I want to create a table in a PostgreSQL database to store user data (Account info, contact info, geographical info). I have typed out the following SQL and I am wondering is it acceptable for PostgreSQL (in terms of best practices, data types, and lengths)?
CREATE TABLE users (
userID SERIAL,
username VARCHAR(255) NOT NULL,
password VARCHAR(60) NOT NULL,
email VARCHAR(255) NOT NULL,
active VARCHAR(255) NOT NULL,
lenderAcct BOOLEAN NOT NULL DEFAULT FALSE,
resetToken VARCHAR(255) DEFAULT NULL,
resetComplete VARCHAR(3) DEFAULT 'No',
CONSTRAINT users_pk PRIMARY KEY (userID),
firstName VARCHAR(20) NOT NULL,
middleName VARCHAR(20),
lastName VARCHAR(20) NOT NULL,
primaryPhone VARCHAR(50) NOT NULL,
primaryPhoneExt VARCHAR(10),
altPhone VARCHAR(50),
altPhoneExt VARCHAR(10),
fax VARCHAR(50),
legalAddress1 VARCHAR(25) NOT NULL,
legalAddress2 VARCHAR(25),
legalCity VARCHAR(25) NOT NULL,
legalState VARCHAR(25) NOT NULL,
legalZip VARCHAR(16) NOT NULL,
legalCountry VARCHAR(25) NOT NULL,
mailAddress1 VARCHAR(25) NOT NULL,
mailAddress2 VARCHAR(25),
mailCity VARCHAR(25) NOT NULL,
mailState VARCHAR(25) NOT NULL,
mailZip VARCHAR(16) NOT NULL,
mailCountry VARCHAR(25) NOT NULL
);
The PostgreSQL side of the table looks ok. You could consider changing the varchar(x) types to text (check out this answer). The PRIMARY KEY can be introduced as userID SERIAL PRIMARY KEY, ... although this has no effect on the table structure.
Best practises cannot be commented on more without knowing your full table structure but those considerations are mostly not PostgreSQL specific anyway.

ORA-00913: too many values Error (Even though I have checked my table and entries)

Here's is the table :
create table Student(
student_id char(6) primary key,
name varchar(10) not null,
department char(20),
grade char(6),
percentage smallint,
contact_no char(12),
address varchar(50)
);
This is what I'm inserting :
insert into Student
values ('ST-01','Zain','coe','A+',74,'9999777865','New Delhi');
Why ORA-00913: too many values error when I'm not inserting extra values ?