ALTER
ALTER is a SQL command used to modify an existing database object like a table, database, or column. It provides functionalities like adding, modifying, or deleting a column from the existing table. It also facilitates renaming of objects and enabling or disabling constraints and indexes.
Example
CREATE TABLE employees ( ID INT PRIMARY KEY, Name VARCHAR(30), Age INT);
ALTER TABLE employeesADD COLUMN Position VARCHAR(30);
SELECT * FROM employees;
#End of Example Code
Output
+----+------+-----+----------+| ID | Name | Age | Position |+----+------+-----+----------+
#End of Output
Explanation
In the example provided, a new table called ‘employees’ is created with three columns: ‘ID’, ‘Name’ and ‘Age’. The ALTER TABLE
statement is then used to add a new column ‘Position’ to the ‘employees’ table. The SELECT
statement is used to view the contents of the updated ‘employees’ table.
Example
ALTER TABLE employeeADD COLUMN email VARCHAR(50);
Output
No output is generated as ALTER TABLE
runs silently without returning a report.
Explanation
In the provided example, the ALTER TABLE
statement is used to modify the structure of the ‘employee’ table by adding a new column named ‘email’ of type VARCHAR
with a maximum length of 50 characters.
Example
ALTER TABLE EmployeesADD Email varchar(255);
Output
Command(s) completed successfully.
Explanation
In the example provided, the ALTER TABLE
statement is used to add a new column named Email
of datatype varchar(255)
to the Employees
table. The varchar(255)
specifies that the maximum length for the entries in the Email
column is 255 characters. Execution of the code does not return an output table. Instead, a success message is returned, indicating the completion of the command.
Example
ALTER TABLE employeesMODIFY (salary NUMBER(8, 2));
Output
The new definition of the salary
column would be NUMBER(8,2)
. Since this is a DDL operation and doesn’t result any rows being returned, there is no actual output.
Explanation
The ALTER TABLE
statement is used to add, delete/drop or modify columns in an existing table. In this example, the ALTER TABLE
statement modifies the data type of the salary
column of the employees
table to NUMBER(8,2)
.
Example
CREATE TABLE Employees ( ID INTEGER PRIMARY KEY, name TEXT, age INTEGER);
ALTER TABLE EmployeesADD COLUMN email TEXT;
Output
No output is returned for the ALTER TABLE statement in SQLite.
Explanation
In the example provided, a table named ‘Employees’ is created first with three columns: ‘ID’, ‘name’, and ‘age’. The ‘ALTER TABLE’ statement then adds a new column ‘email’ to the existing ‘Employees’ table. SQLite doesn’t return any output for this action.