Sunday 4 October 2020

Create a user table in the PostgreSQL

Here are some common recommendations and guidelines I came across:

  • In PostgreSQL user is a reserved keyword.
  • It is usually not a good idea use reserved words for identifiers (tables, columns).
  • Also name a table with plural (such as "users") is not recommended.
  • Table name refers to one row in the table, and not the total content of the table (we know that the Person table contains all the Persons and one row represents a person).


Therefore, we can name the User table as person OR account. Here is the query:

CREATE TABLE person (
    user_id serial PRIMARY KEY,
    name VARCHAR ( 100 ) NOT NULL,
    email VARCHAR ( 150 ) UNIQUE NOT NULL,
    username VARCHAR ( 50 ) UNIQUE NOT NULL,
    password VARCHAR ( 150 ) NOT NULL,
    created TIMESTAMP NOT NULL,
    last_updated TIMESTAMP
);

Its a good practice to give column name as user_id instead of id. Other columns can be different according to requirements / scenario.


No comments:

Post a Comment