1. 程式人生 > >mysql--簡單操作

mysql--簡單操作

varchar arc 範圍 select 顯示 條件判斷 val change lar

一、數據庫的基本操作

  1. 查看當前所有存在的數據庫
    show databases;  //mysql 中不區分大小寫。(databank 是之前創建的)

    技術分享圖片

  2. 創建數據庫
    create database database_name;  //database_name 為新建的數據庫的名稱,該名稱不能與已經存在的數據庫重名

    如:創建一個名為 test_db 的數據庫

    技術分享圖片

    查看數據庫 test_db 的定義:

    show create database test_db\G    //將會顯示數據庫的創建信息

    技術分享圖片

  3. 使用數據庫
    use database_name;

    如:使用數據庫 test_db

    技術分享圖片

  4. 刪除數據庫
    drop database database_name;
    如:刪除數據庫 test_db

    技術分享圖片

二、數據表的基本操作(以 stu 表為例)

  1. 創建數據表
    create table stu(
        id int(11) primary key auto_increment,  //主鍵 id, id 值自動增加
        name varchar(22) not null
    , //非空 salary float default 1000.00); //默認值為 1000.00
  2. 查看數據表結構
    desc table_name;  //describle table_name;  查看表的基本結構

    技術分享圖片

    show create table table_name\G        //查看表的詳細結構

    技術分享圖片

  3. 修改數據表

    alter table table_oldname rename [to] table_newname;  //修改表名。    to 為可選參數,使用與否均不影響結果

    技術分享圖片

    alter table table_name modify <
    字段名> <數據類型>; //修改字段的數據類型。 表名 是當前所在表, 字段名 是目標修改字段, 數據類型 是新數據類型

    技術分享圖片

    修改字段名

    alter table table_name change <舊字段名> <新字段名> <新數據類型>;  //修改字段名。    如不需要修改字段的數據類型,須將新數據類型設置成和原來一樣。

    添加字段

    alter table table_name add <新字段名> <數據類型> [約束條件] [first | after 已存在的字段名];  //添加字段
    
    如:
    alter table staff add address varchar(55);  //無約束條件
    
    alter table staff add address varchar(55) not null;  //有約束條件(要求非空)
    
    alter table staff add address varchar(55) first;  //成為表的第一列
    
    alter table staff add address varchar(55) after name;  //在表的某一列之後

    刪除字段

    alter table table_name drop <字段名>;  //刪除字段

    修改字段的排列位置

    alter table table_name modify <字段1> <數據類型> first | after <字段2>;  //修改字段的排列位置。    數據類型 是指 字段1 的數據類型
    
    如:
    alter table staff modify adress varchar(55) first;  //修改字段為表的第一個字段
    
    alter table staff modify adress varchar(55) after id;  // adress 字段將移至表的 id 字段之後

  4. 刪除數據表
    drop table table_name;  //無關聯的表

三、插入、更新與刪除數據

  1. 表中插入數據
    insert into table_name (column_list) values (value_list);
    
    如:
    //建表
        create table staff(
        id int(11) primary key auto_increment,
        name varchar(22) not null,
        salary float default 1000);
    
    //為所有字段插入數據
        insert into staff(id,name,salary)
        values(1,Joan,2000),values(2,Qwe,900);

    技術分享圖片

    //為表的指定字段插入數據
        insert into staff(id,name)
        values(3,Wer),(4,於玉);   //salary 取默認值
    
        insert into staff(name,salary)
        values(Tin,2048),(Jimy,1024);  //id 值自動增加

    技術分享圖片

  2. 復制表

    create table table_new select * from table_old;    //全部復制(表的結構會改變)
    
    create table table_new select (column_list) from table_old;    //部分復制
    
    如:
    create table newstaff select * from staff;
    
    create table newstaff select id,salary from staff;     

    技術分享圖片

  3. 表中刪除數據

    delete from table_name [where <condition>];    //刪除指定記錄
    
    delete from table_name;    //刪除表中所有記錄

  4. 表中更新數據
    update table_name
    set column_name1=values1,... [where <condition>];    //修改指定字段的值
    
    如:
    update staff set salary=2048 where id=9;
    
    update staff set salary=1024 where id between 5 and 9;
  5. 表中查詢數據

    單表查詢

    //單表查詢
    
    select * from staff;    //*)查詢所有字段數據
    
    select 字段名1,字段名2,...字段名n from table_name;    //查詢指定字段數據(某幾列)
    
    select 字段名1,字段名2,...字段名n from table_name where 條件;    //查詢指定記錄

    where 條件判斷符

    操作符

    說明

    =

    相等

    <> , !=

    不相等

    <

    小於

    <=

    小於或者等於

    >

    大於

    >=

    大於或者等於

    between

    位於兩者之間

    //in 關鍵字的查詢
    
    select * from staff where id in(1,2,8);    //id=1、id=2、id=8
    
    select * from staff where id not in(1,2,8);
    
    //between and 的範圍查詢
    
    select * from staff where id between 1 and 8;    //id-->1至8 的記錄
    
    select * from staff where id not between 1 and 8;    //id-->1至8 以外的記錄
    
    
    //like 的字符匹配查詢
    
    select * from staff where name like t%;    //查詢所有名字以 t 開頭的記錄
    
    select * from staff where name like %t%;    //查詢所有名字中包括 t 的記錄
    
    select * from staff where name like t%y;     //查詢所有名字以 t 開頭並以 y 結尾的記錄
    
    select * from staff where name like ___y;     //查詢所有名字以 y 結尾並且前面只有三個字符的記錄
    
    
    //查詢空值
    
    select * from staff where salary is null;    //查詢字段 salary 的值為空的記錄
    
    
    //andor 的多條件查詢
    
    select * from staff where id=1 and salary>=1000;
    
    
    //查詢結果不重復
    
    select distinct 字段名 from 表名;    //distinct 關鍵字可以消除重復的記錄值

    對查詢結果排序

    //order by 默認情況下,升序排
    
    select * from staff order by salary [asc];
    
    select * from staff order by salary desc;    //降序排
    
    
    //group by  該關鍵字常和集合函數一起用,如: MAX()、MIN()、COUNT()、SUM()、AVG()
    
    [group by 字段] [having <條件表達式>]    //基本語法格式
    
    如:根據 salary 對 staff 表中的數據進行分組,並顯示人員數大於等於1的分組信息
    select salary,group_concat(name) as names from staff
    group by salary having count(name)>=1;
    
    select salary,count(*) as total_num from staff
    group by salary with rollup;  //with rollup 關鍵字用來統計記錄數量
    
    select * from staff group by salary,name;  //多字段分組
    
    
    //group by + order by
    
    select name, sum(2*salary) as salary_total from staff
    group by salary having sum(2*salary)>2000
    order by salary_total;
    
    
    //使用 limit 限制查詢結果的數量
    
    limit [位置偏移量,] 行數     //基本語法格式
    
    select * from staff limit 4;    //從第一行開始顯示,顯示共四行
    select * from staff limit 4,4;    //從第5行開始顯示,顯示共四行

    使用集合函數查詢

    // count() 函數
    
    count(*) 計算表中行的總數,不管某列值是否為空
    count(字段名) 計算指定列下總的行數,空值的行不被記錄
    
    select count(*) as total_num from staff;
    select count(salary) as salary_num from staff;
    
    
    // sum()函數。計算時忽略值為 NULL 的行
    
    select sum(salary) as salary_total from staff;
    
    
    // avg()函數
    
    select avg(salary) as avg_salary from staff;
    
    
    // max()函數、min()函數。以 max 為例
    
    select max(salary) as max_salary from staff;
    select name,max(salary) as max_salary from staff;

  

mysql--簡單操作