客户机—服务器软件
1、mysql是一种DBMS(数据库软件):成本(开源),性能(执行快),可信赖(很多企业在用,社区也活跃),简单(安装简单)
2、DBMS可分为两类:一类为基于共享文件系统的DBMS,另一类为基于客户机—服务器的DBMS。前者(包括诸如Microsoft Access和FileMaker)用于桌面用途,通常不用于高端或更关键的应用。
MySQL、Oracle以及Microsoft SQL Server等数据库是基于客户机—服务器的数据库。
3、客户机—服务器应用分为两个不同的部分。服务器部分是负责所有数据访问和处理的一个软件。这个软件运行在称为数据库服务器的计算机上。与数据文件打交道的只有服务器软件。关于数据、数据添加、删除和数据更新的所有请求都由服务器软件完成。
MySQL版本
select 列名, 列名, 列名 from 表名;
3、检索所有列
select * from 表名;检索不需要的列通常会降低检索和应用程序的性能。
4、检索不同的行,即找出一列中的不同值
select distinct 列名 from 表名;
MySQL的当前版本为版本5① (虽然许多公司正在使用MySQL 3和4)。
下面是最近版本中引入的主要更改。
4——InnoDB引擎,增加事务处理(第26章)、并(第17章)、改
进全文本搜索(第18章)等的支持。
4.1——对函数库、子查询(第14章)、集成帮助等的重要增加。
5——存储过程(第23章)、触发器(第25章)、游标(第24章)、
视图(第22章)等。
[root@localhost mysql_dmp]# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 22 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> show databases;#返回可用数据库的一个列表。 +--------------------+ | Database | +--------------------+ | information_schema | | learn_mysql | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use learn_mysql;#USE语句并不返回任何结果。选择数据库
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables;#返回当前选择的数据库内可用表的列表。
+-----------------------+ | Tables_in_learn_mysql | +-----------------------+ | customers | | customers2 | | orderitems | | orders | | productnotes | | products | | vendors | +-----------------------+ 7 rows in set (0.00 sec) mysql> show columns from customers;#对每个字段返回一行,行中包含字段名、数据类型、是否允许NULL、键信息、默认值以及其他信息(如字段cust_id的auto_increment)。
+--------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+---------+----------------+
| cust_id | int(11) | NO | PRI | NULL | auto_increment |
| cust_name | char(50) | NO | | NULL | |
| cust_address | char(50) | YES | | NULL | |
| cust_city | char(50) | YES | | NULL | |
| cust_state | char(5) | YES | | NULL | |
| cust_zip | char(10) | YES | | NULL | |
| cust_country | char(50) | YES | | NULL | |
| cust_contact | char(50) | YES | | NULL | |
| cust_email | char(255) | YES | | NULL | |
+--------------+-----------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
mysql> describe customers;#效果和show columns from customers一样
+--------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+---------+----------------+
| cust_id | int(11) | NO | PRI | NULL | auto_increment |
| cust_name | char(50) | NO | | NULL | |
| cust_address | char(50) | YES | | NULL | |
| cust_city | char(50) | YES | | NULL | |
| cust_state | char(5) | YES | | NULL | |
| cust_zip | char(10) | YES | | NULL | |
| cust_country | char(50) | YES | | NULL | |
| cust_contact | char(50) | YES | | NULL | |
| cust_email | char(255) | YES | | NULL | |
+--------------+-----------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
自动增量是表列需要的胃一直值。例如,订单编号、雇员ID或顾客ID等。
show status;用于显示广泛的服务器状态信息
show status
View Code
show grants;用来显示授予用户的安全权限
show errors; 和 show warnings; ,用来显示服务器错误或警告信息
1、检索单个列
select 列名 from 表名;
show status;用于显示广泛的服务器状态信息
1 mysql> show status; 2 +-----------------------------------------------+--------------------------------------------------+ 3 | Variable_name | Value | 4 +-----------------------------------------------+--------------------------------------------------+ 5 | Aborted_clients | 3 | 6 | Aborted_connects | 6 | 7 | Binlog_cache_disk_use | 0 | 8 | Binlog_cache_use | 0 | 9 | Binlog_stmt_cache_disk_use | 0 | 10 | Binlog_stmt_cache_use | 0 | 11 | Bytes_received | 507 | 12 | Bytes_sent | 5786 | 13 | Com_admin_commands | 0 | 14 | Com_assign_to_keycache | 0 | 15 | Com_alter_db | 0 | 16 | Com_alter_db_upgrade | 0 | 17 | Com_alter_event | 0 | 18 | Com_alter_function | 0 | 19 | Com_alter_instance | 0 | 20 | Com_alter_procedure | 0 | 21 | Com_alter_server | 0 | 22 | Com_alter_table | 0 | 23 | Com_alter_tablespace | 0 | 24 | Com_alter_user | 0 | 25 | Com_analyze | 0 | 26 | Com_begin | 0 | 27 | Com_binlog | 0 | 28 | Com_call_procedure | 0 | 29 | Com_change_db | 1 | 30 | Com_change_master | 0 | 31 | Com_change_repl_filter | 0 | 32 | Com_check | 0 | 33 | Com_checksum | 0 | 34 | Com_commit | 0 | 35 | Com_create_db | 0 | 36 | Com_create_event | 0 | 37 | Com_create_function | 0 | 38 | Com_create_index | 0 | 39 | Com_create_procedure | 0 | 40 | Com_create_server | 0 | 41 | Com_create_table | 0 | 42 | Com_create_trigger | 0 | 43 | Com_create_udf | 0 | 44 | Com_create_user | 0 | 45 | Com_create_view | 0 | 46 | Com_dealloc_sql | 0 | 47 | Com_delete | 0 | 48 | Com_delete_multi | 0 | 49 | Com_do | 0 | 50 | Com_drop_db | 0 | 51 | Com_drop_event | 0 | 52 | Com_drop_function | 0 | 53 | Com_drop_index | 0 | 54 | Com_drop_procedure | 0 | 55 | Com_drop_server | 0 | 56 | Com_drop_table | 0 | 57 | Com_drop_trigger | 0 | 58 | Com_drop_user | 0 | 59 | Com_drop_view | 0 | 60 | Com_empty_query | 0 | 61 | Com_execute_sql | 0 | 62 | Com_explain_other | 0 | 63 | Com_flush | 0 | 64 | Com_get_diagnostics | 0 | 65 | Com_grant | 0 | 66 | Com_ha_close | 0 | 67 | Com_ha_open | 0 | 68 | Com_ha_read | 0 | 69 | Com_help | 0 | 70 | Com_insert | 0 | 71 | Com_insert_select | 0 | 72 | Com_install_plugin | 0 | 73 | Com_kill | 0 | 74 | Com_load | 0 | 75 | Com_lock_tables | 0 | 76 | Com_optimize | 0 | 77 | Com_preload_keys | 0 | 78 | Com_prepare_sql | 0 | 79 | Com_purge | 0 | 80 | Com_purge_before_date | 0 | 81 | Com_release_savepoint | 0 | 82 | Com_rename_table | 0 | 83 | Com_rename_user | 0 | 84 | Com_repair | 0 | 85 | Com_replace | 0 | 86 | Com_replace_select | 0 | 87 | Com_reset | 0 | 88 | Com_resignal | 0 | 89 | Com_revoke | 0 | 90 | Com_revoke_all | 0 | 91 | Com_rollback | 0 | 92 | Com_rollback_to_savepoint | 0 | 93 | Com_savepoint | 0 | 94 | Com_select | 2 | 95 | Com_set_option | 0 | 96 | Com_signal | 0 | 97 | Com_show_binlog_events | 0 | 98 | Com_show_binlogs | 0 | 99 | Com_show_charsets | 0 | 100 | Com_show_collations | 0 | 101 | Com_show_create_db | 0 | 102 | Com_show_create_event | 0 | 103 | Com_show_create_func | 0 | 104 | Com_show_create_proc | 0 | 105 | Com_show_create_table | 0 | 106 | Com_show_create_trigger | 0 | 107 | Com_show_databases | 2 | 108 | Com_show_engine_logs | 0 | 109 | Com_show_engine_mutex | 0 | 110 | Com_show_engine_status | 0 | 111 | Com_show_events | 0 | 112 | Com_show_errors | 0 | 113 | Com_show_fields | 9 | 114 | Com_show_function_code | 0 | 115 | Com_show_function_status | 0 | 116 | Com_show_grants | 0 | 117 | Com_show_keys | 0 | 118 | Com_show_master_status | 0 | 119 | Com_show_open_tables | 0 | 120 | Com_show_plugins | 0 | 121 | Com_show_privileges | 0 | 122 | Com_show_procedure_code | 0 | 123 | Com_show_procedure_status | 0 | 124 | Com_show_processlist | 0 | 125 | Com_show_profile | 0 | 126 | Com_show_profiles | 0 | 127 | Com_show_relaylog_events | 0 | 128 | Com_show_slave_hosts | 0 | 129 | Com_show_slave_status | 0 | 130 | Com_show_status | 1 | 131 | Com_show_storage_engines | 0 | 132 | Com_show_table_status | 0 | 133 | Com_show_tables | 2 | 134 | Com_show_triggers | 0 | 135 | Com_show_variables | 0 | 136 | Com_show_warnings | 0 | 137 | Com_show_create_user | 0 | 138 | Com_shutdown | 0 | 139 | Com_slave_start | 0 | 140 | Com_slave_stop | 0 | 141 | Com_group_replication_start | 0 | 142 | Com_group_replication_stop | 0 | 143 | Com_stmt_execute | 0 | 144 | Com_stmt_close | 0 | 145 | Com_stmt_fetch | 0 | 146 | Com_stmt_prepare | 0 | 147 | Com_stmt_reset | 0 | 148 | Com_stmt_send_long_data | 0 | 149 | Com_truncate | 0 | 150 | Com_uninstall_plugin | 0 | 151 | Com_unlock_tables | 0 | 152 | Com_update | 0 | 153 | Com_update_multi | 0 | 154 | Com_xa_commit | 0 | 155 | Com_xa_end | 0 | 156 | Com_xa_prepare | 0 | 157 | Com_xa_recover | 0 | 158 | Com_xa_rollback | 0 | 159 | Com_xa_start | 0 | 160 | Com_stmt_reprepare | 0 | 161 | Compression | OFF | 162 | Connection_errors_accept | 0 | 163 | Connection_errors_internal | 0 | 164 | Connection_errors_max_connections | 0 | 165 | Connection_errors_peer_address | 0 | 166 | Connection_errors_select | 0 | 167 | Connection_errors_tcpwrap | 0 | 168 | Connections | 23 | 169 | Created_tmp_disk_tables | 2 | 170 | Created_tmp_files | 6 | 171 | Created_tmp_tables | 6 | 172 | Delayed_errors | 0 | 173 | Delayed_insert_threads | 0 | 174 | Delayed_writes | 0 | 175 | Flush_commands | 1 | 176 | Handler_commit | 0 | 177 | Handler_delete | 0 | 178 | Handler_discover | 0 | 179 | Handler_external_lock | 0 | 180 | Handler_mrr_init | 0 | 181 | Handler_prepare | 0 | 182 | Handler_read_first | 2 | 183 | Handler_read_key | 2 | 184 | Handler_read_last | 0 | 185 | Handler_read_next | 0 | 186 | Handler_read_prev | 0 | 187 | Handler_read_rnd | 0 | 188 | Handler_read_rnd_next | 48 | 189 | Handler_rollback | 0 | 190 | Handler_savepoint | 0 | 191 | Handler_savepoint_rollback | 0 | 192 | Handler_update | 0 | 193 | Handler_write | 24 | 194 | Innodb_buffer_pool_dump_status | Dumping of buffer pool not started | 195 | Innodb_buffer_pool_load_status | Buffer pool(s) load completed at 200309 3:13:36 | 196 | Innodb_buffer_pool_resize_status | | 197 | Innodb_buffer_pool_pages_data | 357 | 198 | Innodb_buffer_pool_bytes_data | 5849088 | 199 | Innodb_buffer_pool_pages_dirty | 0 | 200 | Innodb_buffer_pool_bytes_dirty | 0 | 201 | Innodb_buffer_pool_pages_flushed | 140 | 202 | Innodb_buffer_pool_pages_free | 7834 | 203 | Innodb_buffer_pool_pages_misc | 0 | 204 | Innodb_buffer_pool_pages_total | 8191 | 205 | Innodb_buffer_pool_read_ahead_rnd | 0 | 206 | Innodb_buffer_pool_read_ahead | 0 | 207 | Innodb_buffer_pool_read_ahead_evicted | 0 | 208 | Innodb_buffer_pool_read_requests | 3292 | 209 | Innodb_buffer_pool_reads | 288 | 210 | Innodb_buffer_pool_wait_free | 0 | 211 | Innodb_buffer_pool_write_requests | 1525 | 212 | Innodb_data_fsyncs | 74 | 213 | Innodb_data_pending_fsyncs | 0 | 214 | Innodb_data_pending_reads | 0 | 215 | Innodb_data_pending_writes | 0 | 216 | Innodb_data_read | 4788736 | 217 | Innodb_data_reads | 320 | 218 | Innodb_data_writes | 216 | 219 | Innodb_data_written | 3826688 | 220 | Innodb_dblwr_pages_written | 85 | 221 | Innodb_dblwr_writes | 2 | 222 | Innodb_log_waits | 0 | 223 | Innodb_log_write_requests | 149 | 224 | Innodb_log_writes | 36 | 225 | Innodb_os_log_fsyncs | 40 | 226 | Innodb_os_log_pending_fsyncs | 0 | 227 | Innodb_os_log_pending_writes | 0 | 228 | Innodb_os_log_written | 138240 | 229 | Innodb_page_size | 16384 | 230 | Innodb_pages_created | 70 | 231 | Innodb_pages_read | 287 | 232 | Innodb_pages_written | 140 | 233 | Innodb_row_lock_current_waits | 0 | 234 | Innodb_row_lock_time | 0 | 235 | Innodb_row_lock_time_avg | 0 | 236 | Innodb_row_lock_time_max | 0 | 237 | Innodb_row_lock_waits | 0 | 238 | Innodb_rows_deleted | 0 | 239 | Innodb_rows_inserted | 211 | 240 | Innodb_rows_read | 195 | 241 | Innodb_rows_updated | 0 | 242 | Innodb_num_open_files | 30 | 243 | Innodb_truncated_status_writes | 0 | 244 | Innodb_available_undo_logs | 128 | 245 | Key_blocks_not_flushed | 0 | 246 | Key_blocks_unused | 6688 | 247 | Key_blocks_used | 10 | 248 | Key_read_requests | 254 | 249 | Key_reads | 5 | 250 | Key_write_requests | 159 | 251 | Key_writes | 43 | 252 | Last_query_cost | 1.399000 | 253 | Last_query_partial_plans | 1 | 254 | Locked_connects | 0 | 255 | Max_execution_time_exceeded | 0 | 256 | Max_execution_time_set | 0 | 257 | Max_execution_time_set_failed | 0 | 258 | Max_used_connections | 8 | 259 | Max_used_connections_time | 2020-03-09 20:17:25 | 260 | Not_flushed_delayed_rows | 0 | 261 | Ongoing_anonymous_transaction_count | 0 | 262 | Open_files | 49 | 263 | Open_streams | 0 | 264 | Open_table_definitions | 125 | 265 | Open_tables | 194 | 266 | Opened_files | 294 | 267 | Opened_table_definitions | 0 | 268 | Opened_tables | 7 | 269 | Performance_schema_accounts_lost | 0 | 270 | Performance_schema_cond_classes_lost | 0 | 271 | Performance_schema_cond_instances_lost | 0 | 272 | Performance_schema_digest_lost | 0 | 273 | Performance_schema_file_classes_lost | 0 | 274 | Performance_schema_file_handles_lost | 0 | 275 | Performance_schema_file_instances_lost | 0 | 276 | Performance_schema_hosts_lost | 0 | 277 | Performance_schema_index_stat_lost | 0 | 278 | Performance_schema_locker_lost | 0 | 279 | Performance_schema_memory_classes_lost | 0 | 280 | Performance_schema_metadata_lock_lost | 0 | 281 | Performance_schema_mutex_classes_lost | 0 | 282 | Performance_schema_mutex_instances_lost | 0 | 283 | Performance_schema_nested_statement_lost | 0 | 284 | Performance_schema_prepared_statements_lost | 0 | 285 | Performance_schema_program_lost | 0 | 286 | Performance_schema_rwlock_classes_lost | 0 | 287 | Performance_schema_rwlock_instances_lost | 0 | 288 | Performance_schema_session_connect_attrs_lost | 0 | 289 | Performance_schema_socket_classes_lost | 0 | 290 | Performance_schema_socket_instances_lost | 0 | 291 | Performance_schema_stage_classes_lost | 0 | 292 | Performance_schema_statement_classes_lost | 0 | 293 | Performance_schema_table_handles_lost | 0 | 294 | Performance_schema_table_instances_lost | 0 | 295 | Performance_schema_table_lock_stat_lost | 0 | 296 | Performance_schema_thread_classes_lost | 0 | 297 | Performance_schema_thread_instances_lost | 0 | 298 | Performance_schema_users_lost | 0 | 299 | Prepared_stmt_count | 0 | 300 | Qcache_free_blocks | 1 | 301 | Qcache_free_memory | 1031832 | 302 | Qcache_hits | 0 | 303 | Qcache_inserts | 0 | 304 | Qcache_lowmem_prunes | 0 | 305 | Qcache_not_cached | 24 | 306 | Qcache_queries_in_cache | 0 | 307 | Qcache_total_blocks | 1 | 308 | Queries | 290 | 309 | Questions | 17 | 310 | Select_full_join | 0 | 311 | Select_full_range_join | 0 | 312 | Select_range | 0 | 313 | Select_range_check | 0 | 314 | Select_scan | 6 | 315 | Slave_open_temp_tables | 0 | 316 | Slow_launch_threads | 0 | 317 | Slow_queries | 0 | 318 | Sort_merge_passes | 0 | 319 | Sort_range | 0 | 320 | Sort_rows | 0 | 321 | Sort_scan | 0 | 322 | Ssl_accept_renegotiates | 0 | 323 | Ssl_accepts | 0 | 324 | Ssl_callback_cache_hits | 0 | 325 | Ssl_cipher | | 326 | Ssl_cipher_list | | 327 | Ssl_client_connects | 0 | 328 | Ssl_connect_renegotiates | 0 | 329 | Ssl_ctx_verify_depth | 0 | 330 | Ssl_ctx_verify_mode | 0 | 331 | Ssl_default_timeout | 0 | 332 | Ssl_finished_accepts | 0 | 333 | Ssl_finished_connects | 0 | 334 | Ssl_server_not_after | Mar 7 10:13:34 2030 GMT | 335 | Ssl_server_not_before | Mar 9 10:13:34 2020 GMT | 336 | Ssl_session_cache_hits | 0 | 337 | Ssl_session_cache_misses | 0 | 338 | Ssl_session_cache_mode | Unknown | 339 | Ssl_session_cache_overflows | 0 | 340 | Ssl_session_cache_size | 0 | 341 | Ssl_session_cache_timeouts | 0 | 342 | Ssl_sessions_reused | 0 | 343 | Ssl_used_session_cache_entries | 0 | 344 | Ssl_verify_depth | 0 | 345 | Ssl_verify_mode | 0 | 346 | Ssl_version | | 347 | Table_locks_immediate | 145 | 348 | Table_locks_waited | 0 | 349 | Table_open_cache_hits | 2 | 350 | Table_open_cache_misses | 7 | 351 | Table_open_cache_overflows | 0 | 352 | Tc_log_max_pages_used | 0 | 353 | Tc_log_page_size | 0 | 354 | Tc_log_page_waits | 0 | 355 | Threads_cached | 0 | 356 | Threads_connected | 8 | 357 | Threads_created | 8 | 358 | Threads_running | 1 | 359 | Uptime | 62493 | 360 | Uptime_since_flush_status | 62493 | 361 | validate_password_dictionary_file_last_parsed | 2020-03-09 03:13:36 | 362 | validate_password_dictionary_file_words_count | 0 | 363 +-----------------------------------------------+--------------------------------------------------+ 364 358 rows in set (0.00 sec)
SHOW CREATE DATABASE和SHOW CREATE TABLE,分别用来显示创建特定数据库或表的MySQL语句;
mysql> show create learn_mysql; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'learn_mysql' at line 1 mysql> show create database learn_mysql; +-------------+----------------------------------------------------------------------+ | Database | Create Database | +-------------+----------------------------------------------------------------------+ | learn_mysql | CREATE DATABASE `learn_mysql` /*!40100 DEFAULT CHARACTER SET utf8 */ | +-------------+----------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> show tables; +-----------------------+ | Tables_in_learn_mysql | +-----------------------+ | customers | | customers2 | | orderitems | | orders | | productnotes | | products | | vendors | +-----------------------+ 7 rows in set (0.00 sec) mysql> show create table products; +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | products | CREATE TABLE `products` ( `prod_id` char(10) NOT NULL, `vend_id` int(11) NOT NULL, `prod_name` char(255) NOT NULL, `prod_price` decimal(8,2) NOT NULL, `prod_desc` text, PRIMARY KEY (`prod_id`), KEY `fk_products_vendors` (`vend_id`), CONSTRAINT `fk_products_vendors` FOREIGN KEY (`vend_id`) REFERENCES `vendors` (`vend_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
mysql> show grants; +-------------------------------------------------------------+ | Grants for root@% | +-------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION | +-------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
mysql> show errors; Empty set (0.00 sec) mysql> show warmings; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warmings' at line 1 mysql> show warnings;测试了下,好像只显示最新的一条数据。 +-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Error | 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'warmings' at line 1 | +-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
select 列名 from 表名;
mysql> select prod_name from products; +----------------+ | prod_name | +----------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | Detonator | | Bird seed | | Carrots | | Fuses | | JetPack 1000 | | JetPack 2000 | | Oil can | | Safe | | Sling | | TNT (1 stick) | | TNT (5 sticks) | +----------------+ 14 rows in set (0.00 sec)
PS:如果没有明确排序查询结果,则返回的数据的排序没有特殊意义。返回数据的顺序可能是数据被添加到表中的顺序,也可能不是。只要返回相同数目的行,就是正常的。
多条SQL语句必须以分号(;)分隔。
View Code
SQL语句不区分大小写,因此SELECT与select是相同的。同样,写成Select也没有关系。许多SQL开发人员喜欢对所有SQL关键字使用大写,而对所有列和表名使用小写,这样做使代码更易于阅读和调试。
View Code
2、检索多个列select 列名, 列名, 列名 from 表名;
mysql> select prod_id,prod_name,prod_price from products; +---------+----------------+------------+ | prod_id | prod_name | prod_price | +---------+----------------+------------+ | ANV01 | .5 ton anvil | 5.99 | | ANV02 | 1 ton anvil | 9.99 | | ANV03 | 2 ton anvil | 14.99 | | DTNTR | Detonator | 13.00 | | FB | Bird seed | 10.00 | | FC | Carrots | 2.50 | | FU1 | Fuses | 3.42 | | JP1000 | JetPack 1000 | 35.00 | | JP2000 | JetPack 2000 | 55.00 | | OL1 | Oil can | 8.99 | | SAFE | Safe | 50.00 | | SLING | Sling | 4.49 | | TNT1 | TNT (1 stick) | 2.50 | | TNT2 | TNT (5 sticks) | 10.00 | +---------+----------------+------------+ 14 rows in set (0.00 sec)
select * from 表名;检索不需要的列通常会降低检索和应用程序的性能。
mysql> select * from products; +---------+---------+----------------+------------+----------------------------------------------------------------+ | prod_id | vend_id | prod_name | prod_price | prod_desc | +---------+---------+----------------+------------+----------------------------------------------------------------+ | ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook | | ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case | | ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case | | DTNTR | 1003 | Detonator | 13.00 | Detonator (plunger powered), fuses not included | | FB | 1003 | Bird seed | 10.00 | Large bag (suitable for road runners) | | FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) | | FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long | | JP1000 | 1005 | JetPack 1000 | 35.00 | JetPack 1000, intended for single use | | JP2000 | 1005 | JetPack 2000 | 55.00 | JetPack 2000, multi-use | | OL1 | 1002 | Oil can | 8.99 | Oil can, red | | SAFE | 1003 | Safe | 50.00 | Safe with combination lock | | SLING | 1003 | Sling | 4.49 | Sling, one size fits all | | TNT1 | 1003 | TNT (1 stick) | 2.50 | TNT, red, single stick | | TNT2 | 1003 | TNT (5 sticks) | 10.00 | TNT, red, pack of 10 sticks | +---------+---------+----------------+------------+----------------------------------------------------------------+ 14 rows in set (0.00 sec)
select distinct 列名 from 表名;
mysql> select distinct vend_id from products; +---------+ | vend_id | +---------+ | 1001 | | 1002 | | 1003 | | 1005 | +---------+ 4 rows in set (0.00 sec)
distinct只能放在所有列前,且只用于select语句,当有多个列时,distinct是应用到所有列,而不仅仅紧挨在后面的一列。
用LIMIT子句可以展示返回数据指定行,比如第一行或前几行
行0 检索出来的第一行为行0而不是行1。因此,LIMIT 1, 1将检索出第二行而不是第一行。
mysql> select cust_name,cust_address from customers;#共5行 +----------------+---------------------+ | cust_name | cust_address | +----------------+---------------------+ | Coyote Inc. | 200 Maple Lane | | Mouse House | 333 Fromage Lane | | Wascals | 1 Sunny Place | | Yosemite Place | 829 Riverside Drive | | E Fudd | 4545 53rd Street | +----------------+---------------------+ 5 rows in set (0.00 sec) mysql> select cust_name,cust_address from customers limit 2;#显示前两行。 +-------------+------------------+ | cust_name | cust_address | +-------------+------------------+ | Coyote Inc. | 200 Maple Lane | | Mouse House | 333 Fromage Lane | +-------------+------------------+ 2 rows in set (0.00 sec) mysql> select cust_name,cust_address from customers limit (2,3); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(2,3)' at line 1 mysql> select cust_name,cust_address from customers limit 2,4;#表示从行2开始的4行,不过行数不过,mysql只返回它能返回的那么多行
+----------------+---------------------+ | cust_name | cust_address | +----------------+---------------------+ | Wascals | 1 Sunny Place | | Yosemite Place | 829 Riverside Drive | | E Fudd | 4545 53rd Street | +----------------+---------------------+ 3 rows in set (0.00 sec) mysql> select cust_name,cust_address from customers limit 2,3;#表示从行2开始的3行 +----------------+---------------------+ | cust_name | cust_address | +----------------+---------------------+ | Wascals | 1 Sunny Place | | Yosemite Place | 829 Riverside Drive | | E Fudd | 4545 53rd Street | +----------------+---------------------+ 3 rows in set (0.00 sec) mysql> select cust_name,cust_address from customers limit 2,2;#表示从行2开始的2行 +----------------+---------------------+ | cust_name | cust_address | +----------------+---------------------+ | Wascals | 1 Sunny Place | | Yosemite Place | 829 Riverside Drive | +----------------+---------------------+ 2 rows in set (0.00 sec) mysql> select cust_name,cust_address from customers limit 3 offset 2;#表示从行2开始的3行,和limit 2,3一样
+----------------+---------------------+
| cust_name | cust_address |
+----------------+---------------------+
| Wascals | 1 Sunny Place |
| Yosemite Place | 829 Riverside Drive |
| E Fudd | 4545 53rd Street |
+----------------+---------------------+
3 rows in set (0.00 sec)