Home  >  Article  >  Database  >  What command can be used in sql to modify the structure of the table?

What command can be used in sql to modify the structure of the table?

下次还敢
下次还敢Original
2024-05-07 06:27:16800browse

The command in SQL used to modify the table structure is ALTER TABLE, which allows you to change columns, constraints, and indexes without losing data. Common ALTER TABLE operations include adding or dropping columns (ADD/DROP COLUMN), modifying column data types (ALTER COLUMN), adding or dropping constraints (ADD/DROP CONSTRAINT), and adding or dropping indexes (ADD/DROP INDEX).

What command can be used in sql to modify the structure of the table?

The command used to modify the table structure in SQL

The command used to modify the table structure in SQL isALTER TABLE. It allows you to change columns, constraints, and indexes in a table without losing data.

Use the ALTER TABLE command

ALTER TABLE The basic format of the syntax is as follows:

<code>ALTER TABLE table_name
ALTER_SPECIFICATION</code>

where , table_name is the name of the table to be modified, and ALTER_SPECIFICATION specifies the changes to be made.

COMMONALTER_SPECIFICATION

  • ##Add column: ADD COLUMN column_name data_type
  • Delete column: DROP COLUMN column_name
  • Modify column data type: ALTER COLUMN column_name data_type
  • Add constraint: ADD CONSTRAINT constraint_name constraint_definition
  • Delete constraint: DROP CONSTRAINT constraint_name
  • Add index: ADD INDEX index_name (column_list)
  • Delete index: DROP INDEX index_name

Example

The following example will add a new column named

phone_number to the customers table:

<code class="sql">ALTER TABLE customers
ADD COLUMN phone_number VARCHAR(20);</code>
The following example will delete the

shipping_address column from the orders table:

<code class="sql">ALTER TABLE orders
DROP COLUMN shipping_address;</code>
The following example will change the

products table The data type of the price column is DECIMAL:

<code class="sql">ALTER TABLE products
ALTER COLUMN price DECIMAL(8, 2);</code>

The above is the detailed content of What command can be used in sql to modify the structure of the table?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn