How To Create New Column In Sql
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 TABLE
ADD COLUMN
statement as follows:
Code language: SQL (Structured Query Language) ( sql )
ALTER TABLE table_name ADD COLUMN new_column_name data_type constraint;
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:
Code language: SQL (Structured Query Language) ( sql )
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;
PostgreSQL ADD COLUMN statement examples
The following CREATE TABLE
statement creates a new table named customers
with two columns: id
and customer_name
:
Code language: SQL (Structured Query Language) ( sql )
DROP TABLE IF EXISTS customers CASCADE; CREATE TABLE customers ( id SERIAL PRIMARY KEY, customer_name VARCHAR NOT NULL );
The following statement uses the ALTER TABLE ADD COLUMN
statement to add the phone
column to the customers
table:
Code language: SQL (Structured Query Language) ( sql )
ALTER TABLE customers ADD COLUMN phone VARCHAR;
And the following statement adds the fax
and email
columns to the customers
table:
Code language: SQL (Structured Query Language) ( sql )
ALTER TABLE customers ADD COLUMN fax VARCHAR, ADD COLUMN email VARCHAR;
To view the structure of the customers
table in the psql
tool, you can use the \d
command like this:
Code language: SQL (Structured Query Language) ( sql )
\d customers
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.
Code language: SQL (Structured Query Language) ( sql )
INSERT INTO customers (customer_name) VALUES ('Apple'), ('Samsung'), ('Sony');
Suppose that you want to add the contact_name
column to the customers
table:
Code language: SQL (Structured Query Language) ( sql )
ALTER TABLE customers ADD COLUMN contact_name VARCHAR NOT NULL;
PostgreSQL issued an error:
Code language: SQL (Structured Query Language) ( sql )
ERROR: column "contact_name" contains null values
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:
Code language: SQL (Structured Query Language) ( sql )
ALTER TABLE customers ADD COLUMN contact_name VARCHAR;
Second, update the values in the contact_name
column.
Code language: SQL (Structured Query Language) ( sql )
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;
Third, set the NOT NULL
constraint for the contact_name
column.
Code language: SQL (Structured Query Language) ( sql )
ALTER TABLE customers ALTER COLUMN contact_name SET NOT NULL;
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