案例:Mysql基本查询练习

+++++++++++++++++++++++++++++++++++++++++++++++++++

表基本操作练习题

1 复制user表的所有记录到teadb库的teacher表里

只复制user表的表结构给新teadb库的tea2表

mysql> create table teacher select * from teadb;

Query OK, 41 rows affected (0.32 sec)

Records: 41  Duplicates: 0  Warnings: 0

 

mysql> select * from teacher;

+—-+———————+——+——+——–+———-+——-+——-+—————————————————————–+—————————+—————-+———-+

| id | name                | sex  | age  | s_year | password | uid   | gid   | comment                                                         | homedir                   | shell          | pay      |

+—-+———————+——+——+——–+———-+——-+——-+—————————————————————–+—————————+—————-+———-+

|  1 | root                | boy  |   21 |   1990 | x        |     0 |     0 | root                                                            | /root                     | /sbin/nologin  | 30000.00 |

|  2 | bin                 | boy  |   21 |   1990 | x        |     1 |     1 | bin                                                             | /bin                      | /sbin/nologin  |  5000.00 |

|  4 | adm                 | boy  |   21 |   1990 | x        |     3 |     4 | adm                                                             | /var/adm                  | /sbin/nologin  |  5000.00 |

|  5 | lp                  | boy  |   21 |   1990 | x        |     4 |     7 | lp                                                              | /var/spool/lpd            | /sbin/nologin  |  5000.00 |

|  6 | sync                | boy  |   21 |   1990 | x        |     5 |     0 | sync                                                            | /sbin                     | /sbin/nologin  |  5000.00 |

|  7 | shutdown            | boy  |   21 |   1990 | x        |     6 |     0 | shutdown                                                        | /sbin                     | /sbin/shutdown |  5000.00 |

|  8 | halt                | boy  |   21 |   1990 | x        |     7 |     0 | halt                                                            | /sbin                     | /sbin/halt     |  5000.00 |

|  9 | mail                | boy  |   21 |   1990 | x        |     8 |    12 | mail                                                            | /var/spool/mail           | /sbin/nologin  |  5000.00 |

| 10 | operator            | girl |   21 |   1990 | x        |    11 |     0 | operator                                                        | /root                     | /sbin/nologin  | 10000.00 |

| 11 | games               | girl |   21 |   1990 | x        |    12 |   100 | games                                                           | /root                     | /sbin/nologin  | 10000.00 |

| 12 | ftp                 | girl |   21 |   1990 | x        |    14 |    50 | FTP User                                                        | /var/ftp                  | /sbin/nologin  | 10000.00 |

| 13 | nobody              | girl |   21 |   1990 | x        |    99 |    99 | Nobody                                                          | /                         | /sbin/nologin  | 10000.00 |

| 14 | systemd-network     | girl |   21 |   1990 | x        |   192 |   192 | systemd Network Management                                      | /root                     | /sbin/nologin  | 10000.00 |

| 16 | polkitd             | girl |   21 |   1990 | x        |   999 |   998 | User for polkitd                                                | /                         | /sbin/nologin  | 10000.00 |

| 17 | libstoragemgmt      | girl |   21 |   1990 | x        |   998 |   996 | daemon account for libstoragemgmt                               | /var/run/lsm              | /sbin/nologin  | 10000.00 |

| 18 | rpc                 | girl |   21 |   1990 | x        |    32 |    32 | Rpcbind Daemon                                                  | /var/lib/rpcbind          | /sbin/nologin  | 10000.00 |

| 19 | colord              | girl |   21 |   1990 | x        |   997 |   995 | User for colord                                                 | /var/lib/colord           | /sbin/nologin  | 10000.00 |

| 20 | saslauth            | girl |   21 |   1990 | x        |   996 |    76 | Saslauthd user                                                  | /run/saslauthd            | /sbin/nologin  | 10000.00 |

| 21 | abrt                | girl |   21 |   1990 | x        |   173 |   173 |                                                                 | /root                     | /sbin/nologin  | 10000.00 |

| 22 | rtkit               | girl |   21 |   1990 | x        |   172 |   172 | RealtimeKit                                                     | /root                     | /sbin/nologin  | 10000.00 |

| 23 | radvd               | girl |   21 |   1990 | x        |    75 |    75 | radvd user                                                      | /                         | /sbin/nologin  | 10000.00 |

| 24 | chrony              | girl |   21 |   1990 | x        |   995 |   993 |                                                                 | /var/lib/chrony           | /sbin/nologin  | 10000.00 |

| 25 | tss                 | girl |   21 |   1990 | x        |    59 |    59 | Account used by the trousers package to sandbox the tcsd daemon | /dev/null                 | /sbin/nologin  | 10000.00 |

| 26 | usbmuxd             | girl |   21 |   1990 | x        |   113 |   113 | usbmuxd user                                                    | /root                     | /sbin/nologin  | 10000.00 |

| 27 | geoclue             | girl |   21 |   1990 | x        |   994 |   991 | User for geoclue                                                | /var/lib/geoclue          | /sbin/nologin  | 10000.00 |

| 28 | qemu                | girl |   21 |   1990 | x        |   107 |   107 | qemu user                                                       | /root                     | /sbin/nologin  | 10000.00 |

| 29 | rpcuser             | girl |   21 |   1990 | x        |    29 |    29 | RPC Service User                                                | /var/lib/nfs              | /sbin/nologin  | 10000.00 |

| 30 | nfsnobody           | girl |   21 |   1990 | x        | 65534 | 65534 | Anonymous NFS User                                              | /var/lib/nfs              | /sbin/nologin  | 10000.00 |

| 31 | setroubleshoot      | girl |   21 |   1990 | x        |   993 |   990 |                                                                 | /var/lib/setroubleshoot   | /sbin/nologin  | 10000.00 |

| 32 | pulse               | girl |   21 |   1990 | x        |   171 |   171 | PulseAudio System Daemon                                        | /root                     | /sbin/nologin  | 10000.00 |

| 33 | gdm                 | girl |   21 |   1990 | x        |    42 |    42 |                                                                 | /var/lib/gdm              | /sbin/nologin  | 10000.00 |

| 34 | gnome-initial-setup | girl |   21 |   1990 | x        |   992 |   987 |                                                                 | /run/gnome-initial-setup/ | /sbin/nologin  | 10000.00 |

| 35 | sshd                | girl |   21 |   1990 | x        |    74 |    74 | Privilege-separated SSH                                         | /var/empty/sshd           | /sbin/nologin  | 10000.00 |

