Linux作业3

一、CentOS7.x安装Maria DB

编写repo文件
[root@centos ~]# vim /etc/yum.repos.d/mariadb.repo

[mariadb]
name=mariadb-10.4
baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64/
gpgcheck=0

安装mariadb
[root@centos ~]# yum install mariadb-server -y

二、启动Maria DB

开机启动并立即启动
[root@centos ~]# systemctl enable --now mariadb
防火墙放行服务
[root@centos ~]# firewall-cmd --add-service=mysql --per
[root@centos ~]# firewall-cmd --reload

三、初始化Maria DB

[root@centos ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):	//输入root用户密码
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n		//更改为unix套接字认证
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y					//更改root密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y					//移除匿名用户
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y			//禁止root用户远程登陆
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y	//移除测试数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y			//更新权限表
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

四、设置密码

没有密码:
mysqladmin -uroot password "123456"
修改密码:
mysqladmin -uroot -p"888888" password "123456"
登录后修改:
[root@kongd ~]# mysql -uroot -p密码
方法1:alter user root@'localhost' identified by '123456' // 5.5版本不支持
方法2:set password for root@localhost = password('12345')
方法3:

MariaDB [(none)]> update mysql.user
	-> set password=password('123456')
	-> where User="root" and Host="localhost";
	MariaDB [(none)]> flush privileges;

五、登录

命令行
[root@kongd ~]# mysql -u root -p123456 -D mysql -h localhost
-u:用户名
-p:密码,注意-p和密码不能有空格
-D:数据库名
-h:主机
windows下的客户端(GUI)
Navicat for MySQL
授权一个用户:
grant all on *.* to admin@'%' identified by '123456';

六、支持中文

设置服务器默认字符集
[root@centos ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

重启服务:
[root@kongd my.cnf.d]# systemctl restart mariadb
测试:
MariaDB [(none)]> show variables like 'character%';

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

七、表的创建

1,创建student表和score表
Sutdent 表的定义

字段名字段描述数据类型主键 外键 非空 唯一 自增
Id学号INT(10)是否是是是
Name姓名VARCHAR(20)否否是否否
Sex性别VARCHAR(4)否否否否否
Birth出生年份YEAR否否否否否
Department院系VARCHAR(20)否否是否否
Address家庭住址VARCHAR(50)否否否否否

Score 表的定义

字段名字段描述数据类型主键 外键 非空 唯一 自增
Id编号INT(10)是否是是是
Stu_id学号INT(10)否否是否否
C_name课程名VARCHAR(20)否否否否否
Grade分数INT(10)否否否否否

SQL语句

create table student (
id int(10) primary key not null unique auto_increment, 
name varchar(20) not null,
sex varchar(4),
birth year,
department varchar(20) not null,
address varchar(50));

create table score (
id int(10) primary key not null unique auto_increment,
stu_id int(10) not null,
c_name varchar(20),
grade int(10));

2,查看表结构

MariaDB [mysql]> desc student;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| id         | int(10)     | NO   | PRI | NULL    | auto_increment |
| name       | varchar(20) | NO   |     | NULL    |                |
| sex        | varchar(4)  | YES  |     | NULL    |                |
| birth      | year(4)     | YES  |     | NULL    |                |
| department | varchar(20) | NO   |     | NULL    |                |
| address    | varchar(50) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
6 rows in set (0.001 sec)

MariaDB [mysql]> desc score;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(10)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(10)     | NO   |     | NULL    |                |
| c_name | varchar(20) | YES  |     | NULL    |                |
| grade  | int(10)     | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.001 sec)

3,增加记录

insert into student(id,name,sex,birth,department,address)values
('801','张老大', '男',1985,'计算机系', '北京市海淀区'),
('802','张老二', '男',1986,'中文系', '北京市昌平区'),
('803','张三', '女',1990,'中文系', '湖南省永州市'),
('804','李四', '男',1990,'英语系', '辽宁省阜新市'),
('805','王五', '女',1991,'英语系', '福建省厦门市'),
('806','王六', '男',1988,'计算机系', '湖南省衡阳市');

insert into score (stu_id,c_name,grade)values
(801,'计算机',98),
(801,'英语', 80),
(802, '计算机',65),
(802, '中文',88),
(803, '中文',95),
(804, '计算机',70),
(804, '英语',92),
(805, '英语',94),
(805, '计算机',90),
(806, '英语',85);

查看

MariaDB [db1]> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 802 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
| 803 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
| 804 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 806 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.000 sec)

