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.

Alter Table in SQL

Alter Table in SQL

? ALTER TABLE in SQL

The ALTER TABLE statement is used to change the structure of an existing table — such as adding, modifying, renaming, or deleting columns and constraints.


? Basic Syntax

ALTER TABLE table_name-- perform operation (e.g., ADD, DROP, MODIFY, etc.)

? 1. Add a Column

ALTER TABLE employeesADD birth_date DATE;

? 2. Drop a Column

ALTER TABLE employeesDROP COLUMN birth_date;

? 3. Modify a Column

MySQL:

ALTER TABLE employeesMODIFY COLUMN name VARCHAR(150);

PostgreSQL:

ALTER TABLE employeesALTER COLUMN name TYPE VARCHAR(150);

SQL Server:

ALTER TABLE employeesALTER COLUMN name VARCHAR(150);

? 4. Rename a Column

MySQL 8+ / PostgreSQL / SQL Server:

ALTER TABLE employeesRENAME COLUMN old_name TO new_name;

? 5. Rename a Table

ALTER TABLE old_table_nameRENAME TO new_table_name;

? 6. Add a Constraint

ALTER TABLE employeesADD CONSTRAINT unique_email UNIQUE (email);

? 7. Drop a Constraint

MySQL:

ALTER TABLE employeesDROP INDEX unique_email;

PostgreSQL / SQL Server:

ALTER TABLE employeesDROP CONSTRAINT unique_email;

? Summary Table

OperationSyntax Example
Add ColumnADD column_name TYPE
Drop ColumnDROP COLUMN column_name
Modify ColumnMODIFY COLUMN col TYPE (MySQL)
Rename ColumnRENAME COLUMN old TO new
Rename TableRENAME TO new_table_name
Add ConstraintADD CONSTRAINT ...
Drop ConstraintDROP CONSTRAINT constraint_name

Let me know your database type (MySQL, PostgreSQL, SQL Server, etc.) if you want exact syntax for your case!

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