1. 程式人生 > 實用技巧 >第3章 MySQL常用增刪改查操作命令

第3章 MySQL常用增刪改查操作命令

在MySQL中最基礎運用的就是增刪改查命令,這篇文章我將詳細的介紹mysqi的基礎命令。

1.登入方式

mysql的登陸方式有三種,如下:

  • mysql
  • mysql -uroot
  • mysql -uroot -pzxc123
  • [root@mysql mysql-5.5.22]# mysql -uroot -p123123
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.5.22-log Source distribution
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MySQL [(none)]>  

2. 登入後會有MySQL [(none)]>的提示符,下面我們來修改一下自己的命令提示符:prompt命令

  • MySQL [(none)]> prompt \u@Carrie ->
    PROMPT set to '\u@Carrie ->'
    
    root@Carrie ->
    

      

3.   退出MySQL的方式

  • quit
  • ctrl+c
  • ctrl +d

4.修改密碼

第一種方式

  • mysqladmin -uroot -p123123 password zxc123
  • [root@mysql ~]# mysqladmin -uroot -p123123 password zxc123
    [root@mysql ~]# mysql -uroot -p123123
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    [root@mysql ~]# mysql -uroot -pzxc123
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MySQL connection id is 7
    Server version: 5.5.22-log Source distribution
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MySQL [(none)]> 
    

      

第二種方式

  • 在資料庫中修改命令
  • updata from user set password='456' where user=root and localhost

5.建立一個數據庫

用create建立資料庫

  • MySQL [(none)]> create database Carrie;
    Query OK, 1 row affected (0.00 sec)
    

      

6.檢視資料庫

用show 檢視資料庫

  • MySQL [(none)]> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | Carrie             |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    5 rows in set (0.00 sec)  

7.使用資料庫

用use使用資料庫

  • MySQL [(none)]> use Carrie;
    Database changed
    MySQL [Carrie]>
    

      

8.刪除資料庫Carrie

用drop刪除資料庫

  • MySQL [Carrie]> drop database Carrie;
    Query OK, 0 rows affected (0.00 sec)
    

      

9.建立資料庫中的表

用creat建立表

  • MySQL [Carrie]> create table student (id int(3),name varchar(10),age int(3));
    Query OK, 0 rows affected (0.00 sec)
    

      

10.查看錶欄位

表字段就是表中的結構,也是表中的表頭資訊。

用 desc查看錶欄位

  • MySQL [Carrie]> desc student;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(10) | YES  |     | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    

      

11.刪除表

用drop刪除表

  • MySQL [Carrie]> drop table student;
    Query OK, 0 rows affected (0.00 sec)
    

      

12.對錶結構的增刪改方法

  • 新增入表字段,用alter table 表名 add進行新增
  • MySQL [Carrie]> alter table student add sex varchar(5);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    

      

  • 新增表字段還可以選定新增的位置,如下
  • MySQL [Carrie]> alter table student add code int(10) after id;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    

      

  • 表結構最終成型如下
  • MySQL [Carrie]> desc student;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(3)      | YES  |     | NULL    |       |
    | code  | int(10)     | YES  |     | NULL    |       |
    | name  | varchar(10) | YES  |     | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | sex   | varchar(5)  | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    5 rows in set (0.00 sec)
    

      

  • 刪除表結構中的內容
  • MySQL [Carrie]> alter table student drop code;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    MySQL [Carrie]> desc student;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(10) | YES  |     | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | sex   | varchar(5)  | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    

      

  • 修改表結構的型別
  • MySQL [Carrie]> desc student;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(10) | YES  |     | NULL    |       |
    | age   | int(3)      | YES  |     | NULL    |       |
    | sex   | varchar(5)  | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    
    MySQL [Carrie]> alter table student modify age int(10);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0 Duplicates: 0 Warnings: 0
    
    MySQL [Carrie]> desc student;
    
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(3)      | YES  |     | NULL    |       |
    | name  | varchar(10) | YES  |     | NULL    |       |
    | age   | int(10)     | YES  |     | NULL    |       |
    | sex   | varchar(5)  | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    

      

  • 修改表結構的名稱
  • MySQL [Carrie]> alter table student change name name_student varchar(10);
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    MySQL [Carrie]> desc student;
    +--------------+-------------+------+-----+---------+-------+
    | Field        | Type        | Null | Key | Default | Extra |
    +--------------+-------------+------+-----+---------+-------+
    | id           | int(3)      | YES  |     | NULL    |       |
    | name_student | varchar(10) | YES  |     | NULL    |       |
    | age          | int(10)     | YES  |     | NULL    |       |
    | sex          | varchar(5)  | YES  |     | NULL    |       |
    +--------------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    

      

13.修改表名

  • MySQL [Carrie]> alter table student rename to student_data;
    Query OK, 0 rows affected (0.00 sec)
    
    MySQL [Carrie]> desc student;
    ERROR 1146 (42S02): Table 'Carrie.student' doesn't exist
    MySQL [Carrie]> desc student_data;
    +--------------+-------------+------+-----+---------+-------+
    | Field        | Type        | Null | Key | Default | Extra |
    +--------------+-------------+------+-----+---------+-------+
    | id           | int(3)      | YES  |     | NULL    |       |
    | name_student | varchar(10) | YES  |     | NULL    |       |
    | age          | int(10)     | YES  |     | NULL    |       |
    | sex          | varchar(5)  | YES  |     | NULL    |       |
    +--------------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec
    

