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 阅读 31 次 更新于 2025-09-09 00:24:17 我来答关注问题0
  • 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中,可以使用ALTER TABLE语句来添加新的字段。具体步骤如下:1. 打开MySQL客户端,连接到相应的数据库。2. 使用ALTER TABLE语句,指定要添加字段的表名。3. 使用ADD关键字指定要添加的字段名、数据类型及其他属性。示例:sql ALTER TABLE 表名 ADD 列名 数据类型;例如,向名为"s...

  • 1. 列的添加:使用ALTER TABLE语句,可以在已有的表中添加新的列,语法如下:ALTER TABLE table_name ADD COLUMN column_name datatype;其中,table_name是需要添加列的表名,column_name是新添加的列名,datatype是该列的数据类型。2. 列的修改:使用ALTER TABLE语句,可以修改已有列的数据类型、大小...

  • 1. 在新建表中添加列 新建表似乎是最常见的对表增加列的方法了。我们可以使用SQL语句,传入必要的参数,即可完成创建列的目的。代码片段:CREATE TABLE table_name (column_1 data_type constraint_name,column_2 data_type constraint_name,column_n data_type constraint_name );其中,指的是要创建...

  • MySQL中的ALTER语句是用于对已有表进行修改的强有力工具,其主要功能包括:删除列:通过ALTER TABLE table_name DROP COLUMN column_name;命令删除表中的某个列,这在优化表结构或调整数据库设计时非常有用。增加列:使用ALTER TABLE table_name ADD COLUMN column_name column_type;命令在表中添加新列...

檬味博客在线解答立即免费咨询

mySQL相关话题

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