MariaDB [db1]> select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  1 |    801 | 计算机    |    98 |
|  2 |    801 | 英语      |    80 |
|  3 |    802 | 计算机    |    65 |
|  4 |    802 | 中文      |    88 |
|  5 |    803 | 中文      |    95 |
|  6 |    804 | 计算机    |    70 |
|  7 |    804 | 英语      |    92 |
|  8 |    805 | 英语      |    94 |
|  9 |    805 | 计算机    |    90 |
| 10 |    806 | 英语      |    85 |
+----+--------+-----------+-------+
10 rows in set (0.000 sec)

八、单表查询示例

1.查询 student 表的所有记录

MariaDB [db1]> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 802 | 张老二    ||  1986 | 中文系       | 北京市昌平区       |
| 803 | 张三      ||  1990 | 中文系       | 湖南省永州市       |
| 804 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 806 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+

2.查询 student 表的第 2 条到 4 条记录

MariaDB [db1]> select * from student limit 1,3;
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | birth | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 802 | 张老二    ||  1986 | 中文系     | 北京市昌平区       |
| 803 | 张三      ||  1990 | 中文系     | 湖南省永州市       |
| 804 | 李四      ||  1990 | 英语系     | 辽宁省阜新市       |
+-----+-----------+------+-------+------------+--------------------+

3.从 student 表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

MariaDB [db1]> select id,name,department from student;
+-----+-----------+--------------+
| id  | name      | department   |
+-----+-----------+--------------+
| 801 | 张老大    | 计算机系     |
| 802 | 张老二    | 中文系       |
| 803 | 张三      | 中文系       |
| 804 | 李四      | 英语系       |
| 805 | 王五      | 英语系       |
| 806 | 王六      | 计算机系     |
+-----+-----------+--------------+

4.从 student 表中查询计算机系和英语系的学生的信息

MariaDB [db1]>  SELECT * FROM student WHERE department IN ('计算机系','英语系');
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 804 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
| 806 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+

5.从 student 表中查询年龄 28~32 岁的学生信息

MariaDB [db1]> SELECT id,name,sex,2020-birth AS age,department,address FROM student WHERE 2020-birth BETWEEN  28 AND 32;
+-----+--------+------+------+--------------+--------------------+
| id  | name   | sex  | age  | department   | address            |
+-----+--------+------+------+--------------+--------------------+
| 803 | 张三   ||   30 | 中文系       | 湖南省永州市       |
| 804 | 李四   ||   30 | 英语系       | 辽宁省阜新市       |
| 805 | 王五   ||   29 | 英语系       | 福建省厦门市       |
| 806 | 王六   ||   32 | 计算机系     | 湖南省衡阳市       |
+-----+--------+------+------+--------------+--------------------+

6.从 student 表中查询每个院系有多少人

MariaDB [db1]> select department ,count(id) from student
    -> group by department;
+--------------+-----------+
| department   | count(id) |
+--------------+-----------+
| 中文系       |         2 |
| 英语系       |         2 |
| 计算机系     |         2 |
+--------------+-----------+

巩固练习:查询score表学习每个课程的人数

MariaDB [db1]> select c_name,count(stu_id) from score group by c_name;
+-----------+---------------+
| c_name    | count(stu_id) |
+-----------+---------------+
| 中文      |             2 |
| 英语      |             4 |
| 计算机    |             4 |
+-----------+---------------+

7.从 score 表中查询每个科目的最高分

MariaDB [db1]> SELECT c_name,MAX(grade) FROM score GROUP BY c_name;
+-----------+------------+
| c_name    | MAX(grade) |
+-----------+------------+
| 中文      |         95 |
| 英语      |         94 |
| 计算机    |         98 |
+-----------+------------+

8.查询李四的考试科目(c_name)和考试成绩(grade)

MariaDB [db1]> SELECT c_name, grade
    -> FROM score WHERE stu_id=
    -> (SELECT id FROM student
    -> WHERE name= '李四' );
+-----------+-------+
| c_name    | grade |
+-----------+-------+
| 计算机    |    70 |
| 英语      |    92 |
+-----------+-------+

9.用连接的方式查询所有学生的信息和考试信息

MariaDB [db1]> SELECT student.id,name,sex,birth,department,address,c_name,grade
    -> FROM student,score
    -> WHERE student.id=score.stu_id;
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       | 计算机    |    98 |
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       | 英语      |    80 |
| 802 | 张老二    ||  1986 | 中文系       | 北京市昌平区       | 计算机    |    65 |
| 802 | 张老二    ||  1986 | 中文系       | 北京市昌平区       | 中文      |    88 |
| 803 | 张三      ||  1990 | 中文系       | 湖南省永州市       | 中文      |    95 |
| 804 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       | 计算机    |    70 |
| 804 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       | 英语      |    92 |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       | 英语      |    94 |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       | 计算机    |    90 |
| 806 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       | 英语      |    85 |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+