| 36 | avahi               | girl |   21 |   1990 | x        |    70 |    70 | Avahi mDNS/DNS-SD Stack                                         | /var/run/avahi-daemon     | /sbin/nologin  | 10000.00 |

| 37 | postfix             | girl |   21 |   1990 | x        |    89 |    89 |                                                                 | /var/spool/postfix        | /sbin/nologin  | 10000.00 |

| 38 | ntp                 | girl |   21 |   1990 | x        |    38 |    38 |                                                                 | /etc/ntp                  | /sbin/nologin  | 10000.00 |

| 39 | tcpdump             | girl |   21 |   1990 | x        |    72 |    72 |                                                                 | /                         | /sbin/nologin  | 10000.00 |

| 40 | lisi                | girl |   21 |   1990 | x        |  1000 |  1000 | lisi                                                            | /home/lisi                | /bin/bash      | 10000.00 |

| 41 | mysql               | girl |   21 |   1990 | x        |    27 |    27 | MySQL Server                                                    | /var/lib/mysql            | /bin/false     | 10000.00 |

| 42 | rtestd              | boy  |   21 |   1990 | NULL     |  1000 |  NULL | NULL                                                            | NULL                      | NULL           |  5000.00 |

| 43 | rtest2d             | boy  |   21 |   1990 | NULL     |  2000 |  NULL | NULL                                                            | NULL                      | NULL           |  5000.00 |

+—-+———————+——+——+——–+———-+——-+——-+—————————————————————–+—————————+—————-+———-+

41 rows in set (0.00 sec)

 

 

mysql> create table tea2 select * from teadb where 1=2;

