後臺開發學習筆記(十九、mysql基本使用)

其實我現在也不是很清楚mysql的基本使用,不過學習嘛,就是通過不會學到會,這樣纔有信心和激情,接下來一起探索一下mysql的基本使用,哪裏有不對的地方,歡迎留言交流。

首先先推舉兩篇博客,這兩篇博客看着很不錯,感覺寫的很好,學習的過程就是不斷像前面的人學習。

debian配置mysql,建立數據庫和表
debian9 安裝任意版本mysql

19.1 mysql常用操作

雖然嘗試了一下想跳過這些命令的,結果發現還是不能跳過,還是要老老實實的學學這些重用操作。

19.1.1 顯示數據庫

mysql> show databases;   #要加s
+--------------------+
| Database           |
+--------------------+
| aaa                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

mysql剛開始就有幾個數據庫了,那個aaa是我自己創建的,還沒刪除不要在意這些細節。

19.1.2 顯示數據庫中的表

mysql> use sys
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> 

再顯示數據庫中的表的時候,要先選擇使用的庫,因爲不選擇的話,都不知道使用的是哪個庫,所以這裏使用了use命令選擇庫。

mysql> show tables;
+-----------------------------------------------+
| Tables_in_sys                                 |
+-----------------------------------------------+
| host_summary                                  |
| host_summary_by_file_io                       |
| host_summary_by_file_io_type                  |
| host_summary_by_stages                        |
| host_summary_by_statement_latency             |
| host_summary_by_statement_type                |
| innodb_buffer_stats_by_schema                 |
| innodb_buffer_stats_by_table                  |
| innodb_lock_waits                             |
| io_by_thread_by_latency                       |
| io_global_by_file_by_bytes                    |
| io_global_by_file_by_latency                  |
| io_global_by_wait_by_bytes                    |
| io_global_by_wait_by_latency                  |
| latest_file_io                                |
| memory_by_host_by_current_bytes               |
| memory_by_thread_by_current_bytes             |
| memory_by_user_by_current_bytes               |
| memory_global_by_current_bytes                |
| memory_global_total                           |
| metrics                                       |
| processlist                                   |
| ps_check_lost_instrumentation                 |
| schema_auto_increment_columns                 |
| schema_index_statistics                       |
| schema_object_overview                        |
| schema_redundant_indexes                      |
| schema_table_lock_waits                       |
| schema_table_statistics                       |
| schema_table_statistics_with_buffer           |
| schema_tables_with_full_table_scans           |
| schema_unused_indexes                         |
| session                                       |
| session_ssl_status                            |
| statement_analysis                            |
| statements_with_errors_or_warnings            |
| statements_with_full_table_scans              |
| statements_with_runtimes_in_95th_percentile   |
| statements_with_sorting                       |
| statements_with_temp_tables                   |
| sys_config                                    |
| user_summary                                  |
| user_summary_by_file_io                       |
| user_summary_by_file_io_type                  |
| user_summary_by_stages                        |
| user_summary_by_statement_latency             |
| user_summary_by_statement_type                |
| version                                       |
| wait_classes_global_by_avg_latency            |
| wait_classes_global_by_latency                |
| waits_by_host_by_latency                      |
| waits_by_user_by_latency                      |
| waits_global_by_latency                       |
| x$host_summary                                |
| x$host_summary_by_file_io                     |
| x$host_summary_by_file_io_type                |
| x$host_summary_by_stages                      |
| x$host_summary_by_statement_latency           |
| x$host_summary_by_statement_type              |
| x$innodb_buffer_stats_by_schema               |
| x$innodb_buffer_stats_by_table                |
| x$innodb_lock_waits                           |
| x$io_by_thread_by_latency                     |
| x$io_global_by_file_by_bytes                  |
| x$io_global_by_file_by_latency                |
| x$io_global_by_wait_by_bytes                  |
| x$io_global_by_wait_by_latency                |
| x$latest_file_io                              |
| x$memory_by_host_by_current_bytes             |
| x$memory_by_thread_by_current_bytes           |
| x$memory_by_user_by_current_bytes             |
| x$memory_global_by_current_bytes              |
| x$memory_global_total                         |
| x$processlist                                 |
| x$ps_digest_95th_percentile_by_avg_us         |
| x$ps_digest_avg_latency_distribution          |
| x$ps_schema_table_statistics_io               |
| x$schema_flattened_keys                       |
| x$schema_index_statistics                     |
| x$schema_table_lock_waits                     |
| x$schema_table_statistics                     |
| x$schema_table_statistics_with_buffer         |
| x$schema_tables_with_full_table_scans         |
| x$session                                     |
| x$statement_analysis                          |
| x$statements_with_errors_or_warnings          |
| x$statements_with_full_table_scans            |
| x$statements_with_runtimes_in_95th_percentile |
| x$statements_with_sorting                     |
| x$statements_with_temp_tables                 |
| x$user_summary                                |
| x$user_summary_by_file_io                     |
| x$user_summary_by_file_io_type                |
| x$user_summary_by_stages                      |
| x$user_summary_by_statement_latency           |
| x$user_summary_by_statement_type              |
| x$wait_classes_global_by_avg_latency          |
| x$wait_classes_global_by_latency              |
| x$waits_by_host_by_latency                    |
| x$waits_by_user_by_latency                    |
| x$waits_global_by_latency                     |
+-----------------------------------------------+
101 rows in set (0.00 sec)

