• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

PostgreSQL Command: ALTER TABLE

January 2, 2024

PostgreSQL Command: ALTER TABLE

PostgreSQL is a powerful and feature-rich open-source relational database management system. It offers a wide range of commands and functionalities to manage and manipulate database tables. One such command is the ALTER TABLE command, which allows you to modify the structure of an existing table.

Modifying Table Columns

The ALTER TABLE command can be used to add, modify, or delete columns in an existing table. Let’s explore some of the common use cases:

Adding a Column

To add a new column to an existing table, you can use the ADD COLUMN clause. Here’s an example:

ALTER TABLE employees
ADD COLUMN email VARCHAR(255);

This command adds a new column named email of type VARCHAR(255) to the employees table.

Modifying a Column

If you need to modify the data type or constraints of an existing column, you can use the ALTER COLUMN clause. Here’s an example:

ALTER TABLE employees
ALTER COLUMN email SET NOT NULL;

This command modifies the email column in the employees table and sets it as NOT NULL, meaning it cannot contain null values.

Deleting a Column

To remove a column from a table, you can use the DROP COLUMN clause. Here’s an example:

ALTER TABLE employees
DROP COLUMN email;

This command deletes the email column from the employees table.

Modifying Table Constraints

In addition to modifying columns, the ALTER TABLE command can also be used to add, modify, or delete constraints on a table.

Adding a Constraint

To add a new constraint to a table, you can use the ADD CONSTRAINT clause. Here’s an example:

ALTER TABLE employees
ADD CONSTRAINT pk_employees PRIMARY KEY (id);

This command adds a primary key constraint named pk_employees to the employees table, using the id column as the primary key.

Modifying a Constraint

If you need to modify an existing constraint, you can use the ALTER CONSTRAINT clause. Here’s an example:

ALTER TABLE employees
ALTER CONSTRAINT pk_employees RENAME TO pk_emp;

This command renames the pk_employees constraint to pk_emp in the employees table.

Deleting a Constraint

To remove a constraint from a table, you can use the DROP CONSTRAINT clause. Here’s an example:

ALTER TABLE employees
DROP CONSTRAINT pk_emp;

This command deletes the pk_emp constraint from the employees table.

Conclusion

The ALTER TABLE command in PostgreSQL provides a flexible and powerful way to modify the structure of existing database tables. Whether you need to add, modify, or delete columns or constraints, PostgreSQL offers a wide range of options to meet your requirements.

For more information about VPS hosting solutions, visit Server.HK.

Recent Posts

  • How to Deploy a Node.js Application on Hong Kong VPS: Complete Guide
  • How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  • How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  • CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  • Top 5 Use Cases for a Hong Kong Dedicated Server in 2026

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot