1 把/etc/passwd文件的内容存储到userdb库下的user表里,并做如下配置:
[root@host51 ~]# cp /etc/passwd /var/lib/mysql
mysql> create table user(name char(50),password char(10),uid int,gid int,comment char(150),homedir char(100),shell char(80));
mysql> load data infile “/var/lib/mysql-files/passwd” into table user fields terminated by “:” lines terminated by “\n”;
2 在用户名字段下方添加s_year字段 存放出生年份 默认值是1990
mysql> alter table user add s_year int default 1990 after name;
3 在用户名字段下方添加字段名sex 字段值只能是gril 或boy 默认值是 boy
mysql> alter table user add sex enum(“gril”,“boy”) default “boy” after name;
4 在sex字段下方添加 age字段 存放年龄 不允许输入负数。默认值 是 21
mysql> alter table user add age tinyint unsigned default 21 fater sex;
5 把uid字段值是10到50之间的用户的性别修改为 girl
mysql> update user set sex=“gril” where uid between 10 and 50;
6 统计性别是girl的用户有多少个。
mysql> select count(sex) from user where sex=“gril”;
7 查看性别是girl用户里 uid号 最大的用户名 叫什么。
mysql> select name from user where uid = (select max(uid) from user where sex=“gril”);
8 添加一条新记录只给name、uid 字段赋值 值为rtestd 1000
mysql> insert into user(name,uid) values(“rtestd”,1000);
9 添加一条新记录只给name、uid 字段赋值 值为rtest2d 2000
mysql> insert into user(name,uid) values(“rtest2d”,2000);
10 显示uid 是四位数的用户的用户名和uid值。
mysql> select name uid from user where uid="____";
11 显示名字是以字母r 开头 且是以字母d结尾的用户名和uid。
mysql> select name,uid from user where name regexp “^r.*d$”;
12 查看是否有 名字以字母a开头 并且是 以字母c结尾的用户。
mysql> select name from user where name regexp “^a.*c$”;
没有
13 把gid 在100到500间用户的家目录修改为/root
mysql> update user set homedir="/root" where gid between 100 and 500;
14 把用户是 root 、 bin 、 sync 用户的shell 修改为 /sbin/nologin
mysql> update user set shell="/sbin/nologin" where name=“root” or name=“bin” or name=“sync”;
15 查看 gid 小于10的用户 都使用那些shell
mysql> select shell,gid from user where gid < 10;
16 删除 名字以字母d开头的用户。
mysql> delete from user where name regexp “^d”;
17 查询 gid 最大的前5个用户 使用的 shell
mysql> select shell,name,gid from user where gid order by gid desc limit 5;
18 查看那些用户没有家目录
mysql> select * from user where homedir is null;
**19 把gid号最小的前5个用户信息保存到/mybak/min5.txt文件里。 **
[root@host51 ~]# mkdir -p /mybak/
[root@host51 ~]# chown mysql /mybak/
[root@host51 ~]# vim /etc/my.cnf
secure_file_priv="/mybak/"
[root@host51 ~]# systemctl restart mysqld
mysql> select * from user where gid is not null order by gid limit 5 into outfile “/mybak/min5.txt” lines terminated by “\n”;
**20 使用系统命令useradd 命令添加登录系统的用户 名为lucy **
[root@host51 ~]# useradd luc
[root@host51 ~]# tail -1 /etc/passwd
lucy❌1001:1001::/home/lucy:/bin/bash
21 把lucy用户的信息 添加到user表里
mysql> insert into user(name,password,uid,gid,comment,homedir,shell) values(“lucy”,“x”,“1001”,“1001”,"","/home/lucy","/bin/bash");
22 删除表中的 comment 字段
mysql> alter table user drop comment;
23 设置表中所有字段值不允许为空
mysql> delete from user where password is null;
mysql> alter table user modify name char(50) not null,modify sex enum(“gril”,“boy”) default “boy” not null,modify age tinyint unsigned default 21 not null,modify s_year int default 1990 not null,modify password char(10) not null,modify uid int not null,modify gid int not null, modify homedir char(100) not null,modify shell char(80) not null;
24 删除root 用户家目录字段的值
mysql> update user set homedir="" where name=“root”;
25 显示 gid 大于500的用户的用户名 家目录和使用的shell
mysql> select name,homedir,shell from user where gid > 500;
26 删除uid大于100的用户记录
mysql> delect from user where uid > 100;
27 显示uid号在10到30区间的用户有多少个。
mysql> select count(name) from user where uid between 10 and 30;
28 显示uid号是100以内的用户使用的shell。
mysql> select uid,shell from user where uid < 100;
29 显示uid号最小的前10个用户的信息。
mysql> select * from user order by uid limit 10;
30 显示表中第10条到第15条记录
mysql> select * from user limit 9;6;
31 显示uid号小于50且名字里有字母a 用户的详细信息
mysql> select * from user where uid < 50 and name regexp “a”;
32 只显示用户 root bin daemon 3个用户的详细信息。
mysql> select * from user where name=“root” or name="bin"or name=“daemon”;
33 显示除root用户之外所有用户的详细信息。
mysql> select * from user where name !=“root”;
34 统计user表name 字段有多少条记录
mysql> select count(name) from user;
35 显示名字里含字母c 用户的详细信息
mysql> select * from user where name regexp “c”;
36 在sex字段下方添加名为pay的字段,用来存储工资,默认值15000.00
mysql> alter table user add pay float(7,2) default 15000 after sex;
37 把所有女孩的工资修改为10000
mysql> update user set pay=“10000” where sex=“gril”;
38 把root用户的工资修改为30000
mysql> update user set pay=“30000” where name=“root”;
39 给adm用户涨500元工资
mysql> update user set pay=pay+500 where name=“adm” ;
40 查看所有用户的名字和工资
mysql> select name,pay from user;
41 查看工资字段的平均值
mysql> select avg(pay) from user;
42 显示工资字段值小于平均工资的用户名。
mysql> select name,pay from user where pay < (select avg(pay) from user );
43 查看女生里uid号最大用户名
mysql> select name from user where uid order by uid desc limit 1;
44 查看bin用户的uid gid 字段的值 及 这2个字段相加的和
mysql> select uid,gid ,uid+gid from user where name=“bin”;