目錄
2-4、刪除記錄 delete (刪除表裏的行),刪則至少是一行
3、對記錄做select update delete時可以使用的基本匹配條件的表示方式:
3-1、數值比較 > , >= , < , <= , = , !=
3-2、字符比較:= != is null is not null
4、對記錄做select update delete時可以使用的高級匹配條件的表示方式:
4-3、計算 操作 + - * / % (字段類型 必須是數值類型)
4-4、聚集函數(MySQL服務自帶的對數據做統計的命令);
5-2、sql查詢 order by 字段名 排序方式; 排序 asc desc
6-2-1、格式 1 : select 字段名列表 from 表名列表[ where 條件];
6-2-2、格式2 嵌套查詢: 把內層的查詢結果作爲外層查詢的查詢條件
1、數據導入、導出
數據導入:把系統文件的內容存儲到數據庫服務器的表裏--------有格式規律
數據導出:把數據庫服務器的表裏的記錄存儲到系統文件裏---有格式規律
1-1、自定義導出、導入數據 文件存儲路徑
mysql> show variables like "secure_file_priv";----來查看當前定義的導出、導入數據文件存儲路徑
如何自定義數據導出、導入文件存儲的目錄
[root@host50 ~]# mkdir /data
[root@host50 ~]# chown mysql /data/----該目錄的所有者必須爲mysql,否則mysqld程序執行者mysql將對其沒有相關權限
]#vim /etc/my.cnf
[mysqld]
secure_file_priv="/data"----定義的導出、導入數據文件存儲路徑爲/data
:wq
]#systemctl restart mysqld----重起服務生效
mysql> show variables like "secure_file_priv";---複查看是否更改成功
1-2、導入數據的步驟:
把系統文件拷貝指定的數據導出、導入文件存儲目錄下
創建存儲文件內容的表
導入數據 (命令格式)
mysql> system cp /etc/passwd /data/
mysql> system ls /data
passwd
create table db3.user(name char(50),password char(1),uid int(2),gid int(2),comment varchar(150),homedir char(150),
shell char(50),index(name));
MySQL> load data infile "/data/passwd" into table db3.user fields terminated by ":" lines terminated by "\n";----將/mydata/passwd文件中的內容導入db3庫的user表(每列之間通過:來分隔,每行之間通過換行來分隔)
1-3、數據導出:
mysql> sql查詢命令 into outfile "目錄名/文件名";
select * from t1 into outfile "/mydata/t1.txt";----列間不加分隔符的導出
select * from t1 into outfile "/mydata/t2.txt" fields terminated by "#";------列間通過加#分隔符來導出
mysql> select * from t1 into outfile "/mydata/t3.txt" fields terminated by "#" lines terminated by "\n";----列間通過加#分隔符行間通過換行來導出
2、管理表記錄
通過四種方式:
添加新記錄 insert into
查詢記錄 select
更新記錄字段的值 update
刪除記錄 delete
2-1、添加新記錄 insert into
insert into user values
(42,"bob","x",2000,2000,"student user","/home/bob","/bin/bash"),(43,"bob","x",2000,2000,"student user","/home/bob","/bin/bash");----添加多行可以通過逗號分隔
insert into user (name,shell,uid) values("tom","/sbin/nologin",1928),("alice","/sbin/nologin",1948);----一次可以只給部分字段賦值
2-2、查詢記錄 select
select 字段名列表 from 表 where 條件;
select nam,uid,shell from user;
2-3、更新記錄字段的值 update
update 表 set 字段名=值,字段名=值 ,.... where 條件;
update user set password="A",gid=1000;
2-4、刪除記錄 delete (刪除表裏的行),刪則至少是一行
delete from 表 where 條件;
delete from user where name="bob";
2-5、增刪改查注意事項:
- 字段值要與字段類型相匹配
- 讀與字符類型的字段,要使用單引或雙引
- 若不使用where等來限定條件,會更新所有記錄
- 限定條件時,只更新匹配條件的記錄
3、對記錄做select update delete時可以使用的基本匹配條件的表示方式:
3-1、數值比較 > , >= , < , <= , = , !=
where 字段名 符號 數值
select * from user where id <= 10;
selct name,uid from user where uid=9;
select name,uid,gid from user where uid=gid;
3-2、字符比較:= != is null is not null
where 字段名 符號 “值”
select name from user where name="apache";
select name,shell from user where shell != "/bin/bash";
匹配空 is null
匹配空 is not null
select id,name from user where name is null;-----匹配name爲null(空)的
select id,name from user where name="null";-----匹配name裏的值爲null的
select id,name from user where name="";-----匹配name裏的值爲空值的
注意空值和空完全不是一回事
3-3、邏輯比較 and(與) or(或) ! (非)
select * from user where name="root" and uid=1 and shell="/bin/bash";
select name,uid from user where name="root" or uid=1;
3-4、( ) 提高執行順序
select name,uid from user
where ( name="root" or name="bin" ) and uid=1;
3-5、範圍內匹配
select name from user
where name in ("root","sync","lucy","bob");
select name,uid from user where uid in (1,7,27,1000);
select name,shell from user where shell not in ("/bin/bash","/sbin/nologin");
select * from user where uid between 10 and 20;
select * from user where uid >=10 and uid<=20;
distinct (只適合select查詢使用)-------可以統計字段值分多少類
select distinct shell from user;
select distinct shell from user where uid <= 500;
4、對記錄做select update delete時可以使用的高級匹配條件的表示方式:
4-1、where 字段名 like '表達式';
- _ 匹配任意一個字符
- % 匹配零個或多個字符
select name from user where name like '___' ;匹配所有四個字符及四個字符以上的name字段
select name from user where name like '%a%' ;---匹配name字段值至少包含一個a的
select name from user where name like '%___%' ;-----匹配所有四個字符及四個字符以上的name字段
select id,name from user where name like '%' ;
4-2、正則匹配
where 字段名 regexp '正則表達式';
select name from user
where name regexp '^a.*t$';----匹配name字段值爲a開頭t結尾的
select name from user
where name regexp '[0-9]';----匹配所有包含數字的name字段
select name,uid from user
where uid regexp '....';----匹配所有四個字符及四個字符以上的name字段
select name,uid from user
where uid regexp '^....$';----匹配所有隻包含四個字符的name字段
4-3、計算 操作 + - * / % (字段類型 必須是數值類型)
update user set uid=uid+1 where id<=10;
select name,uid,gid , uid + gid jieguo from user where name="root";----列出uid+gid的結果,並且該字段命名爲jieguo
select name,uid,gid , (uid + gid) / 2 pjz from user where name="root";--列出(uid+gid)/2的結果,並且該字段命名爲plj
select name ,age , 2018-age s_year from user where name ="root";-----列出2018-age的結果,並且該字段命名爲s_year
4-4、聚集函數(MySQL服務自帶的對數據做統計的命令);
注意:字段類型 必須是數值類型
select sum(uid) from user where id<=10;-----對所有uid<10的uid字段的值求和
select avg(uid) from user where id<=10;-----對所有uid<10的uid字段的值求和後做平均
select min(uid) from user where id<=10;----列出所有uid<10的uid字段的最小值
select max(uid) from user where id<=10;----列出所有uid<10的uid字段的最小值
select count(name) from user where shell="/bin/bash";-----列出那麼字段中非空的總行數
select count(*) from user;-------列出user表的總行數
注意函數不能用來做條件查詢:
5、操作查詢結果 分組 排序 限制顯示函數 過濾數據
5-1、sql查詢 group by 字段名; 查詢分組
select shell from user group by shell;------根據shell字段值列出所有的shell字段值類型
select distinct shell from user;-----全表查詢列出所有shell字段值類型-----相對比較慢
5-2、sql查詢 order by 字段名 排序方式; 排序 asc desc
select name,uid from user where uid>=10 and uid<=500
order by uid desc ;-------按降序排列,字段值爲null的作爲最小來排
5-3、limit 限制查詢結果顯示的行數
sql查詢 limit 數字;-------原型爲:sql查詢 limit 0 ,x; 顯示查詢結果從行號爲0的行開始後的前 x行,因爲這裏的第一行行號爲0
sql查詢 limit 數字1 , 數字2; 顯示指定範圍內的行;(0 表示第1行)------從(數字1+1)行開始後的前(數字2)行
select name,shell from user where uid <=500 limit 1,5
select * from user limit 4,3;
5-4、having 條件 在查詢結果裏查找數據
sql查詢 having 條件;
select name,uid from user where uid >=1000
having name is null;-------列出uid大於等於1000的name字段值爲null的行的name字段和uid字段的值
select name,uid from user where uid >=1000
having uid=65534;------列出uid大於等於1000的name字段值爲65534的行的name字段和uid字段的值
select name,uid from user where uid >=1000
having name="bob";------列出uid大於等於1000的name字段值爲bob的行的name字段和uid字段的值
6、多表查詢 ***
6-1、 複製表 (作用: 備份表 、 快速建表)
mysql> create table 表名 sql查詢;--------快速建表
create table user2 select * from db3.user;---user表做母版複製user2表
show tables;
select * from user2;
create table user3 select name,uid,shell from db3.user order by uid desc limit 5;---複製一個表user3包含user表前5行中name,uid,shell字段的列
select * from user3;
select * from user where shell="abc";
create table user4 select * from db3.user where shell="abc";-----複製一個表user4包含user表中shell字段值爲abc的行
select * from user4;
desc user4;
6-2、 多表查詢
6-2-1、格式 1 : select 字段名列表 from 表名列表[ where 條件];
create table t1 select name,uid,shell from db3.user limit 3;
select * from t1;
create table t2
select name,password,uid,homedir from db3.user limit 5;
select * from t2;
笛卡爾集 3 * 5 = 15------總行數爲兩張錶行數的乘積
select * from t1,t2;
select t1.name ,t2.name from t1,t2;
select t1.* ,t2.password,t2.homedir from t1,t2;
select t1.* , t2.password , t2.homedir from t1,t2
where t1.uid = t2.uid;
6-2-2、格式2 嵌套查詢: 把內層的查詢結果作爲外層查詢的查詢條件
select 字段名列表 from 表名 where 條件 (select 字段名列表 from 表名 where 條件);
select name from db3.user order by uid limit 1;
select avg(uid) from db3.user;
select name,uid from db3.user where uid<( select avg(uid) from db3.user);
select name from db4.t1;
select name from db3.user where name in (select name from db4.t1);
6-2-3、格式3 連接查詢
左連接查詢 : 以左邊的表爲主顯示查詢結果
select 字段名列表 from 表名A left join 表名B on 條件;
右連接查詢 : 以右邊的表爲主顯示查詢結果
select 字段名列表 from 表名A right join 表名B on 條件;
create table db4.t3 select name,uid,shell from db3.user limit 5;
select * from db4.t3;
create table db4.t4 select name,uid,shell from db3.user limit 7;
select * from db4.t4;
select * from t3 left join t4 on t3.uid = t4.uid;
select * from t3 right join t4 on t3.uid = t4.uid;
select t3.name,t4.name from t3 right join t4 on t3.uid = t4.uid;
7、MySQL圖形管理工具
7-1 :部署運行環境lamp 或 lnmp
rpm -q httpd
yum -y install httpd
systemctl start httpd
systemctl enable httpd
rpm -q php
rpm -q php-mysql
yum -y install php php-mysql
systemctl restart httpd
7-2 :安裝軟件phpMyAdmin
tar -zxvf phpMyAdmin-2.11.11-all-languages.tar.gz
ls phpMyAdmin-2.11.11-all-languages
mv phpMyAdmin-2.11.11-all-languages /var/www/html/phpadmin
cd /var/www/html/
7-3 : 創建配置,指定管理數據庫服務器
]#cd phpadmin
]#cp config.sample.inc.php config.inc.php
]#vim config.inc.php
<?php
.....
17 $cfg['blowfish_secret'] = 'pljabc';
31 $cfg['Servers'][$i]['host'] = 'localhost';
......
?>
:wq
步驟4 : 客戶端訪問
打開瀏覽器輸入URL http://192.168.4.50/phpadmin
用戶 root
密碼 123456
感謝您的拜讀,喜歡請點贊!!!