Insert into Redshift with default auto increment isn't working - amazon-web-services

I'm doing something similar to this example from the Redshift documentation but when I insert into the table I get an error Cannot insert a NULL value into column id. The documentation explicitly says that this should auto increment automatically.
create table category_ident
(id bigint identity(0,1) not null,
catgroup varchar,
catname varchar,
catdesc varchar);
insert into category_ident(catgroup,catname,catdesc)
select catgroup,catname,catdesc from category;

Related

Problem with starting the trigger in Oracle Apex Application

I created triggers after creating a database in oracle apex. When testing in SQL code everything was fine, they worked. In the application I have the Professors table and when I change the value of the column "name", in the Courses table the value of the column "professor_name" should change. However when I change the value of the column "name" in the Professors table it throws me the error.
Here is the trigger definition:
CREATE OR REPLACE TRIGGER profesori_upd
AFTER UPDATE OF ime on profesori
FOR EACH ROW
BEGIN
UPDATE kursevi set ime_profesora = :new.ime
where pr_jmbg= :new.jmbg_pr;
END;
Here is the table Proffesors:
CREATE TABLE PROFESORI( jmbg_pr VARCHAR2(13) PRIMARY KEY,
ime VARCHAR2(20) NOT NULL,
prezime VARCHAR2(20) NOT NULL,
id_spreme NUMBER(5) NOT NULL,
CONSTRAINT profesor_sprema_fk FOREIGN KEY (id_spreme) REFERENCES
STRUCNE_SPREME(id)
);
Here is the table Courses:
CREATE TABLE KURSEVI(
opis VARCHAR2(40),
id_jezik NUMBER(10) NOT NULL,
id_nivo NUMBER(10) NOT NULL,
pr_jmbg VARCHAR2(13) NOT NULL,
CONSTRAINT kurs_jezik_fk FOREIGN KEY (id_jezik) REFERENCES JEZICI(id_j),
CONSTRAINT kurs_nivo_fk FOREIGN KEY (id_nivo) REFERENCES NIVOI_KURSEVA(id_n),
CONSTRAINT kurs_profesor_fk FOREIGN KEY (pr_jmbg) REFERENCES PROFESORI(jmbg_pr),
CONSTRAINT kurs_pk PRIMARY KEY(id_jezik,id_nivo,pr_jmbg)
);
After denormalization I added columns ime_profesora and prezime_profesora to the table Courses:
ALTER TABLE KURSEVI
ADD ime_profesora VARCHAR2(20);
ALTER TABLE KURSEVI
ADD prezime_profesora VARCHAR2(20);
UPDATE KURSEVI SET ime_profesora=( SELECT ime FROM PROFESORI WHERE profesori.jmbg_pr=kursevi.pr_jmbg);
UPDATE KURSEVI SET prezime_profesora=( SELECT prezime FROM PROFESORI WHERE profesori.jmbg_pr=kursevi.pr_jmbg);
ALTER TABLE KURSEVI
MODIFY ime_profesora varchar2(20) not null;
ALTER TABLE KURSEVI
MODIFY prezime_profesora varchar2(20) not null;
Error occurs after I try to change the value of the column name in the application, it does not say what the error is.

Computed column in POSTGRESQL-Column Derived from a function

Want to create a COMPUTED COLUMN in POSTGRESQL which has to be a migration of MSSQLCODE .The computed column is as a result of a function "FUN_GetExternalSystemNameFull"
MSSQL CODE FOR TABLE
CREATE TABLE ClientApplication(
ClientApplicationId bigint IDENTITY(1,1) NOT NULL,
ClientId bigint NULL,
ExternalSystemNameFull AS (dbo.FUN_GetExternalSystemNameFull(ClientApplicationId,' | ')),
Age bigint
)
function FUN_GetExternalSystemNameFull in POSTGRESQL :
CREATE OR REPLACE FUNCTION "FUN_GetExternalSystemNameFull"(v_subscriptionid bigint, v_separator character varying)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN((SELECT STRING_AGG("c"."ExternalSystemName",coalesce(v_separator,'')) from(select "ExternalSystemName"
from "ClientApplication"
where "ApplicationId" = v_subscriptionId) "c"));
END; $function$
Iam not able to create a POSTGRESQL TABLE with computed column from the function.Please help.
Iam using Postgresql 11

referencing new Table as new_table not visible to the trigger function Postgresql

I am new in Postgresql so this question may be dumb for you guys!
I am tring to use the referenced tables in a trigger function, but some how the function doesn't seem to have access on the referenced alias (new_table) :
CREATE FUNCTION Function_Name()
RETURNS TRIGGER AS
$$
BEGIN
SELECT tbl.some_field INTO new_table.other_field
from some_table as tbl;
return null;
END;
$$ LANGUAGE PLPGSQL;
I am having this error:
"new_table.other_field" is not a known variable
and here is the trigger code:
CREATE TRIGGER Trigger_Name
AFTER INSERT
ON Table_Name
REFERENCING NEW TABLE AS new_table
FOR EACH ROW
EXECUTE FUNCTION Function_Name();
The function code should be executed first and then the trigger's, so how could the function access the alias that is referenced later in the trigger defenition??
and how to access the referenced table aliases in the function?
Note: In my example I am tring to use the alias, so when I use NEW inplace of new_table the function is created successfully!
The problem was that i am tring to set data in the NEW table using the alias name, however to do that i should use the original name NEW and not the referenced alias new_table..
The referenced alias new_table could be used to only get data from, like in FROM CLAUSE, joins and WHERE CLAUSE where one doesn't change the data in the referenced table.
Update:
here is an example of what i did to test that:
create table test_table2(
id int primary key,
name varchar(255)
)
create table test_table(
id int primary key,
name varchar(255)
)
create or replace function F_test_table()
returns trigger as $$
begin
insert into test_table2(id, name)
select id, name from new_table;
return null;
end;
$$ LANGUAGE PLPGSQL;
drop trigger if exists tr_test_table ON test_table;
create trigger tr_test_table
AFTER INSERT
ON test_table
REFERENCING NEW TABLE AS new_table
for each row ------- row level trigger ---------
EXECUTE FUNCTION F_test_table();
insert into test_table
values(1, '11111')
select * from test_table2
notice that the trigger inserts the data into test_table2

