1. 程式人生 > >MySQL-資料庫增刪改查

MySQL-資料庫增刪改查

查詢資料庫:show databases;

建立資料庫:create database 庫名;

刪除資料庫:drop database 庫名

建立表:create table 表名(欄位名 資料型別【屬性】【索引】)

查看錶結構:desc 表名

查看錶:show tables

插入資料:insert into 表名 value()

檢視建立表資訊:show create table 表名

檢視指定庫的表:show tables where 目標庫名

欄位

  增加:alter table 表名 add 欄位名 資料型別 【屬性】【索引】

    往指定位置後面插入欄位:alter table 表名 add 欄位名 資料型別 【屬性】 【索引】after 指定的欄位名

    往第一個位置插入:alter table 表名 add 欄位名 資料型別【屬性】【索引】first

  刪除:alter table 表名 drop 欄位名

 

  修改:alter table 表名 change 舊欄位 新欄位 資料型別 【屬性】【索引】

  條件語句:>  <  >=  <=  !=  and  or  等等

資料

  新增:insert into 表名 value()

  刪除:delete from 表名 (慎用,刪除整個表資料)

     delete from 表名 where 條件語句

  修改:update 表名 set 欄位名=值 where 條件語句

  ★查詢:精確查詢:select * from 表名 where 條件語句

      運算子查詢:select * from 表名 where id = 1+1

            select * from 表名 where id < 100

      邏輯查詢:select * from 表名 where and條件

           select * from 表名 where or條件

      模糊查詢:select * from 表名 where 列名 like'值'  值:%a%(查詢中間有a的資料)  a%(查詢以a開頭的資料)  %a(查詢以a結尾的資料)

      排序與受限查詢:select * from 表名 where order by 列名 desc  desc:表示從大到小排序  asc:表示從小到大排序

              select * form 表名 limit x,y  x:表示跳過多少條  y:表示去多少條

      聚合排序:select count(列名) from 表名  

           count:計算表中某個列或多個列中資料的次數

           avg:平均值

           max:最大值

           min:最小值

           sum:總和  一般用於計算價格

      區間查詢:select * from 表名 where 欄位 between 0 and 10  查詢0到10區間的資料

      分組查詢:select 展示的列 from 表名 group by 參考列

           select name,count(列) from 表名 group by name

           select  name,count(content)  from  表名  group  by   name  having   count(content)  >  5  having 是在聚合的基礎上再篩選

           分組查詢一般與聚合查詢一起使用