1、mysql与java类型对应表
bigint Long
tinyint Integer
decimal BigDecimal
datetime Date
timestamp Date
2、视图:由查询结果形成的一张虚拟表。
当一个查询结果出现非常频繁时,并且该查询结果进行子查询出现的非常频繁,此时可以将一个查询结果保存为一张虚拟的表,来供其他查询使用。
优点:
- 可以简化查询
- 可以进行权限的控制。比如说将表的权限封闭,在开发相应的视图权限时,视图中只开放部分的数据。
- 大数据分表。比如当一张表的行数超过100万行,就会变慢,此时一种有效的方法就是将一张表拆开为多张表来保存。例如分为t1、t2、t3、t4,具体操作打开链接。
视图和表的关系:
视图是表的查询结果,当表的数据变化时,会影响视图的结果。
视图中数据和表中数据一一对应时,而且还有如果要将数据插入(insert操作)视图中,该数据必须包含表中没有默认值的列数据。
3、触发器:用来监视某种情况并发出某种操作,与函数非常相似,都需要声明和执行,但触发器不是由程序来调用的,而是由事件来触发的、激活从而实现执行。
1、创建简单触发器,在向学生表INSERT数据时,学生数增加,DELETE学生时,学生数减少;
CREATE TABLE student_info
(
stu_no
INT(11) NOT NULL AUTO_INCREMENT,
stu_name
VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (stu_no
)
) ENGINE=INNODB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
CREATE TABLE student_count
(
student_count
INT(11) DEFAULT 0
);
? TRIGGER
CREATE TRIGGER trigger_student_count_insert
AFTER INSERT
ON student_info FOR EACH ROW
UPDATE student_count SET student_count=student_count+1;
CREATE TRIGGER trigger_student_count_delete
AFTER DELETE
ON student_info FOR EACH ROW
UPDATE student_count SET student_count=student_count-1;
INSERT INTO student_info VALUES(NULL,‘张明’),(NULL,‘李明’),(NULL,‘王明’);
DELETE FROM student_info WHERE stu_name IN(‘张明’,‘李明’);
TRUNCATE TABLE student_info;
TRUNCATE TABLE student_count;
DELIMITER
C
R
E
A
T
E
T
R
I
G
G
E
R
t
r
i
g
g
e
r
s
t
u
d
e
n
t
c
o
u
n
t
i
n
s
e
r
t
A
F
T
E
R
I
N
S
E
R
T
O
N
s
t
u
d
e
n
t
i
n
f
o
F
O
R
E
A
C
H
R
O
W
B
E
G
I
N
U
P
D
A
T
E
s
t
u
d
e
n
t
c
o
u
n
t
S
E
T
s
t
u
d
e
n
t
c
o
u
n
t
=
s
t
u
d
e
n
t
c
o
u
n
t
+
1
W
H
E
R
E
s
t
u
d
e
n
t
c
l
a
s
s
=
0
;
U
P
D
A
T
E
s
t
u
d
e
n
t
c
o
u
n
t
S
E
T
s
t
u
d
e
n
t
c
o
u
n
t
=
s
t
u
d
e
n
t
c
o
u
n
t
+
1
W
H
E
R
E
s
t
u
d
e
n
t
c
l
a
s
s
=
N
E
W
.
s
t
u
d
e
n
t
c
l
a
s
s
;
E
N
D
CREATE TRIGGER trigger_student_count_insert AFTER INSERT ON student_info FOR EACH ROW BEGIN UPDATE student_count SET student_count=student_count+1 WHERE student_class=0; UPDATE student_count SET student_count=student_count+1 WHERE student_class= NEW.student_class; END
CREATETRIGGERtriggerstudentcountinsertAFTERINSERTONstudentinfoFOREACHROWBEGINUPDATEstudentcountSETstudentcount=studentcount+1WHEREstudentclass=0;UPDATEstudentcountSETstudentcount=studentcount+1WHEREstudentclass=NEW.studentclass;END
DELIMITER ;
DELIMITER
C
R
E
A
T
E
T
R
I
G
G
E
R
t
r
i
g
g
e
r
s
t
u
d
e
n
t
c
o
u
n
t
d
e
l
e
t
e
A
F
T
E
R
D
E
L
E
T
E
O
N
s
t
u
d
e
n
t
i
n
f
o
F
O
R
E
A
C
H
R
O
W
B
E
G
I
N
U
P
D
A
T
E
s
t
u
d
e
n
t
c
o
u
n
t
S
E
T
s
t
u
d
e
n
t
c
o
u
n
t
=
s
t
u
d
e
n
t
c
o
u
n
t
−
1
W
H
E
R
E
s
t
u
d
e
n
t
c
l
a
s
s
=
0
;
U
P
D
A
T
E
s
t
u
d
e
n
t
c
o
u
n
t
S
E
T
s
t
u
d
e
n
t
c
o
u
n
t
=
s
t
u
d
e
n
t
c
o
u
n
t
−
1
W
H
E
R
E
s
t
u
d
e
n
t
c
l
a
s
s
=
O
L
D
.
s
t
u
d
e
n
t
c
l
a
s
s
;
E
N
D
CREATE TRIGGER trigger_student_count_delete AFTER DELETE ON student_info FOR EACH ROW BEGIN UPDATE student_count SET student_count=student_count-1 WHERE student_class=0; UPDATE student_count SET student_count=student_count-1 WHERE student_class= OLD.student_class; END
CREATETRIGGERtriggerstudentcountdeleteAFTERDELETEONstudentinfoFOREACHROWBEGINUPDATEstudentcountSETstudentcount=studentcount−1WHEREstudentclass=0;UPDATEstudentcountSETstudentcount=studentcount−1WHEREstudentclass=OLD.studentclass;END
DELIMITER ;
INSERT INTO student_info VALUES(NULL,‘AAA’,1),(NULL,‘BBB’,1),(NULL,‘CCC’,2),(NULL,‘DDD’,2),(NULL,‘ABB’,1),(NULL,‘ACC’,1);
DELETE FROM student_info WHERE stu_name LIKE ‘A%’;
2、创建包含多条执行语句的触发器:
创建触发器,在INSERT时首先增加学生总人数,然后判断新增的学生是几年级的,再增加对应年级的学生总数;
创建触发器,在DELETE时首先减少学生总人数,然后判断删除的学生是几年级的,再减少对应年级的学生总数;
delimiter:重新定义结束标识符,后面跟空格和重新定义的结束标识符。
使用BEGIN…END结构,可以定义一个执行多句SQL的触发器。
NEW在触发器为INSERT事件类型时有效,表示当前正在插入的数据;同理,OLD在触发器类型为DELETE事件类型时
有效,表示当前正在删除的数据。
如上面的示例中,可以在触发器中使用NEW.student_class取得正在插入的学生信息中年级值,使用
OLD.student_class取得正在删除的学生信息中的年级值。
https://blog.csdn.net/Goskalrie/article/details/53020631
存储过程就像我们编程语言中的函数一样,封装了我们的代码(PLSQL、T-SQL);
优点:能够将代码封装起来保存在数据库之中,执行效率高;
缺点:每个数据库的存储过程语法几乎都不一样,不利于维护和迭代;
视图是一张虚表,建立在已存在的表的基础上的,已存在的叫基表,一个基表可以有0或多个视图;
索引是一种快速查询表中内容的机制,类似于字典中的目录,存储在表之外;