I created a table named "Seats";
CREATE TABLE Seats
(id serial PRIMARY KEY,
line text NOT NULL ,
screeningroom text NOT NULL,
seatnumber text NOT NULL
);
after that i tried an INSERT Script which gave me:
INSERT INTO public.seats(
id, line, screeningroom, seatnumber)
VALUES (?,?, ?, ?);
inside values i put
VALUES (1,'a', 1, 1);
and it gave me a SQL STATE error 23505:
ERROR: A violation of the constraint imposed by a unique index or a unique constraint occurred.
DETAIL: key (id) =(1) already exists.
can anybody tell me how to resolve this error?
Do not input a value into a serial column. It is defined as serial so the database can increment it for you:
INSERT INTO public.seats (line, screeningroom, seatnumber)
VALUES (?, ?, ?);
I'm running PostgreSQL 9.3.14 on Ubuntu 16.04.1 LTS on pgAdmin III. I wrote some code in Sublime Text, saved it as a plain text, and am running it via the terminal. Before I run the code, I make sure to go into pgAdmin and delete/drop the database.
Here is my code:
CREATE DATABASE movie_info;
CREATE TABLE movie_attributes
(
Title varchar(255),
Duration integer,
IMDB_Rating integer,
Release_Date integer
);
ALTER TABLE movie_attributes ADD PRIMARY KEY (id);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('Zoolander', 105, 6.6, 2001);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('Training Day', 122, 7.7, 2001);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('Band of Brothers', 705, 9.5, 2001);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('The Shawshank Redemption', 144, 9.3, 1994);
CREATE TABLE cinema_info
(
id integer CONSTRAINT cinema_info_pk PRIMARY KEY,
Title varchar(255),
Theatre_Number integer UNIQUE,
Showtime varchar(255)
);
ALTER TABLE cinema_info ADD PRIMARY KEY (id);
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('Zoolander', 2, '5:30');
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('Training Day', 3, '6:30');
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('Band of Brothers', 1, '7:30');
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('The Shawshank Redemption', 4, '5:30');
Here are the errors I'm receiving in Terminal:
psql -f ~/Documents/Sublime\ Text\ Docs/2016_09_09\ -\ Test\ Movie\ Database\
>
CREATE DATABASE
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:9: ERROR: relation "movie_attributes" already exists
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:11: ERROR: multiple primary keys for table "movie_attributes" are not allowed
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:14: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Zoolander, 105, 7, 2001).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:17: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Training Day, 122, 8, 2001).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:20: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Band of Brothers, 705, 10, 2001).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:23: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, The Shawshank Redemption, 144, 9, 1994).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:33: ERROR: relation "cinema_info" already exists
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:35: ERROR: multiple primary keys for table "cinema_info" are not allowed
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:39: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Zoolander, 2, 5:30).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:42: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Training Day, 3, 6:30).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:45: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Band of Brothers, 1, 7:30).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database:48: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, The Shawshank Redemption, 4, 5:30).
Since I'm dropping the database every time, I'm not sure how "movie_attributes" already exists, and I'm not sure how I have null primary key values since I'm adding the primary key with the ALTER TABLE line. I'm also stumped as to how there are multiple primary keys when I'm only adding one per table.
I've tried a number of different approaches, but myself and everyone I've talked to cannot get it to work. What am I doing wrong? Thank you!
EDIT: Following Doon's advice, I'm now receiving this error:
psql -f ~/Documents/Sublime\ Text\ Docs/2016_09_09\ -\ Test\ Movie\ Database\ Fixed
CREATE DATABASE
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:10: ERROR: relation "movie_attributes" already exists
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:12: ERROR: multiple primary keys for table "movie_attributes" are not allowed
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:15: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Zoolander, 105, 7, 2001).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:18: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Training Day, 122, 8, 2001).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:21: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Band of Brothers, 705, 10, 2001).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:24: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, The Shawshank Redemption, 144, 9, 1994).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:35: ERROR: multiple primary keys for table "cinema_info" are not allowed
LINE 4: id integer CONSTRAINT cinema_info_pk PRIMARY KEY,
^
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:37: ERROR: multiple primary keys for table "movie_attributes" are not allowed
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:41: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Zoolander, 2, 5:30).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:44: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Training Day, 3, 6:30).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:47: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, Band of Brothers, 1, 7:30).
psql:/home/ryan/Documents/Sublime Text Docs/2016_09_09 - Test Movie Database Fixed:50: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, The Shawshank Redemption, 4, 5:30).
Here's the revised code:
CREATE DATABASE movie_info;
CREATE TABLE movie_attributes
(
id serial PRIMARY KEY,
Title varchar(255),
Duration integer,
IMDB_Rating integer,
Release_Date integer
);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('Zoolander', 105, 6.6, 2001);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('Training Day', 122, 7.7, 2001);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('Band of Brothers', 705, 9.5, 2001);
INSERT INTO movie_attributes (Title, Duration, IMDB_Rating, Release_Date)
VALUES ('The Shawshank Redemption', 144, 9.3, 1994);
CREATE TABLE cinema_info
(
id serial PRIMARY KEY,
id integer CONSTRAINT cinema_info_pk PRIMARY KEY,
Title varchar(255),
Theatre_Number integer UNIQUE,
Showtime varchar(255)
);
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('Zoolander', 2, '5:30');
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('Training Day', 3, '6:30');
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('Band of Brothers', 1, '7:30');
INSERT INTO cinema_info (Title, Theatre_Number, Showtime)
VALUES ('The Shawshank Redemption', 4, '5:30');
Any ideas?
when creating your tables, you are not creating anything that auto increments your primary keys (id). You should use either serial or bigserial which create sequences behind the scenes for you..
also when you create the database, it doesn't automatically connect you to it, so after creating you need to connect to the new DB using \c. What is probably happening is that you are creating the tables in your template or default database, hence the duplicate keys, relation exists errors..
CREATE DATABASE movie_info;
\c movie_info
CREATE TABLE movie_attributes
(
id serial PRIMARY KEY,
Title varchar(255),
Duration integer,
IMDB_Rating integer,
Release_Date integer
);
CREATE TABLE cinema_info
(
id serial PRIMARY KEY,
Title varchar(255),
Theatre_Number integer UNIQUE,
Showtime varchar(255)
);
should fix the current issue.
as for the multiple primary keys, you specify id as primary key in the DDL, then are trying to re-add. Hence the multiple keys (even though they are the same key..)
another option would be to specify the ids, on your insert statements.
Simulating UPSERT was already discusssed before. In my case though, I have PRIMARY KEY and additional UNIQUE constraint, and I want upsert semantic with respect to primary key - replacing existing row if it exists, while checking the unique constraint.
Here's an attempt using insert-or-replace:
drop table if exists test;
create table test (id INTEGER, name TEXT, s INTEGER,
PRIMARY KEY (id, s),
UNIQUE (name, s));
insert or replace into test values (1, "a", 0);
insert or replace into test values (1, "a", 0);
insert or replace into test values (2, "b", 0);
insert or replace into test values (2, "a", 0);
The last statement is replaces both rows. This is documented behavior of 'insert or replace', but not what I want.
Here is an attempt with "on conflict replace":
drop table if exists test;
create table test (id INTEGER, name TEXT, s INTEGER,
PRIMARY KEY (id, s) on conflict replace,
UNIQUE (name, s));
insert into test values (1, "a", 0);
insert into test values (1, "a", 0);
I get "UNIQUE constraint failed" right away. The problem disappears if don't share column between both primary key and unique constraint:
drop table if exists test;
create table test (id INTEGER, name TEXT,
PRIMARY KEY (id) on conflict replace,
UNIQUE (name));
insert into test values (1, "a");
insert into test values (1, "a");
insert into test values (2, "b");
insert into test values (2, "a");
Here, I get constraint violation on the very last statement, which is precisely right. Sadly, I do need to share a column between constraints.
Is this something I don't understand about SQL, or SQLite issue, and how do I get the desired effect, except for first trying insert and then doing update on failure?
Can you try to apply the ON CONFLICT REPLACE clause to the UNIQUE constraint also?
create table test (id INTEGER, name TEXT,
PRIMARY KEY (id) on conflict replace,
UNIQUE (name) on conflict replace);
SQLite is an embedded database without client/server communication overhead, so it is not necessary to try to do this in a single statement.
To simulate UPSERT, just execute the UPDATE/INSERT statements separately:
c.execute("UPDATE test SET s = ? WHERE id = ? AND name = ?", [0, 1, "a"])
if c.rowcount == 0:
c.execute("INSERT INTO test(s, id, name) VALUES (?, ?, ?)", [0, 1, "a"])
Since SQLite 3.24.0, you can just use UPSERT.
Good day,
I have a table, which some columns, and BELTID set to primary key, and IS_AUTOINCREMENT set to YES.
I wish to insert a row of data inside this data without key in the BELTID, I am expect the BELDID will auto generate.
The query is as follow:
INSERT INTO mySchema.TABLE1(TYPE, ORIGINALBATCHID, MANUAL)
VALUES ('TEST', 124, 1);
I get this error:
SQLSTATE: 23505. A violation of the constraint imposed by a unique index or a unique constraint occurred.
Then I change my query to:
INSERT INTO mySchema.TABLE1(BELTID, TYPE, ORIGINALBATCHID, MANUAL)
VALUES (123, 'TEST', 124, 1);
And I am getting another error:
SQLSTATE: 428C9 A ROWID column cannot be specified as the target column of an INSERT or UPDATE.
Kindly advise on what mistake I make.
i'm facing a problem with a primary key in PostgreSQL, my plan was to make an addition of two values and set this to one primary key, how could this be realized (first try below):
/* Tabelle fuer die Test*/
create table Test(
var_a integer,
var_b integer,
var_key integer,
var_key = var_a + var_b,
primarykey(var_key),
);
if i call this with a foreign key then it should be one value:
foreign key (var_key_f) references Test(var_key),
EDIT: I know th option of two multiple primary keys entries but i want to have only one primary key, so that i not have to reference over two vars again. I need to have both variables generated to one primary key.
It appears this can be accomplished without triggers: (pg-9.3):
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE test
( var_key INTEGER NOT NULL PRIMARY KEY
, var_a INTEGER NOT NULL
, var_b INTEGER NOT NULL
, var_key_f INTEGER REFERENCES test(var_key)
, CONSTRAINT the_sum CHECK (var_a+var_b = var_key)
);
INSERT INTO test(var_key, var_a, var_b) VALUES(42, 21, 21); -- Ok
INSERT INTO test(var_key, var_a, var_b) VALUES(666, 660, 6); -- Ok
INSERT INTO test(var_key, var_a, var_b) VALUES(34, 21, 11); -- bad sum
INSERT INTO test(var_key, var_a, var_b) VALUES(666, 600, 66); -- duplicate sum
INSERT INTO test(var_key, var_a, var_b, var_key_f) VALUES(14, 6, 8, 42); -- Ok
INSERT INTO test(var_key, var_a, var_b, var_key_f) VALUES(13, 5, 8, 43); -- Bad FK
Result:
NOTICE: drop cascades to table tmp.test
DROP SCHEMA
CREATE SCHEMA
SET
CREATE TABLE
INSERT 0 1
INSERT 0 1
ERROR: new row for relation "test" violates check constraint "the_sum"
DETAIL: Failing row contains (34, 21, 11, null).
ERROR: duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (var_key)=(666) already exists.
INSERT 0 1
ERROR: insert or update on table "test" violates foreign key constraint "test_var_key_f_fkey"
DETAIL: Key (var_key_f)=(43) is not present in table "test".