Postgresql add existing column to composite primary key - postgresql

I have a table in postgresql with a composite primary key. The primary key consists of two columns named:
DATETIME, UID
I have a another (non-null) column named ACTION already existing in this table. How do I add ACTION to the composite primary key? Ie: I'd like the resulting primary key of the table to be the triplet:
DATETIME, UID, ACTION

First drop the primary key constraint. You can get the name of the constraint by typing
\d my_table
and look under indexes for something like:
"my_table_pkey" PRIMARY KEY, btree (datetime, uid)
Drop it by doing:
alter table my_table drop constraint my_table_pkey;
Then create the new composite primary key by doing:
alter table my_table add constraint my_table_pkey primary key (datetime, uid, action);

Related

Change PRIMARY KEY CONSTRAINT in PostgreSql using Play Framework Evolutions

I need to change a PRIMARY KEY CONSTRAINT in PostgreSql from a single to a composite key, using Scala's Play Framework Evolutions. This Change Primary Key post gives me a good head start, so I will adapt their example; suppose my db evolutions 1.sql file is:
-- !Ups
CREATE TABLE city (
city_id BIGSERIAL PRIMARY KEY,
group_id int,
"version" int
);
CREATE INDEX city__version ON city("version");
CREATE INDEX city__group_id ON city(group_id);
-- !Downs
DROP TABLE city;
I want to change the PK to include 2 more columns, like:
CREATE TABLE city (
city_id BIGSERIAL,
group_id int,
"version" int,
CONSTRAINT city_pk PRIMARY KEY (city_id, group_id, "version");
);
-- ...
I think the 2.sql file should look like:
-- !Ups
ALTER TABLE city
DROP COLUMN city_id,
ADD COLUMN city_id BIGSERIAL,
ADD CONSTRAINT city_pk PRIMARY KEY (city_id, group_id, "version");
-- !Downs
ALTER TABLE city
DROP CONSTRAINT city_pk,
DROP COLUMN city_id,
ADD COLUMN city_id BIGSERIAL PRIMARY KEY;
But I wonder if I need to truncate the table first*, since I'm dropping an essential column... If so, should I include a TRUNCATE command to the evolution file before the ALTER TABLE command? Otherwise, is there another way to change the PRIMARY KEY w/o DROP + ADD COLUMN?
* it's OK to do it on my use case.
Just tested my suggested changes and it worked. The 2.sql file is the same as suggested; there was no need to truncate the table first, and the original data was preserved, including the city_id:
-- !Ups
ALTER TABLE city
DROP COLUMN city_id,
ADD COLUMN city_id BIGSERIAL,
ADD CONSTRAINT city_pk PRIMARY KEY (city_id, group_id, "version");
-- !Downs
ALTER TABLE city
DROP CONSTRAINT city_pk,
DROP COLUMN city_id,
ADD COLUMN city_id BIGSERIAL PRIMARY KEY;

How to add foreign key in PostgreSQL

I created this first table named 'bookstore' where Primary Key is book_name:
create table bookstore (book_name varchar primary key, author varchar, price decimal);
I am trying to create a second table named 'name' where name is primary key. I want to make this primary key- author.name as a foreign key of bookstore.author.
create table author (name varchar primary key, place varchar,
constraint fk_author_bookstore foreign key(name) references bookstore(author));
But the error is: ERROR: there is no unique constraint matching given keys for referenced table "bookstore"
SQL state: 42830
I am new to SQL, so, hoping to get some help. If you can, please write the correct code.
Thanks
Name column in author table is primary key and it's referenced as foreign key in bookstore table.
-- PostgreSQL (v11)
create table author (name varchar primary key, place varchar);
create table bookstore (book_name varchar primary key, author varchar, price decimal
, CONSTRAINT fk_author_bookstore
FOREIGN KEY(author)
REFERENCES author(name));
Please check from url https://dbfiddle.uk/?rdbms=postgres_11&fiddle=8394f796433ed8bc170c2889286b3fc2
Add foreign key after table creation
-- PostgreSQL(v11)
ALTER TABLE bookstore
ADD CONSTRAINT fk_author_bookstore FOREIGN KEY (author)
REFERENCES author (name);
Please check from url https://dbfiddle.uk/?rdbms=postgres_11&fiddle=d93cf071bfd0e3940dfd256861be813c

Postgresql ALTER TABLE ADD KEY equivalent

What is the equivalent of this in postgresql ?
--
-- Index for the table `Artist`
--
ALTER TABLE `Artist`
ADD PRIMARY KEY (`idArtist`),
ADD UNIQUE KEY `name` (`name`,`firstName`);
ADD KEY `idFilm` (`idFilm`);
Adding a primary key is the same.
alter table artist
add primary key (idartist);
For the unique constraint you have two choices:
alter table artist
add constraint name unique (name, firstname);
or a unique index:
create unique index name on artist (name, firstname);
I think the key thing, simply adds a regular index:
create index idfilm on artist (idfilm);

postgres doesn't drop not null

I drop not null on primary key column, and it executes, after I check schema of table and there is not null
table:
-- auto-generated definition
create table warehouses
(
id_warehouse serial not null
constraint warehouse_pkey
primary key,
responsible_person varchar(30) not null
);
script to drop not null:
alter table warehouses
drop constraint warehouse_pkey;
alter table warehouses
alter id_warehouse drop not null;
alter table warehouses
add constraint warehouse_pkey
primary key (id_warehouse);
Per information here:
https://www.postgresql.org/docs/current/sql-createtable.html
PRIMARY KEY (column constraint)
The PRIMARY KEY constraint specifies that a column or columns of a table can contain only unique (non-duplicate), nonnull values. Only one primary key can be specified for a table, whether as a column constraint or a table constraint.
A column defined as PRIMARY KEY will also have the NOT NULL constraint set.

Adding a NOT VALID foreign key in Postgres

Example schema: http://sqlfiddle.com/#!1/3d410
I've already got a table and I want to add a new, not valid foreign key to the table. What's the correct syntax for adding a NOT VALID foreign key?
CREATE TABLE junks (
id serial PRIMARY KEY,
name text
);
CREATE TABLE trunks (
id serial PRIMARY KEY,
name text
-- no fk
);
-- and the below does not work!
--ALTER TABLE trunks ADD junk serial REFERENCES junks(id) NOT VALID;
You first add the column:
alter table trunks add column junk serial;
Then add the constraint to the table:
alter table trunks add constraint the_constraint_name
FOREIGN KEY (junk)
REFERENCES junks (id)
not valid;
This works:
ALTER TABLE trunks ADD CONSTRAINT FK_junk_id
FOREIGN KEY (id)
REFERENCES junks(id)
NOT VALID
;
See, e.g.: http://www.postgresql.org/docs/devel/static/ddl-alter.html#AEN2758