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?

不言
Release: 2019-03-02 16:50:39
Original
25920 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
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 Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
sql optimization or
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template