Page 256 - PYTHON-12
P. 256

  SQL TRUNCATE Statement


               The SQL TRUNCATE command is used to delete all the rows from the table and free the space
               containing the table.
               Syntax to TRUNCATE a table:

                 TRUNCATE TABLE<table_name>;
               For example,

               To delete all the rows from student table, the statement will be:
                 TRUNCATE TABLE student;

               Difference between DELETE and TRUNCATE Statements
               DELETE Statement: This command deletes only the rows from the table based on the condition
               given in the where clause or deletes all the rows from the table if no condition is specified. But it
               does not free the space containing the table.
               TRUNCATE Statement: This command is used to delete all the rows from the table and free the
               space containing the table.
               10. ALTER TABLE Command


               The ALTER TABLE command is used to modify the definition (structure) of a table by modifying the
               definition of its columns. The ALTER TABLE command is used to perform the following operations:
                 To add a column to an existing table.
                 To rename any existing column.
                 To change the datatype of any column or to modify its size.
                 To remove or physically delete a column.
               A. Adding a column to an existing table:

               Once a table has been created, new columns can be added later on, if required. The new column
               is added with NULL values for all the records/rows in the table. It is possible to add, delete and
               modify columns with ALTER TABLE statement.
               Syntax for adding a new column:
               ALTER TABLE <table_name> ADD(<column_name><datatype> [size]);


               For example, to add a new column Mobile_no of type integer in the table student:
               ALTER TABLE student ADD (Mobile_no integer);
               Thus, the above statement shall add a new column Mobile_no into the table student with NULL
               value in it.
           Computer Science with Python–XII  12.26 B. Adding a column with default value:
                 POINT TO REMEMBER

                 We have just added a column and there will be no data (NULL) under this attribute. UPDATE command can
                 be used to supply values/data to this column.





               ALTER TABLE command can be used to add a new column to an existing table with default values.
               Syntax for adding a column with a default value:

               ALTER TABLE <table_name>
               ADD ([column_name1]<datatype1>default data);
   251   252   253   254   255   256   257   258   259   260   261