T
T
TemaKam2021-03-28 18:53:27
PostgreSQL
TemaKam, 2021-03-28 18:53:27

Why does postgre sql require to enter key field values ​​during INSERT?

valid request

INSERT INTO abonents
VALUES ('Petrov', 'Pobeda 9, 1', '8909999231')

does not want to be executed on the table
oxjKUDt.jpg
where the key field was created as: bilet_number integer GENERATED ALWAYS AS IDENTITY

so why does it require this field when inserting?
UGrGeyj.jpg
if I specify specific columns in INSERT, then it's normal,
but so that without specifying it would be possible to add like this - is it possible to do it?
in MS SQL it was possible

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-03-28
@TemaKam

It does not require a key field, but just a field. With INSERT without specifying the names of the fields, it is necessary to transfer values ​​for all columns to VALUES.

and so that without instructions it would be possible to add like this - is it possible to do it?

can
INSERT INTO abonents
VALUES (DEFAULT, 'Petrov', 'Pobeda 9, 1', '8909999231')

S
Slava Rozhnev, 2021-03-28
@rozhnev

It is possible without DEFAULT, you should only list the names of the columns that you insert:

CREATE TABLE abonents (
  id serial,
  	name varchar(64),
  	address varchar(64),
  	phone varchar(64)
);

INSERT INTO abonents (name, address, phone) VALUES ('Petrov', 'Pobeda 9, 1', '8909999231');

SELECT * FROM abonents;

PostgreSQL fiddle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question