mysql-5.7.1 SQL语句示范

mysql-5.7.1 SQL语句示范

素材:

由于小编不想手敲20年了,所以就地取材,利用系统自带的文件做练手 /etc/passwd
这个文件的内容会因人而异,所以结果不要太过于执着,主要还是在sql语句,要以不变应万变嘛!毕竟工作这是必备的嘛~
废话不多说了,直接上!盘他

建立表格结构,数据导入

注意:小编的默认导出的路径已经修改,查看变量“secure_file_priv”的值

create  table  user  (
name  varchar(20),
pass char(1),
uid int,
gid int,
coment varhcar(200), 
home varchar(180), 
bash  varchar(180));

load  data  infile  "/mysql/passwd "
    -> into  table   user  
    -> fields  terminated  by  ':'
    -> lines  terminated  by  '\n'
    -> ;

6 在用户名字段下方添加s_year字段 存放出生年份 默认值是1990

 alter table  user   add  s_year   date   not  null  default  "1990-01-01"  after  name;

7 在用户名字段下方添加字段名sex 字段值只能是gril 或boy 默认值是 boy

alter  table  user   add  sex   enum('boy','girl') default  "boy" not  null after  name   ;

8 在sex字段下方添加 age字段 存放年龄 不允许输入负数。默认值 是 21

alter  table   user  
    -> add 
    -> age  
    -> tinyint  
    -> unsigned
    -> default  21 
    -> after  sex  ; 

9 把id字段值是10到50之间的用户的性别修改为 girl

alter  table  user  
    -> add  
    -> id  int  
    -> primary  key  
    -> auto_increment  
    -> first  ; 

update  user  set  sex  ='girl' where  id  between  10  and  20  ; 

10 统计性别是girl的用户有多少个。

select  count(sex) from    user  where  sex =  'girl '

12 查看性别是girl用户里 uid号 最大的用户名 叫什么。

select  name  from  user  where  uid  = (select  max(uid) from  user  ) and  sex =  'girl';

13 添加一条新记录只给name、uid 字段赋值 值为rtestd 1000

insert  into  user(name,uid)  values(name='rtestd' ,'1000');

添加一条新记录只给name、uid 字段赋值 值为rtest2d 2000

insert  into  user(name,uid)  values(name='rtest2d' ,'2000');

14 显示uid 是四位数的用户的用户名和uid值。

select  name  ,  uid  from  user  where  uid  >=1000  and  uid  <= 9999;

15 显示名字是以字母r 开头 且是以字母d结尾的用户名和uid。

select  name  ,uid  from  user  where  name like 'r%d'; 

16 查看是否有 名字以字母a开头 并且是 以字母c结尾的用户。

select  name  from  user  where  name  like  'a%c' ; 

8 把gid 在100到500间用户的家目录修改为/root

update  user   set  home  ='/root' where  uid  between  100 and  500  ; 

9 把用户是 root 、 bin 、 sync 用户的shell 修改为 /sbin/nologin

update  user  set bash='/sbin/nologin' where  name  in  ('root','bin','sync');

10 查看 gid 小于10的用户 都使用那些shell

select  bash  from  user  where  gid  <10 group  by  bash  ; 

12 删除 名字以字母d开头的用户。

delete  from  user  where  name  like 'd%' ;

13 查询 gid 最大的前5个用户 使用的 shell

select  bash  from user  where  gid  in  (select  gid from  user order by gid  desc   limit  5 ) ;

14 查看那些用户没有家目录

select  name  from  user  where  home  is null  ;

15 把gid号最小的前5个用户信息保存到/mybak/min5.txt文件里。

select  *  from  user  order  by  gid  limit  5   into  outfile  "/mysql/min5.txt"

使用useradd 命令添加登录系统的用户 名为lucy

system  useradd  lucy  

16 把lucy用户的信息 添加到user1表里

create table  user1  select   name  ,pass, uid,gid,comment, home ,bash   from  user  where  7 < 3 ; 
system  cp  /etc/passwd  /mysql/passwd1 
load  data  infile  "/mysql/passwd1" 
into  table  user1 
fields  terminated  by  ':'
lines  terminated  by '\n';

17 删除表中的 comment 字段

alter  table  user1  
drop  
comment  ; 

18 设置表中所有字段值不允许为空

alter  table user  
modify  
name varchar(50) not  null  ; 

19 删除root 用户家目录字段的值

update  user  set home=''  
where  name ='root';

20 显示 gid 大于500的用户的用户名 家目录和使用的shell

select  name  ,  gid  , bash from  user1 
where  gid  > 500 ; 

21 删除uid大于100的用户记录

delete  from  user1  where  uid > 100 ;

22 显示uid号在10到30区间的用户有多少个。

select  count(*) from  user1  where  uid >= 10 and  uid  <=30 ;

23 显示uid号是100以内的用户使用shell的类型。

select  bash  from  user1  where  uid <=100 group  by  bash  ; 

24 显示uid号最小的前10个用户的信息。

select  * from  user1  order  by  uid  limit  10  ; 

25 显示表中第10条到第15条记录

select  *  from  user  limit 10,15 ;

26 显示uid号小于50且名字里有字母a 用户的详细信息

select  * from  user  where  name  like  '%a%' and  uid  < 50  ;

27 只显示用户 root bin daemon 3个用户的详细信息。

select  * from  user  where  name  in ('bin','root','daemon');

28 显示除root用户之外所有用户的详细信息。

select  * from  user  where  name != 'root';

29 统计username 字段有多少条记录

select  count(name) from  user  ; 

30 显示名字里含字母c 用户的详细信息

select  * from  user  where  name  like  "%c%" ; 

31 在sex字段下方添加名为pay的字段,用来存储工资,默认值 是5000.00

alter table user 
add 
pay   double  default  5000.00
after  sex ;

32 把所有女孩的工资修改为10000

update  user  set  pay=10000  where  sex  = 'girl' ;

33 把root用户的工资修改为30000

update user  set  pay =30000 where  name = 'root' ;

给adm用户涨500元工资

update user  set  pay =5000+500 where  name = 'adm' ;

34 查看所有用户的名字和工资

select  name , pay  from  user  ;

35 查看工资字段的平均值

select  avg(pay) from  user ; 

36 查看工资字段值小于平均工资的用户 是谁。

select  name  from  user  where  pay  <  (select  avg(pay) from user );

查看女生里谁的uid号最大

select  name  from  user  where  (select  max(uid) from user  ) and  sex  = 'girl' ;

38 查看bin用户的uid gid 字段的值 及 这2个字段相加的和

select  uid  , gid ,uid+gid  as  sum    from  user  where  name =  'bin' ;

SQL语句还是要多加修炼打怪,不要小看基础的 update和delete的操作要多加注意!!!做好备份!做好备份!!做好备份!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值