文章目录
- 搭建mysql服务
- 创建一个以你名字为名的数据库,并创建一张表
- 查看下该新建的表有无内容(用select语句)
- 往新建的student表中插入数据(用insert语句
- 修改andy的年龄为50
- 以age字段降序排序
- 查询student表中年龄最小的3位同学
- 查询student表中年龄最大的4位同学
- 查询student表中名字叫tom的记录
- 查询student表中名字叫tom且年龄大于19岁的记录
- 查询student表中年龄在20到50之间的记录
- 修改natasha的年龄为100
- 删除student中名字叫zhangshan且年龄小于等于20的
搭建mysql服务
[root@mysql ~]# systemctl stop firewalld
[root@mysql ~]# systemctl disable firewalld
[root@mysql ~]# getenforce
Disabled
[root@mysql ~]# wget http://dev.mysql.com/get/mysql57‐community‐release‐e
l7‐10.noarch.rpm
[root@mysql ~]# yum ‐y install mysql57‐community‐release‐el7‐10.noarch.rp
m
[root@mysql ~]# yum ‐y install mysql‐community‐server mysql‐community‐cli
ent mysql‐community‐common mysql‐community‐devel
[root@mysql ~]# systemctl start mysqld
[root@mysql ~]# ss ‐antl
State Recv‐Q Send‐Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
创建一个以你名字为名的数据库,并创建一张表
student,该表包含三个字段(id,name,age),表结
构如下:
[root@mysql ~]# mysql ‐uroot ‐pwyh123!
mysql> create database wyh;
mysql> use wyh;
mysql> create table student(id int NOT NULL,name VARCHAR(100) NOT NULL,ag
e tinyint NULL);
mysql> desc wyh.student;
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| Field | Type | Null | Key | Default | Extra |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+‐‐‐‐‐‐‐+‐‐‐‐‐‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐‐+
查看下该新建的表有无内容(用select语句)
mysql> select * from student;
Empty set (0.00 sec)
往新建的student表中插入数据(用insert语句
mysql> insert into student (id,name,age) values (1,'tom',20),
(2,'natasha',17),(3,'harry',15),(4,'andy',20),(5,'jerry',19),(6,'jack',21);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from student;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 1 | tom | 20 |
| 2 | natasha | 17 |
| 3 | harry | 15 |
| 4 | andy | 20 |
| 5 | jerry | 19 |
| 6 | jack | 21 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
6 rows in set (0.00 sec)
修改andy的年龄为50
mysql> update student set age = 50 where name = 'andy';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 1 | tom | 20 |
| 2 | natasha | 17 |
| 3 | harry | 15 |
| 4 | andy | 50 |
| 5 | jerry | 19 |
| 6 | jack | 21 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
6 rows in set (0.00 sec)
以age字段降序排序
mysql> select * from student order by age desc;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 4 | andy | 50 |
| 6 | jack | 21 |
| 1 | tom | 20 |
| 5 | jerry | 19 |
| 2 | natasha | 17 |
| 3 | harry | 15 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
6 rows in set (0.00 sec)
查询student表中年龄最小的3位同学
mysql> select * from student order by age limit 3;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 3 | harry | 15 |
| 2 | natasha | 17 |
| 5 | jerry | 19 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
3 rows in set (0.00 sec)
查询student表中年龄最大的4位同学
mysql> select * from student order by age desc limit 4;
+‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 4 | andy | 50 |
| 6 | jack | 21 |
| 1 | tom | 20 |
| 5 | jerry | 19 |
+‐‐‐‐+‐‐‐‐‐‐‐+‐‐‐‐‐‐+
4 rows in set (0.00 sec)
查询student表中名字叫tom的记录
mysql> select * from student where name = 'tom';
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
| 1 | tom | 20 |
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
1 row in set (0.00 sec)
查询student表中名字叫tom且年龄大于19岁的记录
mysql> select * from student where name = 'tom' and age > 19;
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
| 1 | tom | 20 |
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
1 row in set (0.00 sec)
查询student表中年龄在20到50之间的记录
mysql> select * from student where age between 20 and 50;
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
| 1 | tom | 20 |
| 4 | andy | 50 |
| 6 | jack | 21 |
+‐‐‐‐+‐‐‐‐‐‐+‐‐‐‐‐‐+
3 rows in set (0.00 sec)
修改natasha的年龄为100
mysql> update student set age = 100 where name = 'natasha';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 1 | tom | 20 |
| 2 | natasha | 100 |
| 3 | harry | 15 |
| 4 | andy | 50 |
| 5 | jerry | 19 |
| 6 | jack | 21 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
6 rows in set (0.00 sec)
删除student中名字叫zhangshan且年龄小于等于20的
记录
mysql> delete from student where name = 'tom' and age <= 20;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| id | name | age |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
| 2 | natasha | 100 |
| 3 | harry | 15 |
| 4 | andy | 50 |
| 5 | jerry | 19 |
| 6 | jack | 21 |
+‐‐‐‐+‐‐‐‐‐‐‐‐‐+‐‐‐‐‐‐+
5 rows in set (0.00 sec)