mysql> 

選擇了數據庫了之後,就是顯示錶了,還是用show命令,加tables(記得有一個s),即可顯示數據庫中的表。

19.1.3 顯示數據表的結構

mysql> describe host_summary;
+------------------------+---------------+------+-----+---------+-------+
| Field                  | Type          | Null | Key | Default | Extra |
+------------------------+---------------+------+-----+---------+-------+
| host                   | varchar(255)  | YES  |     | NULL    |       |
| statements             | decimal(64,0) | YES  |     | NULL    |       |
| statement_latency      | varchar(11)   | YES  |     | NULL    |       |
| statement_avg_latency  | varchar(11)   | YES  |     | NULL    |       |
| table_scans            | decimal(65,0) | YES  |     | NULL    |       |
| file_ios               | decimal(64,0) | YES  |     | NULL    |       |
| file_io_latency        | varchar(11)   | YES  |     | NULL    |       |
| current_connections    | decimal(41,0) | YES  |     | NULL    |       |
| total_connections      | decimal(41,0) | YES  |     | NULL    |       |
| unique_users           | bigint        | NO   |     | 0       |       |
| current_memory         | varchar(11)   | YES  |     | NULL    |       |
| total_memory_allocated | varchar(11)   | YES  |     | NULL    |       |
+------------------------+---------------+------+-----+---------+-------+
12 rows in set (0.01 sec)

這個命令就很好的看到了表中的數據了。(還有另一個select命令,但是感覺用的不怎麼樣,這裏就不寫了)

19.1.4 顯示錶中的記錄

當初沒加這個,是因爲看了一些感覺是亂碼所以不加,到之後才發現自己添加的才靠譜。
select * from 表名;
例如:顯示mysql庫中user表中的紀錄。所有能對MySQL用戶操作的用戶都在此表中。

mysql> select * from ren;
+----+--------+
| id | xm     |
+----+--------+
|  1 | nihao  |
|  2 | 賬號   |
+----+--------+
2 rows in set (0.00 sec)

這個多好,id是第一列,目前有兩條
xm,姓名 也有兩個

19.1.5 創建數據庫

mysql> create database chen;
Query OK, 1 row affected (0.02 sec)

mysql> 

創建數據庫,chen是數據庫的名字,這時候的database沒有s,我們可以用show databases來查看:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| chen               |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

是不是就看到chen這個表。

19.1.6 建表

操作表的時候記得選擇數據庫。

mysql> use chen;
Database changed
mysql> create table ren(id int(3) auto_increment not null primary key, xm char(8));
Query OK, 0 rows affected, 1 warning (0.10 sec)

use 庫名;
create table 表名 (字段設定列表);
例如:在剛創建的aaa庫中建立表name,表中有id(序號,自動增長),xm(姓名)二個字段。

創建表的時候,需要添加一個字段設置信息,因爲我們是存儲數據的,數據肯定有一些字段的。
可以通過describe查看

mysql> describe ren;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int     | NO   | PRI | NULL    | auto_increment |
| xm    | char(8) | YES  |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> 

我這裏剛剛就是創建了兩個字段。

19.1.7 增加記錄

例如:增加幾條相關紀錄。

mysql> insert into ren(id,xm) values (0,'賬號');
Query OK, 1 row affected (0.02 sec)

這樣插入,終於找到了正常的方法了,搞了半天。
select方式查看,就在上面了,就是這時候查看了感覺不多才加的。

19.1.8 修改紀錄

有增加就有修改,來看看mysql的修改命令:

mysql> update ren set xm='張三' where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> 

set是修改部分,where是修改那個項,我現在爲了方便,就是添加了兩項,沒辦法只能用id做判斷了。
這次也可以用select來判斷。

19.1.9 刪除紀錄

終於到刪了,到了刪除,基本就快完結了。

mysql> delete from ren where xm='張三';
Query OK, 1 row affected (0.01 sec)
mysql> 

一刪就準,這是刪除表中的一個記錄而已。

19.1.10 刪庫和刪表

drop database 庫名;
drop table 表名;
刪除數據庫和刪除表的命令是一樣的:

mysql> drop table ren;
Query OK, 0 rows affected (0.08 sec)
mysql> drop database chen;
Query OK, 0 rows affected (0.04 sec)

刪除比較簡單,就這樣吧。

感覺這一篇沒啥技術含量,並且都是借鑑別人的,不過相對於我這種初涉數據庫的人來說,有了這一篇的操作,下次再用命令行操作mysql就不會那麼懵逼了,明天繼續學習mysql,計劃明天可以用代碼來代替這些命令,希望明天順利。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章