语法
------------------------------------------------------
CREATE TRIGGER trigger_name
ON {table_name | view_name}
{FOR | After | Instead of} [insert, update,delete]
AS
sql_statement
-------------------------------------------------------
关键词 trigger
-------------------------------------------------------
create trigger 触发器名称 //创建触发器
after insert on table1 //触发器类型,什么时候启动触发器
for each row //受影响的行
begin //开始
insert into table2(table2_id) values(new.table1_id);// 触发器要执行的业务
end;
普通员工无法查看薪资,管理成可以查看,
SQL语句举例:
if( type = "employee" ){
String sql = "select id, name from employee";
}
if (tyepe = "manager"){
String sql = " select * from employee";
}
String sql = " select * from employee";
最笨的办法是创建两张表,区别是一张有薪资 一张没有薪资字段。维护数据的时候,必须同时维护两张表。
什么是视图:一个人包含某个查询的虚拟表
对视图进行操作,依托于真实的表
主要目的简化语句
对性能没有改善
视图允许嵌套
视图不能索引,没有关联,没有默认值
视图的用途
筛选表中的行,降低数据库的复杂程度,
防止未经许可的用户访问敏感数据,提高安全性
将多个物理数据抽象为一个逻辑数据库
一次编写多次使用
可授权访问表的特定部分
封装计算字段
视图的基本操作和语法
创建视图
CREATE VIEW view_name AS SELECT column_naem(s) FROM table_name WHERE condition ---------------------------------------------- 创建视图 ,关键字 view:create view 视图名 as select 字段 from 表名;
更新视图
CREATE OR REPLACE VIEW view_name AS SELECT table_name WHERE condition
撤销视图
DROP VIEW view_name
使用视图
SELECT * FROM view_name
他是一条SQL查询语句
本身不包含数据
是一张虚表
查询数据
语法
SELECT select_list //要查询的列名称 FROM table_list // 要查询的表名称 WHERE condition //行条件 GROUP BY grouping_columns // 对结果分组 HAVING condition //分组后的条件 ORDER BY sorting_columns // 对结果分组 LIMIT offset_start,row_count // 结果限定
出去重复记录:distinct 一个列可能有多个重复的值,如果只想要不同的值,那么久用distinct:select distinct columns_name,column_name from table_name;
目标:from
简单链接:inner join,若干表中有至少一个匹配,则返回行:
全连接:full outer join:返回左右表中所有记录
左连接left join 如果表中有至少一个匹配,则返回行
有链接:right join,即使左表中没有匹配,也从右表中返回所有行
别名:as --> select column_name from table_name as name
链接join
select column_name from table1 inner join table2 on table1.column_name=column_name
select column_name(s) from table1 full outer join table2 on table1.column_name=table2.column_name;
select column_name from table1 left join table2 on table1.column_name = table2.column_name left join 关键字从左表(table1)返回所用行,即使右表(table2)中没有匹配,如果右表中没有匹配,则结果为null。
select column_name from table1 right join table2 on table1.column_name = table2.column_name 关键字right join从右表中返回所有行,即使左表中没有匹配的,如果左表中没匹配则返回null。
过滤:where
查询EMP表中的SAL列中等于5000,3000,1500的值 select * from EMP where SAL(5000,3000,1500) ----------------------------------------------------------- select score from student where id=11 and score in (20,30): 相当于:select from student where id =11 and score=20 or score = 30;
查询emp中的comm列,中的空值 select * from emp where comm is null; --------------------------------------------------------------------- select score is null from student where id =9; 返回值为0或者1.
select score btween 20 and 30 from student where id = 10;表示score>=20 & score <=3
and:并且 select * from emp where sal>2000 and sal<3000
or:或者 select * from emp where sal> or sal <3000
not:非: select * from emp where not sal>=500;
between and 判断值是否在某个区间内:
is null:判断是否为null
关系运算符:=、!=、<>(不等于)、>、<、<=、>=; --->select * from table_name where name="smith"
group by:他后面的的字段不能用别名,使用汇总函数的时候,才会用它select后面的字段(除了汇总函数)都要出现在他的后面。
having :having就是为group而生的,用来过滤分组,where是对select结果进行限制。cube,rollup.
查询
多表关联查询:
内联结:
外联结:分为左联结和联结(left join 和 right join)
select * from mess left join user on "user".id = 1; select * from student left join classes on student.cid = classes.id and classes.name = "Java";
select * from mess right join user on "user".id = 1; select * from student right join classes on student.cid = classes.id and classes.name = "Java";
嵌套查询:
select * from orders where customer_id = (select id from sustomer where name ="卫庄")
联结查询:
select * from mess inner join user where mess.user_id = "user".id select * from student , clsses where student.cid and classes.name = "Java"; // 可以将长的字段名字或者表的 名字 都可以用 as + 新的字符代替原来的名字。
like(模糊查询)
如果不带%号,等同于精确查询,实际没有意义。模糊查询使用较多。%占位符 为空 不为null
like:查询某个字段包含“字符”关键字的数据 select * form student where name like "%查询的关键字符%"
在数据库中查询以字符“我”开头的数据:select * from student where name like "我%"
在数据库中查询以字符“我”结尾 的数据: elect * from student where name like "%我"
查询字段长度为 n(即字符的个数,中英文一样) 的数据:用下划线条数表示长度n的数。select * from student where name like "_";
name 长度为3,并且以“我” 开头;select * from student where name like "我_ _"
name 长度为3 ,并且“我” 在中间;select * from student where name like "我";
name 长度为3,并且“我” 最后;select * from student where name like "_ _我";
字符集[charlist] :读取name以字母A到H字母开头的: select * from where name like regexp '^A.H';
不在任何字符列表中的单一字符charlist :选取name不以A到H字母开头的网站: select * from emp where like regexp '^A.H'
select column_name(s) from table1 union select column_name(s) from table2
union使用列子
select cust_name,cust_contact,cust_emall from customers where cust_state in("il","in","mi") union select cust_name,cust_contact,cust_emall from customers where cust_name = "fun4All"