mysql資料庫匯入匯出 查詢 修改表記錄

來源:互聯網
上載者:User

標籤:sbin   avg   []   使用   求和   串連   最大   mysqld   gid   

mysql資料匯入匯出:

匯入:
把系統的檔案的內容,儲存到資料庫的表裡

匯入資料的基本格式:
mysql> load data infile "檔案名稱" into table 表名 fields terminated by ‘分隔字元‘ lines terminated by ‘\n‘;

執行個體:把系統使用者資訊儲存到hydra01庫下的userinfo表裡
mysql> create table userinfo(name char(20),password char(1),uid int(2),gid int(2),comment varchar(50),homedir varchar(60),shell varchar(25),index(name));
mysql> load data infile "/etc/passwd" into table hydra01.userinfo fields terminated by ":" lines terminated by ‘\n‘;(匯入資料)
mysql> alter table userinfo add id int(2) auto_increment primary key first;(添加編號)
mysql> desc userinfo;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| name | char(20) | YES | MUL | NULL | |
| password | char(1) | YES | | NULL | |
| uid | int(2) | YES | | NULL | |
| gid | int(2) | YES | | NULL | |
| comment | varchar(50) | YES | | NULL | |
| homedir | varchar(60) | YES | | NULL | |
| shell | varchar(25) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+

匯出:
把資料庫中表裡的記錄儲存到系統檔案裡

匯出資料的基本格式:
格式一:mysql> select * from 庫.表 into outfile "檔案名稱";
格式二:mysql> select * from 庫.表 into outfile "檔案名稱" fields terminated by "符號";

執行個體:把mysql庫下user表裡的所有記錄儲存到系統檔案xx.txt裡
mysql> select * from mysql.user into outfile "xx.txt";
[[email protected] ~]# find / -name "xx.txt"
/var/lib/mysql/xx.txt(匯出的內容預設存放在mysql下)

也可以匯出到指定目錄下
[[email protected] ~]# mkdir mysqldata
[[email protected] ~]# chown mysql /root/mysqldata(更改屬主)
mysql> select * from mysql.user into outfile "/root/mysqldata/xx2.txt";


——————————————————————————————————————————————————————————————————

管理表記錄

向表中插入新記錄
一次向表中插入一條新記錄,給新記錄的每個欄位都賦值
格式:mysql> insert into 庫名.表名 values(欄位值列表);
執行個體:給hydra01庫的userinfo表插入一條新資訊
mysql> insert into hydra01.userinfo values(26,"hydra","x",2003,20003,"hail hydra","/home/hydra","/bin/bash");


一次向表中插入多條新記錄,給新記錄的每個欄位都賦值
格式:mysql> insert into 庫名.表名 values(欄位值列表),values(欄位值列表),(欄位值列表);
執行個體:給hydra01庫的userinfo表插入多條新資訊
mysql> insert into hydra01.userinfo values(29,"FBI","x",1937,1937,"hail hydra","/home/hydra","/bin/bash"),(30,"CIA","x",1937,1937,"hail hydra","/home/hydra","/bin/bash");


一次向表中插入多條新記錄,給新記錄的指定欄位賦值
格式:mysql> insert into 庫名.表名(欄位名列表) values(欄位值列表);
執行個體:給hydra01庫的userinfo表插入新資訊,並只給指定欄位賦值
mysql> insert into hydra01.userinfo(name,password,uid,gid,comment,homedir,shell)values("Anonymousx","x",1937,1937,"teacher","/home/Anonymousx","/sbin/nogin");

查看效果
mysql> select * from userinfo;
+----+------------+----------+------+-------+------------------------------+---------------------+----------------+
| 26 | hydra | x | 2003 | 20003 | hail hydra | /home/hydra | /bin/bash |
| 27 | FBI | x | 2003 | 2003 | hail hydra | /home/hydra | /bin/bash |
| 28 | CIA | x | 2003 | 2003 | hail hydra | /home/hydra | /bin/bash |
| 29 | FBI | x | 1937 | 1937 | hail hydra | /home/hydra | /bin/bash |
| 30 | CIA | x | 1937 | 1937 | hail hydra | /home/hydra | /bin/bash |
| 31 | Anonymous | NULL | NULL | NULL | NULL | NULL | NULL |
| 32 | Anonymousx | x | 1937 | 1937 | teacher | /home/Anonymousx | /sbin/nogin |
+----+------------+----------+------+-------+------------------------------+---------------------+----------------+


—————————————————————————————————————————————————————————————————————————————————————

查詢表
格式:
select 欄位名列表 from 庫名.表名 where 條件;

條件的表示方式:
數值比較:
條件式格式設定:欄位名 符號 數值
= != < > <= >=
執行個體:mysql> select * from userinfo where id <=10;

字元比較:
條件式格式設定:欄位名 符號 "值"
= != "
執行個體:mysql> select * from userinfo where shell!="sbin/nologin";

範圍匹配:
條件式格式設定:欄位名 符號 匹配
between..adn.. /在..之間
in (值列表) /在裡面
not in(值列表) /不在裡面
執行個體:mysql> select * from userinfo where uid between 10 and 20;

邏輯匹配:
條件式格式設定:欄位名 符號 匹配 (多個查詢條件時使用)
and:多個條件同時匹配
or:多個條件,匹配某一條件就可以
!:取反
執行個體:mysql> select * from userinfo where name="mysql" or uid=3000 ;

匹配空:
條件式格式設定:欄位名 符號 匹配
is null
執行個體:mysql> select * from userinfo where name is null;

匹配非空:
條件式格式設定:欄位名 符號 匹配
is not null
執行個體:mysql> select * from userinfo where name is not null;

