Home >Database >Mysql Tutorial >What are the operations to modify the table structure in sql?

What are the operations to modify the table structure in sql?

不言
不言Original
2019-03-02 16:50:3925881browse

What are the operations to modify the table structure in sql?

The operations of modifying 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 do this below. Let’s look at these operations individually.

1. Add table fields

ALTER TABLE 表名
ADD 字段名 字段类型 DEFAULT NULL

2. Delete table fields

alter table table name drop field Name

Example:

alter table user drop name;

3. Field rename

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
ALTER COLUMN field name changed type

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

The above is the detailed content of What are the operations to modify the table structure in sql?. 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