day35 初识数据库

数据库 - 数据的仓库(集散地) - database - 实现数据持久化和数据管理
持久化 - 将数据从内存转移到能够长久保存数据的存储介质的过程

数据库的分类:关系型数据库(SQL)和非关系型数据库(NoSQL)

文件系统 / 层次数据库 / 网状数据库

关系型数据库

  • 1970s - E.F.Codd - IBM研究员 - System R
  • 理论基础:关系代数和集合论
  • 具体表象:用二维表来保存数据 - 学生表
    ~ 行:一条记录 - 一个学生的信息
    ~ 列:一个字段 - 学生的某个属性,例如:学号、姓名、出生日期
    ~ 主键列:能够唯一标识一条记录的列,例如:学生的学号
  • 编程语言:SQL - 结构化查询语言
    ~ DDL - 数据定义语言 - create / drop / alter
    ~ DML - 数据操作语言 - insert / delete / update / select
    ~ DCL - 数据控制语言 - grant / revoke

LAMP = Linux + Apache + MySQL + PHP

PHP ---> Java
MySQL ---> Oracle
Linux ---> 小型机

去IOE运动
IBM的小型机
Oracle的数据库
EMC的存储设备

关系型数据库产品:

  • Oracle - 甲骨文
  • IBM DB2
  • Microsoft SQLServer
  • Sybase

  • MySQL
  • PostgreSQL
  • SQLite

连接MySQL的图形化客户端工具:

  • Navicat for MySQL - 病猫
  • SQLyog - 海豚
  • Toad for MySQL - 蛤蟆

学生(学号、姓名、性别、生日、家庭住址)
学院(编号、名称、网站、……)
老师(工号、姓名、性别、生日、职称、所在学院编号)
课程(编号、名称、学分)

设计数据库中的表 - ER图(实体关系图)- 概念模型图

读者、图书
用户、购物车、商品、订单
用户、单车
人、身份证

实体:学生、学院
关系:属于
重数:多对一

操作实例:
创建用户识别码
create user 'root'@'%' identified by '123456';
重置权限
flush privileges;
赋予所有特权
grant all privileges on . to 'root'@'%' with grant option;

-- 如果存在名为school的数据库就删除它
drop database if exists school;

-- 创建名为school的数据库并指定默认的字符集为utf-8
create database school default charset utf8;

-- 切换到school数据库上下文环境
use school;

-- 创建学生表
create table tb_student
(
stuid int not null,
stuname varchar(20) not null,
stusex bit default 1,
stubirth date,
primary key (stuid)
);

-- 修改学生表
添加stuaddr属性
alter table tb_student add column stuaddr varchar(255);
改变stuaddr属性
alter table tb_student change column stuaddr varchar(511);
删除stuaddr属性
alter table tb_student drop column stuaddr;

-- 修改学生表添加学院编号(colid)列
alter table tb_student add column colid int;

-- 修改学生表添加外键约束(参照完整性)

alter table tb_student add constraint fk_student_colid foreign key (colid) references tb_college (colid);

-- 更新学生表为学生指定所属学院
update tb_student set colid=1 where stuid between 1001 and 1006;
update tb_student set colid=2 where stuid in (1007, 1008);
update tb_student set colid=3 where stuid=1009;
16:33:18

-- 创建老师表
create table tb_teacher
(
teaid int not null comment '工号',
teaname varchar(20) not null comment '姓名',
teasex bit default 1 comment '性别',
teabirth date comment '生日',
teatitle varchar(10) default '助教' comment '职称',
colid int not null comment '所在学院'

-- primary key (teaid),
-- foreign key (colid) references tb_college (colid)

);

添加主键

alter table tb_teacher add constraint pk_teacher_teaid primary key (teaid);

添加外键约束

alter table tb_teacher add constraint fk_teacher_colid foreign key (colid) references tb_college (colid);

你可能感兴趣的:(day35 初识数据库)