模糊查詢:
條件式格式設定:欄位名 like ‘運算式‘
%:匹配0個或多個字元
_:匹配任意一個字元
執行個體:mysql> select * from userinfo where name like ‘____‘;

正則匹配:
條件式格式設定:欄位名 regexp ‘Regex‘
^:開頭
$:結尾
.:任一字元
*:任一字元
[]:區間
執行個體:mysql> select * from userinfo where uid regexp ‘[0-100]‘;

數學計算:
條件式格式設定:欄位名 計算 as 起名字 from 計算的表;
+ - * / %(取於)
執行個體:mysql> select uid,gid,(uid+gid)/2 as zhi from userinfo;

聚集合函式:
條件式格式設定:select xxx(欄位名) from 表;
max(欄位名)求最大值
min(欄位名)求最小值
avg(欄位名)求評價值
sum(欄位名)求和
count(欄位名)統計值個數
執行個體:mysql> select max(uid) from userinfo;

給查詢結果排序:
格式:order by 欄位名 排序方式;
排序方式:
asc:升序(預設排序方式)
desc:降序
執行個體:mysql> select name,uid from userinfo where uid >=10 and uid <=50 order by uid desc;

給查詢結果分組:
格式:group by 欄位名
執行個體: mysql> select shell from userinfo where uid <=10 group by shell;

限制顯示查詢結果顯示的記錄數inmit:
格式:limit 行數;
執行個體:mysql> select name,uid from userinfo where uid <=8 limit 2;


綜合測試:
1.輸出userinfo表中uid號最大的使用者資訊
測試:mysql> select * from userinfo order by uid desc limit 1;

2.輸出表中uid號是兩位元的最大的使用者名稱和uid號
測試:mysql> select name,uid from userinfo where uid regexp ‘^..$‘ order by uid desc limit 1;


查看錶中合格記錄,【所有】欄位的值
格式:mysql> select * from 庫名.表名 wher 條件;
執行個體:
mysql> select * from userinfo where id <=10;(數值比較)
mysql> select * from userinfo where shell!="sbin/nologin";(字元比較)
mysql> select * from userinfo where uid between 10 and 20;(範圍匹配)
mysql> select * from userinfo where uid in (5,15,25);(範圍匹配)
mysql> select * from userinfo where name in ("apache","mysql");(範圍匹配)
mysql> select * from userinfo where name not in ("apache","mysql");(範圍匹配)
mysql> select * from userinfo where name regexp ‘[0-9]‘;(正則)

查看錶中符合條件記錄,【指定】欄位的值
格式:mysql> select 指定欄位 from 庫名.表名 wher 條件;
執行個體:
mysql> select id from userinfo where id <=10;(數值比較)
mysql> select name from userinfo where shell="sbin/nologin";(字元比較)
mysql> select name,id from userinfo where uid between 10 and 20;(範圍匹配)
mysql> select name from userinfo where name="mysql" and uid=3000 ;(邏輯匹配)
mysql> select name from userinfo where name="mysql" or uid=3000 ;(邏輯匹配)
mysql> select name from userinfo where name is null;(匹配空)
mysql> select name from userinfo where name is not null;(匹配非空)
mysql> select name from userinfo where name like ‘____‘;(模糊查詢)
mysql> select max(uid) from userinfo;(聚集合函式)
mysql> select uid,gid,(uid+gid)/2 as zhi from userinfo;(數學計算)

—————————————————————————————————————————————————————————————————————————————————————

巢狀查詢:把內層查詢結果作為外層查詢的查詢條件。
格式:where 條件(子查詢);
執行個體:把userinfo表中uid欄位的值小於次欄位平均值的使用者名稱和uid號顯示出來
測試:mysql> select name,uid from userinfo where uid < (select avg(uid) from userinfo);
執行個體:巢狀查詢可以跨庫查詢
測試:mysql> select name from userinfo where name in (select user from mysql.user where user="root" and host="localhost");

複製表:(原表的索引不會被複製)
格式:create table 新名字 select * from 被複製的表
測試:mysql> create table userinfox select * from userinfo;
執行個體:複製userinfo表的前十條
測試;mysql> create table userinfox1 select * from userinfo limit 10;

複製表結構:(複製表結構不會複製資料)
格式:格式:create table 新名字 select * from 被複製的表 where 1 = 2;
測試:mysql> create table userinfox2 select * from userinfo where 1 = 2;

多表查詢:
格式一:select 欄位名 from 表名,表名;
格式二:select 欄位名 from 表名,表名 where 條件;
測試:mysql> select * from userinfox,hydra;
測試:mysql> select userinfo.name,userinfox.name from userinfo,userinfox where userinfo.uid = userinfox.uid;

串連查詢:
左串連查詢:left join ...on(以左邊的表為主顯示查詢結果)
格式:select * from 左表 left join 右表 on 條件;
測試:mysql> select * from userinfo left join userinfox on userinfo.name=userinfox.name;

右連結查詢:right join ...on(以右邊的表為主顯示查詢結果)
格式:select * from 左表 right join 右表 on 條件;
測試:mysql> select * from userinfo right join userinfox on userinfo.name=userinfox.name;

 

修改表記錄
批量修改:
格式:update 庫名.表名 set 欄位名=值,欄位名=值;
測試:mysql> update userinfox set uid=0,gid=0;
修改符合條件記錄欄位的值:
格式:update 庫名.表名 set 欄位名=值,欄位名=值 where 條件;
測試:mysql> update userinfox set homedir="/opt" where name="/bin/bash";


刪除表記錄
刪除所有記錄:
格式:delete from 表名;
測試:delete from hydra01;
刪除合格記錄:
格式:delete from 表名 where 條件;
測試:mysql> delete from userinfo where id is not null;


————————————————————————————————————————————————————————————————————————

 

mysql資料庫匯入匯出 查詢 修改表記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.