「数据库」如何「切换」操作? (数据库怎么切换)

数据库是一种非常重要的数据管理工具,很多企业都会使用数据库来管理企业的数据,提高数据的存储和管理效率。在使用数据库的过程中,经常需要进行「切换」操作,例如切换数据库、表、模式等等。本文将介绍在使用数据库的过程中如何进行「切换」操作。

一、切换数据库

在使用数据库的过程中,往往会有多个数据库存在,需要进行切换。下面以MySQL数据库为例,介绍如何切换数据库。

1. 查看当前所在数据库

使用命令SHOW DATABASES;可以查看当前所有的数据库。使用命令SELECT DATABASE();可以查看当前所在的数据库。例如:

mysql> SHOW DATABASES;

+——————–+

| Database |

+——————–+

| information_schema |

| mysql |

| performance_schema |

| sakila |

| sys |

| test |

| world |

+——————–+

7 rows in set (0.00 sec)

mysql> SELECT DATABASE();

+————+

| DATABASE() |

+————+

| NULL |

+————+

1 row in set (0.00 sec)

2. 切换数据库

使用命令USE DATABASE_NAME; 可以切换到指定的数据库。例如:

mysql> USE world;

Database changed

使用命令SELECT DATABASE();可以查看切换后的数据库。

二、切换表

在数据库中,一个数据库往往包含多个表。在切换表的时候,需要注意汇总表可能会产生数据误操作。

1. 查看当前所在的表

使用命令SHOW TABLES;可以查看当前所有的表。例如:

mysql> SHOW TABLES;

+——————-+

| Tables_in_test |

+——————-+

| mysql_test_table |

| student |

| student_course |

| test_table |

| users |

+——————-+

5 rows in set (0.00 sec)

使用命令SELECT * FROM table_name;即使用一个SELECT语句来查看当前表的全部数据。例如:

mysql> SELECT * FROM users;

+—-+————+———-+———————+

| id | name | password | eml |

+—-+————+———-+———————+

| 1 | test_user1 | 123456 | test_user1@qq.com |

| 2 | test_user2 | 123456 | test_user2@qq.com |

| 3 | test_user3 | 123456 | test_user3@qq.com |

| 4 | test_user4 | 123456 | test_user4@qq.com |

| 5 | test_user5 | 123456 | test_user5@qq.com |

+—-+————+———-+———————+

5 rows in set (0.00 sec)

2. 切换表

使用命令USE DATABASE_NAME; 可以切换到指定的表,例如:

mysql> SELECT * FROM users;

+—-+————+———-+———————+

| id | name | password | eml |

+—-+————+———-+———————+

| 1 | test_user1 | 123456 | test_user1@qq.com |

| 2 | test_user2 | 123456 | test_user2@qq.com |

| 3 | test_user3 | 123456 | test_user3@qq.com |

| 4 | test_user4 | 123456 | test_user4@qq.com |

| 5 | test_user5 | 123456 | test_user5@qq.com |

+—-+————+———-+———————+

5 rows in set (0.00 sec)

mysql> USE test;

Database changed

mysql> SHOW TABLES;

+——————-+

| Tables_in_test |

+——————-+

| mysql_test_table |

| student |

| student_course |

| test_table |

| users |

+——————-+

5 rows in set (0.00 sec)

mysql> USE users;

Database changed

mysql> SELECT * FROM users;

+—-+————+———-+———————+

| id | name | password | eml |

+—-+————+———-+———————+

| 1 | test_user1 | 123456 | test_user1@qq.com |

| 2 | test_user2 | 123456 | test_user2@qq.com |

| 3 | test_user3 | 123456 | test_user3@qq.com |

| 4 | test_user4 | 123456 | test_user4@qq.com |

| 5 | test_user5 | 123456 | test_user5@qq.com |

+—-+————+———-+———————+

5 rows in set (0.00 sec)

三、切换模式

在数据库中,一个模式往往包含多个对象,例如表、视图、存储过程等等。在切换模式时,需要明确要切换到哪一个模式下面执行操作。

1. 查看当前所在模式

使用命令SHOW TABLES; 可以查看当前模式下的所有对象。例如:

mysql> SHOW TABLES;

+——————-+

| Tables_in_test |

+——————-+

| mysql_test_table |

| student |

| student_course |

| test_table |

| users |

+——————-+

5 rows in set (0.00 sec)

2. 切换模式

使用命令USE DATABASE_NAME; 可以切换到指定的模式,例如:

mysql> USE information_schema;

Database changed

mysql> SHOW TABLES;

+————————————+

| Tables_in_information_schema |

+————————————+

| CHARACTER_SETS |

| COLLATIONS |

| COLLATION_CHARACTER_SET_APPLICABILITY|

| COLUMN_PRIVILEGES |

| COLUMNS |

| ENGINES |

| EVENTS |

| FILES |

| GLOBAL_STATUS |

| GLOBAL_VARIABLES |

| KEY_COLUMN_USAGE |

| PARAMETERS |

| PARTITIONS |

| PLUGINS |

| PROCESSLIST |

| PROFILING |

