Home > Database > Mysql Tutorial > body text

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

不言
Release: 2019-03-02 16:50:39
Original
25746 people have browsed it

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

2. Delete table fields

alter table table name drop field Name

Example:

alter table user drop name;
Copy after login

3. Field rename

alter table table name rename old field name to new field name

Example:

alter table  user  rename oldname to newname;
Copy after login

4. Change the field type

ALTER TABLE table name
ALTER COLUMN field name changed type

Example:

alter table user alter name varchar(50);
Copy after login

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!

Related labels:
sql
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!