Adding NOT NULL constraint to Cloud Spanner table

If a Cloud Spanner table is created with nullable columns, is it possible to add a NOT NULL constraint on a column without recreating the table?
You can add a NOT NULL constraint to a non-key column. You must first ensure that all rows actually do have values for the column. Spanner will scan the data to verify before fully applying the NOT NULL constraint. More information about how to alter tables is here and here.
However, you can not add such a constraint to a key column. That kind of change would require rewriting all the data in the table, because the nullness of the key affects how the data is encoded. The only option for making that change is to create a new table that's set up the way you want, make code changes to support using both tables temporarily, gradually move the data from the old table to the new table, and eventually change the code to use only the new table and drop the old table. If you further then wanted the original table name, you'd have to do the whole thing again.
Unfortenately there is not way to add not null column
The way to do it:
1 add nullable column
ALTER TABLE table1 ADD COLUMN column1 STRING(255)
UPDATE table1.column1, SET NOT NULL VALUE to the column (if the table is not empty).
UPDATE TABLE table1 SET column1 = "<GENERATED DATA>"
Add constraint
ALTER TABLE table1 ADD COLUMN column1 STRING(255) NOT NULL
Thanks.
Creating a non-nullable column in Spanner on an existing table is typically a three step process:
# add new column to table
ALTER TABLE <table_name> ADD COLUMN <column_name> <value_type>;
# create default values
UPDATE <table_name> SET <column_name>=<default_value> WHERE TRUE;
# add constraint
ALTER TABLE <table_name> ALTER COLUMN <column_name> <value_type> NOT NULL;

sql Column with multiple values (query implementation in a cpp file )

I am using this link.
I have connected my cpp file with Eclipse to my Database with 3 tables (two simple tables
Person and Item
and a third one PersonItem that connects them). In the third table I use one simple primary and then two foreign keys like that:
CREATE TABLE PersonsItems(PersonsItemsId int not null auto_increment primary key,
Person_Id int not null,
Item_id int not null,
constraint fk_Person_id foreign key (Person_Id) references Person(PersonId),
constraint fk_Item_id foreign key (Item_id) references Items(ItemId));
So, then with embedded sql in c I want a Person to have multiple items.
My code:
mysql_query(connection, \
"INSERT INTO PersonsItems(PersonsItemsId, Person_Id, Item_id) VALUES (1,1,5), (1,1,8);");
printf("%ld PersonsItems Row(s) Updated!\n", (long) mysql_affected_rows(connection));
//SELECT newly inserted record.
mysql_query(connection, \
"SELECT Order_id FROM PersonsItems");
//Resource struct with rows of returned data.
resource = mysql_use_result(connection);
// Fetch multiple results
while((result = mysql_fetch_row(resource))) {
printf("%s %s\n",result[0], result[1]);
}
My result is
-1 PersonsItems Row(s) Updated!
5
but with VALUES (1,1,5), (1,1,8);
I would like that to be
-1 PersonsItems Row(s) Updated!
5 8
Can somone tell me why is this not happening?
Kind regards.
I suspect this is because your first insert is failing with the following error:
Duplicate entry '1' for key 'PRIMARY'
Because you are trying to insert 1 twice into the PersonsItemsId which is the primary key so has to be unique (it is also auto_increment so there is no need to specify a value at all);
This is why rows affected is -1, and why in this line:
printf("%s %s\n",result[0], result[1]);
you are only seeing 5 because the first statement failed after the values (1,1,5) had already been inserted, so there is still one row of data in the table.
I think to get the behaviour you are expecting you need to use the ON DUPLICATE KEY UPDATE syntax:
INSERT INTO PersonsItems(PersonsItemsId, Person_Id, order_id)
VALUES (1,1,5), (1,1,8)
ON DUPLICATE KEY UPDATE Person_id = VALUES(person_Id), Order_ID = VALUES(Order_ID);
Example on SQL Fiddle
Or do not specify the value for personsItemsID and let auto_increment do its thing:
INSERT INTO PersonsItems( Person_Id, order_id)
VALUES (1,5), (1,8);
Example on SQL Fiddle
I think you have a typo or mistake in your two queries.
You are inserting "PersonsItemsId, Person_Id, Item_id"
INSERT INTO PersonsItems(PersonsItemsId, Person_Id, Item_id) VALUES (1,1,5), (1,1,8)
and then your select statement selects "Order_id".
SELECT Order_id FROM PersonsItems
In order to achieve 5, 8 as you request, your second query needs to be:
SELECT Item_id FROM PersonsItems
Edit to add:
Your primary key is autoincrement so you don't need to pass it to your insert statement (in fact it will error as you pass 1 twice).
You only need to insert your other columns:
INSERT INTO PersonsItems(Person_Id, Item_id) VALUES (1,5), (1,8)