MySQL基础学习数据库查询创建修改删除以及使用

查询数据库:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+--------------------+
6 rows in set (0.03 sec)

查询创建数据库命令以及其默认字符集:

mysql> show create database school;
+----------+--------------------------------------------------------------------
--------------------------------+
| Database | Create Database
|
+----------+--------------------------------------------------------------------
--------------------------------+
| school | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!
80016 DEFAULT ENCRYPTION='N' */ |
+----------+--------------------------------------------------------------------
--------------------------------+
1 row in set (0.00 sec)

创建数据库:

mysql> create database data_view;
Query OK, 1 row affected (0.41 sec)

已经存在的数据库不能重复创建,会报错:

mysql> create database data_view;
ERROR 1007 (HY000): Can't create database 'data_view'; database exists

利用判断是否存在创建数据库就不会报错:

有就不创建,没有就会创建

mysql> create database if not exists data_view;
Query OK, 1 row affected, 1 warning (0.23 sec)

创建数据库时指定字符集:

比如:gbk

mysql> create database if not exists data_test character set gbk;
Query OK, 1 row affected (0.38 sec)

此时查看下刚创建数据库的字符集已经变成gbk:

mysql> show create database data_test;
+-----------+-------------------------------------------------------------------
-----------------------------------+
| Database | Create Database
|
+-----------+-------------------------------------------------------------------
-----------------------------------+
| data_test | CREATE DATABASE `data_test` /*!40100 DEFAULT CHARACTER SET gbk */
/*!80016 DEFAULT ENCRYPTION='N' */ |
+-----------+-------------------------------------------------------------------
-----------------------------------+
1 row in set (0.00 sec)

修改数据库字符集:

mysql> alter database data_test character set utf8;
Query OK, 1 row affected, 1 warning (0.43 sec)

删除数据库(危险方式):

mysql> drop database if exists data_test;
Query OK, 0 rows affected (0.32 sec)

查看下是否还有该数据库

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| data_view |
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+--------------------+
7 rows in set (0.00 sec)

数据库的使用:

使用之前先查看一下当前使用的数据库:

mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)

null表示当前未使用使用数据库;

使用数据库

mysql> use data_view;
Database changed

查看一下当前使用的数据库:

mysql> select database();
+------------+
| database() |
+------------+
| data_view |
+------------+
1 row in set (0.00 sec)

posted @ 2020-04-15 01:24  ls93559  阅读(215)  评论(0编辑  收藏  举报