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!