How to insert multiple dummy records with same data for column of type bytea in PostgreSQL? - bulkinsert

I want to insert 10000 records into Postgresql.
Columns - bytea (Primary key), varchar, varchar
Primary key should be different for each row. How can I do it using insert query or pgscript?

Related

I need to write a sp that will insert the characters after the first 4000 characters on the second line

I have a column named 'Products' and its VARCHAR2(4000). In some cases I send more characters than 4000. It looks like '1,2,3,4,5,6...'. How can i insert first 4000 characters into one row and insert remaining to another row but of course it should consider the comma.
I tried to use array system but couldnt figure it out.
Don't use comma-demilited strings.
If you are going to store relational data then use another table.
If you are currently using:
CREATE TABLE main_table (
id NUMBER
GENERATED ALWAYS AS IDENTITY
PRIMARY KEY,
product_ids VARCHAR2(4000)
);
Then change it to:
CREATE TABLE main_table (
id NUMBER
GENERATED ALWAYS AS IDENTITY
PRIMARY KEY
);
CREATE TABLE main_table_products (
id REFERENCES main_table (id),
product_id NUMBER
-- Assuming you have a products table
REFERENCES products (id),
CONSTRAINT main_table_products__pk PRIMARY KEY (id, product_id)
);
Then you know:
The product_ids are all the correct data type,
You have referential constraints insuring that the product_ids are valid products.
You do not have duplicate product_ids for each id as the primary key prevents it.
You can insert any number of product_ids.
You can easily delete or update product_ids.
If you use a delimited VARCHAR2 string then you do not (out-of-the-box) have any of those benefits.

order by clause is different with CHAR(8) AND NVARCHAR(8) columns with Sql Server

I am using SQL Server 2016 and i have the same table structure setup in two databases ie database 1 and database 2. The table structure is the same on both tables for table T, the primary key columns are of char(8) on one and NVARCHAR(8) on the other.
The primary key column is of type CHAR(8) on table T in database 1
The primary key column is of type NVARCHAR(8) on table T in database
2
My primary key has keys as below, they have special characters
'........
'/*-/*-
'/////
'/////...
+65+965+
//
- -
Issue
When I order by the primary key in the two tables i get different order of the keys. How can this be resolved without having to change the column type?
I tried casting the column to the other type but that did not help with ordering.

INSERTING a single field into a table, referenced from another table

I need to insert a field in a that references an id field in another table.
The id field it is to going is next to the field 'test' (column - codedescription, table typecategory) and coming from an id field next to the word 'assessment' (column categorydescription, table typecategory)
INSERT INTO codetype
(typecategoryid)
Where codedescription='test'
SELECT id FROM typecategory WHERE categorydescription='Assessment Types'
There are plenty of examples of inserting entire columns but nobody has written how to insert a single field from another table.
table - codetype
id bigserial primary key
codedescription varchar
typecategoryid bigint foreign key to typecatogory on the ID column
Table - typecategory
ID big serial primary key
categorydescription varchar
If the column already exists and there are are already records in the rest of the columns in the table, then you need an UPDATE statement, not an INSERT.
Looks like this post might help you: Update a column of a table with a column of another table in PostgreSQL
maybe
UPDATE codetype c
SET c.typecategoryid = t.id
FROM typecategory t
WHERE c.codedescription = 'test' and t.categorydescription='Assessment Types'

Link columns in two tables and trigger to automatically update and insert data

I have done a lot of searching and have not found exactly what I am looking for. Sorry if this is a duplicate question, I did not see one that matched my needs.
I have 2 tables:
students:
ID int autoincrement
RegistrationNumber nvarchar
fullname nvarchar
address nvarchar
stream nvarchar
phone nvarchar
and so on
The other table is results:
ID int autoincrement
RegistrationNumber nvarchar
fullname nvarchar
stream nvarchar
GPA nvarchar
I want to connect these tables so that the results table get values from students table, and any insertion/update in students table automatically updates/inserts data accordingly in results table columns to be connected in both tables are RegistrationNumber, fullname and stream.
You need a trigger (or two) that will modify the table Results after insert or update on the Students table. You can start from here: CREATE TRIGGER.
Beside that, not sure if you can, but my suggestion is to have the table Results like this:
Student_ID int
GPA nvarchar
In this way after each insert you have to insert only the Student_ID and when you update a student you won't need to change Results.
Also you can define Student_ID as a Foreign Key to be sure to keep referential integrity.

Two or more table with serial with uniq nums in every table

I've 2 table with serial field (in table "m" it's field "uniq" and in table "u" it's field "uniq").
But, if I insert data in (for example) u. Autoincrement function make +1 for next row in u (from 1 to 2), but if after this action I insert data in another table (for example) m autoincrement field write down not next value in column (1,2,3..), but 3, even if in field was 1.
It means, what autoincrement function incremented every single value in database in series, but not in the table.
sorry for such a poor description of the problem and bad english = )
Try something like this if you want having an id which is unique in all tables:
CREATE SEQUENCE id_seq;
CREATE TABLE table1(id INTEGER PRIMARY KEY DEFAULT NEXTVAL('id_seq'),Test1 varchar);
CREATE TABLE table2(id INTEGER PRIMARY KEY DEFAULT NEXTVAL('id_seq'),Test2 varchar);
try something like this to create unique id for each table
CREATE TABLE table3(id serial,Test3 varchar);
CREATE TABLE table4(id serial,Test4 varchar);
SQL Fiddle
If I understand correctly, you want a unique ID over tables "a" and "b". So create one table with a serial column just for having your key (eg. "id_table") and all other tables have this key as foreign key. Every time you need a new ID, you insert in your "id_table" and point to this new key.