數(shù)據(jù)庫 - 數(shù)據(jù)的倉庫(集散地) - database - 實現(xiàn)數(shù)據(jù)持久化和數(shù)據(jù)管理
持久化 - 將數(shù)據(jù)從內(nèi)存轉(zhuǎn)移到能夠長久保存數(shù)據(jù)的存儲介質(zhì)的過程
數(shù)據(jù)庫的分類:關系型數(shù)據(jù)庫(SQL)和非關系型數(shù)據(jù)庫(NoSQL)
文件系統(tǒng) / 層次數(shù)據(jù)庫 / 網(wǎng)狀數(shù)據(jù)庫
關系型數(shù)據(jù)庫
- 1970s - E.F.Codd - IBM研究員 - System R
- 理論基礎:關系代數(shù)和集合論
- 具體表象:用二維表來保存數(shù)據(jù) - 學生表
~ 行:一條記錄 - 一個學生的信息
~ 列:一個字段 - 學生的某個屬性,例如:學號、姓名、出生日期
~ 主鍵列:能夠唯一標識一條記錄的列,例如:學生的學號 - 編程語言:SQL - 結(jié)構(gòu)化查詢語言
~ DDL - 數(shù)據(jù)定義語言 - create / drop / alter
~ DML - 數(shù)據(jù)操作語言 - insert / delete / update / select
~ DCL - 數(shù)據(jù)控制語言 - grant / revoke
LAMP = Linux + Apache + MySQL + PHP
PHP ---> Java
MySQL ---> Oracle
Linux ---> 小型機
去IOE運動
IBM的小型機
Oracle的數(shù)據(jù)庫
EMC的存儲設備
關系型數(shù)據(jù)庫產(chǎn)品:
- Oracle - 甲骨文
- IBM DB2
- Microsoft SQLServer
- Sybase
- MySQL
- PostgreSQL
- SQLite
連接MySQL的圖形化客戶端工具:
- Navicat for MySQL - 病貓
- SQLyog - 海豚
- Toad for MySQL - 蛤蟆
學生(學號、姓名、性別、生日、家庭住址)
學院(編號、名稱、網(wǎng)站、……)
老師(工號、姓名、性別、生日、職稱、所在學院編號)
課程(編號、名稱、學分)
設計數(shù)據(jù)庫中的表 - ER圖(實體關系圖)- 概念模型圖
讀者、圖書
用戶、購物車、商品、訂單
用戶、單車
人、身份證
實體:學生、學院
關系:屬于
重數(shù):多對一
操作實例:
創(chuàng)建用戶識別碼
create user 'root'@'%' identified by '123456';
重置權(quán)限
flush privileges;
賦予所有特權(quán)
grant all privileges on . to 'root'@'%' with grant option;
-- 如果存在名為school的數(shù)據(jù)庫就刪除它
drop database if exists school;
-- 創(chuàng)建名為school的數(shù)據(jù)庫并指定默認的字符集為utf-8
create database school default charset utf8;
-- 切換到school數(shù)據(jù)庫上下文環(huán)境
use school;
-- 創(chuàng)建學生表
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
-- 創(chuàng)建老師表
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)
);