ALTER TABLE table_name
ALTER COLUMN column_name new_data_type(size);
#SQL Server
ALTER TABLE Customers
ALTER COLUMN age VARCHAR(2);
#MySQL
ALTER TABLE Customers
MODIFY COLUMN age VARCHAR(2);
#Oracle
ALTER TABLE Customers
MODIFY age VARCHAR(2);
#PostgreSQL
ALTER TABLE Customers
ALTER COLUMN age TYPE VARCHAR(2);
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
Changes the data type of a table’s column.
Example: In the ‘users’ table, make the column ‘incept_date’ into a
‘datetime’ type.
ALTER TABLE users
ALTER COLUMN incept_date datetime;
-- With check if field already exists
IF NOT EXISTS(SELECT 1 FROM sys.columns
WHERE Name = N'Email'
AND Object_ID = Object_ID(N'dbo.Customers'))
BEGIN
ALTER TABLE Customers
ADD Email varchar(255);
END
ALTER TABLE table_name
DROP PRIMARY KEY;
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);
ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;
ALTER TABLE table_name
ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);
ALTER TABLE table_name
DROP INDEX MyUniqueConstraint;