Page 23 - Modular_V2.0_SQL_Flipbook
P. 23

ALTER TABLE command also allows for change in the definition of a column, like changing the size
                 of the column or adding a constraint to it. For example,

                     ALTER TABLE Students

                     MODIFY Address VARCHAR(60);
                 The above command uses MODIFY to change the definition of an existing column of table.
                     ALTER TABLE Students

                     ADD CONSTRAINT PRIMARY KEY(StudentID);
                 The above command is used to add Primary Key Constraint to a column after the table is created.

                 You can also add more than one columns at the same time by using the ADD COLUMN statement
                 multiple times with the ALTER TABLE command.

                 Deleting a Column

                 SQL provides the DROP COLUMN clause with the ALTER TABLE command to delete one or more
                 columns from a table. The syntax to delete a column is as follows:

                     ALTER TABLE table_name DROP COLUMN column_name;

                 For example,

                     ALTER TABLE Students

                     DROP COLUMN Address;
                 Preceding command will delete the Address column from the Students table.

                 Changing the Data Type of a Column

                 The MODIFY COLUMN clause is used with the ALTER TABLE command to change the data type of a
                 column. The syntax to change the data type of a column is as follows:

                     ALTER TABLE table_name MODIFY COLUMN column_name datatype;

                 For example,

                     ALTER TABLE students MODIFY COLUMN marks char(30);

                     DELETING A TABLE

                 The DROP TABLE command is used to delete a table from a database. The syntax of the DROP TABLE

                 command is as follows:
                     DROP TABLE table_name;

                 For example,

                     DROP TABLE Students;
                 Once the preceding command is executed, the Students table will be deleted.








                                                                                                                     21
                                                                                               Introduction to MySQL
   18   19   20   21   22   23   24   25   26   27   28