數據庫語句操作

數據庫語句操作

數據庫介紹

關係型數據庫:基於關係模型而設計的數據庫系統
E-R 實體-關係圖

訪問mysql數據庫

mysql服務
    計算機服務可以開啓,關閉
    
    cmd 命令行可以開啓,關閉
        net start mysql
        net stop mysql

連接數據庫
    
    cmd 命令行:mysql -uroot -p
    
    navicat 軟件連接數據庫
    
    phpmyadmin  “網站”連接數據庫
        安裝(配置)該站點
        1,hosts 文件中設定域名解析:www.phpmyadmin7.com   
        2,拷貝網站文件到指定目錄:
        3,httpd-bhosts.conf 文件中設置站點
            <VirtualHost *:5001>
                ...
            <VirtulHost>

數據庫操作

create database 數據庫名;
show databases;
drop database 數據庫名;

alter database 數據庫名 .. ..;
use 數據庫名

數據表操作

create table 表名稱(id int(10),name char(20));
show create table 表名();   
show tables;
desc 表名;
drop table 表名;

//添加字段
alter table 表名 add 字段名 字段類型 [字段屬性。] [after某字段名或first]
    alter table 'test' add salary float;    //放在最後
    alter table 'test' add edu varchar(20) after salary;    //放在最後

//修改字段
alter table 表名 change 舊字段名 新字段名 字段類型 [字段屬性]
    alter table test change salary gongzi int default 0;
//修改字段其他信息
alter table 表名 modify 要修改的字段名 字段類型 [字段屬性]
    alter table table1 modify edu varchar(10) after gongzi;
    


//刪除字段
alter table 表名 drop 要刪除的字段名;

//修改表名
alter table 表名 rename 新的表名;

//修改字符集
alter table 表名 charset=新的字符集;

//插入數據
insert into 表名 (字段名1,字段名2 。。) values (數據1,數據2.。)

//查詢數據
select 字段名,。。 from 表名 [where 條件]
select *from 表名

//刪除數據
delete from 表名 [where 條件]; 可使用and

//修改數據
update 表名 set 字段名1=新值1,字段名2=新值2,。。 [where 條件]
    update info set content="新內容" where id=3;

source 數據庫文件物理地址

mysql 數據類型

列類型
    
    數值型
        整數型
            tinyint
            smallint
            mediumint
            int
            bigint
            
        小數型
            浮點
                float
                double
                
            定點
                decimal
                
    字符串型
        set
        enum
        blob
        text
        varchar
        char
        
        
    日期時間型
        year
        timestamp
        time
        date
        datetime
auto_increment; 自增長
primary_key 主鍵

實體與實體的關係

實體(Enity)
    一個表中的一行數據實際就是指對某物的描述性數據,所以一行數據
    就是一個實體,有時候實體也指整個表
實體間關係(relationship)
    是指不同實體數據之間的關係,很多時候就是指表和表之間的關係
    一對一關係,一對多關係,多對多關係

高級查詢

select 子句
    [from 子句]
    [where 子句]      //過濾條件
    [group by 子句]   //分組
    [having 子句]     //對中間結果進一步篩選
    [order by 子句]   //排序
    [limit 子句]      //限制結果數量
    ;


select id as username from user;    //別名

select distinct user_name,user_pass from user;//消除重複


where 子句
    select id from user where id>3;

