1. 程式人生 > >MySQL基礎《一》資料庫的命令操作

MySQL基礎《一》資料庫的命令操作

1、建立資料庫

1.1 CREATE {DATABASE|SCHEMA} db_name;

mysql> CREATE DATABASE bingwu;或CREATE SCHEMA bingwu;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW DATABASES;        
+--------------------+
| Database           |
+--------------------+
| bingwu             |

1.2檢查資料庫是否存在,不存在則建立CREATE DATABASE[IF NOT EXISTS] db_name;

mysql> CREATE DATABASE IF NOT EXISTS bingwu;
Query OK, 1 row affected, 1 warning (0.00 sec)

1.3 建立資料的同時指定編碼方式;

CREATE DATABASE [IF NO EXISTS] db_name [DEFAULT] CHARCTER SET [=]  charset;

注意:資料庫的名稱要有意義;名稱不能包含特殊字元或者是MySQL關鍵字

2、 檢視全部資料庫 SHOW DATABASES|SCHEMAS;

mysql> SHOW DATABASES;                      
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bingwu             |

3、檢視指定的資料庫詳細資訊 SHOW CREATE DATABASE db_name;

mysql> SHOW CREATE DATABASE bingwu;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| bingwu   | CREATE DATABASE `bingwu` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

4、修改指定資料庫編碼方式  alter database db_name [default] character set [=] charset;

mysql> ALTER DATABASE bingwu DEFAULT CHARACTER SET=utf8
Query OK, 1 row affected (0.00 sec)

mysql> 

5、開啟指定資料庫 use db_name;

mysql> USE bingwu;
Database changed

6、得到當前開啟的資料庫 select databse()|schema();

mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| bingwu     |
+------------+
1 row in set (0.00 sec)

mysql> 

7、刪除指定的資料庫drop database db_name; 如果資料庫存在刪除:drop database [if exists] db_name;

mysql> DROP DATABASE bingwu1; 
Query OK, 0 rows affected (0.01 sec)

mysql> DROP DATABASE IF EXISTS bingwu1;# 不存在會有警告
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

8、檢視警告warning

mysql> show warnings;
+-------+------+-------------------------------------------------------+
| Level | Code | Message                                               |
+-------+------+-------------------------------------------------------+
| Note  | 1008 | Can't drop database 'bingwu1'; database doesn't exist |
+-------+------+-------------------------------------------------------+
1 row in set (0.00 sec)

mysql