溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL數(shù)據(jù)庫的一些總結(jié)和講義

發(fā)布時間:2020-04-25 14:45:40 來源:億速云 閱讀:247 作者:三月 欄目:MySQL數(shù)據(jù)庫

本文主要給大家介紹MySQL數(shù)據(jù)庫的一些總結(jié)和講義,希望可以給大家補充和更新些知識,如有其它問題需要了解的可以持續(xù)在億速云行業(yè)資訊里面關(guān)注我的更新文章的。

 

數(shù)據(jù)導(dǎo)入與導(dǎo)出

(1)數(shù)據(jù)導(dǎo)入mysql>load data infile "目錄/文件名" into table 表名    /默認(rèn)目錄為 /var/lib/mysql-files,可修改配置文件改變默認(rèn)目錄,如不修改配置文件,默認(rèn)且一定要在這個目錄下
    >field terminated by "字段間隔符"            /導(dǎo)入的文本數(shù)據(jù)的字段間隔符號         
    >lines terminated by "\n"               /導(dǎo)入的文本數(shù)據(jù)的行結(jié)束符號
mysql> show variables like "secure_file_priv"; 查看默認(rèn)目錄
/etc/my.cnf
secure_file_priv="/myfile"

  把/etc/passwd 數(shù)據(jù)導(dǎo)入到表user中
  a.創(chuàng)建表user(表中字段要與導(dǎo)入的數(shù)據(jù)中字段相同) 
    create table user(
    >name char(15),
    >passwd char(8),
    >uid int,
    >gid int,
    >comment varchar(50),
    >homedir varchar(30),
    >shell varchar(30),
    >index(name),
    >unique index(uid)
    >);

    b.把/etc/passwd 數(shù)據(jù)放入默認(rèn)目錄/var/lib/mysql-files
    cp /etc/passwd /var/lib/mysql-files/

    c.數(shù)據(jù)導(dǎo)入
    load data infile "/var/lib/mysql-files/passwd" into table user \
    >fields terminated by ":" lines terminated by "\n";  /此表中字段間隔符為":",行結(jié)束符號為"\n".

    d.驗證
    select * from user;

    (2)數(shù)據(jù)導(dǎo)出   把表記錄通過sql查詢存儲到系統(tǒng)文件中
    >sql查詢   into outfile "目錄/文件名" fields terminated by "字段間隔符" lines terminated by "行結(jié)束符號"
                    /目錄默認(rèn)/var/lib/mysql-files 如不修改配置文件,默認(rèn)且一定要在這個目錄下

    #把uid小于100的數(shù)據(jù)導(dǎo)出到user1.txt文件中
   >select * from user where uid < 100 into outfile "/var/lib/mysql-files/user.txt" \
   >fields terminated by "*"\     /字段間隔符為 *
   >lines terminated by "#"       /行間隔符為 #

#################################################################################

管理表記錄

增

insert into 庫名.表名 values(值列表),(值列表);            /給所有字段賦值
insert into 庫名.表名(字段名列表) values(值列表),(值列表);  /只給個別字段賦值

查

select * from 庫名.表名;        /查詢表所有字段數(shù)據(jù)
select 字段名列表 from 庫名.表名     /查詢個別字段數(shù)據(jù)
#select name ,uid from user;

select * from 庫名.表名 where 條件;
#select * from user where name="root";  /查詢name字段為root的所有數(shù)據(jù)

條件匹配的表示方式: 
數(shù)值比較   >  >=   <   <=  =  !=
字符比較   =   !=

范圍內(nèi)匹配
(a) 字段名 in (值列表)     在...里
#select * from user where name in ("root","tom","apache");  /字段名匹配幾個 查詢幾條
(b)字段名 between 值1 and 值2   在...之間
#select * from user where uid between 10 and 50;   /字段名匹配幾個 查詢幾條
(c)not in
#select * from user where name not in ("root","tom","apache"); /相當(dāng)與 in 的取反,匹配的都不顯示

匹配空/非空
(a) is null
#select * from user where name is null;
(b) is not null
#select * fro user where name is not null;

不顯示重復(fù)值
(a)distinct 字段名
#select shell from user; /查詢表中的shell字段數(shù)據(jù)  ,其中有很多重復(fù)值
#select distinct shell from user  /把重復(fù)的值只顯示一次,可以快速查看此字段中都有那些數(shù)據(jù)

邏輯匹配  :   有多個條件
邏輯與  and    多個條件必須都成立
邏輯或  or        多個條件有一個條件成立即可
邏輯非  !         取反
#select name from user where name="tom" and uid=500;  /查詢 name字段為tom 且uid=500 ,顯示name字段數(shù)據(jù) 條件都必須成立
#select name from user where name="root" or uid=100;  /查詢name字段數(shù)據(jù) 其條件為 name=“root” 或者uid=100

