alter table 所有语法
时间: 2024-06-17 22:06:28 浏览: 191
alter table 语句是用于修改现有数据库表的结构,可以通过以下语法来实现:
1. 添加列
ALTER TABLE table_name ADD column_name datatype;
2. 修改列
ALTER TABLE table_name MODIFY column_name datatype;
3. 重命名列
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
4. 删除列
ALTER TABLE table_name DROP COLUMN column_name;
5. 修改表名
ALTER TABLE table_name RENAME TO new_table_name;
6. 添加主键
ALTER TABLE table_name ADD PRIMARY KEY (column_name);
7. 删除主键
ALTER TABLE table_name DROP PRIMARY KEY;
8. 添加外键
ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES other_table (column_name);
9. 删除外键
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
10. 添加索引
ALTER TABLE table_name ADD INDEX index_name (column_name);
11. 删除索引
ALTER TABLE table_name DROP INDEX index_name;
阅读全文