Home > Database > SQL > body text

Statements to modify table structure in sql

下次还敢
Release: 2024-04-28 09:15:26
Original
707 people have browsed it

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

Statements to modify table structure in sql

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>
Copy after login

For example:

<code class="sql">ALTER TABLE customers ADD phone_number VARCHAR(10);</code>
Copy after login

Delete column

<code class="sql">ALTER TABLE table_name DROP COLUMN column_name;</code>
Copy after login

For example:

<code class="sql">ALTER TABLE customers DROP COLUMN age;</code>
Copy after login

Modify columns

<code class="sql">ALTER TABLE table_name ALTER COLUMN column_name NEW_DATA_TYPE;</code>
Copy after login

For example:

<code class="sql">ALTER TABLE customers ALTER COLUMN name VARCHAR(50);</code>
Copy after login

Add constraints

<code class="sql">ALTER TABLE table_name ADD CONSTRAINT constraint_name [CONSTRAINT_TYPE];</code>
Copy after login

For example:

<code class="sql">ALTER TABLE orders ADD CONSTRAINT FK_customer FOREIGN KEY (customer_id) REFERENCES customers (customer_id);</code>
Copy after login

Delete constraints

<code class="sql">ALTER TABLE table_name DROP CONSTRAINT constraint_name;</code>
Copy after login

For example:

<code class="sql">ALTER TABLE orders DROP CONSTRAINT FK_customer;</code>
Copy after login

Add index

<code class="sql">ALTER TABLE table_name ADD INDEX index_name (column_name);</code>
Copy after login

For example:

<code class="sql">ALTER TABLE customers ADD INDEX idx_name (name);</code>
Copy after login

Delete index

<code class="sql">ALTER TABLE table_name DROP INDEX index_name;</code>
Copy after login

For example:

<code class="sql">ALTER TABLE customers DROP INDEX idx_name;</code>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template