MySQL系列:資料庫基本操作(1)

libingql發表於2017-02-10

1. 登入資料庫

mysql -h localhost -u root -p

2. 資料庫基本操作

2.1 檢視資料庫

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
6 rows in set (0.00 sec)

2.2 建立資料庫

mysql> CREATE DATABASE db;
mysql> CREATE DATABASE IF NOT EXISTS db;

  指定字符集:

mysql> CREATE DATABASE db CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

2.3 檢視建立資料庫定義

mysql> SHOW CREATE DATABASE db \G;
*************************** 1. row ***************************
       Database: db
Create Database: CREATE DATABASE `db` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)

2.4 使用資料庫

mysql> USE db;

2.5 顯示使用的資料庫

mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| db         |
+------------+

2.6 刪除資料庫

mysql> DROP DATABASE db;

3. 檢視資料庫系統支援的儲存引擎型別

mysql> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

  其中,Support中DEFAULT表示預設儲存引擎。

3.1 InnoDB儲存引擎

  InnoDB事務型資料庫的首選引擎,支援事務安全表(ACID),支援行鎖和外來鍵。MySQL5.5.5之後,InnoDB作為預設儲存引擎。

  InnoDB儲存引擎中,建立的表的表結構儲存在.frm檔案中資料和索引儲存在innodb_data_home_dir和innodb_data_file_path定義的表空間中

  使用InnoDB時,MySQL將在資料目錄下建立ibdata1的自動擴充套件資料檔案,以及兩個ib_logfile0和ib_logfile1的日誌檔案。

  innodb_data_file_path:指定innodb tablespace檔案。如果不在my.ini檔案中指定innodb_data_home_dir和innodb_data_file_path,則預設會在datadir目錄下建立ibdata1 作為innodb tablespace。

[mysqld]

#Path to the database root
datadir="C:/ProgramData/MySQL/MySQL Server 5.6/Data/"

  查詢innodb_data_home_dir和innodb_data_file_path引數設定:

mysql> SHOW VARIABLES LIKE 'innodb_data%';
+-----------------------+------------------------+
| Variable_name         | Value                  |
+-----------------------+------------------------+
| innodb_data_file_path | ibdata1:12M:autoextend |
| innodb_data_home_dir  |                        |
+-----------------------+------------------------+

  my.ini中設定innodb_data_home_dir和innodb_data_file_path引數:

[mysqld]

innodb_data_home_dir = /data/3306
innodb_data_file_path = ibdata1:12M:autoextend:max:1G

3.2 MyISAM儲存引擎

  MyISAM具有較高的插入、查詢速度,但不支援事務。MySQL5.5.5之前為預設儲存引擎。

  MyISAM儲存引擎的表儲存成3個檔案,檔名稱與表名相同,副檔名包括frm、MYD和MYI。其中,frm副檔名檔案儲存表的結構,MYD副檔名檔案儲存資料(MYData縮寫),MYI副檔名檔案儲存索引(MYIndex縮寫)。

  MyISAM儲存引擎的優點:佔用空間小,處理速度快;缺點:不支援實務的完整性和併發性。

4. 查詢預設儲存引擎

mysql> show variables like 'storage_engine';
+----------------+--------+
| Variable_name  | Value  |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+
1 row in set (0.00 sec)

  可以在my.ini中修改預設儲存引擎,將“default-storage-engine=INNODB”改為“default-storage-engine=MyISAM”,重啟服務,修改生效。

5. 檢視資料庫字符集

mysql> SELECT DISTINCT table_schema,table_collation
    -> FROM information_schema. TABLES
    -> WHERE table_schema NOT IN ('performance_schema', 'sys', 'test', 'mysql', 'information_schema');
+--------------+-----------------+
| table_schema | table_collation |
+--------------+-----------------+
| db           | utf8_general_ci |
+--------------+-----------------+

相關文章