mysql基础教程 -- 数据库的基本操作

实验环境

mysql版本:5.7.22
操作系统:ubuntu-16.04-x64

创建数据库

命令格式:
CREATE DATABASE db_name
db_name为要创建的数据库名。
例:

mysql> CREATE DATABASE school;                        │                                                     
Query OK, 1 row affected (0.00 sec)                   │                                                     
                                                      │                                                     
mysql> SHOW DATABASES;                                │                                                     
+--------------------+                                │                                                     
| Database           |                                │                                                     
+--------------------+                                │                                                     
| information_schema |                                │                                                     
| mysql              |                                │                                                     
| performance_schema |                                │                                                     
| school             |                                │                                                     
| sys                |                                │                                                     
| test               |                                │                                                     
| test1              |                                │                                                     
+--------------------+                                │                                                     
7 rows in set (0.00 sec)          

查看数据库

  • 切换数据库
    命令格式:USE db_name
    db_name为要切换的数据库名
    例:

    mysql> USE test;                                                         
    Database changed
    
  • 查看所有数据库
    命令格式:SHOW DATABASES;
    例:

    mysql> SHOW DATABASES;                                                  
    +--------------------+                                │                                                     
    | Database           |                                │                                                     
    +--------------------+                                │                                                     
    | information_schema |                                │                                                     
    | mysql              |                                │                                                     
    | performance_schema |                                │                                                     
    | sys                |                                │                                                     
    | test               |                                │                                                     
    | test1              |                                │                                                     
    +--------------------+                                │                                                     
    6 rows in set (0.00 sec) 
    
  • 查看数据创建语句
    SHOW CREATE DATABASE db_name
    db_name为要查看的数据库名
    例:

    mysql> SHOW CREATE DATABASE test;                                               
    +----------+------------------------------------------│                                                     
    -----------------------+                              │                                                     
    | Database | Create Database                          │                                                     
                           |                              │                                                     
    +----------+------------------------------------------│                                                     
    -----------------------+                              │                                                     
    | test     | CREATE DATABASE `test` /*!40100 DEFAULT C│                                                     
    HARACTER SET latin1 */ |                              │                                                     
    +----------+------------------------------------------│                                                     
    -----------------------+                              │                                                     
    1 row in set (0.00 sec)
    
  • 查看当前使用的数据库
    命令格式:SELECT DATABASE()
    例:

    mysql> SELECT DATABASE();                             
    +------------+                                        │                                                     
    | DATABASE() |                                        │                                                     
    +------------+                                        │                                                     
    | test       |                                        │                                                     
    +------------+                                        │                                                     
    1 row in set (0.00 sec)  
    

删除数据库

命令格式:
DROP DATABASE db_name
db_name为要删除的数据库名。
例:

mysql> DROP DATABASE school;                                                
Query OK, 0 rows affected (0.00 sec)

存储引擎

  • 查看数据库支持的所有存储引擎
    命令格式:SHOW ENGINES
    例:

     mysql> SHOW ENGINES;
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    | Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    | MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
    | CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
    | MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
    | BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
    | PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
    | MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
    | ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
    | InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
    | FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
    +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    9 rows in set (0.00 sec)
    
  • 查看默认存储引擎
    命令格式:SHOW VARIABLES LIKE ‘%storage_engine%’;
    例:

    mysql> SHOW VARIABLES LIKE '%storage_engine%';                                                              
    +----------------------------------+--------+
    | Variable_name                    | Value  |
    +----------------------------------+--------+
    | default_storage_engine           | InnoDB |
    | default_tmp_storage_engine       | InnoDB |
    | disabled_storage_engines         |        |
    | internal_tmp_disk_storage_engine | InnoDB |
    +----------------------------------+--------+
    4 rows in set (0.00 sec)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值