MYSQL 数据库操作<二>

SQL分类

DDL (数据定义语句)
  数据定义语言 - Data Definition Language
  用来定义数据库的对象,如数据表、视图、索引等
DML  (数据操纵语句)
  数据处理语言 - Data Manipulation Language
  在数据库表中更新,增加和删除记录
  如 update, insert, delete --- 增删改
DCL (数据控制语句)
  数据控制语言 – Data Control Language
  指用于设置用户权限和控制事务语句
  如grant,revoke,if…else,while,begin transaction
DQL (数据查询语句)
  数据查询语言 – Data Query Language
  select

数据库操作

  • 1.创建数据库
方法一:
   create database 数据库名字;
   例如: create database day06;

方法二:
   create database 数据库名字 character set 字符集;
   例如: create database day06_1 character set utf8;

方法三:
   create database 数据库名字 character set 字符集 collate 校对规则;
   例如: create database day06_2 character set utf8 collate utf8_bin;

创建成功打印如下:

Query OK, 1 row affected, 1 warning (0.05 sec)

  • 2.查看数据库
查看所有数据库
  show databases;    

查看数据库定义的语句
  show create database 数据库名字;
  例如:show create database day06;
  例如:show create database day06_1;

查询成功打印如下:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| day06 |
| day06_1 |
| day06_2 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)

+----------+----------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------------------------------------+
| day06 | CREATE DATABASE day06 /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ |
+----------+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

  • 3.修改数据库操作
修改数据的字符集
  alter database 数据库的名字 character set 字符集;
  例如:  alter database day06_1 character set gbk;

修改前打印如下:

mysql> mysql> show create database day06_1;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| day06_1 | CREATE DATABASE day06_1 /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)

修改

mysql> alter database day06_1 character set gbk;
Query OK, 1 row affected (0.06 sec)

修改后打印如下:

mysql> show create database day06_1;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| day06_1 | CREATE DATABASE day06_1 /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

  • 4.删除数据库
drop database 数据库名字;
例如:  drop database day06_1;

打印如下:

删除数据库

mysql> drop database day06_1;
Query OK, 0 rows affected (0.04 sec)

查询数据库,day06_1表示已经删除

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| day06 |
| day06_2 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.00 sec)

  • 5.其他数据库操作
use 数据库名字;
例如: use day06;

查看一下当前正在使用的数据库
例如:select database();

打印如下:

mysql> use day06;
Database changed

mysql> select database();
+------------+
| database() |
+------------+
| day06 |
+------------+
1 row in set (0.01 sec)

你可能感兴趣的:(MYSQL 数据库操作<二>)