mysql 運算符
    算術運算符
    比較運算符
    邏輯運算符
        && 或 and
        || 或 or
        !  或 not
    其他特殊運算符
        like 模糊查詢
            xxx字段 like ‘%關鍵字%’
            
            % 表示任意個數的任意字符
            ‘_’(下槓) 表示‘任意一個字符’
            
            select *from product where pro_name like '%電視機%'
            select *from user where name like '羅%'; //羅。。。。。
            select *from user where name like '羅_'; //羅+任意一個字符

        between 範圍限定運算符
            用於判斷某個字段的值是否在給定的兩個數據範圍之間
                xxx字段 between 值1 and 值2
            含義:相當於 xxx字段>=值1 and xxx字段<= 值2
            select *from product where price between 5000 and 10000;

            
            
        in 運算符
            用於判斷某個字段的值是否在給出的若干個‘可選值’範圍
                xxx字段 in (值1,值2.。。)
            含義:該字段的值相當於所列出的任意一個值,就算滿足條件
                籍貫 in (‘北京’,‘山東‘);//某人籍貫滿足上述一個條件就行
            select *from product where chandi in('北京’,’上海‘);
            select *from product where chandi='北京‘ or chandi=’上海‘;
            
            
            
        is 運算符
            用於判斷一個字段中的是“是否存在”,只有兩個寫法:
                where content is null;  //不能寫成 content=null
                where content is not null;  //不能寫成 content!=null
            select *from shunxing where f4 is null;

group by 語句
    分組本身的字段信息
        select *from product group by pingpai;//對其按品牌進行分組
    
    一組的綜合統計信息,主要包括:
        計數值: count(字段),表示求出一組中原始數據的行數
        最大值: max(字段),表示求出一組中該字段的最大值
        最小值: min(字段),                   小
        平均值: avg(字段),                   平均值
        總和值: sum(字段),                   累加和
        
        這5個函數被稱爲聚合函數
        
        查詢出各個品牌的產品平均價
        select pingpai,avg(price) from product group by pinpai;
        
        查詢出各個產地的產品數量,平均價,最高價,最低價
        select chandi,count(*) as 數量,avg(price) as 平均價,max(price)
            as 最高價,min(price),sum(price) as 價格之和 from ’product' group by chandi;
        
        查詢出產品表中聯想品牌的產品總數
        select count(*) as 總數 from product where pinpai='聯想';
        
        多條件分組
        將product表中的所有商品以品牌和產地進行分組,並求出每一組的數量
        select pinpai,chandi,count(*) as 數量 from product group by pinpai,chandi;
        
having 子句
    having 篩選條件
    是用於對group by 分組的結果進行的條件篩選
    
    查詢出品牌平均價超過5000的所有品牌的平均價,最高價,以及產品的數量
    select pinpai,avg(price) as 平均價,max(price) as 最高價,count(*) as 數量
        from ’product' group by pinpai having avg(price)>5000;

order by 子句
    對錢所取得的數據按給定的字段進行排序
    排序方式有:正序 asc,倒序 desc,默認是 asc
    
    對所有產品按價格從高到低進行排序
    select *from product order by price desc;
    
    對所有品牌的平均價從高到低的順序進行排序,並列出品牌名和平均價
    select pinpai,avg(price) as 平均價 from product group by pinpai order by 平均價 desc;
    
limit 子句
    
    limit 起始行號,行數
    
    表示對前面所取得的數據進行數量上的篩選,取得從某行開始的多少行
    
    取出商品表中價格最高的3個商品,並按倒序排列出來
    select *from product order by price desc limit 0,3;

高級插入

同時插入多行記錄
    insert into 表名(字段1,字段2.。) values (值1,值2.。),(值1,值2.。).;

插入查詢的結果數據
    insert into 表名(字段1,字段2.。) select (xx1,xx2...).. ;
    
set語法插入數據
    insert into user set username='bao',pass='123';
    
蠕蟲複製
    針對一個表的數據,進行快速的賦值並插入到所需要的表中
    以期在短時間內具備大量數據,以用於測試或其他特殊場合,比如:
        1,將一個表的大量數據,複製到另一個表中
        2,將一個表的數據複製到本身表中以產生大量數據
        
        insert into user3a (user_name,user_pass) select user_name,
            user_pass,edu from user3;

主鍵衝突解決辦法
    1,忽略    --終止插入,數據不改變
        insert ignore into 表名(字段..) values(值..);
    2,替換    --刪除原紀錄,插入新紀錄
        replace into into 表名(字段。。) values (值..)
    3,更新    --設置爲去更新原有數據(而並不插入)
        insert into 表名(字段) values (值..) on duplicate key update xx字段=新的值

高級刪除

按指定順序刪除指定數量的數據
    delete form 表名 where .. [order by 字段名..] [limit 數量n];
    
trancate 清空
    trancate 表名;
    表示清空指定表中的所有數據並將表恢復到初始化狀態;

高級更新

update set 字段名1=字段值1. where .. [order by 字段名..] [limit 數量n];

update user set age=22 order by id desc limit 2;

聯合(union)查詢

聯合查詢概念
    指將2個或2個以上的字段數量相同的查詢結果,“縱向堆疊”後合併成一個結果;

select id,f1,f2 from join1
union   [all 或 distinct]
select id2,c1,c2 from join2

[order by 字段 [asc 或 desc]]
[limit 起始行號,數量]

連接(join)查詢

將兩個查詢的結果以橫向對接的方式合併起來的結果
select .. from 表1 [連接方式] join 表2 [on 連接條件] where ..;

假設 表1 有 n1 行, m1 列
    表2 有 n2 行, m2 列
    
    則表1 和表2 ‘連接’ 之後,就會有
        n1 * n2 行;
        m1+m2 列;
        
        select *from join1,join join2;
        
交叉連接(cross join)
    from 表1 [cross] join 表2
    
    交叉連接 稱爲 笛卡爾積
    執行所有連接
    

內連接(inner join)
    from 表1 [inner] join 表2 on 連接條件
    
    在交叉連接的基礎上,通過on條件而篩選出來的部分數據
    
    找出所有價格大於5000的家用電器的商品的完整信息(含所屬類別)
    select *from product as p inner join product_type as t
        on p.protype_id = t.protyp_id
        where price>5000 and protype_name='家用電器';
    
外連接
    
    左外連接
        from 表1 left [outer] join 表2 on 連接條件
        保證左邊表的數據都能取出的一種連接
        在內連接的基礎上加上 左邊表中所有不能滿足條件的數據
        
        select *from join1 left join join2 on join.f1 = join.c1
        
        
    右外連接
        from 表1 right [outer] join 表2 on 連接條件
        和 左外連接正好相反
        
        找出所有類別及各類別中的商品(需列出類別名稱,商品名稱,價格,品牌和產地)
        select protype_name,pro_name,price,pinpai,chandi
            from product_type as t left join product as p
                on p.protype_id = t.protype_id;

自連接
    自己跟自己連接
    
    from 表名 as  a [連接形式] join 表名 as b on a.xx 字段1 = b.xx字段名
    select a.area_name,b.area_name from area as a join area as b on a.parent_id=b.id;
    
    找出所有省份及其下屬城市:
    select province.area_name,city.area_name
        from area as province join area as city on province.id = city.parent_id;

子查詢
    
    一個正常查詢語句的某個部分(比如select部分,from部分,where部分)  
        又出現了一種查詢形式
        
        select *from xx表名 where price>=(一個子查詢語句)
        
        其實就是 select語句的嵌套查詢
        select *from product where>(select avg(price) from product);
        
    標量子查詢
        標量子查詢的結果,可以直接當一個值來使用
        
        找出產品表中價格大於北京產地的產品平均價的所有產品
        select *from product where price>(
            select avg(price) from product where chandi='北京'
        )
        
    列子查詢
        列子查詢查出的結果爲一列數據,類似
            select pinpai from product where chandi='背景'
            結果爲一列數據
        
        列子查詢通常用在where子句的in運算符中,代替in運算符中的字面值列表數據
    
        
    行子查詢
        行子查詢出的結果通常是一行
        select pinpai from product where price=11499;
        
        行子查詢的結果通常是跟 行構造符 一起,在where子句中作爲
        條件數據,類似
            where (字段1,字段2) = (行子查詢)
        或
            where row(字段1,字段2) = (行子查詢);
        
        找出跟單價最高的商品同品牌同產地的所有商品
        select *from product where row(pinpai,chandi) =(
            select pinpai,chandi from product where price =(
                select max(price) from product;
            )
        )
        
    表子查詢
        當一個子查詢查出的結果是 "多行多列" 的時候,就是表子查詢
        查詢的結果相當於一個表,可以直接當做一個表來使用
        
    
    有關子查詢的語句
         in 關鍵字
            在子查詢中主要用在列子查詢中
            
            找出聯想品牌的商品都有哪些類別
            select *from product_type where protype_id in(
                select distinct protype_id from product where pinpai='聯想'
            )
            
                        
         any 關鍵字
            any 關鍵字用在比較操作符的後面,表示查詢結果的多個數據中的任一個滿足
            該比較符就算滿足
            
            找出在北京生產的但價格比在深圳生產的任一商品貴的商品
            select* from product where chandi='北京' and price>any(
                select price from product where chandi='深圳';
            )
                       
         all 關鍵字
            select* from product where chandi='北京' and price>all(
                select price from product where chandi='深圳';
            )

         exists子查詢
            where exists(任何子查詢)

            該子查詢如果 有數據,則該exists() 的結果爲true,即相當於 while true

數據管理

數據備份
    備份整個數據庫
    mysqldump.exe -h 主機地址 -u 用戶名 -p 密碼 數據庫名 > 備份文件名(含路徑)
    
    該語句是mysql/bin 中的一個命令,不是sql語句,所以不是登錄後使用
    
    備份單個表
    mysqldump.exe -h 主機地址 -u 用戶名 -p 密碼 數據庫名 表名 > 備份文件名(含路徑)
                           
    
數據還原
    mysql.exe -h 主機地址 -u 用戶名 -p 密碼 目標數據庫名 < 想要還原的備份文件名(含路徑)

用戶管理

用戶管理
    用戶賬號的管理:創建,刪除,改密
    用戶權限的管理:授予權限,取消權限

查看用戶
    use mysql;
    select *from user;
           
創建用戶
    create user '用戶名' [@'允許登錄的地址'] identified by ‘密碼’;
    
    ‘允許登錄的地址’就是允許登錄的客戶端的ip地址
        1,‘localhost’ 表示只能本地登錄
        2,'%'表示任何位置都可以登錄
        3,該部分可以省略,如果省略,默認就是‘%’;
        4,後續涉及到用戶的操作,都是這個格式
        
    create user 'user1' identified by '123';
    create user 'user2'@'localhost' identified;
    create user 'user3'@'192.168.1.103' identified;
    
刪除用戶
    drop user 用戶[@'允許登錄的地址'];

修改/設置用戶
    set password for 用戶[@'允許登錄的地址'] = password('密碼')

授予用戶權限
    grant 操作1,操作2.。。 on *.* 或 數據庫名 或數據庫名 表名 to 用戶[@'允許登錄的地址']
    
取消用戶授權
    revoke 操作1,操作2.。。 on *.* 或 數據庫名 或數據庫名 表名 to 用戶[@'允許登錄的地址']
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章