1. 程式人生 > 其它 >MySQL的基礎語法

MySQL的基礎語法

1.建立資料庫

建立資料庫lol

mysql> CREATE DATABASE lol;
Query OK, 1 row affected (0.01 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lol |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql>

建立資料庫如果存在則不建立,不會報錯

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

mysql>

建立資料庫完整的寫法

mysql> CREATE DATABASE IF NOT EXISTS lol2 DEFAULT CHARSET UTF8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW CREATE DATABASE lol2;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| lol2 | CREATE DATABASE lol2

/*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)

2.刪除資料庫lol 刪除指定的資料庫,建立和刪除database沒有s複數的,查詢有s
mysql> DROP database lol;
Query OK, 0 rows affected (0.00 sec)

mysql>

4.檢視資料庫裡面有多少張表,以及檢視user資訊
mysql> show tables;

4.1.檢視user資料表的資訊

檢視資料表的結構

mysql> desc user;

檢視user表中的所有資訊

* 表示所有的欄位的意思

mysql> select * from user;

指定欄位查詢 user,host,password

mysql> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *A23AD8335836A5AB6123EFE033BF7345077222F3 |
| root | master01 | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | master01 | |
| root | 10.0.0.% | *A23AD8335836A5AB6123EFE033BF7345077222F3 |
+------+-----------+-------------------------------------------+
7 rows in set (0.00 sec)

6.查詢當前資料庫的字符集資訊
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 | /application/mysql-5.6.40/share/charsets/ |
+--------------------------+-------------------------------------------+
8 rows in set (0.00 sec)