Introduction
ALTER command is among the most essential SQL capabilities to know for database manipulation and administration. This Information Definition Language (DDL) command can alter current tables by including, eradicating, or updating columns, constraints, and indexes. We’ll take a look at other ways to make use of the ALTER command to spotlight its flexibility. As well as, ALTER TABLE statements in SQL could also be used to alter the information kind of an current column to match new knowledge wants.
If you happen to’re simply beginning out to discover SQL, right here’s a newbie’s information that can assist you: SQL For Information Science: A Newbie Information
Overview
- Be taught the aim and performance of the SQL ALTER command.
- Perceive how and the place to make use of SQL’s ALTER TABLE statements.
- Discover varied eventualities and examples demonstrating using this SQL perform.
Purposes of ALTER Command
The ALTER command in SQL can be utilized for a number of functions. Listed here are some frequent purposes:
Pattern Desk
Right here’s a pattern desk on which we might be testing out varied ALTER TABLE statements.
Including a New Column
So as to add a brand new column in our Worker desk, you should utilize the ADD COLUMN assertion.
ALTER TABLE staff
ADD COLUMN birth_date DATE;
SELECT emp_id, birth_date from Staff WHERE emp_id BETWEEN 1 AND 5;
This provides a brand new column named `birth_date` of kind DATE within the desk, however the values are empty as we simply added a brand new column in Staff.
Modifying an Current Column
To switch an current column, use the MODIFY or ALTER COLUMN clause.
ALTER TABLE staff
MODIFY COLUMN birth_date DATETIME;
The `birth_date` column within the `staff` desk modifications from DATE to DATETIME kind. Equally we will change different knowledge sorts as properly for different columns.
Dropping a Column
To take away an current column out of your desk, use the DROP COLUMN in SQL.
ALTER TABLE staff
DROP COLUMN birth_date;
This SQL question removes the birth_date column from the staff desk.
Including Constraints
The ALTER command’s makes use of prolong to including constraints to a desk like including a main key, overseas key, or distinctive constraint.
ALTER TABLE staff
ADD CONSTRAINT pk_employee_id PRIMARY KEY (emp_id);
This can add a main key constraint named `pk_employee_id` on the `emp_id` column of the `staff` desk.
Eradicating Constraints
We use the DROP CONSTRAINT to take away a constraint from the desk.
ALTER TABLE staff
DROP CONSTRAINT pk_employee_id;
This can take away the first key constraint (pk_employee_id) from the Staff desk.
Renaming a Desk
The ALTER command may rename a desk.
ALTER TABLE staff
RENAME TO workers;
This can rename the `staff` desk to `workers`.
Conclusion
DDL’s ALTER command could be very efficient for managing and altering database desk buildings. Customers can add, change, or delete columns and constraints, offering the flexibleness required for adaptable database design and administration. This command makes it simple to replace database buildings as wants change.
Be taught Extra: SQL: A Full Fledged Information from Fundamentals to Superior Stage
Steadily Requested Questions
A. Sure you should utilize ALTER for this use case. Right here’s an instance:ALTER TABLE staff
MODIFY COLUMN employee_id VARCHAR(10);
A. You possibly can add a default worth to an current column utilizing the ALTER command by utilizing SET DEFAULT. Right here’s an instance:ALTER TABLE staff
ALTER COLUMN employee_id SET DEFAULT 'UNKNOWN';
A. The ALTER TABLE and DROP COLUMN instructions can be utilized to drop a column. Right here’s an instance of dropping department_id from the staff desk:ALTER TABLE staff
DROP COLUMN department_id;