10.计算每个学生的总成绩

MariaDB [db1]> SELECT student.id,name,SUM(grade) FROM student,score
    -> WHERE student.id=score.stu_id
    -> GROUP BY id;
+-----+-----------+------------+
| id  | name      | SUM(grade) |
+-----+-----------+------------+
| 801 | 张老大    |        178 |
| 802 | 张老二    |        153 |
| 803 | 张三      |         95 |
| 804 | 李四      |        162 |
| 805 | 王五      |        184 |
| 806 | 王六      |         85 |
+-----+-----------+------------+

11.计算每个考试科目的平均成绩

MariaDB [db1]> SELECT c_name,AVG(grade) FROM score GROUP BY c_name;
+-----------+------------+
| c_name    | AVG(grade) |
+-----------+------------+
| 中文      |    91.5000 |
| 英语      |    87.7500 |
| 计算机    |    80.7500 |
+-----------+------------+

12.查询计算机成绩低于 95 的学生信息

MariaDB [db1]> SELECT * FROM student
    -> WHERE id IN
    -> (SELECT stu_id FROM score
    -> WHERE c_name="计算机" and grade<95);
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | birth | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 802 | 张老二    ||  1986 | 中文系     | 北京市昌平区       |
| 804 | 李四      ||  1990 | 英语系     | 辽宁省阜新市       |
| 805 | 王五      ||  1991 | 英语系     | 福建省厦门市       |
+-----+-----------+------+-------+------------+--------------------+

13.查询同时参加计算机和英语考试的学生的信息

MariaDB [db1]> SELECT *  FROM student
    -> WHERE id =ANY
    -> ( SELECT stu_id FROM score
    -> WHERE stu_id IN (
    ->  SELECT stu_id FROM
    ->  score WHERE c_name=  '计算机')
    -> AND c_name= '英语' );
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       |
| 804 | 李四      ||  1990 | 英语系       | 辽宁省阜新市       |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       |
+-----+-----------+------+-------+--------------+--------------------+

14.将计算机考试成绩按从高到低进行排序

MariaDB [db1]> SELECT stu_id, grade
    -> FROM score WHERE c_name= '计算机'
    -> ORDER BY grade DESC;
+--------+-------+
| stu_id | grade |
+--------+-------+
|    801 |    98 |
|    805 |    90 |
|    804 |    70 |
|    802 |    65 |
+--------+-------+

15.从 student 表和 score 表中查询出学生的学号,然后合并查询结果

MariaDB [db1]> SELECT id  FROM student
    -> UNION
    -> SELECT stu_id  FROM score;
+-----+
| id  |
+-----+
| 801 |
| 802 |
| 803 |
| 804 |
| 805 |
| 806 |
+-----+

16.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

MariaDB [db1]> SELECT student.id, name,sex,birth,department, address, c_name,grade
    -> FROM student, score
    -> WHERE
    -> (name LIKE  '张%'  OR name LIKE  '王%')
    -> AND
    -> student.id=score.stu_id ;
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       | 计算机    |    98 |
| 801 | 张老大    ||  1985 | 计算机系     | 北京市海淀区       | 英语      |    80 |
| 802 | 张老二    ||  1986 | 中文系       | 北京市昌平区       | 计算机    |    65 |
| 802 | 张老二    ||  1986 | 中文系       | 北京市昌平区       | 中文      |    88 |
| 803 | 张三      ||  1990 | 中文系       | 湖南省永州市       | 中文      |    95 |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       | 英语      |    94 |
| 805 | 王五      ||  1991 | 英语系       | 福建省厦门市       | 计算机    |    90 |
| 806 | 王六      ||  1988 | 计算机系     | 湖南省衡阳市       | 英语      |    85 |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+

17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

MariaDB [db1]> SELECT student.id, name,sex,birth,department, address, c_name,grade
    -> FROM student, score
    -> WHERE address LIKE '湖南%'   AND
    -> student.id=score.stu_id;
+-----+--------+------+-------+--------------+--------------------+--------+-------+
| id  | name   | sex  | birth | department   | address            | c_name | grade |
+-----+--------+------+-------+--------------+--------------------+--------+-------+
| 803 | 张三   ||  1990 | 中文系       | 湖南省永州市       | 中文   |    95 |
| 806 | 王六   ||  1988 | 计算机系     | 湖南省衡阳市       | 英语   |    85 |
+-----+--------+------+-------+--------------+--------------------+--------+-------+

版权声明:本文为qq_36148336原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。