Atikh's DBA blog
  • Home
  • Oracle
  • MySQL
  • MongoDB
  • PostgreSQL
  • Snowflake
  • About Me
  • Contact Us

Table Creation in PostgreSQL

 Atikh Shaikh     PostgreSQL     No comments   

We have created database tech_db in previous article, now we will create tables in PostgreSQL database. Table creation in postgres is very much similar to creating table in any other RDBMS database, we can make use of data types described here

In oracle we use desc to see definition of any table in PostgreSQL, we use \d or \d+ to see list of columns and its description

We will create table name employee with few columns in it

CREATE TABLE employee (
emp_id int NOT NULL,
emp_name varchar(255) NOT NULL,
age int NOT NULL,
PRIMARY KEY(emp_id)
);

you can see, we have defined all columns as NOT NULL and primary key as emp_id, same can be defined as below as well


CREATE TABLE employee (
emp_id int NOT NULL PRIMARY KEY,
emp_name varchar(255) NOT NULL,
age int NOT NULL
);

List of tables can be fetched using \dt 
tech_db=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | employee | table | postgres
(1 row)
tech_db=#

Create table using select as statement


create table emp_2019 as select * from employee where age>26;

List tables
tech_db=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | emp_2019 | table | postgres
 public | employee | table | postgres
(2 rows)

tech_db=#

Create unlogged table
Unlogged tables are created with skipping write-ahead log, these are faster tables but can be recovered in case of crash and can not be replicated. syntax is similar to create table just with additional key word unlogged . for example


CREATE UNLOGGED TABLE student (
student_id int NOT NULL,
student_name varchar(255),
DOB date,
DOJ date
);

Describing table
tech_db=# \d employee;
            Table "public.employee"
  Column  |          Type          | Modifiers
----------+------------------------+-----------
 emp_id   | integer                | not null
 emp_name | character varying(255) | not null
 age      | integer                | not null
Indexes:
    "employee_pkey" PRIMARY KEY, btree (emp_id)

tech_db=#
tech_db=# \d+ employee;
                                Table "public.employee"
  Column  |          Type          | Modifiers | Storage  | Stats target | Description
----------+------------------------+-----------+----------+--------------+-------------
 emp_id   | integer                | not null  | plain    |              |
 emp_name | character varying(255) | not null  | extended |              |
 age      | integer                | not null  | plain    |              |
Indexes:
    "employee_pkey" PRIMARY KEY, btree (emp_id)

tech_db=#


Recommendation while creating tables
  • use lower case name for tables
  • use lower case name for columns
PostgreSQL column constraints
NOT NULL - value can not be null
UNIQUE - value must be unique
PRIMARY KEY - not null + unique
CHECK - enables to have check value before insert or update
REFERENCE - constraints the value of column in another table

PostgreSQL table constraints
Similar to column constraints just they are applied to whole table.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Author

Atikh Shaikh
View my complete profile

Categories

  • MongoDB (18)
  • Oracle 12c (30)
  • Oracle12cR2 New Feature (3)
  • PostgreSQL (20)
  • RMAN (10)
  • Snowflake (8)
  • mysql (23)
  • oracle (74)

Blog Archive

  • ►  2018 (38)
    • ►  November (25)
    • ►  December (13)
  • ▼  2019 (33)
    • ►  January (15)
    • ►  February (6)
    • ►  March (2)
    • ▼  April (5)
      • Data Types in PostgreSQL
      • Database Creation in PostgreSQL
      • Different Server Logs in MySQL
      • Table Creation in PostgreSQL
      • Creating User Account in MongoDB
    • ►  May (5)
  • ►  2020 (5)
    • ►  April (1)
    • ►  May (2)
    • ►  July (2)
  • ►  2021 (8)
    • ►  June (3)
    • ►  July (3)
    • ►  August (1)
    • ►  December (1)
  • ►  2022 (33)
    • ►  May (3)
    • ►  June (10)
    • ►  July (3)
    • ►  August (4)
    • ►  September (8)
    • ►  October (3)
    • ►  November (2)
  • ►  2023 (14)
    • ►  February (1)
    • ►  April (5)
    • ►  May (2)
    • ►  June (1)
    • ►  September (1)
    • ►  October (1)
    • ►  December (3)
  • ►  2024 (5)
    • ►  January (2)
    • ►  March (3)
  • ►  2025 (6)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)

Popular Posts

  • ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
    In previous articles, we have learned about user creation and grants  in MySQL in detail, but there are a few privileges called global priv...
  • ORA-29283: invalid file operation: unexpected "LFI" error (1509)[29437]
    I was trying to export the schema in my windows PC, it got stuck with below error    C:\Users\shaik\Videos\technodba exp>expdp userid...
  • Oracle Dataguard Broker Configuration (DGMGRL)
    Data Guard Broker is a command-line interface that makes managing primary and standby databases easy. DBA can use a single command to switch...
  • Data pump (expdp/impdp) troubleshooting in Oracle 23ai
    The latest database version, Oracle 23ai, i.e., version 23c with AI features enabled, is powerful and brings a lot of features to view and t...
  • PostgreSQL : pg_hba. conf configuration file
    In PostgreSQL, there are a number of configuration files, some of the files needs to be managed by postgres DBA, out of which  pg_hba.conf ...

Labels

oracle Oracle 12c mysql PostgreSQL MongoDB oracle 19c Oracle23c oracle19c Orale PDB-CDB oracle12c python AWS Oracle ASM Virtualbox pluggable database storage engine

Pages

  • Disclaimer
  • Privacy Policy

Follow TechnoDBA

Copyright © Atikh's DBA blog | Powered by Blogger