Query OK, 0 rows affected (0.29 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc tea2;

+———-+——————–+——+—–+———+——-+

| Field    | Type               | Null | Key | Default | Extra |

+———-+——————–+——+—–+———+——-+

| id       | int(3)             | NO   |     | 0       |       |

| name     | char(50)           | YES  |     | NULL    |       |

| sex      | enum(‘boy’,’girl’) | YES  |     | boy     |       |

| age      | int(2) unsigned    | YES  |     | 21      |       |

| s_year   | int(4)             | YES  |     | 1990    |       |

| password | char(30)           | YES  |     | NULL    |       |

| uid      | int(3)             | YES  |     | NULL    |       |

| gid      | int(3)             | YES  |     | NULL    |       |

| comment  | char(80)           | YES  |     | NULL    |       |

| homedir  | char(50)           | YES  |     | NULL    |       |

| shell    | char(50)           | YES  |     | NULL    |       |

| pay      | float(7,2)         | YES  |     | 5000.00 |       |

+———-+——————–+——+—–+———+——-+

12 rows in set (0.00 sec)

 

2 查看teadb库的tea2表的表结构,并删除 id 字段

mysql> create table tea2 select * from teadb where 1=2;

Query OK, 0 rows affected (0.29 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc tea2;

+———-+——————–+——+—–+———+——-+

| Field    | Type               | Null | Key | Default | Extra |

+———-+——————–+——+—–+———+——-+

| id       | int(3)             | NO   |     | 0       |       |

| name     | char(50)           | YES  |     | NULL    |       |

| sex      | enum(‘boy’,’girl’) | YES  |     | boy     |       |

| age      | int(2) unsigned    | YES  |     | 21      |       |

| s_year   | int(4)             | YES  |     | 1990    |       |

| password | char(30)           | YES  |     | NULL    |       |

| uid      | int(3)             | YES  |     | NULL    |       |

| gid      | int(3)             | YES  |     | NULL    |       |

| comment  | char(80)           | YES  |     | NULL    |       |

| homedir  | char(50)           | YES  |     | NULL    |       |

| shell    | char(50)           | YES  |     | NULL    |       |

| pay      | float(7,2)         | YES  |     | 5000.00 |       |

+———-+——————–+——+—–+———+——-+

12 rows in set (0.00 sec)

 

mysql> alter table tea2 drop id;

Query OK, 0 rows affected (0.76 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc tea2;

+———-+——————–+——+—–+———+——-+

| Field    | Type               | Null | Key | Default | Extra |

+———-+——————–+——+—–+———+——-+

| name     | char(50)           | YES  |     | NULL    |       |

| sex      | enum(‘boy’,’girl’) | YES  |     | boy     |       |

| age      | int(2) unsigned    | YES  |     | 21      |       |

| s_year   | int(4)             | YES  |     | 1990    |       |

| password | char(30)           | YES  |     | NULL    |       |

| uid      | int(3)             | YES  |     | NULL    |       |

| gid      | int(3)             | YES  |     | NULL    |       |

| comment  | char(80)           | YES  |     | NULL    |       |

| homedir  | char(50)           | YES  |     | NULL    |       |

| shell    | char(50)           | YES  |     | NULL    |       |

| pay      | float(7,2)         | YES  |     | 5000.00 |       |

+———-+——————–+——+—–+———+——-+

11 rows in set (0.00 sec)

 

3 把/etc/passwd文件的内容保存到teadb库的tea2表里

mysql> create table teadb(

-> name char(50),

-> password char(30),

-> uid int(3),

-> gid int(3),

-> comment char(80),

-> homedir char(50),

-> shell char(50)

-> );

mysql> desc teadb;

+———-+———-+——+—–+———+——-+

| Field    | Type     | Null | Key | Default | Extra |

+———-+———-+——+—–+———+——-+

| name     | char(50) | YES  |     | NULL    |       |

| password | char(30) | YES  |     | NULL    |       |

| uid      | int(3)   | YES  |     | NULL    |       |

| gid      | int(3)   | YES  |     | NULL    |       |

| comment  | char(80) | YES  |     | NULL    |       |

| homedir  | char(50) | YES  |     | NULL    |       |

| shell    | char(50) | YES  |     | NULL    |       |

+———-+———-+——+—–+———+——-+

 

Qmysql> load data infile “/mysqldir/passwd” into table teadb fields terminated by “:” lines terminated by “\n”;

Query OK, 41 rows affected (0.07 sec)

Records: 41  Deleted: 0  Skipped: 0  Warnings: 0

uery OK, 0 rows affected (0.28 sec)

 

对teadb库的tea2表执行如下操作:

 

4 把name字段设置为index字段

mysql> alter table teadb add index(name);

Query OK, 0 rows affected (0.24 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

5 添加记录编号字段id  在所有字段上方,字段值可以自动增长。

mysql> alter table teadb add id int(3) primary key auto_increment first;

Query OK, 0 rows affected (0.61 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc teadb;

+———-+———-+——+—–+———+—————-+

| Field    | Type     | Null | Key | Default | Extra          |

+———-+———-+——+—–+———+—————-+

| id       | int(3)   | NO   | PRI | NULL    | auto_increment |

| name     | char(50) | YES  | MUL | NULL    |                |

| password | char(30) | YES  |     | NULL    |                |

| uid      | int(3)   | YES  |     | NULL    |                |

| gid      | int(3)   | YES  |     | NULL    |                |

| comment  | char(80) | YES  |     | NULL    |                |

| homedir  | char(50) | YES  |     | NULL    |                |

| shell    | char(50) | YES  |     | NULL    |                |

+———-+———-+——+—–+———+—————-+

8 rows in set (0.00 sec)

 

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

mysql> alter table teadb add s_year int(4) default 1990 after name;

Query OK, 0 rows affected (0.57 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc teadb;

+———-+———-+——+—–+———+—————-+

| Field    | Type     | Null | Key | Default | Extra          |

+———-+———-+——+—–+———+—————-+

| id       | int(3)   | NO   | PRI | NULL    | auto_increment |

| name     | char(50) | YES  | MUL | NULL    |                |

| s_year   | int(4)   | YES  |     | 1990    |                |

| password | char(30) | YES  |     | NULL    |                |

| uid      | int(3)   | YES  |     | NULL    |                |

| gid      | int(3)   | YES  |     | NULL    |                |

| comment  | char(80) | YES  |     | NULL    |                |

| homedir  | char(50) | YES  |     | NULL    |                |

| shell    | char(50) | YES  |     | NULL    |                |

+———-+———-+——+—–+———+—————-+

9 rows in set (0.00 sec)

 

 

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

mysql> alter table teadb add sex enum(“boy”,”girl”) default “boy”;

mysql> alter table teadb change sex sex enum(“boy”,”girl”) default “boy” after name;

mysql> desc teadb;

+———-+——————–+——+—–+———+—————-+

| Field    | Type               | Null | Key | Default | Extra          |

+———-+——————–+——+—–+———+—————-+

| id       | int(3)             | NO   | PRI | NULL    | auto_increment |

| name     | char(50)           | YES  | MUL | NULL    |                |

| sex      | enum(‘boy’,’girl’) | YES  |     | boy     |                |

| s_year   | int(4)             | YES  |     | 1990    |                |

| password | char(30)           | YES  |     | NULL    |                |

| uid      | int(3)             | YES  |     | NULL    |                |

| gid      | int(3)             | YES  |     | NULL    |                |

| comment  | char(80)           | YES  |     | NULL    |                |

| homedir  | char(50)           | YES  |     | NULL    |                |

| shell    | char(50)           | YES  |     | NULL    |                |

+———-+——————–+——+—–+———+—————-+

10 rows in set (0.00 sec)

 

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

mysql> alter table teadb add age int(2) unsigned default 21 after sex;

Query OK, 0 rows affected (0.82 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> desc teadb;

+———-+——————–+——+—–+———+—————-+

| Field    | Type               | Null | Key | Default | Extra          |

+———-+——————–+——+—–+———+—————-+

| id       | int(3)             | NO   | PRI | NULL    | auto_increment |

| name     | char(50)           | YES  | MUL | NULL    |                |

| sex      | enum(‘boy’,’girl’) | YES  |     | boy     |                |

| age      | int(2) unsigned    | YES  |     | 21      |                |

| s_year   | int(4)             | YES  |     | 1990    |                |

| password | char(30)           | YES  |     | NULL    |                |

| uid      | int(3)             | YES  |     | NULL    |                |

| gid      | int(3)             | YES  |     | NULL    |                |

| comment  | char(80)           | YES  |     | NULL    |                |

| homedir  | char(50)           | YES  |     | NULL    |                |

| shell    | char(50)           | YES  |     | NULL    |                |

+———-+——————–+——+—–+———+—————-+

11 rows in set (0.00 sec)

 

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

mysql> select id,name,sex,age,s_year from teadb where id between 10 and 50;

+—-+———————+——+——+——–+

| id | name                | sex  | age  | s_year |

+—-+———————+——+——+——–+

| 10 | operator            | boy  |   21 |   1990 |

| 11 | games               | boy  |   21 |   1990 |

| 12 | ftp                 | boy  |   21 |   1990 |

| 13 | nobody              | boy  |   21 |   1990 |

| 14 | systemd-network     | boy  |   21 |   1990 |

| 15 | dbus                | boy  |   21 |   1990 |

| 16 | polkitd             | boy  |   21 |   1990 |

| 17 | libstoragemgmt      | boy  |   21 |   1990 |

| 18 | rpc                 | boy  |   21 |   1990 |

| 19 | colord              | boy  |   21 |   1990 |

| 20 | saslauth            | boy  |   21 |   1990 |

| 21 | abrt                | boy  |   21 |   1990 |

| 22 | rtkit               | boy  |   21 |   1990 |

| 23 | radvd               | boy  |   21 |   1990 |

| 24 | chrony              | boy  |   21 |   1990 |

| 25 | tss                 | boy  |   21 |   1990 |

| 26 | usbmuxd             | boy  |   21 |   1990 |

| 27 | geoclue             | boy  |   21 |   1990 |

| 28 | qemu                | boy  |   21 |   1990 |

| 29 | rpcuser             | boy  |   21 |   1990 |

| 30 | nfsnobody           | boy  |   21 |   1990 |

| 31 | setroubleshoot      | boy  |   21 |   1990 |

| 32 | pulse               | boy  |   21 |   1990 |

| 33 | gdm                 | boy  |   21 |   1990 |

| 34 | gnome-initial-setup | boy  |   21 |   1990 |

| 35 | sshd                | boy  |   21 |   1990 |

| 36 | avahi               | boy  |   21 |   1990 |

| 37 | postfix             | boy  |   21 |   1990 |

| 38 | ntp                 | boy  |   21 |   1990 |

| 39 | tcpdump             | boy  |   21 |   1990 |

| 40 | lisi                | boy  |   21 |   1990 |

| 41 | mysql               | boy  |   21 |   1990 |

+—-+———————+——+——+——–+

32 rows in set (0.00 sec)

 

mysql> update teadb set sex=”girl” where id between 10 and 50;

Query OK, 32 rows affected (0.11 sec)

Rows matched: 32  Changed: 32  Warnings: 0

 

mysql> select id,name,sex,age,s_year from teadb where id between 10 and 50;

+—-+———————+——+——+——–+

| id | name                | sex  | age  | s_year |

+—-+———————+——+——+——–+

| 10 | operator            | girl |   21 |   1990 |

| 11 | games               | girl |   21 |   1990 |

| 12 | ftp                 | girl |   21 |   1990 |

| 13 | nobody              | girl |   21 |   1990 |

| 14 | systemd-network     | girl |   21 |   1990 |

| 15 | dbus                | girl |   21 |   1990 |

| 16 | polkitd             | girl |   21 |   1990 |

| 17 | libstoragemgmt      | girl |   21 |   1990 |

| 18 | rpc                 | girl |   21 |   1990 |

| 19 | colord              | girl |   21 |   1990 |

| 20 | saslauth            | girl |   21 |   1990 |

| 21 | abrt                | girl |   21 |   1990 |

| 22 | rtkit               | girl |   21 |   1990 |

| 23 | radvd               | girl |   21 |   1990 |

| 24 | chrony              | girl |   21 |   1990 |

| 25 | tss                 | girl |   21 |   1990 |

| 26 | usbmuxd             | girl |   21 |   1990 |

| 27 | geoclue             | girl |   21 |   1990 |

| 28 | qemu                | girl |   21 |   1990 |

| 29 | rpcuser             | girl |   21 |   1990 |

| 30 | nfsnobody           | girl |   21 |   1990 |

| 31 | setroubleshoot      | girl |   21 |   1990 |

| 32 | pulse               | girl |   21 |   1990 |

| 33 | gdm                 | girl |   21 |   1990 |

| 34 | gnome-initial-setup | girl |   21 |   1990 |

| 35 | sshd                | girl |   21 |   1990 |

| 36 | avahi               | girl |   21 |   1990 |

| 37 | postfix             | girl |   21 |   1990 |

| 38 | ntp                 | girl |   21 |   1990 |

| 39 | tcpdump             | girl |   21 |   1990 |

| 40 | lisi                | girl |   21 |   1990 |

| 41 | mysql               | girl |   21 |   1990 |

+—-+———————+——+——+——–+

32 rows in set (0.00 sec)

 

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

mysql> select count(*) from teadb where sex=”girl”;

+———-+

| count(*) |

+———-+

|       32 |

+———-+

1 row in set (0.00 sec)

 

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

mysql> select max(id) from teadb;

+———+

| max(id) |

+———+

|      41 |

+———+

1 row in set (0.00 sec)

 

 

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

 

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

mysql> insert into teadb(name,uid) values(“rtestd”,1000),(“rtest2d”,2000);

Query OK, 2 rows affected (0.34 sec)

Records: 2  Duplicates: 0  Warnings: 0

 

mysql> select * from teadb where uid=1000 or uid=2000;

+—-+———+——+——+——–+———-+——+——+———+————+———–+

| id | name    | sex  | age  | s_year | password | uid  | gid  | comment | homedir    | shell     |

+—-+———+——+——+——–+———-+——+——+———+————+———–+

| 40 | lisi    | girl |   21 |   1990 | x        | 1000 | 1000 | lisi    | /home/lisi | /bin/bash |

| 42 | rtestd  | boy  |   21 |   1990 | NULL     | 1000 | NULL | NULL    | NULL       | NULL      |

| 43 | rtest2d | boy  |   21 |   1990 | NULL     | 2000 | NULL | NULL    | NULL       | NULL      |

+—-+———+——+——+——–+———-+——+——+———+————+———–+

3 rows in set (0.00 sec)

 

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

mysql> select name,uid from teadb where uid like”____”;

+———+——+

| name    | uid  |

+———+——+

| lisi    | 1000 |

| rtestd  | 1000 |

| rtest2d | 2000 |

+———+——+

3 rows in set (0.00 sec)

 

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

mysql> select name,uid from teadb where name like’r%d’;

+———+——+

| name    | uid  |

+———+——+

| radvd   |   75 |

| rtest2d | 2000 |

| rtestd  | 1000 |

+———+——+

3 rows in set (0.00 sec)

 

 

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

mysql> select name,uid from teadb where name like’a%c’;

Empty set (0.00 sec)

 

 

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

mysql> select gid,homedir from teadb where gid between 100 and 500;

+——+—————-+

| gid  | homedir        |

+——+—————-+

|  100 | /usr/games     |

|  192 | /              |

|  173 | /etc/abrt      |

|  172 | /proc          |

|  113 | /              |

|  107 | /              |

|  171 | /var/run/pulse |

+——+—————-+

7 rows in set (0.00 sec)

 

mysql> update teadb set homedir=”/root” where gid between 100 and 500;

Query OK, 7 rows affected (0.04 sec)

Rows matched: 7  Changed: 7  Warnings: 0

 

mysql> select gid,homedir from teadb where gid between 100 and 500;

+——+———+

| gid  | homedir |

+——+———+

|  100 | /root   |

|  192 | /root   |

|  173 | /root   |

|  172 | /root   |

|  113 | /root   |

|  107 | /root   |

|  171 | /root   |

+——+———+

7 rows in set (0.00 sec)

 

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

mysql> select name,shell from teadb where name=”root” or  name=”bin” or name=”sync”;

+——+—————+

| name | shell         |

+——+—————+

| bin  | /sbin/nologin |

| root | /bin/bash     |

| sync | /bin/sync     |

+——+—————+

3 rows in set (0.00 sec)

 

mysql> update teadb set shell=”/sbin/nologin” where name=”root” or  name=”bin” or name=”sync”;

Query OK, 2 rows affected (0.30 sec)

Rows matched: 3  Changed: 2  Warnings: 0

 

mysql> select name,shell from teadb where name=”root” or  name=”bin” or name=”sync”;

+——+—————+

| name | shell         |

+——+—————+

| bin  | /sbin/nologin |

| root | /sbin/nologin |

| sync | /sbin/nologin |

+——+—————+

3 rows in set (0.00 sec)

 

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

mysql> select name,shell from teadb where gid<10;

+———-+—————-+

| name     | shell          |

+———-+—————-+

| root     | /sbin/nologin  |

| bin      | /sbin/nologin  |

| daemon   | /sbin/nologin  |

| adm      | /sbin/nologin  |

| lp       | /sbin/nologin  |

| sync     | /sbin/nologin  |

| shutdown | /sbin/shutdown |

| halt     | /sbin/halt     |

| operator | /sbin/nologin  |

+———-+—————-+

9 rows in set (0.00 sec)

 

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

mysql> select id,name from teadb where name like “d%”;

+—-+——–+

| id | name   |

+—-+——–+

|  3 | daemon |

| 15 | dbus   |

+—-+——–+

2 rows in set (0.00 sec)

 

mysql> delete from teadb where name like “d%”;

Query OK, 2 rows affected (0.04 sec)

 

mysql> select id,name from teadb where name like “d%”;

Empty set (0.00 sec)

 

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

asc/desc

 

mysql> select name,shell,gid from teadb order by gid desc limit 5;

+—————-+—————+——-+

| name           | shell         | gid   |

+—————-+—————+——-+

| nfsnobody      | /sbin/nologin | 65534 |

| lisi           | /bin/bash     |  1000 |

| polkitd        | /sbin/nologin |   998 |

| libstoragemgmt | /sbin/nologin |   996 |

| colord         | /sbin/nologin |   995 |

+—————-+—————+——-+

5 rows in set (0.00 sec)

 

 

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

mysql> select name,homedir from teadb where homedir=”” or homedir is NULL;

+———+———+

| name    | homedir |

+———+———+

| rtestd  | NULL    |

| rtest2d | NULL    |

+———+———+

2 rows in set (0.00 sec)

 

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

mysql> select gid,name from teadb order by gid asc limit 5;

+——+———-+

| gid  | name     |

+——+———-+

| NULL | rtestd   |

| NULL | rtest2d  |

|    0 | root     |

|    0 | operator |

|    0 | sync     |

+——+———-+

5 rows in set (0.00 sec)

mysql> mysql> select gid,name from teadb order by gid asc limit 5 into outfile “/mysqldir/min5.txt”;

Query OK, 5 rows affected (0.00 sec)

 

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

 

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

mysql> load data infile ‘/mysqldir/passwd1’ into table teadb3 fields terminated by “:” lines terminated by “\n” (name,password,uid,gid,comment,homedir,shell);

Query OK, 1 row affected (0.06 sec)

Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

 

mysql> select * from teadb3;

+—-+———-+——+—–+——–+———-+——+——+———+———————–+—————-+

| id | name     | sex  | age | s_year | password | uid  | gid  | comment | homedir               | shell          |

+—-+———-+——+—–+——–+———-+——+——+———+———————–+—————-+

|  1 | root     | boy  |  21 |   1990 | x        |    0 |    0 | XXX     | /root                 | /sbin/nologin  |

|  2 | bin      | boy  |  21 |   1990 | x        |    1 |    1 | XXX     | /bin                  | /sbin/nologin  |

|  4 | adm      | boy  |  21 |   1990 | x        |    3 |    4 | XXX     | /var/adm              | /sbin/nologin  |

|  5 | lp       | boy  |  21 |   1990 | x        |    4 |    7 | XXX     | /var/spool/lpd        | /sbin/nologin  |

|  6 | sync     | boy  |  21 |   1990 | x        |    5 |    0 | XXX     | /sbin                 | /sbin/nologin  |

|  7 | shutdown | boy  |  21 |   1990 | x        |    6 |    0 | XXX     | /sbin                 | /sbin/shutdown |

|  8 | halt     | boy  |  21 |   1990 | x        |    7 |    0 | XXX     | /sbin                 | /sbin/halt     |

|  9 | mail     | boy  |  21 |   1990 | x        |    8 |   12 | XXX     | /var/spool/mail       | /sbin/nologin  |

| 10 | operator | girl |  21 |   1990 | x        |   11 |    0 | XXX     | /root                 | /sbin/nologin  |

| 11 | games    | girl |  21 |   1990 | x        |   12 |  100 | XXX     | /root                 | /sbin/nologin  |

| 12 | ftp      | girl |  21 |   1990 | x        |   14 |   50 | XXX     | /var/ftp              | /sbin/nologin  |

| 13 | nobody   | girl |  21 |   1990 | x        |   99 |   99 | XXX     | /                     | /sbin/nologin  |

| 18 | rpc      | girl |  21 |   1990 | x        |   32 |   32 | XXX     | /var/lib/rpcbind      | /sbin/nologin  |

| 23 | radvd    | girl |  21 |   1990 | x        |   75 |   75 | XXX     | /                     | /sbin/nologin  |

| 25 | tss      | girl |  21 |   1990 | x        |   59 |   59 | XXX     | /dev/null             | /sbin/nologin  |

| 29 | rpcuser  | girl |  21 |   1990 | x        |   29 |   29 | XXX     | /var/lib/nfs          | /sbin/nologin  |

| 33 | gdm      | girl |  21 |   1990 | x        |   42 |   42 | XXX     | /var/lib/gdm          | /sbin/nologin  |

| 35 | sshd     | girl |  21 |   1990 | x        |   74 |   74 | XXX     | /var/empty/sshd       | /sbin/nologin  |

| 36 | avahi    | girl |  21 |   1990 | x        |   70 |   70 | XXX     | /var/run/avahi-daemon | /sbin/nologin  |

| 37 | postfix  | girl |  21 |   1990 | x        |   89 |   89 | XXX     | /var/spool/postfix    | /sbin/nologin  |

| 38 | ntp      | girl |  21 |   1990 | x        |   38 |   38 | XXX     | /etc/ntp              | /sbin/nologin  |

| 39 | tcpdump  | girl |  21 |   1990 | x        |   72 |   72 | XXX     | /                     | /sbin/nologin  |

| 41 | mysql    | girl |  21 |   1990 | x        |   27 |   27 | XXX     | /var/lib/mysql        | /bin/false     |

|  0 | lucy     | boy  |   0 |      0 | x        | 1001 | 1001 |         | /home/lucy            | /bin/bash      |

+—-+———-+——+—–+——–+———-+——+——+———+———————–+—————-+

24 rows in set (0.00 sec)

 

 

17  删除表中的 comment 字段

mysql> alter table teadb2 drop comment;

Query OK, 0 rows affected (0.48 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

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

mysql> alter table teadb2 change age age int(2) unsigned not null;

Query OK, 0 rows affected (0.47 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> alter table teadb2 change s_year s_year int(4) not null;

Query OK, 0 rows affected (0.51 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc teadb2;

+———-+——————–+——+—–+———+——-+

| Field    | Type               | Null | Key | Default | Extra |

+———-+——————–+——+—–+———+——-+

| id       | int(3)             | NO   |     | 0       |       |

| name     | char(50)           | NO   |     | NULL    |       |

| sex      | enum(‘boy’,’girl’) | NO   |     | NULL    |       |

| age      | int(2) unsigned    | NO   |     | NULL    |       |

| s_year   | int(4)             | NO   |     | NULL    |       |

| password | char(30)           | YES  |     | NULL    |       |

| uid      | int(3)             | YES  |     | NULL    |       |

| gid      | int(3)             | YES  |     | NULL    |       |

| homedir  | char(50)           | YES  |     | NULL    |       |

| shell    | char(50)           | YES  |     | NULL    |       |

+———-+——————–+——+—–+———+——-+

10 rows in set (0.00 sec)

 

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

mysql> select * from teadb where name=’root’;

+—-+——+——+——+——–+———-+——+——+———+———+—————+

| id | name | sex  | age  | s_year | password | uid  | gid  | comment | homedir | shell         |

+—-+——+——+——+——–+———-+——+——+———+———+—————+

|  1 | root | boy  |   21 |   1990 | x        |    0 |    0 | root    | /root   | /sbin/nologin |

+—-+——+——+——+——–+———-+——+——+———+———+—————+

1 row in set (0.00 sec)

 

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

mysql> select name,homedir,shell from teadb where gid>500;

+———————+—————————+—————+

| name                | homedir                   | shell         |

+———————+—————————+—————+

| polkitd             | /                         | /sbin/nologin |

| libstoragemgmt      | /var/run/lsm              | /sbin/nologin |

| colord              | /var/lib/colord           | /sbin/nologin |

| chrony              | /var/lib/chrony           | /sbin/nologin |

| geoclue             | /var/lib/geoclue          | /sbin/nologin |

| nfsnobody           | /var/lib/nfs              | /sbin/nologin |

| setroubleshoot      | /var/lib/setroubleshoot   | /sbin/nologin |

| gnome-initial-setup | /run/gnome-initial-setup/ | /sbin/nologin |

| lisi                | /home/lisi                | /bin/bash     |

+———————+—————————+—————+

9 rows in set (0.00 sec)

 

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

mysql> delete from teadb2 where uid>100;

Query OK, 18 rows affected (0.06 sec)

 

mysql> select name,uid from teadb2;

+———-+——+

| name     | uid  |

+———-+——+

| root     |    0 |

| bin      |    1 |

| adm      |    3 |

| lp       |    4 |

| sync     |    5 |

| shutdown |    6 |

| halt     |    7 |

| mail     |    8 |

| operator |   11 |

| games    |   12 |

| ftp      |   14 |

| nobody   |   99 |

| rpc      |   32 |

| radvd    |   75 |

| tss      |   59 |

| rpcuser  |   29 |

| gdm      |   42 |

| sshd     |   74 |

| avahi    |   70 |

| postfix  |   89 |

| ntp      |   38 |

| tcpdump  |   72 |

| mysql    |   27 |

+———-+——+

23 rows in set (0.01 sec)

 

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

mysql> select count(*) from teadb where uid between 10 and 30;

+———-+

| count(*) |

+———-+

|        5 |

+———-+

1 row in set (0.00 sec)

 

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

mysql> select name,shell from teadb where uid<100;

+———-+—————-+

| name     | shell          |

+———-+—————-+

| root     | /sbin/nologin  |

| bin      | /sbin/nologin  |

| adm      | /sbin/nologin  |

| lp       | /sbin/nologin  |

| sync     | /sbin/nologin  |

| shutdown | /sbin/shutdown |

| halt     | /sbin/halt     |

| mail     | /sbin/nologin  |

| operator | /sbin/nologin  |

| games    | /sbin/nologin  |

| ftp      | /sbin/nologin  |

| nobody   | /sbin/nologin  |

| rpc      | /sbin/nologin  |

| radvd    | /sbin/nologin  |

| tss      | /sbin/nologin  |

| rpcuser  | /sbin/nologin  |

| gdm      | /sbin/nologin  |

| sshd     | /sbin/nologin  |

| avahi    | /sbin/nologin  |

| postfix  | /sbin/nologin  |

| ntp      | /sbin/nologin  |

| tcpdump  | /sbin/nologin  |

| mysql    | /bin/false     |

+———-+—————-+

23 rows in set (0.00 sec)

 

 

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

mysql> select name,uid,shell from teadb order by uid asc limit 10;

+———-+——+—————-+

| name     | uid  | shell          |

+———-+——+—————-+

| root     |    0 | /sbin/nologin  |

| bin      |    1 | /sbin/nologin  |

| adm      |    3 | /sbin/nologin  |

| lp       |    4 | /sbin/nologin  |

| sync     |    5 | /sbin/nologin  |

| shutdown |    6 | /sbin/shutdown |

| halt     |    7 | /sbin/halt     |

| mail     |    8 | /sbin/nologin  |

| operator |   11 | /sbin/nologin  |

| games    |   12 | /sbin/nologin  |

+———-+——+—————-+

10 rows in set (0.00 sec)

 

 

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

mysql> select id,name,uid,shell from teadb limit 9,15;

+—-+—————–+——+—————+

| id | name            | uid  | shell         |

+—-+—————–+——+—————+

| 11 | games           |   12 | /sbin/nologin |

| 12 | ftp             |   14 | /sbin/nologin |

| 13 | nobody          |   99 | /sbin/nologin |

| 14 | systemd-network |  192 | /sbin/nologin |

| 16 | polkitd         |  999 | /sbin/nologin |

| 17 | libstoragemgmt  |  998 | /sbin/nologin |

| 18 | rpc             |   32 | /sbin/nologin |

| 19 | colord          |  997 | /sbin/nologin |

| 20 | saslauth        |  996 | /sbin/nologin |

| 21 | abrt            |  173 | /sbin/nologin |

| 22 | rtkit           |  172 | /sbin/nologin |

| 23 | radvd           |   75 | /sbin/nologin |

| 24 | chrony          |  995 | /sbin/nologin |

| 25 | tss             |   59 | /sbin/nologin |

| 26 | usbmuxd         |  113 | /sbin/nologin |

+—-+—————–+——+—————+

15 rows in set (0.00 sec)

 

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

mysql> select * from teadb where uid<50 and name regexp ‘a’;

+—-+———-+——+——+——–+———-+——+——+———-+—————–+—————+

| id | name     | sex  | age  | s_year | password | uid  | gid  | comment  | homedir         | shell         |

+—-+———-+——+——+——–+———-+——+——+———-+—————–+—————+

|  4 | adm      | boy  |   21 |   1990 | x        |    3 |    4 | adm      | /var/adm        | /sbin/nologin |

|  8 | halt     | boy  |   21 |   1990 | x        |    7 |    0 | halt     | /sbin           | /sbin/halt    |

|  9 | mail     | boy  |   21 |   1990 | x        |    8 |   12 | mail     | /var/spool/mail | /sbin/nologin |

| 10 | operator | girl |   21 |   1990 | x        |   11 |    0 | operator | /root           | /sbin/nologin |

| 11 | games    | girl |   21 |   1990 | x        |   12 |  100 | games    | /root           | /sbin/nologin |

+—-+———-+——+——+——–+———-+——+——+———-+—————–+—————+

5 rows in set (0.00 sec)

 

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

mysql> select * from teadb where name=’root’ or name=’bin’ or name=’daemon’;

+—-+——+——+——+——–+———-+——+——+———+———+—————+

| id | name | sex  | age  | s_year | password | uid  | gid  | comment | homedir | shell         |

+—-+——+——+——+——–+———-+——+——+———+———+—————+

|  2 | bin  | boy  |   21 |   1990 | x        |    1 |    1 | bin     | /bin    | /sbin/nologin |

|  1 | root | boy  |   21 |   1990 | x        |    0 |    0 | root    | /root   | /sbin/nologin |

+—-+——+——+——+——–+———-+——+——+———+———+—————+

2 rows in set (0.00 sec)

 

 

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

mysql> select * from teadb where name!=’root’ and id<10;

+—-+———-+——+——+——–+———-+——+——+———-+—————–+—————-+

| id | name     | sex  | age  | s_year | password | uid  | gid  | comment  | homedir         | shell          |

+—-+———-+——+——+——–+———-+——+——+———-+—————–+—————-+

|  2 | bin      | boy  |   21 |   1990 | x        |    1 |    1 | bin      | /bin            | /sbin/nologin  |

|  4 | adm      | boy  |   21 |   1990 | x        |    3 |    4 | adm      | /var/adm        | /sbin/nologin  |

|  5 | lp       | boy  |   21 |   1990 | x        |    4 |    7 | lp       | /var/spool/lpd  | /sbin/nologin  |

|  6 | sync     | boy  |   21 |   1990 | x        |    5 |    0 | sync     | /sbin           | /sbin/nologin  |

|  7 | shutdown | boy  |   21 |   1990 | x        |    6 |    0 | shutdown | /sbin           | /sbin/shutdown |

|  8 | halt     | boy  |   21 |   1990 | x        |    7 |    0 | halt     | /sbin           | /sbin/halt     |

|  9 | mail     | boy  |   21 |   1990 | x        |    8 |   12 | mail     | /var/spool/mail | /sbin/nologin  |

+—-+———-+——+——+——–+———-+——+——+———-+—————–+—————-+

7 rows in set (0.00 sec)

 

 

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

 

mysql> select count(*) from teadb;

+———-+

| count(*) |

+———-+

|       41 |

+———-+

1 row in set (0.00 sec)

 

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

mysql> select * from teadb where name regexp ‘c’;

+—-+———+——+——+——–+———-+——+——+——————+——————+—————+

| id | name    | sex  | age  | s_year | password | uid  | gid  | comment          | homedir          | shell         |

+—-+———+——+——+——–+———-+——+——+——————+——————+—————+

|  6 | sync    | boy  |   21 |   1990 | x        |    5 |    0 | sync             | /sbin            | /sbin/nologin |

| 18 | rpc     | girl |   21 |   1990 | x        |   32 |   32 | Rpcbind Daemon   | /var/lib/rpcbind | /sbin/nologin |

| 19 | colord  | girl |   21 |   1990 | x        |  997 |  995 | User for colord  | /var/lib/colord  | /sbin/nologin |

| 24 | chrony  | girl |   21 |   1990 | x        |  995 |  993 |                  | /var/lib/chrony  | /sbin/nologin |

| 27 | geoclue | girl |   21 |   1990 | x        |  994 |  991 | User for geoclue | /var/lib/geoclue | /sbin/nologin |

| 29 | rpcuser | girl |   21 |   1990 | x        |   29 |   29 | RPC Service User | /var/lib/nfs     | /sbin/nologin |

| 39 | tcpdump | girl |   21 |   1990 | x        |   72 |   72 |                  | /                | /sbin/nologin |

+—-+———+——+——+——–+———-+——+——+——————+——————+—————+

7 rows in set (0.00 sec)

 

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

mysql> alter table teadb add pay float(7,2) default 5000.00;

Query OK, 0 rows affected (0.67 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> select id,name,pay from teadb where id<5;

+—-+——+———+

| id | name | pay     |

+—-+——+———+

|  1 | root | 5000.00 |

|  2 | bin  | 5000.00 |

|  4 | adm  | 5000.00 |

+—-+——+———+

3 rows in set (0.00 sec)

 

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

mysql> select id,name,sex,pay from teadb where sex=’girl’;

+—-+———————+——+———+

| id | name                | sex  | pay     |

+—-+———————+——+———+

| 10 | operator            | girl | 5000.00 |

| 11 | games               | girl | 5000.00 |

| 12 | ftp                 | girl | 5000.00 |

| 13 | nobody              | girl | 5000.00 |

| 14 | systemd-network     | girl | 5000.00 |

| 16 | polkitd             | girl | 5000.00 |

| 17 | libstoragemgmt      | girl | 5000.00 |

| 18 | rpc                 | girl | 5000.00 |

| 19 | colord              | girl | 5000.00 |

| 20 | saslauth            | girl | 5000.00 |

| 21 | abrt                | girl | 5000.00 |

| 22 | rtkit               | girl | 5000.00 |

| 23 | radvd               | girl | 5000.00 |

| 24 | chrony              | girl | 5000.00 |

| 25 | tss                 | girl | 5000.00 |

| 26 | usbmuxd             | girl | 5000.00 |

| 27 | geoclue             | girl | 5000.00 |

| 28 | qemu                | girl | 5000.00 |

| 29 | rpcuser             | girl | 5000.00 |

| 30 | nfsnobody           | girl | 5000.00 |

| 31 | setroubleshoot      | girl | 5000.00 |

| 32 | pulse               | girl | 5000.00 |

| 33 | gdm                 | girl | 5000.00 |

| 34 | gnome-initial-setup | girl | 5000.00 |

| 35 | sshd                | girl | 5000.00 |

| 36 | avahi               | girl | 5000.00 |

| 37 | postfix             | girl | 5000.00 |

| 38 | ntp                 | girl | 5000.00 |

| 39 | tcpdump             | girl | 5000.00 |

| 40 | lisi                | girl | 5000.00 |

| 41 | mysql               | girl | 5000.00 |

+—-+———————+——+———+

31 rows in set (0.00 sec)

mysql> update teadb set pay=10000 where sex=’girl’;

Query OK, 31 rows affected (0.03 sec)

Rows matched: 31  Changed: 31  Warnings: 0

 

mysql> select id,name,sex,pay from teadb where sex=’girl’;

+—-+———————+——+———-+

| id | name                | sex  | pay      |

+—-+———————+——+———-+

| 10 | operator            | girl | 10000.00 |

| 11 | games               | girl | 10000.00 |

| 12 | ftp                 | girl | 10000.00 |

| 13 | nobody              | girl | 10000.00 |

| 14 | systemd-network     | girl | 10000.00 |

| 16 | polkitd             | girl | 10000.00 |

| 17 | libstoragemgmt      | girl | 10000.00 |

| 18 | rpc                 | girl | 10000.00 |

| 19 | colord              | girl | 10000.00 |

| 20 | saslauth            | girl | 10000.00 |

| 21 | abrt                | girl | 10000.00 |

| 22 | rtkit               | girl | 10000.00 |

| 23 | radvd               | girl | 10000.00 |

| 24 | chrony              | girl | 10000.00 |

| 25 | tss                 | girl | 10000.00 |

| 26 | usbmuxd             | girl | 10000.00 |

| 27 | geoclue             | girl | 10000.00 |

| 28 | qemu                | girl | 10000.00 |

| 29 | rpcuser             | girl | 10000.00 |

| 30 | nfsnobody           | girl | 10000.00 |

| 31 | setroubleshoot      | girl | 10000.00 |

| 32 | pulse               | girl | 10000.00 |

| 33 | gdm                 | girl | 10000.00 |

| 34 | gnome-initial-setup | girl | 10000.00 |

| 35 | sshd                | girl | 10000.00 |

| 36 | avahi               | girl | 10000.00 |

| 37 | postfix             | girl | 10000.00 |

| 38 | ntp                 | girl | 10000.00 |

| 39 | tcpdump             | girl | 10000.00 |

| 40 | lisi                | girl | 10000.00 |

| 41 | mysql               | girl | 10000.00 |

+—-+———————+——+———-+

31 rows in set (0.00 sec)

 

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

mysql> update teadb set pay=30000 where name=’root’;

Query OK, 1 row affected (0.07 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> select id,name,sex,pay from teadb where name=’root’;

+—-+——+——+———-+

| id | name | sex  | pay      |

+—-+——+——+———-+

|  1 | root | boy  | 30000.00 |

+—-+——+——+———-+

1 row in set (0.00 sec)

 

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

mysql> select name,pay from teadb;

+———————+———-+

| name                | pay      |

+———————+———-+

| root                | 30000.00 |

| bin                 |  5000.00 |

| adm                 |  5000.00 |

| lp                  |  5000.00 |

| sync                |  5000.00 |

| shutdown            |  5000.00 |

| halt                |  5000.00 |

| mail                |  5000.00 |

| operator            | 10000.00 |

| games               | 10000.00 |

| ftp                 | 10000.00 |

| nobody              | 10000.00 |

| systemd-network     | 10000.00 |

| polkitd             | 10000.00 |

| libstoragemgmt      | 10000.00 |

| rpc                 | 10000.00 |

| colord              | 10000.00 |

| saslauth            | 10000.00 |

| abrt                | 10000.00 |

| rtkit               | 10000.00 |

| radvd               | 10000.00 |

| chrony              | 10000.00 |

| tss                 | 10000.00 |

| usbmuxd             | 10000.00 |

| geoclue             | 10000.00 |

| qemu                | 10000.00 |

| rpcuser             | 10000.00 |

| nfsnobody           | 10000.00 |

| setroubleshoot      | 10000.00 |

| pulse               | 10000.00 |

| gdm                 | 10000.00 |

| gnome-initial-setup | 10000.00 |

| sshd                | 10000.00 |

| avahi               | 10000.00 |

| postfix             | 10000.00 |

| ntp                 | 10000.00 |

| tcpdump             | 10000.00 |

| lisi                | 10000.00 |

| mysql               | 10000.00 |

| rtestd              |  5000.00 |

| rtest2d             |  5000.00 |

+———————+———-+

41 rows in set (0.00 sec)

 

 

35  查看工资字段的平均值

mysql> select avg(pay) avg from teadb;

+————-+

| avg         |

+————-+

| 9390.243902 |

+————-+

1 row in set (0.00 sec)

 

 

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

查看女生里谁的uid号最大

mysql> select id,name,sex,pay from teadb where pay<(select avg(pay) from teadb);

+—-+———-+——+———+

| id | name     | sex  | pay     |

+—-+———-+——+———+

|  2 | bin      | boy  | 5000.00 |

|  4 | adm      | boy  | 5000.00 |

|  5 | lp       | boy  | 5000.00 |

|  6 | sync     | boy  | 5000.00 |

|  7 | shutdown | boy  | 5000.00 |

|  8 | halt     | boy  | 5000.00 |

|  9 | mail     | boy  | 5000.00 |

| 42 | rtestd   | boy  | 5000.00 |

| 43 | rtest2d  | boy  | 5000.00 |

+—-+———-+——+———+

9 rows in set (0.00 sec)

mysql> select name,uid from teadb where sex=’girl’ order by uid desc limit 1;

+———–+——-+

| name      | uid   |

+———–+——-+

| nfsnobody | 65534 |

+———–+——-+

1 row in set (0.00 sec)

 

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

mysql> select uid,gid,(uid+gid) sum from teadb where name=’bin’;

+——+——+——+

| uid  | gid  | sum  |

+——+——+——+

|    1 |    1 |    2 |

+——+——+——+

1 row in set (0.00 sec)

 

 

39  把teacher表中前7条记录中如下字段的值保存到当前库下 userone表里

id 、 username 、 sex

mysql> create table userone select id,name,sex from teadb limit 7;

Query OK, 7 rows affected (0.31 sec)

Records: 7  Duplicates: 0  Warnings: 0

 

mysql> select * from userone;

+—-+———-+——+

| id | name     | sex  |

+—-+———-+——+

|  1 | root     | boy  |

|  2 | bin      | boy  |

|  4 | adm      | boy  |

|  5 | lp       | boy  |

|  6 | sync     | boy  |

|  7 | shutdown | boy  |

|  8 | halt     | boy  |

+—-+———-+——+

7 rows in set (0.00 sec)

 

 

40  把teacher表中前5条记录中如下字段的值保存到当前库下 usertwo表里

id 、 username 、 sex 、shell

mysql> create table usertwo select id,name,sex,shell from teadb limit 5;

Query OK, 5 rows affected (0.43 sec)

Records: 5  Duplicates: 0  Warnings: 0

 

mysql> select * from usertwo;

+—-+——+——+—————+

| id | name | sex  | shell         |

+—-+——+——+—————+

|  1 | root | boy  | /sbin/nologin |

|  2 | bin  | boy  | /sbin/nologin |

|  4 | adm  | boy  | /sbin/nologin |

|  5 | lp   | boy  | /sbin/nologin |

|  6 | sync | boy  | /sbin/nologin |

+—-+——+——+—————+

5 rows in set (0.00 sec)

此条目发表在mysql分类目录,贴了标签。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注