Get the App
SLTechnology News&Howtos  ›  Database  › 

How sql modifies the table structure

Shulou Source: shulou.com Published: 2022-06-01 02:43:07 10月11日 Update

This article will explain in detail how to modify the table structure of sql. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The operations to modify the table structure in SQL include adding table fields, deleting table fields, renaming fields, changing field types, setting primary keys, and adding not null constraints. Let's take a look at these operations separately.

1. Add table fields

ALTER TABLE table name ADD field name field type DEFAULT NULL

2. Delete the table field

Alter table table name drop field name

Example:

Alter table user drop name

3. Rename the field

Alter table table name rename old field name to new field name

Example:

Alter table user rename oldname to newname

4. Change the field type

ALTER TABLE table name

The type after the ALTER COLUMN field name is changed

Example:

Alter table user alter name varchar (50)

5. Set the primary key

ALTER TABLE table name

ADD CONSTRAINT primary key name PRIMARY KEY (field name)

6. Add not null constraint

ALTER TABLE table name

ALTER COLUMN Field name Field Type NOT NULL

About sql how to modify the table structure to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Fields types structures more examples articles good practical content articles knowledge references help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Huawei NVidia OPPO Reno