Suppose we apply this as a query to the products table created above. Let us review the elementary syntax for dropping the column: If we want to remove any table column in the database table, we will implement the DROP COLUMN command with an ALTER statement. Here, we have changed the Description column name to the Product_Info name. In the above syntax, the original name defines the column name that exists in the table, and NewColumnNamedefines the new name to be given.įor this, let us illustrate the following example:ĬHANGE COLUMN DescriptionProduct_InfoVARCHAR(255) NULL It is a virtuous practice to study or view the attributes associated with a column in a table before we modify using the ALTER query.įor example, we want to alter the column Description to have a NOT NULL column definition and VARCHAR characters’ length to 200.ĬHANGE COLUMN OriginalNameNewColumnNameCol_Definition Here, we have the basic syntax describing the ALTER TABLE MODIFY statement and showing examples accordingly: ALTER TABLE TableName We will use the ALTER TABLE MODIFY command to make some changes in the structure of existing columns in the Products table. You can see a new structure of the table formed using the ALTER TABLE ADD COLUMN statements. Suppose we are adding two more columns in the Products table which is shown below with the help of ALTER query: ALTER TABLE Products Let us add multiple columns to the Products table using the succeeding syntax for ALTER TABLE ADD Query:ĪDD NewColumnNameCol_Definition, You can view the column lists of the Products table using the query below in the database:Īs you can see in the output above that a new column, Price, is now added to the Products table at the end of the lists. Now, we will apply the ALTER TABLE ADD COLUMN statement to add a new column at the end of the columns present in the Products table in the database: ALTER TABLE Products ADD Cost INT NOT NULL But if these options are not provided in the ALTER ADD COLUMN query, the new table column will be affixed at the end of the list of columns in the table. There are two options: add the table column after the last column existing in the table using AFTER ColumnNameKeyword or, as the first column using FIRST ColumnNamekeyword. FIRST | AFTER ColumnName: It determines the position of the new table column to be added.Col_Definition: It includes the Data Type for the new column added, maximum size, and valid CONSTRAINTS.NewColumnName: Mention the name of the new table column to be added.TableName: We will provide the specific table name after the keywords ALTER TABLE where we want to supplement a new column or columns.Here, the above terms are explained as follows: Let us add a column to the Products table using the succeeding syntax for ALTER TABLE ADD Query.ĪDD NewColumnNameCol_Definition We will use the ALTER TABLE ADD command to add one or more table columns to this demo table created above.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |