01 MySQL数据库第一站


👻

🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈

👻 

tips : 由于MySQL不区分大小写,为增加可读性,故本文一律采用小写


1. 显示当前数据库

语法:show databases;

👻tips : 不要忘记加末尾的" ; " ,由于MySQL支持多行输入,所以一个语句的结束需要用 " ; "表示

mysql> show databases;

运行效果如下,则表示正常的运行

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

👻 那我们不小心少输入一个字母会怎么样?

mysql> show database;

👻运行效果:

ERROR 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1

翻译过来就是:你当前有一个SQL语句错误,检查与MySQL服务器版本对应的手册,以了解第1行“database”附近使用的正确语法

2. 创建数据库

👻 简单语法:create database [ if not exists ]  [ 数据库名 ];

数据库名有字母(a ~ z)和数字(0~9)和下划线( _ )组成,开头必须是字母或者下划线

mysql> create database if not exists mydatabase;

👻运行效果:

mysql> create database if not exists mydatabase;
Query OK, 1 row affected (0.00 sec)

👻再使用 show databases; 显示当前的数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
7 rows in set (0.00 sec)

数据库会多出一行 ----  mydatabase,则表明建立数据库成功

👻tips : if not exists 可以省略不写

mysql> create database mydatabase1;
Query OK, 1 row affected (0.00 sec)

👻但当数据库出现重名的时候会报错,加上 if not exists 则不会报错,if not exists 的作用是:如果数据库存在,则不会建立数据库,如果数据库不存在,则建立数据库。

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

👻完整语法:ceate database [if not exists] db_name [create_specification [, create_specification] ...] 

        create_specification:

        [default] character set charset_name [default] collate collation_name

character set  :  指定数据库采用的字符集,默认就行

collate  :  指定数据库字符集的校验规则

3. 删除数据库

👻语法:drop database if exists [数据库名]

mysql> drop database  mydatabase;
Query OK, 0 rows affected (0.00 sec)

if exists 也可以省略不写,但如果数据库中没有你要删除的数据库时,则会报错。

mysql> drop database i123;
ERROR 1008 (HY000): Can't drop database 'i123'; database doesn't exist

👻再次使用 show databases; 显示当前的数据库,可以发现刚刚建立的mydatabase数据库已经消失

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

👻在工作中删除数据库是一个危险的操作,请问尝试!

  • 22
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值