| REFERENTIAL_CONSTRNTS |

| RESOURCE_GROUPS |

| ROUTINES |

| SCHEMATA |

| SCHEMA_PRIVILEGES |

| SESSION_STATUS |

| SESSION_VARIABLES |

| STATISTICS |

| TABLES |

| TABLESPACES |

| TABLE_CONSTRNTS |

| TABLE_PRIVILEGES |

| TRIGGERS |

| USER_PRIVILEGES |

| VIEWS |

+————————————+

32 rows in set (0.00 sec)

mysql> USE information_schema;

mysql> SHOW TABLES;

+————————————+

| Tables_in_performance_schema |

+————————————+

| accounts |

| cond_instances |

| events_wts_current |

| events_wts_history |

| events_wts_history_long |

| external_locks |

| file_instances |

| file_summary_by_event_name |

| file_summary_by_instance |

| host_summary |

| host_summary_by_file_io |

| host_summary_by_file_io_type |

| host_summary_by_stages |

| host_summary_by_statement_latency |

| innodb_buffer_stats_by_schema |

| innodb_buffer_stats_by_table |

| innodb_lock_wts |

| io_by_thread_by_latency |

| io_global_by_file_by_bytes |

| io_global_by_file_by_latency |

| io_global_by_wt_by_bytes |

| io_global_by_wt_by_latency |

| latest_file_uploads |

| memory_summary_by_account_by_event_name|

| memory_summary_by_host_by_event_name|

| memory_summary_by_thread_by_event_name|

| open_table_definitions |

| open_tables |

| performance_schema_backup_locks |

| performance_schema_locker |

| performance_schema_mutex_instances |

| performance_schema_rwlock_instances|

| performance_schema_session_connect_attrs|

| performance_schema_socket_instances|

| performance_schema_stage_instances |

| performance_schema_statement_ysis|

| performance_schema_table_handles |

| performance_schema_table_instances |

| performance_schema_thread_instances|

| performance_schema_users |

| performance_timers |

| prepared_statements_instances |

| processlist |

| ps_check_lost_instrumentation |

| ps_connection_events |

| ps_events_stages_histogram |

| ps_events_stages_summary_by_account_by_event_name|

| ps_events_stages_summary_by_host_by_event_name|

| ps_meta_events |

| ps_mutex_instances |

| ps_rwlock_instances |

| ps_setup_actors |

| ps_setup_objects |

| ps_setup_objects_summary |

| ps_statements_ysis |

| ps_statements_digest |

| ps_statements_digest_history |

| ps_status |

| ps_thread_events |

| ps_tls_channels |

| repeatable_read |

| schema_auto_increment_columns |

| schema_index_statistics |

| schema_object_instances |

| schema_redundant_indexes |

| schema_table_lock_wts |

| schema_table_statistics |

| schema_table_statistics_with_buffer|

| schema_triggers |

| schema_unused_indexes |

| session_account_connect_attrs |

| session_connect_attrs |

| setup_actors |

| setup_consumers |

| setup_instruments |

| setup_objects |

| setup_timers |

| socket_instances |

| stage_class |

| statement_ysis |

| statements_with_errors |

| statements_with_full_table_scans |

| statements_with_runtimes_in_95th_percentile|

| statements_with_sorting |

| statements_with_temp_tables |

| status_by_account |

| status_by_host |

| status_by_thread |

| status_by_user |

| table_handles |

| table_io_wts_summary_by_index_usage|

| table_lock_wts |

| table_partitions |

| table_replication_metadata |

| table_statistics |

| table_statistics_with_buffer |

| thread_pool_groups |

| thread_pool_resize_request |

| thread_pool_slave_threads |

| thread_pool_threads |

| user_defined_functions |

| user_variables_by_thread |

| wsrep_local_bf_aborts |

| wsrep_local_index |

| wsrep_local_state |

| wsrep_slave_FNO |

| wsrep_slave_GAP |

| wsrep_slave_threshold |

+————————————+

121 rows in set (0.00 sec)

四、

在使用数据库的过程中,「切换」操作非常频繁。在切换数据库、表、模式的时候,一定要谨慎处理,以免出现误操作。同时,建议在进行「切换」操作之前确认好自己当前的状态,以免造成不必要的损失。

相关问题拓展阅读:

MYSQL 数据库用什么命令切换数据库,谢谢大家教我。

要用哪个库 就写 

use 库名

就前芹悉可以切换过首差去了慧乎

access:是一种桌面数据库,适合数据量较少的应用,存储数据库(.mdb)文件大小贺燃缺不超过2g字节,数据库中的禅辩对象个数不超段哪过32,768。

程序如何在一台电脑上两个数据库之间切换

在连接的时候数据库的时候选择一下,根据不同选择调用不同连接字符串就行了。

数据库怎么切换的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于数据库怎么切换,「数据库」如何「切换」操作?,MYSQL 数据库用什么命令切换数据库,谢谢大家教我。,程序如何在一台电脑上两个数据库之间切换的信息别忘了在本站进行查找喔。


数据运维技术 » 「数据库」如何「切换」操作? (数据库怎么切换)