MySQL
1、 MySQL 命令:
a) 修改表名:alter table 表名 rename 新表名
Rename table 原表名 to 新表名
b) 修改列名:altertable 表名 change 字段名 新字段 类型
Update 表名 set 字段名=新字段类型
c) 修改字段:alter table 表名 modify 字段名 新字段
Update 表名 set 字段名=新内容 where <表达式>
d) 修改列顺序:alter table 表名 modify 字段名 类型 first----第一行
alter table 表名 modify 字段名 类型 after 字段名-----指定位置
e) 创建数据库:createdatabase <数据库名>
f) 创建表名:create table <表名>(数据);
例子:create table A(id int primarykey auto_increment,name varchar(200));
g) 选择那个数据库:use <数据库名>
h) 增加一个字段:alter table 表名 add<字段名 字段类型>
i) 增加一列到最前面:alter table 表名 add 字段名 字段类型 first
j) 增加一列到指定位置:alter table 表名 add 字段名 字段类型 after 字段名
k) 插入表单元数据:insert into <表名>(要插值的元素) values(值);
例子:insert into A(name) values(‘肖亮亮’);
l) 删除数据库:drop database <数据库名>
m) 删除表:drop table <表名>
n) 删除外键:alter table 表名 drop foreign,key <当前的表名>_FK
o) 删除表中的某行:delete from table_name where <表达式>
p) 展示所有数据库:show databases;
q) 展示所有表: show tables;
r) 导入sql文件:source 文件路径
s) 描述:desc
t) 自动增加:auto_increment
约束:
a) 主键约束:primary key
b) 默认:default
c) 不允许重复:unique
d) 外键:foreign key
e) 不允许为null: not null
f) 检查约束:check
性别 varchar(20) default ‘男’ check(性别=’男’ or 性别=’女’);
Seclect 查询语句:
a) 投影查询:就是允许用户显示需要的列
Select id from <表名>
b) 条件查询:=、>、<、<>、!=、>=、<=、!>、!<
Select * from <表名> where 条件
c) 模糊查询:like
%:包含0个到多个
-:包含一个字符
():指定范围内
(^):不属于指定范围内
Select * from Student s where s.name like ‘肖%’
d) Null 值查询:
Select * from student where name is null;
e) 限制范围查询:between…and…
Select * from student s whereid between <开始> and <结束>;
f) 删除重复字段数据:distinct
Select distinct 字段名 from 表名;
关联外键:
例子:第一张表:Create tableTeacher(teacher_id int primary key auto_increment,teacher_name varchar(200),teacher_sex varchar(200));
第二张表: create table Student(stu_id int primary key auto_increment,teacher_id int,constraint Student_FK foreign key(teacher_id) references teacher(teacher_id), stu_name varchar(200),stu_sex varchar(200));
关联查询:
a) 左连接:left join
左表不管条件是否满足都会出现,右表的条件不满足会以null填充
Select * from <第一张表名s> left join <第二张表名t> on s.id = t.s_id and s.id=1;
b) 右连接:right join
右表不管条件是否满足都会出现,左表的条件不满足会以null填充
Select * from <第一张表名s> right join <第二张表名t> on s.id = t.s_id and s.id=1;
c) 等值连接:inner join
左右表同时满足条件才出现
Select * from <第一张表名s> inner join <第二张表名t> on s.id = t.s_id and s.id=1;
d) 分组查询:group by
Select * fromfrom <第一张表名s>,<第二张表名t> wheres.id=t.s_id group by s.id having s.id = 1;
e) 查询某字段的数据总和(只限于整数、浮点)sum
Select sum(student_age)as 学生总年龄 from student;
f) 查询当前表的总行数count
Select count(*) fromstudent;
g) 查询表的某字段的平均值 avg
Select avg(student_age) as 年龄的平均值 from student;
h) 排序order by
Select * from fromstudent s order by s.stu_age
I) max最大值
II) min最小值
关联子查询:谓词
a) any
select * from class whereclass.class_id >any (select class.class_id from class whereclass.class_id=5);
只要满足一个条件即可,也可以说any是||(或)的意思
b) all
select * from class whereclass.class_id >all (select class.class_id from class whereclass.class_id=5);
同时条件全部满足,也可以说all是&&(与)的意思
c) 限制 limit
Select * from students where s.stu_id limit <开始值>,<结束值>
d) 在…里面… in
Select * from students where s.stu_id in(值,值,值);
e) 不在…里面… not in
Select * from students where s.stu_id not in(值,值,值);
f) 取两个集合的交集 exists
Select * from studentwhere exists (select * from Class where student.class_id = class.class_id)
g) 取两个集合的差集 not exists
Select * from studentwhere not exists (select * from Classwhere student.class_id = class.class_id)
select * from studentwhere not exists (select 1 from class where class.class_id = student.class_id);
h) Union 删除两个集合中的重复值只取一份加没有重复的数据 前提:两张表结构一样
Select class_id fromstudent union select class_id from class;
i) Union All 两个结果的合并
Select class_id fromstudent union All select class_id from class;
JDBC(java date baseconnctivity ):
是用来执行sql语句,可以为多种关系数据库提供统一访问。
Java语言编写一个JDBC:
例子:
Class JDBCUtils{
Public void getConnection(){
Connection c = null;
Statement st=null;
ResultSet rs=null;
Try{
Class.forName(“com.mysql.jdbc.Driver”);//加载驱动
String url = “jdbc:mysql://127.0.1.1:3306/数据库名”;
String username=”root”;
String password=”root”;
c = DriverManager.getConnection(url,username,password);
//获取数据连接
st = c.createStatement();
//获取发送命令的对象
rs = st.executeQuery(“select *from student”);
//根据sql语句得到结果值
where(st.next()){
String username = st.getString(1);
//获取数据库里的第一条数据
}
} catch (ClassNotFoundException e) {
System.out.println(“找不到这个类”);
} catch(SQLException e) {
System.out.println(“SQL异常”);
} finally {
Rs.close();
St.close();
c.close();
}
}
}
View 视图:
概念:是一张虚表,因为无表结构
创建一个视图:
Create orreplace view view_name as select * from table_name;
Create viewview_name as select * from table_name;
操作视图的RUD和操作table表一样
查询自定义的视图:
a) Select table_name from information_schema.views;//查询所有
b) Show table status where commet=’view’;//查询所有
c) Show create view view_name \g; //根据视图名查询
触发器:trigger
语法:create trigger trigger_name after/before insert/update/delete on table_name for each row <sql语句>
比较after/before的区别:
After:在执行前表的sql语句执行前已经加载了
Before:在执行前表的sql语句后加载
创建触发器:
添加(insert) after:create trigger trigger_af_insert after insert on table_name For each row <sql语句>
---insert into table_name<字段名>values<值,值,值>
---updatetable_name set <字段名>=<新内容> where<表达式>
---deletefrom tbale_name where <表达式>
---createtable table_name(字段名 类型……);
添加(insert) before:create trigger trigger_bf_insert before insert on table_name For each row <sql语句>
---insert into table_name<字段名> values<值,值,值>
---updatetable_name set <字段名>=<新内容> where<表达式>
---deletefrom tbale_name where <表达式>
---createtable table_name(字段名 类型……);
删除(delete) after:create trigger trigger_af_delete after deleteon table_name for each row <sql语句>
---insert into table_name<字段名>values<值,值,值>
---updatetable_name set <字段名>=<新内容> where<表达式>
---deletefrom tbale_name where <表达式>
---createtable table_name(字段名 类型……);
删除(delete) before:create trigger trigger_bf_delete before deleteon table_name for each row <sql语句>
---insert into table_name<字段名>values<值,值,值>
---updatetable_name set <字段名>=<新内容> where<表达式>
---deletefrom tbale_name where <表达式>
---createtable table_name(字段名 类型……);
修改(update) after: create trigger trigger_af_update afterupdate on table_name for each row <sql语句>
---insert into table_name<字段名>values<值,值,值>
---updatetable_name set <字段名>=<新内容> where<表达式>
---deletefrom tbale_name where <表达式>
---createtable table_name(字段名 类型……);
修改(update) before: create trigger trigger_bf_update before update on table_name for each row <sql语句>
---insert into table_name<字段名>values<值,值,值>
---updatetable_name set <字段名>=<新内容> where<表达式>
---deletefrom tbale_name where <表达式>
---createtable table_name(字段名 类型……);
查询所有触发器:show triggers;
Select* from information_schema.triggers \g;
索引:
索引的优缺点:
i. 优点:提高数据库查询的速度
ii. 缺点:创建和维护索引需要耗费时间
索引分类:
iii. 普通索引
iv. 唯一性索引àunique
v. 全文索引à使用pulltext参数可以设置索引全文,全文索引只能建在char、varchar、text类型字段上
vi. 单列索引
vii. 多列索引
viii. 空间索引àspatial参数可以设置索引为空间索引
创建索引:
1、 create index indexd_name on table_name(字段名);
在表创建成功后创建索引
2、 create table_name(id int primary key auto_increment,namevarchar(30),index index_name(id));
创建表时创建索引
查询索引:
1、 show create table index_name\G;
删除索引:
1、 drop index index_name on table_name
存储过程:
1、 创建存储过程
delimiter分隔符
// 自定义的分隔符
createprocedure sp_name(in a integer)
创建 存储过程 存储名 (参数可有可无)
Begin 开始
if a=0 then
select * from user order by id asc;
else sql语句
select * from user order by iddesc;
end if;
end;结束
//
2、 创建存储函数
delimiter //
create function sp_name(a char(20))
returns char(30)
return concat('hello,',a,'!');
//
select sp_name('world');查询
3、 变量的使用
4、 定义条件和处理程序
5、 光标的使用
6、 流程控制的使用
7、 调用存储过程和函数
8、 查看存储过程和函数
9、 修改存储过程和函数
10、 删除存储过程和函数
11、 JAVA调用存储过程
1.存储过程和函数的区别
存储过程的作用就是要实现我们的整体功能,他可以调用函数来实现;而函数作为代码的模块,用于完成一个特定的功能,而我们可以调用函数来实现我们要的整体功能。
如果需要多个返回值就要用存储过程,函数只能返回一个值。函数必须返回值,然而存储过程不必。
存储过程可以直接执行,而函数必须是被调用
2触发器
事前触发器运行于触发事件发生之前,而事后触发器运行于触发事件发生之后。通常事前触发器可以获取事件之前和新的字段值。
语句级触发器可以在语句执行前或后执行,而行级触发在触发器所影响的每一行触发一次。
与特定表关联的存储过程。当在该表上执行DML操作时,可以自动触发该存储过程执行相应的操作
触发操作:Update、Insert、Delete
一个触发器只能属于一个表,一个表可有多个触发器
Create Or Replace Trigger <名称>
[Before | After | Delete | Insert |Update [Of <列名表>] [OR
Before | After | Delete | Insert |Update [Of <列名表>] …]
ON <表名>
[For Each Row] --------对于行级触发器
<PL/SQL块>
END;
注意:没有参数。因为触发器是自动执行的,不能向它传参数
3.游标
游标是客户机上开辟的一块内存,用于存放SQL语句返回的中间结果;
游标可以协调PL/SQL与SQL之间的数据处理矛盾(PL/SQL是过程性语言,每次只能处理单个记录;而SQL是描述性语言,每次可以处理多行记录。);
PL/SQL中可以通过游标来过程化存取SQL返回的结果
总结:truncate、delete、drop