数据持久化 - 将数据保存到(在掉电状况下)可以长久保存数据的存储介质中。html
数据库发展史 - 网状数据库、层次数据库、关系数据库、NoSQL数据库。前端
1970年,IBM的研究员E.F.Codd在Communication of the ACM上发表了名为A Relational Model of Data for Large Shared Data Banks的论文,提出了关系模型的概念,奠基了关系模型的理论基础。后来Codd又陆续发表多篇文章,论述了范式理论和衡量关系系统的12条标准,用数学理论奠基了关系数据库的基础。python
关系数据库特色。mysql
理论基础:集合论和关系代数。git
具体表象:用二维表(有行和列)组织数据。程序员
编程语言:结构化查询语言(SQL)。github
E-R图。web
关系数据库产品。算法
安装和配置(以CentOS Linux环境为例)。sql
Linux下有一个MySQL的分支版本,名为MariaDB,它由MySQL的一些原始开发者开发,有商业支持,旨在继续保持MySQL数据库在GNU GPL下开源(由于你们担忧MySQL被甲骨文收购后会再也不开源)。若是决定要直接使用MariaDB做为MySQL的替代品,可使用下面的命令进行安装。
yum install mariadb mariadb-server
若是要安装官方版本的MySQL,能够在MySQL官方网站下载安装文件。首先在下载页面中选择平台和版本,而后找到对应的下载连接。下面以MySQL 5.7.26版本和Red Hat Enterprise Linux为例,直接下载包含全部安装文件的归档文件,解归档以后经过包管理工具进行安装。
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
若是系统上有MariaDB相关的文件,须要先移除MariaDB相关的文件。
yum list installed | grep mariadb | awk '{print $1}' | xargs yum erase -y
接下来能够按照以下所示的顺序用RPM(Redhat Package Manager)工具安装MySQL。
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
可使用下面的命令查看已经安装的MySQL相关的包。
rpm -qa | grep mysql
启动MySQL服务。
先修改MySQL的配置文件(/etc/my.cnf
)添加一行skip-grant-tables
,能够设置不进行身份验证便可链接MySQL服务器,而后就能够以超级管理员(root)身份登陆。
vim /etc/my.cnf
[mysqld]
skip-grant-tables
datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
接下来可使用下面的命令来启动MySQL。
service mysqld start
在CentOS 7中建议使用下面的命令来启动MySQL。
systemctl start mysqld
使用MySQL客户端工具链接服务器。
命令行工具:
mysql -u root
修改超级管理员(root)的访问口令为i_LOVE_macos_123。
use mysql;
update user set authentication_string=password('i_LOVE_macos_123') where user='root'; flush privileges;
将MySQL配置文件中的skip-grant-tables
去掉,而后重启服务器,从新登陆。这一次须要提供用户名和口令才能链接MySQL服务器。
systemctl restart mysqld
mysql -u root -p
也能够选择图形化的客户端工具来链接MySQL服务器,能够选择下列工具之一:
经常使用命令。
查看服务器版本。
select version();
查看全部数据库。
show databases;
切换到指定数据库。
use mysql;
查看数据库下全部表。
show tables;
获取帮助。
? contents;
? functions;
? numeric functions;
? round;
? data types;
? longblob;
DDL
-- 若是存在名为school的数据库就删除它 drop database if exists school; -- 建立名为school的数据库并设置默认的字符集和排序方式 create database school default charset utf8 collate utf8_bin; -- 切换到school数据库上下文环境 use school; -- 建立学院表 create table tb_college ( collid int not null auto_increment comment '编号', collname varchar(50) not null comment '名称', collmaster varchar(20) not null comment '院长', collweb varchar(511) default '' comment '网站', primary key (collid) ); -- 建立学生表 create table tb_student ( stuid int not null comment '学号', stuname varchar(20) not null comment '姓名', stusex bit default 1 comment '性别', stubirth date not null comment '出生日期', stuaddr varchar(255) default '' comment '籍贯', collid int not null comment '所属学院', primary key (stuid), foreign key (collid) references tb_college (collid) ); -- alter table tb_student add constraint fk_student_collid foreign key (collid) references tb_college (collid); -- 建立教师表 create table tb_teacher ( teaid int not null comment '工号', teaname varchar(20) not null comment '姓名', teatitle varchar(10) default '助教' comment '职称', collid int not null comment '所属学院', primary key (teaid), foreign key (collid) references tb_college (collid) ); -- 建立课程表 create table tb_course ( couid int not null comment '编号', couname varchar(50) not null comment '名称', coucredit int not null comment '学分', teaid int not null comment '授课老师', primary key (couid), foreign key (teaid) references tb_teacher (teaid) ); -- 建立选课记录表 create table tb_score ( scid int auto_increment comment '选课记录编号', stuid int not null comment '选课学生', couid int not null comment '所选课程', scdate datetime comment '选课时间日期', scmark decimal(4,1) comment '考试成绩', primary key (scid), foreign key (stuid) references tb_student (stuid), foreign key (couid) references tb_course (couid) ); -- 添加惟一性约束(一个学生选某个课程只能选一次) alter table tb_score add constraint uni_score_stuid_couid unique (stuid, couid);
DML
-- 插入学院数据 insert into tb_college (collname, collmaster, collweb) values ('计算机学院', '左冷禅', 'http://www.abc.com'), ('外国语学院', '岳不群', 'http://www.xyz.com'), ('经济管理学院', '风清扬', 'http://www.foo.com'); -- 插入学生数据 insert into tb_student (stuid, stuname, stusex, stubirth, stuaddr, collid) values (1001, '杨逍', 1, '1990-3-4', '四川成都', 1), (1002, '任我行', 1, '1992-2-2', '湖南长沙', 1), (1033, '王语嫣', 0, '1989-12-3', '四川成都', 1), (1572, '岳不群', 1, '1993-7-19', '陕西咸阳', 1), (1378, '纪嫣然', 0, '1995-8-12', '四川绵阳', 1), (1954, '林平之', 1, '1994-9-20', '福建莆田', 1), (2035, '东方不败', 1, '1988-6-30', null, 2), (3011, '林震南', 1, '1985-12-12', '福建莆田', 3), (3755, '项少龙', 1, '1993-1-25', null, 3), (3923, '杨不悔', 0, '1985-4-17', '四川成都', 3), (4040, '隔壁老王', 1, '1989-1-1', '四川成都', 2); -- 删除学生数据 delete from tb_student where stuid=4040; -- 更新学生数据 update tb_student set stuname='杨过', stuaddr='湖南长沙' where stuid=1001; -- 插入老师数据 insert into tb_teacher (teaid, teaname, teatitle, collid) values (1122, '张三丰', '教授', 1), (1133, '宋远桥', '副教授', 1), (1144, '杨逍', '副教授', 1), (2255, '范遥', '副教授', 2), (3366, '韦一笑', '讲师', 3); -- 插入课程数据 insert into tb_course (couid, couname, coucredit, teaid) values (1111, 'Python程序设计', 3, 1122), (2222, 'Web前端开发', 2, 1122), (3333,