探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
在之前的两篇博客中我们学习了MySQL的DDL、DML、DCL、DQL。在本篇博客中,我们来一起学习:数据的完整性、多表查询、MySQL中的函数、MySQL数据库的备份与恢复。
数据的完整性可确保用户输入到数据库的数据是正确的。为此,需要在创建表时在表中添加约束。
数据完整性的分类
数据库表中的一行数据(即一条记录)表示一个实体(entity),实体完整性的作用就是标识每一行数据(即一条记录)使其不重复
确保实体完整性的三种约束:
主键约束(primary key)
在表中设置一个主键;被标识为主键的数据在表中是唯一的且其值不能为null
设置主键约束(primary key)的第一种方式:
CREATE TABLE student(
id int primary key,
name varchar(50)
);
在该方式中将id字段设置为主键,请参见第2行代码
设置主键约束(primary key)的第二种方式:
CREATE TABLE student(
id int,
name varchar(50),
primary key(id)
);
在该方式中,先定义了字段id,然后设置该字段为主键,请参见第4行代码。
若采用该方式非常便于设置联合主键,请看如下示例:
CREATE TABLE student(
classid int,
studentid int,
name varchar(50),
primary key(classid,studentid)
);
在该示例中,将classid和studentid定义为联合主键,请参见第5行代码。
请注意:不要把联合主键理解成两个主键;它们是以两个字段联合的形式作为主键
设置主键约束(primary key)的第三种方式:
CREATE TABLE student(
id int,
name varchar(50)
);
ALTER TABLE student ADD PRIMARY KEY (id);
在该示例中,先创建了表,然后利用ALTER语句设置id字段为主键。
唯一约束(unique)
为字段添加唯一约束(unique)后该字段对应的值不能重复,是唯一的。
请看如下示例:
CREATE TABLE student(
id int primary key,
name varchar(50) unique
);
在该示例中利用unique关键字为字段name添加唯一约束,请参见第3行代码。
自动增长列(auto_increment)
可用auto_increment关键字标识int类型的字段,设置后该字段的值会自动地增长。常见的做法是给int类型的主键添加auto_increment。请看如下示例:
CREATE TABLE student(
id int primary key auto_increment,
name varchar(50)
);
在该示例中将主键id设置为auto_increment,那么该字段的值会自动地增长。
域完整性用于确保表中单元格的数据正确;所以,域完整性就代表了表中单元格的完整性。
在之前介绍实体完整性时可知:在为字段设置主键约束(primary key)和唯一约束(unique)以及自动增长列(auto_increment)后,当往表中插入数据时会将该数据与该列中其他单元格的数据相比较;满足条件后才会插入到数据库。但是域完整性的作用范围只限定于本单元格,它不会将带插入数据与其他单元格相比较。
常见的域完整性约束:
关于数据类型和check约束,鉴于其比较简单,故在此不再赘述;下面将介绍非空约束(NOT NULL)和默认值约束(DEFAULT)。
非空约束(NOT NULL)
请看如下示例:
CREATE TABLE student(
id int PRIMARY KEY,
name varchar(50) NOT NULL,
gender varchar(10)
);
在该示例中设定name字段为NOT NULL,所以在插入数据时必须为该字段设值。
默认值约束(DEFAULT)
请看如下示例:
CREATE TABLE student(
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE,
gender VARCHAR(10) DEFAULT ‘女‘
);
在该示例中为gender字段设值了默认值,所以可以用如下方式插入数据
insert into student(id,name) values(1,‘toc‘);
insert into student(id,name,gender) values(2,‘jok‘,‘男‘);
insert into student(id,name,gender) values(3,‘jerry‘,default);
引用完整性也叫参照完整性,常用于设值外键。
请看如下示例:
CREATE TABLE student(
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) DEFAULT ‘男‘
);
CREATE TABLE score(
scoreid INT PRIMARY KEY,score
studentid INT ,
scoreresult INT,
CONSTRAINT fk_score_studentid FOREIGN KEY (studentid) REFERENCES student(id)
);
INSERT INTO student(id,name,gender) VALUES(1,‘大泽玛利亚‘,DEFAULT);
INSERT INTO student(id,name,gender) VALUES(1,‘武藤兰姐姐‘,‘女‘);
INSERT INTO student(id,name,gender) VALUES(3,‘苍井空妹妹‘,‘女‘);
INSERT INTO student(id,name,gender) VALUES(4,‘波少野结衣‘,DEFAULT);
INSERT INTO score(scoreid,studentid,scoreresult) VALUES(200,1,98);
INSERT INTO score(scoreid,studentid,scoreresult) VALUES(201,2,97);
INSERT INTO score(scoreid,studentid,scoreresult) VALUES(202,3,93);
INSERT INTO score(scoreid,studentid,scoreresult) VALUES(203,3,91);
INSERT INTO score(scoreid,studentid,scoreresult) VALUES(204,4,88);
INSERT INTO score(scoreid,studentid,scoreresult) VALUES(205,4,69);
在此建立两张表。其中,学生表student作为主表,分数表score作为子表。请注意在score中的studentid学生编号表示成绩是属于哪个学生的,该值必须是student表中id列里的值。所以,利用外键FOREIGN KEY将score中的studentid与student表中id建立起联系,请参见代码第11行。
小结如下:
也可利用SQL语句设置外键,如下:
ALTER TABLE score ADD CONSTRAINT fk_score_studentid FOREIGN KEY(studentid) REFERENCES student(id);
常用的表与表之间的关系有一对一,一对多,多对多,现分别作如下介绍。
请看如下示例:
CREATE TABLE person(
personid INT PRIMARY KEY,
personname VARCHAR(50) NOT NULL
);
CREATE TABLE persondetail(
detailid INT PRIMARY KEY,
job VARCHAR(30),
hobby VARCHAR(50),
address VARCHAR(50)
);
ALTER TABLE persondetail ADD CONSTRAINT fk_detailid_personid FOREIGN KEY (detailid) REFERENCES person(personid);
INSERT INTO person(personid,personname) VALUES(1,‘大泽玛利亚‘);
INSERT INTO person(personid,personname) VALUES(2,‘武藤兰姐姐‘);
INSERT INTO person(personid,personname) VALUES(3,‘苍井空妹妹‘);
INSERT INTO person(personid,personname) VALUES(4,‘波少野结衣‘);
INSERT INTO persondetail(detailid,job,hobby,address) VALUES(1,‘演员‘,‘看书‘,‘东京‘);
INSERT INTO persondetail(detailid,job,hobby,address) VALUES(2,‘诗人‘,‘弹琴‘,‘大阪‘);
INSERT INTO persondetail(detailid,job,hobby,address) VALUES(3,‘作家‘,‘摄影‘,‘千叶‘);
INSERT INTO persondetail(detailid,job,hobby,address) VALUES(4,‘模特‘,‘练字‘,‘仙台‘);
在该示例中存在两张表person和persondetail。person表中的每个人、persondetail中的每条详细信息,这两者一一对应;请参见代码第13行
请看如下示例:
CREATE TABLE student(
studentid INT PRIMARY KEY,
studentname VARCHAR(50) NOT NULL
);
CREATE TABLE report(
scoreid INT PRIMARY KEY,
studentid INT,
score INT
);
ALTER TABLE report ADD CONSTRAINT fk_report_student FOREIGN KEY (studentid) REFERENCES student(studentid);
INSERT INTO student(studentid,studentname) VALUES(1,‘大泽玛利亚‘);
INSERT INTO student(studentid,studentname) VALUES(2,‘武藤兰姐姐‘);
INSERT INTO student(studentid,studentname) VALUES(3,‘苍井空妹妹‘);
INSERT INTO student(studentid,studentname) VALUES(4,‘波少野结衣‘);
INSERT INTO report(scoreid,studentid,score) VALUES(1,1,87);
INSERT INTO report(scoreid,studentid,score) VALUES(2,1,77);
INSERT INTO report(scoreid,studentid,score) VALUES(3,2,67);
INSERT INTO report(scoreid,studentid,score) VALUES(4,2,77);
INSERT INTO report(scoreid,studentid,score) VALUES(5,3,87);
INSERT INTO report(scoreid,studentid,score) VALUES(6,4,77);
在该示例中存在两张表:学生表student和成绩表report。每一个学生对应多门课程的成绩,这就是一对多的关系;请参见代码第13行
请看如下示例:
CREATE TABLE student(
studentid INT PRIMARY KEY,
studentname VARCHAR(50) NOT NULL
);
CREATE TABLE teacher(
teacherid INT PRIMARY KEY,
teachername VARCHAR(50) NOT NULL
);
CREATE TABLE student_teacher_relation(
sid INT,
tid INT
);
ALTER TABLE student_teacher_relation ADD CONSTRAINT fk_sid FOREIGN KEY (sid) REFERENCES student(studentid);
ALTER TABLE student_teacher_relation ADD CONSTRAINT fk_tid FOREIGN KEY (tid) REFERENCES teacher(teacherid);
INSERT INTO student(studentid,studentname) VALUES(1,‘大泽玛利亚‘);
INSERT INTO student(studentid,studentname) VALUES(2,‘武藤兰姐姐‘);
INSERT INTO student(studentid,studentname) VALUES(3,‘苍井空妹妹‘);
INSERT INTO student(studentid,studentname) VALUES(4,‘波少野结衣‘);
INSERT INTO teacher(teacherid,teachername) VALUES(1,‘田中瑞稀‘);
INSERT INTO teacher(teacherid,teachername) VALUES(2,‘奧村麻依‘);
INSERT INTO teacher(teacherid,teachername) VALUES(3,‘大竹里步‘);
INSERT INTO teacher(teacherid,teachername) VALUES(4,‘田中瑞稀‘);
INSERT INTO student_teacher_relation(sid,tid) VALUES(1,1);
INSERT INTO student_teacher_relation(sid,tid) VALUES(1,3);
INSERT INTO student_teacher_relation(sid,tid) VALUES(2,1);
INSERT INTO student_teacher_relation(sid,tid) VALUES(2,2);
INSERT INTO student_teacher_relation(sid,tid) VALUES(2,3);
INSERT INTO student_teacher_relation(sid,tid) VALUES(3,4);
INSERT INTO student_teacher_relation(sid,tid) VALUES(4,1);
在该示例中存在三张表:student、teacher、student_teacher_relation。每个学生可能上几个老师的课,每个老师可能教多个学生,这就是多对多的关系,故在此创建了student_teacher_relation表;请参见代码第20-21行
关于这三张表的关系请参见下图:
原本student和teacher是多对多的关系,为化解该关系引入了student_teacher_relation表;现在转换成了student与student_teacher_relation的一对多以及teacher与student_teacher_relation的一对多。
现在开始进入有些繁琐,但是又非常重要的MySQ多表查询。
合并结果集就是把两个select语句的查询结果合并到一起,即:
SELECT * FROM table1 关键字 SELECT * FROM table2
合并结果集的小结:
请看如下示例:
CREATE TABLE student(
studentid INT PRIMARY KEY,
studentname VARCHAR(50) NOT NULL,
studentaddress VARCHAR(50) DEFAULT ‘东京‘
);
CREATE TABLE person(
personid INT PRIMARY KEY,
personname VARCHAR(50) NOT NULL,
age INT DEFAULT 18,
personaddress VARCHAR(50) DEFAULT ‘大阪‘
);
INSERT INTO student(studentid,studentname) VALUES(1,‘大泽玛利亚‘);
INSERT INTO student(studentid,studentname) VALUES(2,‘武藤兰姐姐‘);
INSERT INTO student(studentid,studentname) VALUES(3,‘苍井空妹妹‘);
INSERT INTO student(studentid,studentname) VALUES(4,‘波少野结衣‘);
INSERT INTO person(personid,personname) VALUES(1,‘田中瑞稀‘);
INSERT INTO person(personid,personname) VALUES(2,‘奧村麻依‘);
INSERT INTO person(personid,personname) VALUES(3,‘大竹里步‘);
INSERT INTO person(personid,personname) VALUES(4,‘波少野结衣‘);
SELECT studentid AS id,studentname AS name FROM student UNION ALL SELECT personid,personname FROM person;
连接查询就是求出多个表的乘积。
比如:table1连接table2,那么查询出的结果就是table1*table2。
但是请注意:连接查询会产生笛卡尔积,比如:集合A={a,b},集合B={0,1,2},则集合A和B的笛卡尔积为{(a,0),(a,1),(a,2),(b,0),(b,1),(b,2)}。这当然不是我们想要的结果,那么怎么去除重复的记录和不需要的记录呢?可通过表之间都存在关联关系(比如外键)去除笛卡尔积。
请看如下示例:
CREATE TABLE student(
studentid INT PRIMARY KEY,
studentname VARCHAR(50) NOT NULL
);
CREATE TABLE report(
scoreid INT PRIMARY KEY,
studentid INT,
score INT
);
ALTER TABLE report ADD CONSTRAINT fk_report_student FOREIGN KEY (studentid) REFERENCES student(studentid);
INSERT INTO student(studentid,studentname) VALUES(1,‘大泽玛利亚‘);
INSERT INTO student(studentid,studentname) VALUES(2,‘武藤兰姐姐‘);
INSERT INTO student(studentid,studentname) VALUES(3,‘苍井空妹妹‘);
INSERT INTO student(studentid,studentname) VALUES(4,‘波少野结衣‘);
INSERT INTO report(scoreid,studentid,score) VALUES(1,1,87);
INSERT INTO report(scoreid,studentid,score) VALUES(2,1,77);
INSERT INTO report(scoreid,studentid,score) VALUES(3,2,67);
INSERT INTO report(scoreid,studentid,score) VALUES(4,2,77);
INSERT INTO report(scoreid,studentid,score) VALUES(5,3,87);
INSERT INTO report(scoreid,studentid,score) VALUES(6,4,77);
现在需要查询出每个学生每门课的成绩,可以这么做:
SELECT * FROM student,report WHERE student.studentid=report.studentid;
这么查询出来发现结果集中有两个studentid,这显然不够直观和美观;所以我们可以在查询时筛选出需要的数据:
SELECT student.studentid,student.studentname,report.scoreid,report.score
FROM student,report
WHERE student.studentid=report.studentid;
在此,筛选出studentid、studentname、scoreid、score即可。这么做目的是达到了,但是觉得SQL语句很长有些臃肿;嗯哼,我们可以给表取列名来解决这个小问题:
SELECT s.studentid,s.studentname,r.scoreid,r.score
FROM student s,report r
WHERE s.studentid=r.studentid;
在此给student表取别名为s,report表取别名为r,再执行查询即可。
原文:http://blog.csdn.net/lfdfhl/article/details/54782740