本文主要是介紹下MySQL的一些基本命令的使用,屬於入門級的小知識。
1.連接數據庫
在終端輸入命令鏈接數據庫
mysql -u (用戶名)-p
然後在提示下輸入密碼。也可把密碼輸在-p後面
2.顯示所有用戶和當前用戶
命令:
show databases;
use mysql;
select host, user from user;
顯示當前用戶:
select user()
mysql命令以分號;結束
創建數據庫:create database 庫名 ;
顯示所有數據庫: show databases;
當前庫數據表結構:show tables; 創建數據表:CREATE TABLE 表名([字段名] [字段類型]([字段要求]) [字段參數], ......);
顯示數據表字段:desc 表名;
增加各個字段alter table 表名 add 字段名 字段類型;
刪庫:drop database 庫名;
刪表:drop table 表名;
查詢: select 字段名 表名 where條件;
修改:update 表名set 修改內容 where條件;
第二部分 例子
1.查看當前mysql的版本
mysql> select version();
+------------+
| version() |
+------------+
| 5.5.33-log |
+------------+
1 row in set (0.00 sec)
2.創建個新庫
mysql> create database db9;
Query OK, 1 row affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db9 |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
8 rows in set (0.00 sec)
3.在數據庫db9中創建表students
mysql> use db9;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table students ( ID INT UNSIGNED AUTO_INCREMENT NOT NULL UNIQUE KEY, Name CHAR(30) NOT NULL, Gender ENUM('F','M') NOT NULL);
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+---------------+
| Tables_in_db9 |
+---------------+
| students |
+---------------+
1 row in set (0.01 sec)
4.查看錶的字段
mysql> desc students;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| ID | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Name | char(30) | NO | | NULL | |
| Gender | enum('F','M') | NO | | NULL | |
+--------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
5.往表內輸入數據
mysql> insert into students values ( 1,'Tom','M'),(2,'David','M'),(3,'July','F'),(4,'Alx','F');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
6.顯示錶裏內容
mysql> select * from students;
+----+-------+--------+
| ID | Name | Gender |
+----+-------+--------+
| 1 | Tom | M |
| 2 | David | M |
| 3 | July | F |
| 4 | Alx | F |
+----+-------+--------+
4 rows in set (0.00 sec)
7.往表裏再添加一行數據
mysql> insert into students values ( 5,'Jerry','M');
Query OK, 1 row affected (0.07 sec)
mysql> select * from students;
+----+-------+--------+
| ID | Name | Gender |
+----+-------+--------+
| 1 | Tom | M |
| 2 | David | M |
| 3 | July | F |
| 4 | Alx | F |
| 5 | Jerry | M |
+----+-------+--------+
5 rows in set (0.02 sec)
8.添加一個字段 Age (在Name後面)
mysql> alter table students add Age TINYINT UNSIGNED NOT NULL AFTER Name;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from students;
+----+-------+-----+--------+
| ID | Name | Age | Gender |
+----+-------+-----+--------+
| 1 | Tom | 0 | M |
| 2 | David | 0 | M |
| 3 | July | 0 | F |
| 4 | Alx | 0 | F |
| 5 | Jerry | 0 | M |
+----+-------+-----+--------+
5 rows in set (0.03 sec)
9.將年齡輸入到Age字段 (修改表)
mysql> update students set Age = 20 where ID = 1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from students;
+----+-------+-----+--------+
| ID | Name | Age | Gender |
+----+-------+-----+--------+
| 1 | Tom | 20 | M |
| 2 | David | 23 | M |
| 3 | July | 13 | F |
| 4 | Alx | 17 | F |
| 5 | Jerry | 27 | M |
+----+-------+-----+--------+
10.查詢年齡小於22的學生,顯示名字和各自的年齡
mysql> select Name,Age from students where Age <= 22;
+------+-----+
| Name | Age |
+------+-----+
| Tom | 20 |
| July | 13 |
| Alx | 17 |
+------+-----+
11.刪除字段Gender
mysql> alter table students drop Gender;
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from students;
+----+-------+-----+
| ID | Name | Age |
+----+-------+-----+
| 1 | Tom | 20 |
| 2 | David | 23 |
| 3 | July | 13 |
| 4 | Alx | 17 |
| 5 | Jerry | 27 |
+----+-------+-----+
12.刪除表
mysql> drop table students;
Query OK, 0 rows affected (0.04 sec)
mysql> show tables;
Empty set (0.00 sec)
13.刪除庫
mysql> drop database db9;
Query OK, 0 rows affected (0.05 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| hellodb |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
(db9被刪除)