数据库--问题2--mysql数据定义语言增删改查管理数据库
Linux系统下,先安装好mysql数据库,然后开始以管理员身份进入数据库,具体可查看我的上一篇博客:https://blog.csdn.net/qq_41103495/article/details/109632408
1.增,创建数据库
在 MySQL 中,创建数据库,语法格式如下:
CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>]
[[DEFAULT] COLLATE <校对规则名>];
[ ]
中的内容是可选的。语法说明如下:
(1)<数据库名>:创建数据库的名称。MySQL 的数据存储区将以目录方式表示 MySQL 数据库,因此数据库名称必须符合操作系统的文件夹命名规则,不能以数字开头,尽量要有实际意义。注意在 MySQL 中不区分大小写。
(2)[IF NOT EXISTS]:在创建数据库之前进行判断,只有该数据库目前尚不存在时才能执行操作。此选项可以用来避免数据库已经存在而重复创建的错误。
(3)[DEFAULT] CHARACTER SET:指定数据库的字符集。指定字符集的目的是为了避免在数据库中存储的数据出现乱码的情况。如果在创建数据库时不指定字符集,那么就使用系统的默认字符集。
(4)[DEFAULT] COLLATE:指定字符集的默认校对规则。
MySQL 的字符集(CHARACTER)和校对规则(COLLATION)是两个不同的概念。
字符集是用来定义 MySQL 存储字符串的方式,校对规则定义了比较字符串的方式。
用命令:show create database tree1;查看指定数据库的信息。(tree1是数据库名)
实际操作:
mysql> create database tree1;
Query OK, 1 row affected (0.00 sec)
mysql> create database tree1;
ERROR 1007 (HY000): Can't create database 'tree1'; database exists
mysql> create database if not exists tree1;
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> create database if not exists tree1 default character set utf8;Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| CY1211 |
| Parking |
| intelligent_park |
| mysql |
| test |
| tree1 |
+--------------------+
7 rows in set (0.00 sec)
mysql> show create database tree1;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| tree1 | CREATE DATABASE `tree1` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
2.删,删除数据库
输入命令:drop database [if exists] tree1;(tree1是数据库名)
mysql> drop database tree1;
Query OK, 0 rows affected (0.01 sec)
mysql> drop database if exists tree1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
3.查,
查创建的数据库,输入命令:show create database tree1;(tree1是数据库名)
查所以数据库,输入命令:show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| CY1211 |
| Parking |
| intelligent_park |
| mysql |
| test |
| tree1 |
+--------------------+
7 rows in set (0.01 sec)
mysql> show create database tree1;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| tree1 | CREATE DATABASE `tree1` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
4.选中某数据库
比如进入数据库CY1211,输入:use CY1211;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| CY1211 |
| Parking |
| intelligent_park |
| mysql |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql> use CY1211;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed