I think I am missing something - postgresql

I need to create an insert statement and it is not working? would love some help. I get no relation. Here is my code:
INSERT INTO movies table (movie_id, release_year, movie_title,movie_descrption, number_in_stock_,rental_or_sale_or_both, rental_daily_rate)
INSERT INTO movies VALUES('10101', '1985','top gun', 'airplains', '50', 'both', '$5'
And here is my table
SQL database table for Movies tables
CREATE TABLE movies_id
(
movie_id VARCHAR NOT NULL PRIMARY KEY,
release_year DATE NOT NULL,
movie_title VARCHAR (50) NOT NULL,
movie_description VARCHAR (50) NOT NULL,
number_in_stock VARCHAR (50) NOT NULL,
rental_or_sale_or_both VARCHAR (50) NOT NULL,
rental_daily_rate VARCHAR (50) NOT NULL
);

'1985' is not a correct DATE value, some field names are mistyped. Try with following statements:
CREATE TABLE movies_id
(
movie_id VARCHAR NOT NULL PRIMARY KEY,
release_year DATE NOT NULL,
movie_title VARCHAR (50) NOT NULL,
movie_description VARCHAR (50) NOT NULL,
number_in_stock VARCHAR (50) NOT NULL,
rental_or_sale_or_both VARCHAR (50) NOT NULL,
rental_daily_rate VARCHAR (50) NOT NULL
);
INSERT INTO movies_id
(movie_id, release_year, movie_title, movie_description, number_in_stock,
rental_or_sale_or_both, rental_daily_rate)
VALUES ('10101', '1985-01-01','top gun', 'airplains', '50', 'both', '$5')
Working Demo

The correct way is:
INSERT INTO movies (movie_id, release_year, movie_title,movie_descrption, number_in_stock_,rental_or_sale_or_both, rental_daily_rate) VALUES('10101', '1985','top gun', 'airplains', '50', 'both', '$5')
or short version insert (considering the creation' order of attributes in table)
INSERT INTO movies VALUES('10101', '1985','top gun', 'airplains', '50', 'both', '$5')

Try to this first create this table and run the insert syntax
Create Table movies(movie_id VARCHAR Not NULL PRIMARY key,
release_year date not null,
movie_title VARCHAR (50) NOT NULL,
movie_description VARCHAR (50) NOT NULL,
number_in_stock VARCHAR (50) NOT NULL,
rental_or_sale_or_both VARCHAR (50) NOT NULL,
rental_daily_rate VARCHAR (50) NOT NULL
);
insert into movies values('10101', '1985','top gun', 'airplains', '50', 'both', '$5');

Related

PostgreSQL gives syntax error at "foreign" or near

These are tables before referances
CREATE TABLE olap.time (
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees (
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers (
idcustomer varchar(10) SERIAL PRIMARY KEY autoincrement,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
After creating this tables I want to create this table
CREATE TABLE olap.fact(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES time(idtime)
));
But I get error as
"ERROR: syntax error at or near "REFERENCES"
LINE 9: FOREIGN KEY (idcustomer REFERENCES olap.customers(idcustom..."
Thanks in advance
The idcustomer from olap.fact and idcustomer from olap.customers has different datatype SERIAL and Varchar(10),
I have corrected the datatypes and validated the code below
CREATE TABLE olap.time (
idtime SERIAL NOT NULL PRIMARY KEY,
year integer,
month integer,
week integer,
day integer
);
CREATE TABLE olap.addressees (
idaddressee integer PRIMARY KEY NOT NULL,
name varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(60) NOT NULL
);
CREATE TABLE olap.customers (
idcustomer varchar(10) PRIMARY KEY ,
name varchar(40) NOT NULL,
city varchar(40) NOT NULL,
zip char(6) NOT NULL,
address varchar(40) NOT NULL,
email varchar(40),
phone varchar(16) NOT NULL,
regon char(9)
);
CREATE TABLE olap.fact(
idtime integer NOT NULL,
idaddressee integer NOT NULL,
idcustomer varchar(10) NOT NULL,
idfact integer NOT NULL,
price numeric(7,2),
PRIMARY KEY (idtime, idaddressee, idcustomer),
FOREIGN KEY (idaddressee) REFERENCES olap.addressees(idaddressee),
FOREIGN KEY (idcustomer) REFERENCES olap.customers(idcustomer),
FOREIGN KEY (idtime) REFERENCES olap.time(idtime)
);

Row not getting updated after PostgresSql query

This is my simple query which deletes a row from table A and inserts it into table B with some additional columns. I still see the row in question in table A and not table B. But transaction shows successful with no errors.
with failed as (delete from transactions where transaction_id='12345' returning *)
insert into failed_notifications ("transaction_id") select "transaction_id" from failed;
Response I get from the postgres
Updated Rows 0
Query with failed as (delete from transactions where transaction_id='12345' returning *)
insert into failed_notifications ("transaction_id") select "transaction_id" from failed
Schema
CREATE TABLE public.failed_notifications (
transaction_id text NOT NULL,
"identity" text NULL,
identity_type text NULL,
country_id text NULL,
updated_at int8 NULL,
created_at int8 NULL,
reason text NULL,
CONSTRAINT failed_notifications_pkey PRIMARY KEY (transaction_id)
);
CREATE TABLE public.transactions (
"identity" text NOT NULL,
identity_type text NULL,
service_id text NULL,
partner_id text NULL,
country_id text NULL,
updated_at int8 NULL,
created_at int8 NULL,
transaction_id text NOT NULL,
start_date int8 NULL,
end_date int8 NULL,
is_sms_triggered bool NULL,
CONSTRAINT transactions_pkey PRIMARY KEY (transaction_id)
);

Postgres Primary Key as varchar is not working

First of all can I give Varchar for primary key? Here is my create table;
CREATE TABLE appinfo (
app_id VARCHAR (30) PRIMARY KEY,
app_secret VARCHAR ( 500 ) NOT NULL,
app_name VARCHAR ( 255 ) NOT NULL,
app_type VARCHAR ( 255 ) NOT NULL,
prog_lang VARCHAR ( 255 ) NULL,
description VARCHAR ( 255 ) NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW()
);
And I am trying to insert below value
INSERT INTO appinfo (app_id, app_secret, app_name, app_type)
VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI', 'API');
but this shows below error;
ERROR: invalid input syntax for type integer: "ABC1"
LINE 2: VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI...
I am very new to Postgres and I am not sure where is this Integer coming from. Or is that because Primary key is always Integer in Postgres?

Constraints on dates

I need Help with implementing a database in SQL Developer. I tried to implement date constraints but it doesn't work the way I want to.
Here is Office Table:
CREATE TABLE Office (
Office_ID varchar2(7) PRIMARY KEY NOT NULL,
Office_AddressLine varchar2(50) NOT NULL,
Office_PostCode varchar2(8) NOT NULL,
Office_Telephone varchar2(11),
Office_Email varchar2(30), PRIMARY KEY(Office_ID),
CONSTRAINT Office_Email_CK CHECK ( Office_Email like '%_#__%._%')
);
Here is Employee Table:
CREATE TABLE Employee (
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL,
Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_Telephone varchar2(11),
Emp_Email varchar2(30),
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID),
CONSTRAINT Emp_DateOfBirth_CK CHECK (Emp_DateOfBirth BETWEEN Date '1900-01-01' AND Date '2018-01-01'),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%')
);
It works this way, but what I want is this:
CREATE TABLE Employee (
Employee_ID varchar2(10) NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL, Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_Telephone varchar2(11),
Emp_Email varchar2(30),
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
PRIMARY KEY (Employee_ID),
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID)
CONSTRAINT fk_staff_patient FOREIGN KEY (Patient_ID) REFERENCES Patient (Patient_ID),
CONSTRAINT Emp_DateOfBirth_CK CHECK (Emp_DateOfBirth BETWEEN Date '1900-01-01' AND Date sysdate),
CONSTRAINT Hire_Date_CK CHECK (Hire_Date <= curdate()),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%')
);
I don't know why sysdate function or getdate doesn't work and I also don't know how to implement the Constraint about the Hire_Date (I cannot do it with <= relationship). As it is now I have to give up in implementing them even if I will lose points.
Any help will be much appreciated.
You'll need a trigger to enforce such a constraint. Here's an example:
SQL> create table test
2 (
3 employee_id varchar2 (10),
4 hire_date date
5 );
Table created.
SQL> create or replace trigger trg_hd_biu
2 before insert or update of hire_date
3 on test
4 for each row
5 begin
6 if :new.hire_date not between date '1900-01-01' and sysdate
7 then
8 raise_application_error (-20001, 'Hire date out of range');
9 end if;
10 end;
11 /
Trigger created.
SQL> insert into test values ('1', date '2018-01-15');
1 row created.
SQL> insert into test values ('2', date '2018-05-25');
insert into test values ('2', date '2018-05-25')
*
ERROR at line 1:
ORA-20001: Hire date out of range
ORA-06512: at "SCOTT.TRG_HD_BIU", line 4
ORA-04088: error during execution of trigger 'SCOTT.TRG_HD_BIU'
SQL>
I have done it already using another approach (because our teacher doesn't want us to create triggers. We have to make use of constraints only) and it works. Thanks for help any way.
CREATE TABLE Employee (
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL,
Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_CurrentDate date default sysdate,
Emp_Telephone varchar2(11) NOT NULL,
Emp_Email varchar2(30) NOT NULL,
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Emp_Supervised_By varchar2(10) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID),
CONSTRAINT Hire_Date_CK check (Hire_Date < Emp_CurrentDate AND (Hire_Date - Emp_DateOfBirth)/365 > 18),
CONSTRAINT Emp_DateOfBirth_CK check (Emp_DateOfBirth > TO_DATE('1900-01-01', 'YYYY-MM-DD')),
CONSTRAINT Emp_Salary_CK check (Emp_Salary > 0 AND Emp_Salary < 150000),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%'),
CONSTRAINT Emp_Telephone_CK CHECK (regexp_like(Emp_Telephone, '^[0123456789]{11}$') AND Emp_Telephone like '0%'),
CONSTRAINT Emp_FirstName_CK CHECK (regexp_like(Emp_FirstName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_LastName_CK CHECK (regexp_like(Emp_LastName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_PostCode_CK CHECK (regexp_like ( Emp_PostCode , '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})')),
CONSTRAINT Emp_Speciality_CK CHECK (regexp_like(Emp_Speciality, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_Qualification_CK CHECK (regexp_like(Emp_Qualification, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_AwardingBody_CK CHECK (regexp_like(Emp_AwardingBody, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,30}$'))
);
What I want to do now is to create a constraint that enables me to set the value of the attribute Emp_Supervised_By automatically as the same value of the Employee_ID attribute when the Emp_Speciality is 'Manager'. Thanks in advance for help!

postgresql cannot insert data to newly added column

In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.