You can modify the table structure through the ALTER TABLE statement. The specific operations are: add a column: ALTER TABLE table_name ADD column_name data_type; delete a column: ALTER TABLE table_name DROP COLUMN column_name; modify a column: ALTER TABLE table_name ALTER COLUMN column_name NEW_DATA_TYPE; Add constraints: ALTER TABLE table_name ADD CONSTRAINT constr
SQL statement to modify the table structure
In SQL, you can Modify the table structure through the ALTER TABLE
statement. This statement can be used to add, delete, or modify a table's columns, constraints, or indexes.
Add column
<code class="sql">ALTER TABLE table_name ADD column_name data_type;</code>
For example:
<code class="sql">ALTER TABLE customers ADD phone_number VARCHAR(10);</code>
Delete column
<code class="sql">ALTER TABLE table_name DROP COLUMN column_name;</code>
For example:
<code class="sql">ALTER TABLE customers DROP COLUMN age;</code>
Modify columns
<code class="sql">ALTER TABLE table_name ALTER COLUMN column_name NEW_DATA_TYPE;</code>
For example:
<code class="sql">ALTER TABLE customers ALTER COLUMN name VARCHAR(50);</code>
Add constraints
<code class="sql">ALTER TABLE table_name ADD CONSTRAINT constraint_name [CONSTRAINT_TYPE];</code>
For example:
<code class="sql">ALTER TABLE orders ADD CONSTRAINT FK_customer FOREIGN KEY (customer_id) REFERENCES customers (customer_id);</code>
Delete constraints
<code class="sql">ALTER TABLE table_name DROP CONSTRAINT constraint_name;</code>
For example:
<code class="sql">ALTER TABLE orders DROP CONSTRAINT FK_customer;</code>
Add index
<code class="sql">ALTER TABLE table_name ADD INDEX index_name (column_name);</code>
For example:
<code class="sql">ALTER TABLE customers ADD INDEX idx_name (name);</code>
Delete index
<code class="sql">ALTER TABLE table_name DROP INDEX index_name;</code>
For example:
<code class="sql">ALTER TABLE customers DROP INDEX idx_name;</code>
By using these ALTER TABLE
statements, the table structure can be easily modified to adapt to changing data needs .
The above is the detailed content of Statements to modify table structure in sql. For more information, please follow other related articles on the PHP Chinese website!