O
O
Oleg Petrov2018-10-27 13:05:02
PostgreSQL
Oleg Petrov, 2018-10-27 13:05:02

What data type to choose for ID in PostgreSQL?

I'm working with PostgreSQL 10.5
1. My database contains ID and share name.
As I understand it is better to choose int for ID, not smallint.
Since Int contains up to +32767, and if the base grows, you will have to change the type
2. There is a type for the stock name character varying(n), varchar(n) Do I need to set its length? Or can the field be left blank?
3. How does setting the field length affect the saving of space in the database? For example varchar(10) and varchar(100) will take up different disk space? For example, now all the names of shares are no more than 10 letters, but what will happen if a name of 30 letters appears in the future? Should I change the length of the string? Is it possible to do this when the database is already partially filled?
4. Why set the field -Not null? Suppose my script cannot parse the name of the ticker correctly and does not pass anything to the line, but writes information to the remaining columns. In this case, the line will not be written or will it give an error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2018-10-27
@Smeilz1

As I understand it is better to choose int for ID, not smallint.

The type must be chosen according to the data.
For example, for a country ID, int would be clearly redundant. And somewhere int is small and bigint is needed
This is a question for you as the author of the database schema.
No way. This is just an additional data limitation.
varchar and text are variable sized types that take up as much space as the data is written to.
It is possible to increase the dimension and it is a fast operation.
An additional data restriction that prohibits setting the special value NULL, that is, "no value". To use or not to use NULL is the decision of the base schema author.
If you try to write NULL to a field marked not null, there will be an error and nothing will be written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question