數(shù)學(xué)運算操作   +   -   *   /   % 
字段類型必須是數(shù)值類型
#select uid,gid,uid+gid he from user; /查詢 uid,gid 和uid+gid 和 的字段信息   he(uid+gid的值的字段名,可變)

模糊查詢
where 字段名 like '表達(dá)式'
(a)"_" 表示一個字符
#select name from user where name like 'r_o_t';    /一個'_'表示一個字符
(b) % 0個或多個字符
#select name from user where name like 'r%';    /查詢name字段r開頭的所有數(shù)據(jù)
#select name from user where name like '%r%';   /查詢name字段包含r的數(shù)據(jù)
#select name from user where name like '%t';    /查詢name字段t結(jié)尾的所有數(shù)據(jù)
#select name from user where name liek 'r%' or name like '%t'; /以r開頭或者t結(jié)尾

正則匹配
where 字段名 regexp '正則表達(dá)式'
#select name from user where name regexp '[0-9]';  /查詢name字段中有數(shù)字的數(shù)據(jù)
#select name from user where name regexp '^[0-9]';  /查詢name字段中 數(shù)字開頭的數(shù)據(jù)
#select name from user where name regexp '^r';     /查詢name字段中 r開頭的數(shù)據(jù)
#select name from user where name regexp 'r.*t';  /查詢name字段中,r t之間包含0個或多個字符的數(shù)據(jù)

統(tǒng)計函數(shù)   字段得是數(shù)值類型

(a)sum(字段名) 求和
#select sum(uid) from user;
(b)avg(字段名) 平均值
#select avg(uid) from user;
(c)max(字段名) 最大值
#select max(uid) from user;
(d)min(字段名) 最小值
#select min(uid) from user;
(e)count(字段名) 統(tǒng)計個數(shù)
#select sum(uid) from user;   /不統(tǒng)計字段值為null  

查詢排序
>sql查詢 order by 字段名 /默認(rèn)升序   desc 降序
#select uid from user order by uid;  /查詢uid字段數(shù)據(jù)并升序排序
#select uid from user order by uid desc;  /查詢uid字段數(shù)據(jù)并降序排序
#select * from user order by uid;  /查詢表中所有數(shù)據(jù)并以uid升序排序

查詢分組
>sql查詢 group by 字段名    /把相同的數(shù)據(jù)放在group組里,相當(dāng)于不顯示重復(fù)值
#select shell from user group by shell; 

限制查詢顯示行數(shù) 
(a)limit 數(shù)字n; 顯示前n行
#select * from user limit 3;
#select * from user where uid between 10 and 50 limit 3;
(b)limit 數(shù)字m,數(shù)字n;  顯示m行后的n行
#select * from user limit 1,2; 顯示第1行后的兩行內(nèi)容 相當(dāng)于顯示2-3行內(nèi)容
#select * from user limit 1,1; 顯示第二行內(nèi)容

嵌套查詢 把內(nèi)層的查詢結(jié)果作為外層的查詢條件
select * from user where 條件 (select * from 表名 where 條件);
#select * from user where name in (select name from user where uid<10);
#select name,uid from user where uid > (select avg(uid) from user);

復(fù)制表  
作用:快速建表 、 備份表
create table 表名 sql查詢
#create table user1 select name,uid,homedir from user limit 3; /把sql查詢的結(jié)果作為新表的結(jié)構(gòu)及數(shù)據(jù),
#create table user2 select name,uid,shell from user limit 4;

多表查詢
select 字段名列表 from 表名列表;笛卡爾集
#select * from user1,user2;   /顯示為 3*4 12 行,user1表的每一行對應(yīng)user2表的每一行
#select * from user1,user2 where user1.name=user2.name and user1.uid=user2.uid; /顯示 user1 與user2 name,uid字段相等的行

連接查詢
(a)左連接查詢  以左邊表(A)為主查詢某個條件下 表A,表B的數(shù)據(jù)
select * from 表A left join 表B on 條件;
#select * from user1 left join user2 on user1.uid=user2.uid;
(b)右連接查詢 以左邊表(B)為主查詢某個條件下 表A,表B的數(shù)據(jù)
#select * from user1 right join user2 on user1.uid=user2.uid;

改
修改某字段內(nèi)的記錄
(a)update 表名 set 字段名="";
#update user1 set uid=3;
(b)update 表面 set 字段名="" where 條件;
#update user2 set uid=2 where name="root";

刪
刪除記錄
(a)delete from 表名;  /刪除表中記錄
#delete from user1;
(b)delete from 表名 where 條件; /刪除某個條件下的一行記錄

#delete from user1 where name="root";

看了以上關(guān)于MySQL數(shù)據(jù)庫的一些總結(jié)和講義,希望能給大家在實際運用中帶來一定的幫助。本文由于篇幅有限,難免會有不足和需要補充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時售前售后,隨時幫您解答問題的。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI