1. 程式人生 > >mysql數據庫導入導出 查詢 修改表記錄

mysql數據庫導入導出 查詢 修改表記錄

oca cal 復制 lds etc gid type let 刪除

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 ‘正則表達式‘
^:開頭
$:結尾
.:任意字符
*:任意字符
[]:區間
實例: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數據庫導入導出 查詢 修改表記錄