banner



How To Create New Column In Sql

PostgreSQL Add Column

Summary: in this tutorial, you will learn how to use the PostgreSQL ADD COLUMN statement to add one or more columns to an existing table.

Introduction to the PostgreSQL ADD COLUMN statement

To add a new column to an existing table, you use the ALTER TABLEADD COLUMN statement as follows:

            

ALTER TABLE table_name ADD COLUMN new_column_name data_type constraint;

Code language: SQL (Structured Query Language) ( sql )

In this syntax:

  • First, specify the name of the table that you want to add a new column to after the ALTER TABLE keyword.
  • Second, specify the name of the new column as well as its data type and constraint after the ADD COLUMN keywords.

When you add a new column to the table, PostgreSQL appends it at the end of the table. PostgreSQL has no option to specify the position of the new column in the table.

To add multiple columns to an existing table, you use multiple ADD COLUMN clauses in the ALTER TABLE statement as follows:

            

ALTER TABLE table_name ADD COLUMN column_name1 data_type constraint, ADD COLUMN column_name2 data_type constraint, ... ADD COLUMN column_namen data_type constraint;

Code language: SQL (Structured Query Language) ( sql )

PostgreSQL ADD COLUMN statement examples

The following CREATE TABLE statement creates a new table named customers with two columns: id and customer_name:

            

DROP TABLE IF EXISTS customers CASCADE; CREATE TABLE customers ( id SERIAL PRIMARY KEY, customer_name VARCHAR NOT NULL );

Code language: SQL (Structured Query Language) ( sql )

The following statement uses the ALTER TABLE ADD COLUMN statement to add the phone column to the customers table:

            

ALTER TABLE customers ADD COLUMN phone VARCHAR;

Code language: SQL (Structured Query Language) ( sql )

And the following statement adds the fax and email columns to the customers table:

            

ALTER TABLE customers ADD COLUMN fax VARCHAR, ADD COLUMN email VARCHAR;

Code language: SQL (Structured Query Language) ( sql )

To view the structure of the customers table in the psql tool, you can use the \d command like this:

            

\d customers

Code language: SQL (Structured Query Language) ( sql )

As can be seen clearly from the output, the phone, fax, and email columns appeared at the end of the column list of the customers table.

Add a column with the NOT NULL constraint to a table that already has data

The following statement inserts data into thecustomers table.

            

INSERT INTO customers (customer_name) VALUES ('Apple'), ('Samsung'), ('Sony');

Code language: SQL (Structured Query Language) ( sql )

Suppose that you want to add the contact_name column to the customers table:

            

ALTER TABLE customers ADD COLUMN contact_name VARCHAR NOT NULL;

Code language: SQL (Structured Query Language) ( sql )

PostgreSQL issued an error:

            

ERROR: column "contact_name" contains null values

Code language: SQL (Structured Query Language) ( sql )

This is because the contact_name column has the NOT NULL constraint. When PostgreSQL added the column, this new column receive NULL, which violates the NOT NULL constraint.

To solve this problem…

First, add the column without the NOT NULL constraint:

            

ALTER TABLE customers ADD COLUMN contact_name VARCHAR;

Code language: SQL (Structured Query Language) ( sql )

Second, update the values in the contact_name column.

            

UPDATE customers SET contact_name = 'John Doe' WHERE id = 1; UPDATE customers SET contact_name = 'Mary Doe' WHERE id = 2; UPDATE customers SET contact_name = 'Lily Bush' WHERE id = 3;

Code language: SQL (Structured Query Language) ( sql )

Third, set the NOT NULL constraint for the contact_name column.

            

ALTER TABLE customers ALTER COLUMN contact_name SET NOT NULL;

Code language: SQL (Structured Query Language) ( sql )

In this tutorial, you have learned how to use the PostgresSQL ADD COLUMN statement to add one or more columns to a table.

Was this tutorial helpful ?

How To Create New Column In Sql

Source: https://www.postgresqltutorial.com/postgresql-add-column/

Posted by: gilliamwough1983.blogspot.com

0 Response to "How To Create New Column In Sql"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel