I get error as "duplicate key value violates unique constraint " - postgresql

I'm working on a data warehouse. I have 4 table on public schema they are customer, product, addressee and orders
Then I created this tables on my olap schema
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)
);
After the creating tables I run these queries
INSERT INTO olap.time (year, month, week, day)
SELECT date_part('year', date), date_part('month', date), date_part('week', date), date_part('day', date)
FROM public.orders
GROUP BY public.orders.date
ORDER BY public.orders.date;
INSERT INTO olap.addressees(idaddressee, name, zip, address)
SELECT idaddressee, name, zip, address
FROM public.addressee;
INSERT INTO olap.customers (idcustomer, name, city, zip, address, email, phone, regon)
SELECT idcustomer, name, city, zip, address, email, phone, regon
FROM public.customer;
And then I try to do these set of query
INSERT INTO olap.fact (idtime, idaddressee, idcustomer, idfact, price)
SELECT olap.time.idtime, olap.addressees.idaddressee, olap.customers.idcustomer, COUNT(*), public.orders.price
FROM (((public.orders
INNER JOIN olap.time ON (date_part('year', public.orders.date) = olap.time.year AND date_part('month', public.orders.date) = olap.time.month AND date_part('week', public.orders.date) = olap.time.week) AND date_part('day', public.orders.date) = olap.time.day)
INNER JOIN olap.addressees ON public.orders.idaddressee = olap.addressees.idaddressee)
INNER JOIN olap.customers ON public.orders.idcustomer = olap.customers.idcustomer)
GROUP BY olap.time.idtime, olap.addressees.idaddressee, olap.customers.idcustomer, public.orders.price;
After running last set of queries I got error
ERROR: syntax error at or near "duplicate"
LINE 1: duplicate key value violates unique constraint"
What can the problem be? Thanks in advance

Related

I can't create this PostgreSQL Query: Grouping by a different condition to the "order by" condition

I need to order my query in a different way i need to group my tables. I need to count how many men are in every department, but organize the query by quantity of people (Not only men, but also women) in every department, in descending way.
This is the diagram and the code of the tables:
Relational model of the tables
CREATE SCHEMA Academico;
CREATE TABLE Academico.PAIS(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_PAIS PRIMARY KEY (ID));
CREATE TABLE Academico.DEPARTAMENTO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
CODIGO int NOT NULL,
DESCRIPCION varchar(120) NULL,
IDPAIS int NOT NULL,
CONSTRAINT PK_DEPARTAMENTO PRIMARY KEY (ID));
CREATE TABLE Academico.CIUDAD(
ID int NOT NULL,
NOMBRE varchar(255) NOT NULL,
CODIGO int NOT NULL,
DESCRIPCION varchar(120) NULL,
IDDEPARTAMENTO int NOT NULL,
CONSTRAINT PK_CIUDAD PRIMARY KEY (ID));
ALTER TABLE Academico.DEPARTAMENTO
ADD CONSTRAINT FK_DEPARTAMENTO_PAIS FOREIGN KEY(IDPAIS)
REFERENCES Academico.PAIS (ID)
on delete restrict on update restrict;
ALTER TABLE Academico.CIUDAD
ADD CONSTRAINT FK_CIUDAD_DEPARTAMENTO FOREIGN KEY(IDDEPARTAMENTO)
REFERENCES Academico.DEPARTAMENTO (ID)
on delete restrict on update restrict;
CREATE TABLE Academico.SEXO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_SEXO PRIMARY KEY (ID));
CREATE TABLE Academico.TIPODOCUMENTO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_TIPODOCUMENTO PRIMARY KEY (ID));
CREATE TABLE Academico.PERSONA(
ID int NOT NULL,
NOMBRE varchar(10) NOT NULL,
APELLIDO varchar(30) NOT NULL,
IDSEXO int NOT NULL REFERENCES Academico.SEXO(id),
IDCIUDAD int NOT NULL REFERENCES Academico.CIUDAD(id),
DOCUMENTO varchar(50) NOT NULL,
IDTIPODOCUMENTO int NOT NULL REFERENCES Academico.TIPODOCUMENTO(id),
FECHANACIMIENTO date NULL CHECK (FECHANACIMIENTO > '1900-01-01'),
FEvarcharEGISTRO date NOT NULL DEFAULT Now() ,
email varchar (355) UNIQUE NOT NULL,
PROFESION varchar(12) NULL,
PERFIL varchar(120) NULL,
CONSTRAINT PK_PERSONA PRIMARY KEY
(ID) );
I tried this two querys that give me the expected results but in a separated way:
select
d.nombre as _departamento, s.nombre as sex, count(1) as total_sexo
from
academico.persona p, academico.sexo s,
academico.ciudad c, academico.departamento d
where
p.idsexo = s.id
and p.idciudad = c.id
and c.iddepartamento = d.id
and upper( s.nombre ) = 'MASCULINO'
group by
d.id,
s.id
order by
d.nombre
-- =======================================================
-- I don't know how to "merge" these two into one query
-- =======================================================
select
d.nombre as _departamento, count(1) as total_gente
from
academico.persona p, academico.ciudad c,
academico.departamento d, academico.sexo s
where
p.idciudad = c.id
and c.iddepartamento = d.id
and p.idsexo = s.id
group by
d.id
order by
total_gente desc
;
I need to get those results with only one query
This is the perfect use for the FILTER (WHERE...) construct.
...
count(1) as total_gente,
count(1) filter (where upper( s.nombre ) = 'MASCULINO') as total_masculino
...
And then take the upper( s.nombre ) = 'MASCULINO' out of the main where clause.

Hello I'm pretty new to PostgreSQL so I trying to create a one to one relationship between the tables account and bank_account

I tried using a foreign key, but it still shows them as being separate and not connected is there something I'm missing here, my goal was to originally link the persons name to the account_balance.
CREATE TABLE accounts (
id SERIAL,
first_name VARCHAR(50),
last_name VARCHAR (50),
username VARCHAR (50),
password VARCHAR (500),
account_Type VARCHAR (10),
bday VARCHAR (50),
PRIMARY KEY(id)
);
CREATE TABLE bank_account(
id SERIAL,
account_number INTEGER PRIMARY KEY,
account_balance INTEGER,
CONSTRAINT bank_users
FOREIGN KEY (id)
REFERENCES accounts(id)
ON DELETE CASCADE
);
INSERT INTO accounts (
first_name,
last_name,
username,
password,
account_Type,
bday)
VALUES ('bob', 'john', 'bob#gmail.com', crypt('bob1',gen_salt('bf')),'Manager', '01/01/1985'),
('Tom', 'lin', 'tom#gmail.com', 'tom1', 'Manager', '5/23/1990');
INSERT INTO bank_account(
account_number,
account_balance)
VALUES ('1234', '50000'),('4332', '100000');

TypeORM: many to many: Get all teams from userID

I have two table: User and Team.
They are in a many-to-many relation.
I would like to get all teams of a specific user.
Seams really simple but I can't find the answer anywhere..
Do you know how can I do ?
On the many-to-many relation must be 3rd table. This table contained user_id and team_id connections.
For example:
CREATE TABLE user (
id serial4 NOT NULL,
first_name varchar(200) NOT NULL,
last_name varchar(200) NULL,
CONSTRAINT user_pk PRIMARY KEY (id)
);
CREATE TABLE team (
id serial4 NOT NULL,
team_name varchar NOT NULL,
team_about text NULL,
CONSTRAINT team_pk PRIMARY KEY (id)
);
CREATE TABLE user_team (
id serial4 NOT NULL,
user_id int4 NOT NULL,
team_id int4 NOT NULL,
CONSTRAINT user_team_pk PRIMARY KEY (id)
);
-- filter and select team by user_id
select t.* from examples.team t
inner join examples.user_team usrt on usrt.team_id = t.id
where usrt.user_id = 2;

I want to resolve this error "no matching unique or primary key for this column-list"

CREATE TABLE EMP(EMP_ID NUMBER(5), F_NAME VARCHAR2(20) NOT NULL, L_NAME VARCHAR2(20) NOT NULL,
EMAIL VARCHAR2(20) NOT NULL, PHONE_NO NUMBER(10) NOT NULL, HIRE_DATE DATE NOT NULL,
JOB_ID NUMBER(5) NOT NULL, SALARY NUMBER(5) NOT NULL, COMMISSION_PCT NUMBER(5),
MANAGER_ID NUMBER(5) NOT NULL, DEPT_ID NUMBER(5) NOT NULL, PRIMARY KEY (EMP_ID),
CONSTRAINT FK_ED FOREIGN KEY (DEPT_ID) REFERENCES DEPT (DEPT_ID),
CONSTRAINT FK_EDM FOREIGN KEY (MANAGER_ID) REFERENCES DEPT (MANAGER_ID));
When I add this line it is throwing the error ORA-02270.
I have already created the table DEPT which includes dept_id as primary key and manager_id as not null. But when I use foreign key as dept_id there is no error but for manager_id it is throwing the error.
Please help me resolve this.
dept_id is a primary key in DEPT, so you can have a foreign key for it in EMP.
manager_id is just a not-null column in DEPT. As it is not part of the primary key in DEPT, you cannot have a foreign key for it in EMP.

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 ?