当前位置:网站首页>数据库系统原理与应用教程(021)—— MySQL 的数据库操作
数据库系统原理与应用教程(021)—— MySQL 的数据库操作
2022-07-16 08:34:00 【睿思达DBA_WGX】
数据库系统原理与应用教程(021)—— MySQL 的数据库操作
数据库是一个专门存储数据对象的容器,包括:数据表、视图、触发器、存储过程等,其中数据表(table)是最基本的数据对象,所有的数据都存放在数据表中。在创建任何数据对象之前,都要先创建数据库,然后在数据库中创建所需要的各种对象。
一、创建数据库的命令
使用 CREATE DATABASE 语句创建数据库,语法格式如下:
CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>] [[DEFAULT] COLLATE <校对规则名>];
/* 说明: (1)[] 中的内容是可选的。 (2)<数据库名>:要创建数据库的名称。数据库名不能和一个已经存在的数据库同名,MySQL 不允许在同一个系统中创建两个相同名称的数据库。 (3)IF NOT EXISTS:只有该数据库不存在时才能执行创建操作,可以避免因为数据库已经存在而引起错误。 (4)[DEFAULT] CHARACTER SET:指定数据库的默认字符集。字符集是用来定义 MySQL 存储字符串的方式。该选项可以省略,如果省略就采用配置文件中指定的字符集。 (5)[DEFAULT] COLLATE:指定字符集的默认校对规则。校对规则用来定义比较字符串的方式,以解决排序和字符分组的问题。该选项可以省略,如果省略就采用配置文件中指定的校对规则。 */
1、查看 MySQL 默认的字符集和校对规则
-- 查看系统默认的字符集
mysql> show variables like 'char%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
-- 查看系统默认的校对规则
mysql> show variables like 'COLL%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
2、查看 MySQL 支持的字符集及校对规则
使用 show character set 命令可以查看 MySQL 支持的字符集及校对规则。
mysql> show character set;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.11 sec)
3、创建数据库 mydb(不指定字符集和校对规则)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> create database mydb;
Query OK, 1 row affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
-- 使用 MySQL 默认的字符集,即:character_set_database 参数对应的字符集。
mysql> show create database mydb;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| mydb | CREATE DATABASE `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
4、创建数据库 wgx(指定字符集和校对规则)
mysql> create database wgx character set latin1 collate latin1_general_ci;
Query OK, 1 row affected (2.18 sec)
mysql> show create database wgx;
+----------+--------------------------------------------------------------------------------+
| Database | Create Database +----------+------------------------------------------------------------------------------+
| wgx | CREATE DATABASE `wgx` /*!40100 DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci */
+----------+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
| wgx |
+--------------------+
6 rows in set (0.00 sec)
二、查看 MySQL 数据库
使用 show databases 命令可以查看已创建的数据库有哪些,命令如下:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
| wgx |
+--------------------+
6 rows in set (0.00 sec)
三、修改数据库
使用 alter database 命令可以修改数据库的属性,主要修改数据库的字符集和校对规则,该命令中各选项的含义和 create database 相同。语法如下:
ALTER DATABASE [数据库名]
[ DEFAULT ] CHARACTER SET <字符集名> |
[ DEFAULT ] COLLATE <校对规则名>
把 wgx 数据库的字符集修改为 gbk,校对规则修改为 gbk_chinese_ci。
mysql> alter database wgx default character set gbk default collate gbk_chinese_ci;
Query OK, 1 row affected (0.01 sec)
mysql> show create database wgx;
+----------+-------------------------------------------------------------+
| Database | Create Database |
+----------+-------------------------------------------------------------+
| wgx | CREATE DATABASE `wgx` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)
四、选择数据库
由于表、视图等对象必须包含到某个数据库中,因此在创建对象和查看之前必须先选择对象所在的数据库。选择数据库的语法如下:
use 数据库名;
选择数据库 wgx,在数据库中创建表 t1:
mysql> use mydb;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.29 sec)
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
五、删除数据库
删除数据库使用 drop database 命令,语法如下:
drop database 数据库名;
删除数据库 wgx:
mysql> drop database wgx;
Query OK, 0 rows affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
边栏推荐
- 软件测试面试:请说一下你工作中发现的最有价值的bug?
- HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life
- Use tcpkill to block packets of the specified TCP connection
- A Zuo's aspiration
- 深度学习中一些注意力机制的介绍以及pytorch代码实现
- Runtime data area & method area (permanent generation / meta space) & stack frame
- Defi中的关键 智能合约是什么?
- 464 sword finger offer (35, 05, 58, 03)
- A lightweight tracking method based on unique identifier of timestamp
- How to set up domain name resolution?
猜你喜欢
![[training Day2] cinema ticket [combinatorics] [Cartland number]](/img/39/a87cef4d6dc1b393b78f7fa4f71be1.png)
[training Day2] cinema ticket [combinatorics] [Cartland number]

在okcc中你了解呼叫并发的含义吗?

465-剑指offer(53-I、53-II、04、50)

How to add PTZ control to the easycvr video retrieval page?

APISIX 如何与 Hydra 集成,搭建集中认证网关助力企业安全

NFT交易平台竞争格局:核心竞争力是什么?

Introduction of some attention mechanisms in deep learning and implementation of pytorch code

Example of declarative transaction management

可再生金融ReFi:提供对地球有利的技术和金融系统

AutoJs学习-应用列表
随机推荐
Entrevue de Test logiciel: Pourriez - vous me dire quel est le bogue le plus précieux que vous avez trouvé dans votre travail?
Class loader & parental delegation mechanism & breaking parental delegation mechanism
APISIX 如何与 Hydra 集成,搭建集中认证网关助力企业安全
軟件測試面試:請說一下你工作中發現的最有價值的bug?
Redis cluster test
The problem and solution of calling glcreateshader to crash
What does SPK interface mean
A lightweight tracking method based on unique identifier of timestamp
ReFi夏季升温:Uniswap v3和绿色资产池在Celo上启动
AcWing 133. earthworm
Do you know the meaning of call concurrency in okcc?
如何利用无常损失从流动资金池中提取价值
What are the differences between free SSL certificates and paid SSL certificates?
464-剑指offer(35、05、58、03)
"Detective Conan" 1049 words painting collapse, the role of frequent "face changes"
465-剑指offer(53-I、53-II、04、50)
基于多尺度残差密集块和块连接级联U-Net的真实图像去噪
社交网络的充分去中心化
自定义类型下(枚举、联合)C语言
Jiulian technology development board is officially integrated into the openharmony backbone