14.對錶中資料進行增刪改查

  • 插入表中的資料
  • MySQL [Carrie]> insert into student_data (id,name_student,age,sex) values (1,'Carrie',18,'woman');
    Query OK, 1 row affected (0.00 sec)

    MySQL [Carrie]> insert into student_data values (3,'Matin',20,'man');
    Query OK, 1 row affected (0.00 sec)

    此處用了了兩種方式插入表資訊

  • 查看錶中資料
  • MySQL [Carrie]> select * from student_data;
    +------+--------------+------+-------+
    | id | name_student | age | sex |
    +------+--------------+------+-------+
    | 1 | Carrie | 18 | woman |
    | 2 | Susie | 19 | woman |
    | 3 | Montin | 20 | man |
    | 2 | Susie | 18 | woman |
    | 3 | Matin | 20 | man |
    +------+--------------+------+-------+
    5 rows in set (0.00 sec)

  • 刪除表中資料
  • MySQL [Carrie]> delete from student_data where id=3;
    Query OK, 2 rows affected (0.00 sec) MySQL [Carrie]> select * from student_data; +------+--------------+------+-------+ | id | name_student | age | sex | +------+--------------+------+-------+ | 1 | Carrie | 18 | woman | | 2 | Susie | 19 | woman | | 2 | Susie | 18 | woman | +------+--------------+------+-------+ 3 rows in set (0.00 sec)

     delete from student_data;清空表 

  • 修改資料
  • MySQL [Carrie]> update student_data set id=1 where id=2;
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2  Changed: 2  Warnings: 0
    
    MySQL [Carrie]> select * from student_data;
    +------+--------------+------+-------+
    | id   | name_student | age  | sex   |
    +------+--------------+------+-------+
    |    1 | Carrie       |   18 | woman |
    |    1 | Susie        |   19 | woman |
    |    1 | Susie        |   18 | woman |
    +------+--------------+------+-------+
    3 rows in set (0.00 sec)
    
  • 指定查看錶中的資料
  • MySQL [Carrie]> select * from student_data;
    +------+--------------+------+-------+
    | id   | name_student | age  | sex   |
    +------+--------------+------+-------+
    |    1 | Carrie       |   18 | woman |
    |    1 | Susie        |   19 | woman |
    |    1 | Susie        |   18 | woman |
    +------+--------------+------+-------+
    3 rows in set (0.00 sec)
    
    MySQL [Carrie]> select name_student from student_data where age=18;
    +--------------+
    | name_student |
    +--------------+
    | Carrie       |
    | Susie        |
    +--------------+
    2 rows in set (0.00 sec)
    
  • 去除表中重複的資訊
  • MySQL [Carrie]> update student_data set name_student='Carrie' where age=18;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 2  Changed: 1  Warnings: 0
    
    MySQL [Carrie]> select * from student_data;
    +------+--------------+------+-------+
    | id   | name_student | age  | sex   |
    +------+--------------+------+-------+
    |    1 | Carrie       |   18 | woman |
    |    1 | Susie        |   19 | woman |
    |    1 | Carrie       |   18 | woman |
    +------+--------------+------+-------+
    3 rows in set (0.00 sec)
    
    MySQL [Carrie]> select distinct *from student_data;
    +------+--------------+------+-------+
    | id   | name_student | age  | sex   |
    +------+--------------+------+-------+
    |    1 | Carrie       |   18 | woman |
    |    1 | Susie        |   19 | woman |
    +------+--------------+------+-------+
    2 rows in set (0.00 sec)
    
  • 給資料排序(預設升序排列,desc降序,asc升序,排序完成後,還可以加limit對資料進行限制,比如limit 2,3只顯示2-3行)
  • MySQL [Carrie]> select *from student_data order by age desc;
    +------+--------------+------+-------+
    | id   | name_student | age  | sex   |
    +------+--------------+------+-------+
    |    2 | Susie        |   19 | woman |
    |    1 | Carrie       |   18 | woman |
    |    1 | Carrie       |   18 | woman |
    +------+--------------+------+-------+
    3 rows in set (0.00 sec)
    

15.聚合函式

常用的求最大,最小,平均數等聚合函式

函式名稱函式作用
MIN 查詢指定列中的最小值
MAX 查詢指定列中的最大值
COUNT 查詢結果總行數統計
SUM 求和,返回指定列的總和
AVG 求平均數,返回指定列的平均值

為了後續例項中便於理解,先在 TEST 資料庫中建立資料表 STUDENT,其表結構和表資料如下

select a.* from table a;

idnamescore
1 zhangsan 98
2 lisi 99
3 wangwu 100
    1. min() 函式
      例:在 student 表中查詢所有分數的最小值

      例:在 student 表中查詢所有姓名的最小值

      注:min() 函式即可查詢數值型別,也可用於字元型別;min() 函式可以判斷字母大小,並返回最小的字元或字串值,字元型資料比較時,按照ASCII碼值大小進行比較,從a到z,a的ASCII碼最小,z的ASCII碼最大;
    2. max() 函式
      例:在 student 表中查詢所有分數的最大值

      例:在 student 表中查詢所有姓名的最大值

      注:與min() 函式類似
    3. count() 函式
      例:在 student 表中查詢所有分數的列表總和
    4. sum() 函式
      例:在 student 表中查詢所有分數的總和
    5. avg() 函式
      例:在 student 表中查詢所有分數的平均值