数据库管理入门之三(数据库管理基础知识)
nanshan 2024-10-15 11:28 6 浏览 0 评论
一、数据导入、导出
1.1定义:把系统文件的内容存储到数据库的表里
1.2步骤:
1.2.1创建存储文件内容的表
1.2.2执行导入数据的sql命令,要求文件在指定路径下才可以导入成功
1.3相关指令:
1.3.1.在数据库里执行系统命令只需要在命令前加system即可,如:
system cp /etc/passwd /var/lib/mysql-files
1.3.2产看默认使用目录(需要导入的文件所存放的的位置)以及目录是否存在
show variables like “secure_file_priv”;
1.3.3导入数据到表的基本格式:
load data infile “目录名/文件名” into table 库名.表名 fields terminated by “列分隔符“ lines terminated by “\n(换行符)”;
ps:字段分隔符与文件内的必须一致,路径为绝对路径,导入数据的表字段类型要与文件字段匹配,需要禁用selinux。
1.3.4为导入的数据增加编号,方便管理(非必要操作)
alter table 表名 add id int(2) primary key auto_increment first;
1.3.5eg:
mysql> create table user(
-> name char(30),
-> password char(1),
-> uid int(2),
-> gid int(2),
-> cooment char(100),
-> homedir char(150),
-> shell char(50),
-> key name(name))engine=innodb default charset=latin1;
mysql> show variables like "secure_file_priv";
mysql> system cp /etc/passwd /var/lib/mysql-files
mysql> load data infile "/var/lib/mysql-files/passwd" into table user
-> fields terminated by ":"
-> lines terminated by "\n";
mysql> alter table user add
-> id int(2) primary key auto_increment first;
mysql> select * from user;
1.4修改导入目录
[root@localhost ~]# mkdir /myload ; chown mysql /myload
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
secure_file_priv="/myload"
[root@localhost ~]# systemctl restart mysqld
mysql> show variables like "secure_file_priv";
1.5表记录导出(表记录存储到系统文件里,不包括字段名)
导出时无需先建立文件名,直接指定即可,不然会重复冲突导不出;
基本用法:select xx from 表 into outfile “目录名/文件名” fields terminated by “分隔符” lines terminated by “换行符”;
ps:导出的内容由sql查询语句决定,需要禁用selinux,分隔符及换行符可不设置,默认为tab;
eg:
mysql> select * from user into outfile
-> "/var/lib/mysql-files/user1.txt";
mysql> select id ,name,password from user where id <=3
-> into outfile "/var/lib/mysql-files/user2.txt"
-> fields terminated by ":"
-> lines terminated by "\n";
二、管理表记录(增删改查)
2.1插入
2.1.1基本用法:
insert into 库.表 valules (字段值列表);
2.1.2一次插入多条记录:
insert into 库.表 valules (字段值列表1),(字段值列表2)...;
2.1.3给指定字段赋值,其他字段默认为空:
insert into 库.表(字段1,字段2...) values(值1,值2...);
2.1.4注意事项:
– 字段值要与字段类型相匹配;
– 对于字符类型的字段,要用双或单引号括起来;
– 依次给所有字段赋值时,字段名可以省略;
– 只给一部分字段赋值时,必须明确写出对应的字段名称。
2.2查
2.2.1基本用法:
select 字段名列表 from 库.表 where 条件;
ps:字段名列表决定显示哪些列,条件决定显示哪些行
2.3改
2.3.1基本用法:
update 库.表 set 字段名=”值“ where 条件;
ps:不加条件即为批量改1
2.4删
2.4.1基本用法:
delete from 库.表 条件;
ps:条件可不加,即删除表内所有记录。
三、匹配条件
3.1数值比较(= > >= < <= !=)
基本格式:
where 字段名 符号 值
where 字段名 符号 字段名(两个字段的字段值都是数值)
eg:
mysql> select * from user where id = 3;
mysql> select name,uid,gid from user where uid=gid;
mysql> update user set password="F",homedir="/student" where uid < 10;
3.2字符比较(只有两种,要么等,要么不等,= !=)
基本格式:
where 字段名 符号 “值”
where 字段名 is null //匹配空
where 字段名 is not null //匹配非空
eg:
mysql> select name from user where shell="/bin/bash";
mysql> select name,shell from user where shell != "/bin/bash";
mysql> select * from user where name="mysql";//查询是否有名字为mysql的用户;
mysql> insert into user values (89,"yaya","S",null,null,"MySQL_ Server","/var/lib/mysql","/bin/false");
mysql> select name,uid from user where uid is null;
mysql> update user set uid=4008 where uid is null and name="yaya";
mysql> select name,uid from user where name="yaya";
3.3逻辑比较
逻辑与 and 多个条件必须同时成立;
逻辑或 or 多个条件中某一个成立即可;
逻辑非 ! not 取反
eg:
mysql> select name,uid,shell from user where name="root" and uid=0 and shell ="/bin/bash";
mysql> select name,uid,shell from user where name="root" or uid=7 or shell="/sbin/nologin";
ps:逻辑判断默认顺序是从前往后执行,若要优先执行,可用“()”括起要优先判断的部分;
eg:
mysql> select name,uid from user where name="root" and uid=1 or uid=3;
mysql> select name,uid from user where uid=0 or uid=3 and name="root"的结果等于:
mysql> select name,uid from user where name="root" and (uid=0 or uid=3);
3.4范围内匹配
基本用法:
where 字段名 in (值1,值2...); //与其中的值匹配的都挑出来
where 字段名not in (值1,值2,...); //不包括值1,值2...其中任何一个值
where 字段名 between 10 and 20; //在10-20的范围内,包括10和20
eg:
mysql> select name from user where name in("apache","mysql","adm","bin");
mysql> select name,shell from user
-> where shell not in ("/bin/bash","/sbin/nologin");
mysql> select name,uid from user where uid in (10,20,30,40);
mysql> select name,uid from user where uid between 1 and 10;
3.5去重显示
当实际工作中只想查看字段下的值有多少种,可以不显示字段的重复值,只显示第一次出现时的值。
基本用法:
selsect distinct shell from ....
eg:
mysql> select distinct shell from user where uid<=10;
3.6高级查询条件
3.6.1模糊匹配
基本用法:
where 字段名 like ‘通配字串’
通配符:
“_”:匹配单个字符,匹配多个则输入相应数量的“_”;
“%”:匹配0~N个字符
ps:null是空值,指没有任何数据,不算0个字符,“”算0个字符;
eg:
mysql> select name,uid from user where name like "___";//“”里为3个_;
mysql> select name,uid from user where name like "r__";
mysql> select name,uid from user where name like "a%t";
mysql> select name ,uid from user where name like '__%__';//__为2个”_”;
//条件为name至少为4个字符才符合条件;
3.6.2正则匹配
基本用法:
where 字段名 regexp ‘正则表达式’;
常用正则符号:
“.”:单个字符;
“*”:任意个字符;
“^”:以什么开头;
“$”:以什么结尾;
“[]”:范围内;
eg:
mysql> select name ,uid from user where uid regexp '....';
条件为含有4个字符的,由于正则是包含即成立,因此包括了4个字符以上的,要严格匹配4个字符,可参考如下命令:
mysql> select name ,uid from user where uid regexp '^....