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 (21)
  • 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 (7)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)
    • ►  August (1)
  • ►  2026 (1)
    • ►  January (1)

Popular Posts

  • 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 ...
  • User Managed Backups in Oracle
    Definition :Backup is real and consistent copy of data from database that could be used to reconstruct the data after and incident. ...
  • 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 : Creating a new pluggable database using PDB$SEED
    As a database administrator, you might come across creating a pluggable database, here is the method to create a pluggable database (PDB), F...
  • Oracle ASM Scrubbing
    What is ASM Scrubbing?   ASM scrubbing is a process used to identify and repair data corruption issues within an ASM disk group. ASM Scr...

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