mysql添加列、删除列,创建主键、备份等常用操作总结

1. 添加列与设置为主键:alter table test_table add column test_column int not null auto_increment FIRST add primary key(test_column);2. 删除列:alter table test_table drop column test_column;3. 修改列字段长度:alter table test_table modify column test_column varchar(50);4. 完全...
mysql添加列、删除列,创建主键、备份等常用操作总结
一. 列常用操作

1. 添加列与设置为主键:

alter table test_table add column test_column int not null auto_increment FIRST add primary key(test_column);

2. 删除列:

alter table test_table drop column test_column;

3. 修改列字段长度:

alter table test_table modify column test_column varchar(50);

4. 完全修改某一列:

alter table test_table change column test1_column test_column varchar(30);

5. 重命名某一列:

alter table test_table change column error_name_column test_column int not null;

二. 针对表的多数操作

1. 修改表的存储引擎:

alter table test_table engine=innodb;

2. 删除表的主键:

alter table test_table drop primary key;

注意:删除自动增长列后才能删除主键,否则会报错。

3. 添加主键:

alter table test_table add primary key(test_column);

4. 添加索引:

alter table test_table add index test_index(test_column);

5. 删除索引:

alter table test_table drop index test_index;

6. 重命名表:

alter table test_table rename new_name_table;

三. 常用查询操作

1. 查看默认存储引擎:

show variable like 'table_type';

2. 查询数据库支持的存储引擎:

show ENGINES \G;

3. 查看指定表的索引:

show index from test_table;

4. 查看服务器字符集与校队规则:

show variables like 'character_set_server';

show variables like 'collation_set_server';

5. 查看视图信息:

show table status where comment='view';

6. 查看表创建信息:

show create table test_table;

7. 查看视图定义:

show create view view_name;

8. 查询触发器信息:

select * from triggers where trigger_name='test_trigger_name';

9. 查看所有触发器:

show triggers \G;

10. 查看存储过程与函数:

show procedure status;

show function status;

11. 查看事件调度器:

show events \G;

12. 检查分区支持:

show variables like '%partition%';

四. 导入导出和备份操作

1. 导出整个数据库:

mysqldump -u root -p test_database > /tmp/test.sql

2. 导出单个表:

mysqldump -u root -p test_database test_table > /tmp/test.sql

3. 导出数据库结构(不包含数据):

mysqldump -u root -p -d --add-drop-table test_database > /tmp/test.sql

4. 导出表内容为excel:

select * from test_table into outfile '/tmp/test.xls'

5. 导入sql文件:

use test;

source /tmp/test.sql

6. 导入excel文件(通过窗口端数据库连接软件实现)2024-10-06
mengvlog 阅读 47 次 更新于 2025-10-30 12:59:16 我来答关注问题0
檬味博客在线解答立即免费咨询

mySQL相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部