Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Add Column in SQL

Add Column in SQL

? ADD COLUMN in SQL

The ADD COLUMN statement is used with the ALTER TABLE command to add a new column to an existing table.


? Syntax

ALTER TABLE table_nameADD column_name data_type [constraints];

? Example 1: Add a Simple Column

ALTER TABLE employeesADD birth_date DATE;

? Adds a birth_date column of type DATE to the employees table.


? Example 2: Add Column with Default Value

ALTER TABLE ordersADD status VARCHAR(20) DEFAULT 'pending';

? Adds a status column with a default value of 'pending'.


? Example 3: Add Multiple Columns (MySQL / PostgreSQL)

ALTER TABLE productsADD COLUMN stock INT,ADD COLUMN available BOOLEAN DEFAULT TRUE;

? Adds two new columns: stock and available.

? Note: In SQL Server, you need to run separate ALTER TABLE statements for each column.


? Common Data Types

  • INT, BIGINT

  • VARCHAR(n), TEXT

  • DATE, DATETIME

  • BOOLEAN, FLOAT, etc.


? Add Column After Specific Column (MySQL Only)

ALTER TABLE employeesADD department_id INT AFTER name;

? Not supported in standard SQL or PostgreSQL.


? Important Notes

  • Existing rows will have NULL in the new column unless a default is specified.

  • Use UPDATE to populate data after adding the column.


? Check Structure After Alter

DESCRIBE employees;   -- MySQL-- or\d employees;         -- PostgreSQL

Let me know if you need examples for your specific database (MySQL, PostgreSQL, SQL Server, etc.) or want to see how to drop or rename a column!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql