标识符

​ 字母、数字、_、$

​ 1、数字不能开头

​ 2、区分大小写

​ 3、不能是关键字

​ 4、不能有空格

​ 5、长度不限,见名知意

​ 6、驼峰式起名(类名每个单词首字母大写、变量名首字母小写之后每个单词首字母大写)

数据类型

​ 数据类型:简单数据类型 和 引用数据类型(复合类型)

​ 简单数据类型:四类八种

​ 数值型:整型:byte、short、int、long 浮点型: float、double

​ 字符型:char ‘ ‘

​ 布尔型:boolean – true/false

​ 引用数据类型:类、集合、数组…

​ 字符串:String “ ”

变量常量

​ 变量:在程序运行过程中值可以发生改变的量,在同一作用域范围内不能重复定义(全局变量和局部变量)

​ 作用域:大括号开始到大括号结束

​ 全局变量:定义在类里面,函数外面的变量

​ 局部变量:定义在函数里面的变量

​ 常量:用final修饰的,标识符所有字母全部大写的,且在程序运行过程中值不能发生改变的量

​ final:最终的意思

转义字符 “\”

​ \n换行、双\表示一个\、 “ \“ ”

注释

​ 单行注释: //注释内容

​ 多行注释: /* 注释内容 */

​ 文档注释: /**

​ * 注释内容

​ */

类型转换

​ 分为:自动转换 和 强制转换

​ 等级:byte、short、char < int < long < float < double

​ 自动转换:由低等级向高等级转换

​ 强制转换:由高等级向低等级转换

运算符

​ 算数运算符:+(拼接)、-、*、/、%(取余)

​ 比较运算符:==、!=、>、 <、 >=、 <= ( String的比较相等equals()方法 )

​ 逻辑运算符:&& &:有一个为false,结果就为false

​ || |:有一个为true,结果就为true

​ !:取反

​ ^:相同为false,不同为true

​ &&与&、||与|的区别:发生短路

​ 二进制运算符:& | ^ <<左移 >>右移

 赋值运算符:+=   -=    *=    /=   %=	

​ 其它运算符:++、– 、 三目运算符

流程控制

​ 顺序语句

​ 条件语句

​ 循环语句

if条件语句

​ 分支结构:单分支,语法结构,if(boolean类型的判断条件){ if代码块 }
​ 双分支,语法结构,if (){ if代码块 }else{ else代码块 }
​ 多分支,语法结构,if(){ }else if(){ }else if(){ }…else{ }

​ switch语句

1
2
3
4
5
6
7
System.out.println("111");
int count = 100;
if(count % 2 !=0) {
System.out.println("你好!");
System.out.println("hello");
}
System.out.println("222");

数组

​ 分为:一维数组 和 二维数组(多维)

​ 概念:存放相同数据类型的多个值

​ 定义:静态定义 和 动态定义

一维数组

​ 定义: 数据类型 [] 数组名 或 数据类型 数组名[]

​ 静态定义:数据类型 [] 数组名 = {值1, 值2, …….};

​ 特点:已经知道了具体的值

​ 定义与初始化不能分开写

​ 动态定义:数据类型 [] 数组名 = new 数据类型[1024];

​ 特点:不知道具体的值,但是规定了数组的大小(存放元素的个数)

​ 定义和初始化可以分开写

二维数组

​ 定义: 数据类型 [][] 数组名 或 数据类型 数组名[][]

​ 静态定义:数据类型 [][] 数组名 = {值1, 值2, …….};

​ 动态定义:数据类型 [][] 数组名 = new 数据类型[5][6];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args) {	
double [][] dds = {
{2.1, 3, 3.33},
{ 4.44 },
{5.55, 6.66}
};

//输出单个元素
System.out.println( dds[2][1] );

//长度
System.out.println( dds.length );
System.out.println( dds[2].length );

for(int i = 0; i < dds.length; i++) {
for(int j = 0; j < dds[i].length; j++) {
System.out.print(dds[i][j] + " ");
}
System.out.println();
}
}

函数

​ 函数的定义:

​ 定义在类里面,main函数外面;一个类里面可以定义多个函数,但不能重复定义。

​ 语法结构:

​ 访问修饰符 函数的返回类型 函数名( 参数 ){ 函数体 }

​ 访问修饰符:public(公共的)、protected(受保护)、默认、private(私有的)

​ 函数的返回类型:无返回(void) 和 有返回(各种数据类型)

​ 函数名:首字母小写,之后每个单词首字母大写

​ 参数:用户调用函数时带过来的值,在函数体中起到作用

​ 函数体:具体的代码实现过程

​ 函数之间的调用:

​ main函数可以调用普通函数;普通函数之间可以相互调用;但是普通函数不能自己调用

​ 自己,会出现死循环;普通函数不能调用main函数,main函数被JVM调用,当main函数执

​ 行完毕,程序结束,JVM结束运行。

面向对象

​ 类和对象

访问修饰符

​ public protected 默认 private

​ 访问域不同:

​ public:公共的,谁都可以访问,访问域最大

​ private:私有的,只有当前类可以访问,访问域最小

​ 默认:什么都不写,只有在同一个包中,不管是否有父子关系,都可以访

​ 问; 在不同的包中,不管是否有父子关系,都不可以访问

​ protected:受保护的,在同一个包,不管是否有父子关系,都可以访

​ 问; 在不同的包中,有父子关系,可以访问;没有父子关系,不能访问

​ 访问域大小:

​ public > protected > 默认 > private

getter/setter方法

​ 作用:传值

抽象类

​ 抽象类:abstract修饰的类

​ 作用:统一约束子类

​ 抽象类里面可以有抽象方法,抽象方法是没有方法体的方法

​ 抽象类不能直接实例化使用,而是通过其子类使用

​ 所有继承抽象类的普通类,必须实现抽象类里面的抽象方法 – 约束子类

​ 抽象类里面可以有抽象方法,也可以有普通方法,不一定必须含义抽象方法

​ 含义抽象方法的类,必须声明为抽象类

接口

​ java的接口是身份的象征,作用:统一约束子类

​ 定义接口不在使用class,是interface,接口里面的方法是没有方法体的方法,

​ 要想使用接口,必须通过实现类使用。

​ 方法默认:public abstract void chaocai() ;

​ 属性默认:public final int LEGS = 10;

内部类

​ 定义在类里面的类

​ Java的内部类:成员内部类、静态内部类、方法内部类、匿名内部类

Java类库

Object

String

封装类

​ 简单数据类型 和 其对应的 包装类型 之间的转换

Integer

​ Integer类包装一个对象中的原始类型int`的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.  int 转  Integer

2. Integer 转 int

3. String 转 int

4. int 转 String

5. String 转 Integer

6. Integer 转 String

7. Integer 转 double

8. 十进制 转 八进制、十六进制

Double

1
2
3
4
5
6
7
8
9
`Double`类包装一个对象中的原始类型`double`的值

1. double 转 Double

2. Double 转 double

3. String 转 double

4. isNaN(double d); 如果参数不是一个数,报true;是一个数,报false。

Character

1
2
3
4
5
`Character`类包装一个对象中的基本类型`char`的值

1. String 转 Char

2. char 转 String

Boolean

1
2
3
4
5
6
布尔类将对象中的基元类型`boolean`的值进行包装

1. boolean 转 Boolean
2. Boolean 转 boolean
3. String 转 boolean
4. boolean 转 String

​ 总结:

​ 1. 简单类型 转 其包装类:使用包装类的构造函数

​ 2. 包装类 转 其简单类型:【包装类对象的】标识符.***Value();

​ char:【包装类对象的】标识符.valueOf(char c);

​ 3. String 转 简单类型:包装类名.parse***(【字符串的】标识符);

​ 4. 简单类型 转 String: 包装类名.toString(【简单类型的】标识符);

集合

​ 数组:可以存放一组相同数据类型的数据结构

​ 特点:①、数组一旦创建,其长度不可改变

​ ②、一个数组中只能存放一种数据类型的数据

​ 集合:可以存放多种数据类型数据的数据结构

​ 特点:①、集合的长度是可以改变的

​ ②、对存放的数据的类型没有限制

​ 在Java中,使用一些类来描述集合,这样的类我们称之为—-集合类;集合类的对象就是

​ 一个集合。

​ 实例化:

​ int [] arr = new int[10];

​ 集合类 标识符 = new 集合类();

​ 学习任务:

​ 1、了解Java中集合类及它们之间的继承关系

​ 2、创建集合类的对象(—-创建集合)

​ 3、向集合中添加元素

​ 4、获取集合中的元素(获取单个元素、删除元素)

​ 5、遍历集合( 迭代器 )

集合的分类

​ 单值集合(v) 键值对集合(k-v)

![](D:\J1906\视频\day16\笔记\01-集合\01-集合分类及继承关系.png)

集合中的区别

​ 1、Collection 和 Collections 的区别

​ Collection是一个接口,是所有单值集合类的父接口;

​ Collections是一个帮助类,这个类中提供了很多对集合的操作的静态方法

​ 2、List接口 和 Set接口 的区别

​ List接口和Set接口都是 Collection接口的子接口;

​ List集合中的元素是有序可重复的

​ Set集合中的元素是无序不可重复的

​ 3、ArrayList 和 LinkedList 的区别

​ ArrayList集合 和 LinkedList集合都是List集合的实现类,其元素都是有序可重复的;

​ ArrayList中的元素的存储是基于数组的实现,元素查询、添加速度较快,但是插入元素

​ 速度较慢;

​ LinkedList中的元素的存储是基于链表的实现,元素的插入较快,查询与添加较慢;

​ 4、ArrayList 和 Vector 的区别

​ ArrayList和Vector都是基于数组的存储;

​ Vector实现了同步,是线程安全的;

​ ArrayList线程非安全的,但是其存储效率较高;

​ 5、HashSet 和 TreeSet 的区别

​ 两者都是Set接口的实现类,其元素都是无序不可重复的;

​ 区别在于其元素的存储方式不同:

​ HashSet是按照hash码散列存储;

​ TreeSet是树形存储;

​ 6、HashMap 和 Hashtable的区别

​ 两者都是Map接口的实现类,都表示键值对集合;

​ HashMap中允许有一个null键和多个null值;

​ Hashtable中不允许有null作为key或value;

​ HaspMap未实现同步,是线程非安全的;

​ Hashtable实现了同步,是线程安全的;

Collection接口中常用的方法(List/Set通用)

Iterator 和 ListIterator

​ ListIterator:可以倒序遍历List集合;

​ Iterator:只能对集合中的元素进行移除操作

​ ListIterator:不仅可以移除元素,而且可以替换当前元素,或插入元素

Map集合常用方法

作业

ArrayList HashSet HashMap Hashtable
创建对象
添加元素
插入元素
移除元素
获取单个元素
遍历集合

异常

​ Java程序运行过程中所发生的异常事件可分为两类:

​ 错误(Error):JVM系统内部错误、资源耗尽等严重情况

​ 异常(Exception): 其它因编程错误或偶然的外在因素导致的一般性问题,

异常分类

![](D:\J1906\视频\day19\笔记\02-异常\01-异常分类.png)

异常处理机制

​ 关键字:try、catch、throw、throws、finally

try_catch异常处理过程:

​ 1. 如果try中的代码块不出现异常,所有代码按顺序执行,catch中的代码块不会执行,

​ finally中的代码块会执行。

​ 2. 如果try中的代码块出现异常

​ ①、在异常出现之前,所有代码块按顺序正常执行

​ ②、抛出异常:一旦发现try中的代码出现异常,首先确认出现异常种类,然后创建

​ 这个异常种类的对象,接着把这个异常对象提交到Java异常堆栈中

​ ③、捕获异常:Java系统一旦发现异常堆栈中有异常对象,就终止try中的代码块的

​ 执行,去寻找一个类型与该异常对象匹配的catch代码块来处理这种异常;处理完成后,

​ 就接着执行catch后的代码块。

​ ④、如果没有找到类型与异常对象匹配的catch,则程序终止。

​ ⑤、finally后的代码块仍然会执行

​ 3. finally后的代码块,不管try中代码块是否出现异常,都会执行

​ **final、finally、finalize()区别

​ 父类异常,可以处理子类异常发生的异常情况

​ 子类异常,不能处理父类异常发生的异常情况

​ 处理异常时,子类在前,父类在后

throws异常处理过程:

​ 向上抛异常,谁调用谁处理

自定义异常

​ 在Java的类库中,定义了很多异常类,每个异常类代表一种异常,但是这些异常类总是有限

的,当我们在实际项目开发中,有很多看似正常的情况,在具体环境下就有可能出现编程异常的

情况(比如:int i = -5; 但是在开发过程中,如果想要把 i 当做年龄来使用,那么就会有异常发生的

可能),所以我们在很多情况下,都要自定义自己的异常处理类。

​ 自定义过程:

​ 1. 创建一个类,继承Exception / RuntimeException类

​ 2. 在该类中重写Exception / RuntimeException类中的构造器

正则表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Test01 {	
public static void main(String[] args) {

//正则表达式每次都要编译
String str = "abcd";
boolean b1 = str.matches(".*2.*");
System.out.println( b1 );

//正则表达式一次编译,多次使用
//先编译正则表达式,把编译号的正则表达存放在p变量中
Pattern p = Pattern.compile(".*a.*");
//用编译好的正则表达式,匹配某个字符串
//返回Mather类,该类中有一个方法是用来判断是否返回要求的
Matcher m = p.matcher("abcdef");
//判断是否符合正则表达式规则
boolean b2 = m.matches();

}

}

​ []:代表一个字符,表示或的元素

​ 例如:a[abc]c,表示字符串有三个字符,第一个字符是a,第二个字符是a或b或c,第

​ 三个字符是c

​ [^]:表示取反

​ a[^abc]c:第二个字符不是a或不是b或不是c,是之外的任何一个字符
​ 预定义字符

​ . : 表示任意一个字符

​ \d: [0-9] 任意数字

​ \s : 表示任意一个空格

​ \w:表示任意一个大小写字母

​ \: 表示一个 \

​ 数量词:? + * {} 修饰最接近它的前一个字符

​ ?:表示一个或没有

​ +:表示一个或多个

​ *:表示0个或多个

​ {n}:表示有n个

​ {n,}:表示至少有n个

​ {n,m}:表示至少有n个,最多有m个

​ 边界: ^ $
​ ^:开始
​ $:结束
​ Java的正则表达式的匹配:是完全匹配
​ 其它语言:子串匹配(JS/SQL)

1
2
String str = "abcd#sadsd#dsds#sdcs";
String [] strs = str.split("#");

IO流

IO的概述

​ 输入、输出

IO的分类

​ Input/Output

​ IO流(输入/输出流):就是和外界进行数据交互的管道

​ IO流的作用:用于程序与外界进行数据的交互

​ 分类

​ 根据数据传输的方向,可以将流分为:

​ 输入流:将外界的数据传入程序中

​ 输出流:将程序中的数据传出到外界

​ 根据处理数据的类型,可以将流分为:

​ 字符流:以字符为单位进行数据的输入/输出

​ 字节流:以字节为单位进行数据的输入/输出

​ 根据数据处理的能力,可以将流分为:

​ 节点流:低级流

​ 处理流:高级流

File类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//把路径封装为一个File对象
File file = new File("D:/code/b.txt");

boolean b1 = file.exists(); //判断此路径是否存在
boolean b2 = file.createNewFile(); //如果不存在,创建文件(不是创建文件夹)
boolean b3 = file.mkdir(); //创建文件夹

boolean b4 = file3.isFile(); //判断文件存在且是一个普通文件
boolean b2 = file3.isDirectory();//判断文件夹

//1.读写状态
boolean b1 = f.canRead();//是否可读
boolean b2 = f.canWrite(); //是否可写

//2.隐藏状态
boolean b3 = f.isHidden();

//3.获取文件内容的长度
long len = f.length();

//4.获取文件/目录的名字
String fileName = f.getName();
//获取文件/目录的路径
String filePath = f.getPath();

//5.获取当前文件/目录 的父目录
String parentPath = f.getParent();
File parentFile = f.getParentFile();

//6.删除
// if( f.delete() ) {
// System.out.println("删除成功!");
// }else {
// System.out.println("删除失败!");
// }

//7.输出文件最后一次修改的时间(单位:毫秒)
long l = f.lastModified();
Date d = new Date(l);

System.out.println( d.toLocaleString() );

1
2
3
4
5
6
7
8
9
10
11
//列举出当前路径下的所有文件和文件夹(以字符串形式显示),包括隐藏文件
String [] ff = f.list();
for(String s : ff) {
System.out.println( s );
}

//列举出当前路径下的所有文件和文件夹(以File对象形式显示),包括隐藏文件
File [] files = f.listFiles();
for(File file : files) {
System.out.println( file.getName() );
}

RandomAccessFile

​ 随机流

​ 即是输入流,有是输出流

字节输入流

字节输出流

![04-OutputStream ](D:\J1906\视频\day20\笔记\03-IO\04-OutputStream .png)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.io03.Stream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test04_copy {

public static void copyFile(String src, String dest) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream( src );
fos = new FileOutputStream( dest );

//方法一:
// int k;
// while((k=fis.read()) != -1) {
// fos.write(k);
// }

//方法二:
// byte [] b = new byte[1024];
// while((fis.read(b)) != -1) {
// fos.write(b);
// }

//方法三:
byte [] b = new byte[1024];
int len;
while((len=fis.read(b)) != -1) {
fos.write(b, 0, len);
}

System.out.println("复制完成!");
} catch (FileNotFoundException e) {
System.out.println("复制失败!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("复制失败!");
e.printStackTrace();
}finally {
try {
if(fos != null) {
fos.close();
}
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}

}


public static void main(String[] args) throws Exception {

copyFile("D:/code/Test02.java", "C:/copy/eee.jpg");

}

}

对象序列化

​ ObjectOutputStream:对象序列化,将程序中的对象输出到外界(保存数据)

​ ObjectInputStream:对象反序列化,将外界存储的对象读取到程序中

网络编程

网络:

​ 将计算机通过网络设备及通信线路连接在一起

网络分类

​ 局域网(校内网)、城域网(都市网)、广域网(万维网)

拓扑结构

​ 计算机–连接–计算机

![](D:\J1906\视频\day22\笔记\04-网络编程\01-拓扑结构.png)

按照传输介质划分

​ 双绞线:8细线 橙白 橙 绿白 蓝 蓝白 绿 棕白 棕 6根有用

​ 同轴电缆:铜绞线

​ 光导纤维、视线介质等

通信流程

分层

InetAddress

​ 该类用来描述网络中计算机地址,一个InetAddress类的对象就代表一个网络地址。

类的作用 一个InetAddress类的对象就代表一个网络地址
构造方法 没有
创建对象 InetAddressInetAddress addr1 = InetAddress.getLocalHost(); InetAddress addr2 = InetAddress.getByName(“192.168.5.254”);
常用方法

Socket

​ socket(套接字):代表一个IP和port组合,也就是两个程序的连接。

​ A程序和B程序之间的通信:

      1. B程序要占有一个端口号
      2. 启动B程序,使其处于等待状态,等待A程序连接
      3. A程序根据B程序的IP和port,向B程序发送连接请求
类的作用 用于向服务端发送连接请求,通过IP和port请求建立连接
构造方法
常用方法

ServerSocket

类的作用 创建一个网络服务,等待客户端来连接

线程

​ 进程中程序执行的多态路径

开启线程

​ 第一步:

​ 1、继承Thread类

​ 2、实现Runnable接口

​ 第二步:

​ 以上两种方式,都需要重写线程类或接口中的run();

​ 第三步:

​ 通过线程类中的start()方法,调用重写的run()方法,开启新线程

​ main方法的执行是主线程;如果想开启多个线程,则必须在main函数中开启,如果想创建新

线程则需要线程类。

生命周期

  1. new创建线程类–> 新生状态

  2. 新生状态–>可以直接让它死亡–>死亡状态

  3. 新生状态–>就绪状态–>死亡

  4. 新生状态–>就绪状态–>分配给时间片–>运行状态–>死亡

  5. 新生状态–>就绪状态–>分配给时间片–>运行状态–>调度–>就绪状态–>分配时间片–>运行状态

  6. 新生状态–>就绪状态–>分配给时间片–>运行状态–>阻塞–>阻塞状态–>解除阻塞事件–>就绪状态–>分配时间片–>运行状态

  7. 新生状态–>就绪状态–>分配给时间片–>运行状态–>阻塞–>阻塞状态–>死亡状态

  8. 反复就绪、调度、阻塞,直到CPU分配给足够的时间片,线程才能执行完成

数据库

​ 1、什么是数据库

​ 存放数据的仓库,为了保证数据的安全性

​ 2、为什么要学数据库

​ 数据是软件的灵魂/核心

​ 3、做为软件开发者,我们学习数据库的哪些内容?

​ 软件开发者—-数据库的使用

​ a、数据库的安装

​ b、数据库软件的操作(增、删、改、查)

​ 数据库开发者—–数据库的原理及研发

数据:DATA

数据库:DATABASE

数据库管理系统:DBMS

数据库系统:DBS

数据库管理员:DBA

主键(PK):能够唯一区分关系中的每一条记录(元祖)的一个属性

外键(FK):表中的一个属性不是当前表的主键,却是另一张表的主键,称之为当前表的外键

展示所有数据库:show databases;

创建数据库:create database aaa;

删除数据库:drop database aaa;

选择/切换数据库:use 数据库名;

展示数据库中所有的表:show tables;

查看表结构:description/desc 表名;

一、select 查

​ 语法结构:select 属性名 from 表名;

​ select检索至少知道两点:

​ ①、查什么

​ ②、从哪里查

1、查找一个表中的一个属性时

​ select 属性名 from 表名;

​ ①、从学生表中查找学生的姓名

​ select sname from t_student;

​ ②、从系主任管理表中找出系主任的姓名

​ select mname from t_man;

​ ③、从系部管理表中找出所有系的名字

​ select dname from t_dept;

2、查找一个表中的多个属性时

​ select 属性1, 属性2,… from 表名;

​ ①、找出学生表中学生的姓名和年龄

​ select sname, sage from t_student;

​ ②、从学生表中找出学生的学号、姓名、英文名

​ select sid, sname, sename from t_student;

​ * 找出所有学生的姓名和系名 – 笛卡尔积现象,联表查询

​ select sname, dname from t_student, t_dept;

3、查询一个表中的所有信息

​ select * from 表名;

​ ①、查找学生表中所有的信息

​ select * from t_student;

4、查询语句–限制结果行 关键字:limit

​ 1)、select 属性名… from 表名 limit n;

​ ①、从学生表中找出前5行学生的姓名和年龄

​ select sname, sage from t_student limit 5;

​ 2)、select 属性名… from 表名 limit n, m; 从第n(从0计数)行开始,取m行

​ ①、从学生表中找出第4行到第10行的学生的所有信息

​ select * from t_student limit 3, 7;

​ ②、从学生表中找出第6行到第12行的学生的学号、姓名

​ select sid, sname from t_student limit 5, 7;

​ select sid, sname from lianxi.t_student[限定名] limit 5, 7;

5、去掉重复项 关键字:distinct

​ ①、从学生表中输出sclass,去掉重复项

​ select distinct sclass from t_student;

​ ②、从学生表中找出学生的姓名、班级编号,并去掉重复行

​ select distinct sname, sclass from t_student;

6、运用数学表达式

​ select 列名+/-/*/ /数字[列名] from 表名;(列值必须是数值型)

​ ①、查找学生10年后的年龄

​ select sage+10 from t_student;

​ “11a”表示11, “a11“表示0;

​ ②、找出学生的学号,姓名,年龄,10年后的年龄,最终结果取前5行

​ select sid, sname, sage, sage+10 from t_student limit 5;

​ ③、输出学生表中学生的姓名,年龄加班级的和

​ select sname, sage+sclass from t_student;

7、处理null值 ifnull(可能出现null的属性名, 赋的值);

​ ①、输出学生的年龄和班级的和,如果sclass有null,则赋值0

​ select sage+ifnull(sclass,0) t_student;

8、取别名 关键字:as

​ 1)、select 属性名 as 别名 from 表名;

​ ①、给学生表中的sname取别名”姓名”,只输出前5个

​ select sname as 姓名 from t_student limit 5;

​ ②、输出学生的姓名,10年后的年龄并以此为别名

​ select sname as 姓名, sage+10 as 10年后的年龄 from t_student;

​ 2)、select 属性名 别名 from 表名;

​ ①、给学生表中的sname取别名”姓名”,只输出前5个

​ select sname 姓名 from t_student limit 5;

​ ②、输出学生的姓名,10年后的年龄并以此为别名

​ select sname 姓名, sage+10 10年后的年龄 from t_student;

​ 3)、select 属性名 as ‘别【空格】名’ from 表名;

​ ①、给学生表中的sname取别名”姓 【空格】 名”,只输出前5个

​ select sname as ‘姓 名’ from t_student;

​ String sql = “select sname as ‘姓 名’ from t_student”;

​ ②、在学生表中找出学生的学号(取别名学号),姓名(取别名姓名),高考分数

​ +100(取别名最终分数),结果取第3行到第7行

​ select sid as 学号, sname as 姓名, sscore+100 as 最终分数 from t_student

​ limit 2, 5;

9、串联操作符 concat()函数

​ 1)、select concat(列名, 列名) from 表名; 将两个列变为字符串拼接起来

​ ①、从学生表中找出学生的学号、姓名并拼接输出

​ select concat(sid, sname) from t_student;

​ 2)、select concat(列名, ‘自己加的字符串’, 列名) from 表名;

​ ①、在学号和姓名中间加一个字符串后输出

​ select concat(sid, ‘的姓名是’, sname) from t_student;

​ select concat(sid, ‘的姓名是’, sname) as 详细信息 from t_student;

10、select now(); 获取当前时间

​ ①、select now();

​ ②、select now() from t_student;

​ ③、select 3+9;

11、排序 按某个属性排序,降序、升序(默认) 关键字:order by

​ 1)、select *** from 表名 order by 属性名 asc; 按属性名由低到高排列

​ ①、找出学生的姓名、高考分数,并按高考分数升序排列

​ select sname, ifnull(sscore, 0) from t_student order by sscore asc;

​ select sname, ifnull(sscore, 0) from t_student order by sscor;

​ asc可以省略不写,因为默认升序排列

​ 2)、select *** from 表名 order by 属性名 desc; 降序排列

​ ①、找出最高分学生的详细信息

​ select * from t_student order by sscore desc limit 1;

​ 注:关键字顺序,from–> order by –> limit

​ ②、找出学生的姓名、分数、年龄(如果为null则10岁),班级(如果为null则1班),

​ 结果按年龄和班级升序

​ select sname, sscore, ifnull(sage, 10), ifnull(sclass, 1) from t_student order by

​ sage, sclass;

​ select sname, sscore, ifnull(sage, 10) as 年龄, ifnull(sclass, 1) as 班级 from

​ t_student order by 年龄, 班级;

​ ③、找出最小年龄学生的姓名、年龄、学号、分数*(如果年龄为null则赋20岁)

​ select sname, ifnull(sage, 20) as 年龄, sid, sscore from t_student order by 年龄

​ limit 1;

12、数据的过滤(即按照某个条件来筛选) 关键字:where

​ 1)、select *** from 表名 where 筛选条件;

​ ①、找出张威的详细信息

​ select * from t_student where sname=’张威’;

​ 2)、筛选条件 在 排序条件之前

​ ①、找出年龄为20岁的学生的详细信息,并按分数降序排列

​ select * from t_student where sage=20 order by sscore desc;

​ 3)、如果有 >、< 、>=、<=、!=、<>; where 属性名>值

​ ①、找出分数在600分以上学生的详细信息

​ select * from t_student where sscore>600;

​ ②、找出分数在600以下的学生的姓名、学号、分数(如果为null则赋1分),

​ 结果按年龄降序,最终取前3

​ select sname, sid, ifnull(sscore, 1) from t_student where ifnull(sscore, 1)<600

​ order by sage desc limit 3;

​ 注:where后不能使用别名

​ 4)、查找范围 where 属性名 between 值1 and 值2;

​ ①、找出年龄在18到20之间的学生的详细信息

​ select * from t_student where sage between 18 and 20;

​ 5)、查找多个条件 关键字: and

​ ①、查找年龄在20岁以下,且分数在500分以上的学生的详细信息

​ select * from t_student where sage<20 and sscore>500;

​ ②、查找分数500分以上,年龄在18到20之间且是1班的学生的详细信息

​ select * from t_student where (sscore>500) and (sage between 18 and 20) and

​ (sclass=1);

​ select * from t_student where (sscore>500) and (sage>=18 and sage<=20) and

​ (sclass=1);

​ 6)、查找多个条件 关键字:or

​ ①、找出学生是1班的,或年龄是22岁以上,或分数500以上的所有男生的详细信息

​ select * from t_student where (sclass=1 or sage>22 or sscore>500) and ssex=’男’;

​ 注:and 优先级高于 or

​ 7)、判断查询属性为null 关键字:is where 属性名 is null;

​ ①、找出年龄为null的学生的详细信息

​ select * from t_student where sage is null;

​ 8)、查询多个相同属性值 关键字:in where 属性名 in(值1, 值2, 值3, …);

​ ①、找出学号是1201, 1205, 1209, 1211的学生的详细信息

​ select * from t_student where sid in(1201, 1205, 1209, 1211);

​ select * from t_student where sid =1201 or sid=1205 or sid=1209 or sid=1211;

​ ②、找出英文名为Apple、Orange、Tea、Ice的学生的姓名、学号、英文名,并按年龄降

​ 序

​ select sname,sage, sid, sename from t_student where sename in(‘Apple’,

​ ‘Orange’, ‘Tea’, ‘Ice’) order by sage desc;

​ ③、找出学号为1201, 1202, 1203, 1204, 1205的学生的详细信息

​ select * from t_student where sid in(1201, 1202, 1203, 1204, 1205);

​ select * from t_student where sid>=1201 and sid<=1205;

​ 9)、当需要对条件取反时; 关键字:not where not 筛选条件;

​ ①、找出学号是1201, 1205, 1209, 1211之外的的学生的详细信息

​ select * from t_student where not sid in(1201, 1205, 1209, 1211);

​ select * from t_student where not (sid =1201 or sid=1205 or sid=1209 or sid=1211);

​ ②、

​ select * from t_student where not (sage>18 and sscore>500);

13、模糊查询 关键字:like like之后又两个符号’_’和’%’

​ 1)、’_’代表一个字符 where 属性名 like ‘规则’;

​ ①、找出姓张,且名字只有两个字符的学生的详细信息

​ select * from t_student where sname like ‘张_’;

​ 2)、’%’代表n个字符(n可以为0)

​ ①、找出英文名中含有‘ee’的学生的姓名和英文名,并按班级排序

​ select sname, sename from t_student where sename like ‘%ee%’

​ order by sclass desc;

​ ②、找出英文名中有字母‘a’和字母’e’的学生的详细信息

​ select * from t_student where sename like ‘%a%e%’ or sename like

​ ‘%e%a%’ order by sclass;

​ 注:模糊查询一般做搜索,用来判断字符串,会把所有数据都搜索一边,称之为通

​ 配符,一般能用其它操作达到目的,就不使用通配符。

14、SQL正则表达式 子串匹配

where 属性名 regexp ‘正则表达式’;

​ select * from t_student where sename regexp ‘[abc]ea’;

​ 1)、

​ ①、找出英文名与正则表达式’ee’匹配的学生的详细信息

​ select * from t_student where sename regexp ‘ee’;

​ select * from t_student where sename regexp ‘^ee$’;

​ ②、找出英文名与正则表达式’a’匹配的学生的详细信息

​ select * from t_student where sename regexp ‘a’;

​ 2)、[] 表示或,取其中之一

​ ①、找出英文名中含有a或b或c 的学生的详细信息

​ select * from t_student where sename regexp ‘[abc]’;

​ ②、找出英文名中含有TA、Ta、tA或ta的学生的详细信息

​ select * from t_student where sename regexp ‘[Tt][Aa]’;

​ 3)、| 表示或

​ ①、找出英文名中有’ee’或‘ea’的学生的详细信息

​ select * from t_student where sename regexp ‘ee|ea’;

​ 4)、^ 表示取反

​ ①、找出英文名中没有’ea’的学生的详细信息

​ select * from t_student where sename regexp ‘[^ea]’;

​ 5)、- 表示区间

​ ①、找出英文名中含有字母的学生的详细信息

​ select * from t_student where sename regexp ‘[a-zA-Z]’;

​ ②、找出英文名中不含a或b或c的学生的详细信息

​ select * from t_student where not sename regexp ‘[abc]’;

​ 6)、\ 表示转义

​ ①、找出英文名中含有 ‘.’ 的学生的详细信息

​ select * from t_student where sename regexp ‘\.’;

​ ②、找出英文名中含有 ‘' 的学生的详细信息

​ select * from t_student where sename regexp ‘\\‘;

​ ③找出英文名中含有 ‘-‘ 的学生的详细信息

​ select * from t_student where sename regexp ‘\-‘;

​ 7)、{} 表示数量

​ ①、找出电话号码中含有3个6的学生详细信息

​ select * from t_student where sphone regexp ‘6{3}’;

​ ②、找出电话号码中含有至少3个,最多5个6的学生详细信息

​ select * from t_student where sphone regexp ‘6{3, 5}’;

​ 8)、其它 ^ $

​ ①、找出英文名中以’A’ 开头的学生的详细信息

​ select * from t_student where sename regexp ‘^A’;

​ ②、匹配手机号

​ select * from t_student where sphone regexp ‘^1[35789][0-9]{9}$’;

15、函数

15-1、文本处理函数

​ 1)、left(str, n); 表示返回字符串str左边的n的个字符

​ ①、返回所有学生的姓名、英文名和英文名左边的3个字母

​ select sname, sename, left(sename, 3) from t_student;

​ 2)、right(str, n); 表示返回字符串str右边的n个字符

​ ①、返回所有学生的姓名、英文名、英文名中第2个和第3个字母

​ 左边3个的右边2个

​ select sname,sename, right(left(sename, 3), 2) from t_student;

​ 3)、length(str) 表示长度

​ ①、返回所有学生的姓名、英文名、英文名的长度

​ select sname,sename, length(sename) from t_student;

​ 4)、locate(substr, str); 表示substr在str中的位置

​ ①、获取学生的英文名和字符串’ee’在英文名中的位置

​ select sename,locate(‘ee’, sename) from t_student;

​ 5)、lower(str); 转小写

​ ①、获取学生的英文名和英文名的小写

​ select sename, lower(sename) from t_student;

​ 6)、upper(str); 转大写

​ ①、获取学生的英文名和英文名的大写

​ select sename, upper(sename) from t_student;

​ 7)、Ltrim(str); 去掉左边空格

​ ①、获取学生的英文名和去掉左边空格

​ select sename, ltrim(sename) from t_student;

​ 8)、Rtrim(str); 去掉右边空格

​ ①、获取学生的英文名和去掉右边空格

​ select sename, rtrim(sename) from t_student;

​ 9)、trim(str); 去掉两边空格

​ ①、获取学生的英文名和去掉左右两边的空格

​ select sename, trim(sename) from t_student;

15-2、时间处理函数

​ 1)、curdate(); 返回日期的函数 select curdate();

​ 2)、curtime();返回时间的函数 select curtime();

​ 3)、now(); 返回当前时间 select now();

​ 4)、sysdate(); 返回系统时间 select sysdate();

​ 5)、UTC_DATE(); 返回国际协调时间 select UTC_DATE();

​ 6)、month(bir); 返回月份

​ ①、找出学生的姓名、生日、和出生月份

​ select sname, sbir, month(sbir) from t_student;

​ 7)、monthname(bir); 返回月份的英文名

​ ①、找出学生的姓名、生日、和出生月份的英文名

​ select sname, sbir, monthname(sbir) from t_student;

​ 8)、month( now() ); 返回今天的月份 select month( now() );

​ 9)、dayname( d ); 返回星期几的英文名 select dayname( now() );

​ 10)、dayofweek( d ); 返回星期的第几天 select dayofweek( now() );

​ 11)、weekofyear( d ); 返回今年的第几个星期 select weekofyear( now() );

​ 12)、dayofyear( d ); 返回今年的第多少条 select dayofyear( now() );

​ 13)、dayofmonth( d ); 返回今天是本月的第多少天 selectdayofmonth( now() );

​ 14)、date_format(日期, 格式); 按固定格式输出

​ select date_format(now(), ‘%Y年-%m月-%d日’);

​ ①、输出学生的姓名,生日,生日按格式(–月–日–年)

​ select snema, sbir, date_format(sbir, ‘%m月-%d日-%Y年’) from

​ t_student;

16、聚集函数(汇总函数)

​ 1)、avg( d ); 返回平均值

​ ①、输出平均成绩

​ select avg(sscore) from t_student;

​ 2)、count(字段/属性); 返回行数

​ ①、统计有多少个学生

​ select count(sid) from t_student;

​ select count(*) from t_student;

​ ②、输出学生的个数和平均年龄

​ select count(*), avg(sage) from t_student;

​ 3)、max(字段/属性); 返回最大值

​ ①、求最大年龄

​ select max(sage) from t_student;

​ ②、求最大年龄的学生的姓名

​ select sname from t_student where sage=(select max(sage) from

​ t_student); 子查询

​ 4)、min(字段/属性); 返回最小值

​ ①、求最小年龄

​ select min(sage) from t_student;

​ 5)、sum(字段/属性); 返回和

​ ①、求总分

​ select sum(sscore) from t_student;

​ ②、求男生的 年龄和 及平均年龄

​ select sum(sage), avg(sage) from t_student where ssex=’男’;

​ ③、统计有多少男生

​ select count(*) from t_student where ssex=’男’;

17、分组 关键字:group by

​ select 聚集函数 from 表名 group by 属性1, 属性2;

​ ①、分别找出男生、女生中的最高成绩

​ select max(sscore) from t_student where ssex=’男’;

​ select max(sscore) from t_student where ssex=’女’;

​ ②、按照班级分组,求每个组的人数

​ select sclass, count(*) from t_student group by sclass;

​ ③、按性别分组,求每个组的最大年龄

​ select ssex, max(sage) from t_student group by ssex;

​ ④、按系编号分组,输出每个组的系编号和500分以上的学生的个数

​ select did, count(*) from t_student where sscore>500 group by did;

​ ⑤、按系编号分组,输出每个组的系编号和500分以上的学生的个数,按系编号降序排

​ 列,并取前2个

​ select did, count(*) from t_student where sscore>500 group by did order

​ by did desc limit 2;

​ 注:关键字的顺序

​ ⑥、按性别和班级分组,求每个组的人数,并输出组名(性别、班级)

​ select ssex, sclass, count(*) from t_student group by ssex, sclass;

​ select sclass, ssex, count(*) from t_student group by sclass,ssex;

​ ⑦、按性别和班级分组,求每个组的人数,并输出组名(性别、班级),结果按人数降序排

​ 列

​ select ssex, sclass, count(*) from t_student group by ssex, sclass order by

​ count(*) desc;

​ ⑧、按性别、系编号、班级分组,求每个组的人数

​ select ssex,did, sclass, count(*) from t_student group by ssex, did, sclass;

​ 横向统计 :关键字rollup

​ select ssex,did, sclass, count(*) from t_student group by rollup(ssex, did,

​ sclass);

18、分组后过滤 关键字:having 放在group by后

​ ⑨、按性别、系编号分组,输出人数大于5的组的编号和人数

​ select ssex, did, count() from t_student group by ssex, did having count()>5

*完整的关键字顺序

​ select distinct 属性1,…, 聚集函数 from 表名 where 筛选条件 group by 属性1,…

​ having 过滤条件 order by 属性1,… limit n, m;

​ where的条件不能使用聚集函数,having的条件可以使用聚集函数,having必须跟

​ 在group by后,没有group by 就没有having;

​ ⑩、按性别、系编号将300分以上的学生分组,输出人数大于4小于8的组的性别、

​ 编号、人数,结果按系编号降序排列,最终取第1个值

​ select ssex,did, count() from t_student where sscore>300 group by ssex, did having ) >4 and count()<8 order by did desc limit 1;

19、子查询

​ 1)、select *** from 表名 where *** (select * from 表名 where *);

​ ①、找出跟王涛一个班的学生的详细信息

​ select sclass from t_student where sname=’王涛’;

​ select * from t_student where sclass = (select sclass from t_student where

​ sname=’王涛’);

​ ②、找出与1203或1205年龄一样大的学生的姓名和年龄

​ select sage from t_student where sid in (1203, 1205);

​ select sname, sage from t_student where sage in (select sage from t_student

​ where sid in (1203, 1205));

​ ③、找到学号为1210的学生的系名

​ select did from t_student where sid=1210;

​ select dname from t_dept where did=(select did from t_student where

​ sid=1210);

​ ④、找出刘某某的系主任名

​ select did from t_student where sname=’刘某某’;

​ select mid from t_dept where did=(

​ select did from t_student where sname=’刘某某’);

​ select mname from t_man where mid=(

​ select mid from t_dept where did=(

​ select did from t_student where sname=’刘某某’));

​ ⑤、找出比刘某某年龄大 且 分数比1203高的学生的详细信息

​ select * from t_student where sage>(select sage from t_student

​ where sname=’刘某某’) and sscore>(select sscore fromt_student where sid=1203);

​ ⑥、找出最高分数学生的姓名、学号、分数

​ select sname, sid, sscore from t_student where sscore=(select max(sscore)

​ from t_student);

​ ⑦、统计平均分以上的学生有多少人

​ select count(*) from t_student where sscore>(select avg(sscore) from

​ t_student);

​ ⑧、找出比全校平均年龄,小的每个班的平均年龄

​ select sclass, avg(sage) from t_student group by sclass having avg(sage)<(select

​ avg(sage) from t_student);

​ 2)、关键字:all

​ ①、找出比2班每个学生都大的学生信息

​ select * from t_student where sage> all (select sage from t_student where

​ sclass=2);

​ ②、找出比500以上每个学生都大的学生的姓名

​ select sname from t_student where sage> all (select sage from t_student

​ where score>500);

​ 3)、关键字 :any 只要有一个匹配

​ ①、查询年龄与Tea或Ice或Eat年龄相同的学生的姓名

​ select sname from t_student where sage = any (select sage fromt_student

​ where sename in(‘Tea’, ‘Ice’, ‘Eat’));

20、联合查询

​ 1)、

​ ①、查找每个学生的姓名和系名

​ select sname, dname from t_student, t_dept; 笛卡尔积现象

​ select sname, dname from t_student, t_dept where t_student.did=t_dept.did;

​ ②、查找每个学生的姓名,系编号,系名

​ select sname, t_dept.did, dname from t_student, t_dept where

​ t_student.did=t_dept.did;

​ 取别名:

​ select sname, d.did, dname from t_student as s, t_dept as d where

​ s.did=d.did;

​ ③、输出学生的学号、姓名、系编号、系名、系主任编号、系主任姓名

​ select sid, sname, d.did, dname, m.mid, mname from t_student as s, t_dept as d,

​ t_man as m where s.did=d.did and d.mid=m.mid;

​ 输出分数是500分以上学生的学号、姓名、系编号、系名、系主任编号、系主任姓名
​ 且按年龄降序,结果取前3个

​ select sid, sname, d.did, dname, m.mid, mname from t_student as s,

​ t_dept as d, t_man as m where s.did=d.did and d.mid=m.mid and sscore>500

​ order by sage desc limit 3;

​ ④、输出最低分学生的姓名和系名

​ select sname, dname from t_student, t_dept where t_student.did=t_dept.did

​ and sscore=(select min(sscore) from t_student);

​ select sname, dname from t_student as s, t_dept as d where s.did=d.did

​ and sscore=(select min(sscore) from s); 子查询不能使用别名

​ select sname, dname from s, t_dept where s.did=t_dept.did and sscore=(select

​ min(sscore) from t_student as s); 子查询不能使用别名

​ 2)、内部联结 关键字:inner join … on 语法: 表名 inner join 表名 on 条件;

​ ⑤、在学生表和系部表中找出学生的姓名的系名(使用内联结)

​ select sname, dname from t_student inner join t_dept on

​ t_student.did=t_dept.did;

​ 3)、外联结—-左联结 left outer join…on

​ ⑥、在学生表和系部表中找出学生的姓名的系名(使用左联结)

​ select sname,dname from t_student left outer join t_dept on

​ t_student.did=t_dept.did;

​ 4)、外联结—-右联结 right outer join…on

​ ⑥、在学生表和系部表中找出学生的姓名的系名(使用右联结)

​ select sname,dname from t_student right outer join t_dept on

​ t_student.did=t_dept.did;

二、创建表SQL

​ 关键字:create

​ 语法:create table 表名(

​ 字段名1 数据类型 auto_increment, – 自动递增

​ 字段名2 数据类型 not null, – 不能为null

​ 字段名3 varchar(长度), – 设置长度

​ 字段名4 数据类型 default 值, – 设置默认值

​ ……

​ primary key(字段名1), – 设置主键

​ foreign key(当前表中的字段名) references 关联表(字段名【主键)

​ );

​ 创建user表

​ create table user(

​ u_id int auto_increment,

​ u_username varchar(10),

​ u_password varchar(10) not null,

​ u_age int default 0,

​ primary key(u_id)

​ );

​ 创建books表,创建外键关联user中的u_id

​ create table books(

​ b_id int primary key auto_increment,

​ b_name varchar(10) not null,

​ b_publish varchar(50),

​ b_date date,

​ b_price double default 0.00,

​ u_id int,

​ foreign key(u_id) references user(u_id)

​ );

三、insert 增 (添加)

​ 前提条件:查看表结构

​ desc 表名;

​ 1)、insert into 表名 values(值1, 值2, …); 值1,值2与数据库表中的先后顺序有关

​ insert into user values(1, ‘张三’, ‘zs123’, 18);

​ insert into user values(12, ‘李四’, ‘ls666’, 20);

​ 2)、insert into 表名(字段名1, 字段名2, …) values(值1, 值2, …); 值1, 值2与前面的字段名顺序有关

​ insert into user(u_id, u_username, u_password, u_age) values(30, ‘ww’, ‘ww123’, 22);

​ insert into user(u_username, u_password) values(‘ww’, ‘ww123’);

​ 主键因为有自动递增可以不写,自动递增以当前表中最大的主键值为基数递增

​ 3)、一张表中的数据,添加到另一张表中(前提两张表的字段要完全一样)

​ insert into A select * from B;

​ ①、把学生表中成绩大于500且是女生的学号、姓名、电话号码(作为密码)、年龄,添加到user

​ 表中

​ insert into user(u_id,u_username,u_password, u_age) select sid, sname, sphone, sage

​ from t_student where sscore>500 and ssex=’女’;

四、update 修

​ 1)、update 表名 set 字段名=新值 where *=值;

​ update user set u_username=’ccc’ where u_id=30;

​ update user set u_username=’ccc’, u_password=’ccc123’ where u_id=35;

​ update user set u_age = 50; – 没有筛选条件,就会把整张表中的这个属性值全部修改掉

​ ①、把学生表中600分以上的学生性别全部改为男

​ update t_student set ssex=’男’ where sscore>600;

​ select ssex from t_student where sscore>600;

五、delete 删

​ delete from 表名 where *=值; 按某个条件删除

​ ①、删除学生表中的所有女生

​ delete from t_student where ssex=’女’;

六、索引 关键字:index

​ 索引:是帮助Mysql高效获取数据的数据结构。

​ 分类:普通索引、唯一索引、主键索引、全文索引、多列索引

​ 没有索引时:

​ select * from t_student where sname=’张威’;

​ 会把表中所有记录扫描一边

explain

explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。

img

1、普通索引

​ select * from t_student where sname=’张威’;

​ 1)、创建索引

​ ①、create index 索引名 on 表名(列名);

​ 给学生表中的sname创建索引

​ create index aaa on t_student(sname);

​ ②、修改表时添加索引

​ alter table 表名 add index 索引名(列名);

​ ③、创建表的同时,创建索引

​ create table 表名(

​ id int,

​ name varchar(20) primary key,

​ index 索引名(列名id) – 创建索引

​ );

​ 2)、展示一个表上有哪些索引

​ 语法:show index from 表名;

​ ①、展示学生表上有哪些索引

​ show index from t_student;

​ 3)、删除索引

​ 语法:drop index 索引名 on 表名;

​ ①、删除学生表中sname上的索引aaa

​ drop index aaa on t_student;

2、唯一索引

​ 1)、创建唯一索引

​ 语法:create unique index 索引名 on 表名(列名);

​ ①、给英文名创建唯一索引

​ create unique index i_unique_sename on t_student(sename);

​ 2)、展示一个表上有哪些索引

​ 语法:show index from 表名;

​ ①、展示学生表上有哪些索引

​ show index from t_student;

​ 3)、删除索引

​ 语法:drop index 索引名 on 表名;

​ ①、删除学生表中sename上的索引i_unique_sename

​ drop index i_unique_sename on t_student;

3、主键索引

​ 主键是一种唯一性索引,但它必须指定为“PRIMARY KEY”。

4、全文索引

​ MySQL从3.23.23版开始支持全文索引和全文检索。在MySQL中,全文索引的索引类型

​ 为FULLTEXT。全文索引可以在VARCHAR或者TEXT类型的列上创建。

5、多列索引

​ Create table tab(

           id int,
 
          name varchar(20),
 
          sex char(4),

​ index indexName(name,sex)

​ );

七、视图 关键字:view

​ 一个视图就是一张虚拟的表(一般视图只查询,不做增删改)

​ 1)、展示数据库上的所有视图

​ show tables;

​ 2)、创建视图

​ 语法:create view 视图名 as select查询语句;

​ ①把 学生的学号、姓名、系编号、系名、系主任编号、系主任姓名 创建为一个视图

​ create view v_s_d_m as select sid,sname,d.did,dname,m.mid,mname from

​ t_student as s, t_dept as d, t_man as m where s.did=d.did and d.mid=m.mid;

​ select CONCAT(dname,”的系主任是:”, mname) from v_s_d_m;

​ ②、把500分以上的学生信息定义为一个视图

​ create view v_student_score_500 as select * from t_student where

​ sscore>500;

​ 3)、删除视图

​ 语法:drop view 视图名;

​ drop view v_student_score_500;

​ 4)、更改视图

​ 语法:create or replace 视图名 as select查询语句;

​ 注:如果来自一个表中的数据组成的视图,增删改视图里面的数据会对原表中的数据产

​ 生影响;

​ 如果来自多张标准中的数据组成的视图,增删改视图里面的数据不会对原表中的数

​ 据产生影 响;

​ 一般视图只用来查询,不要对使用进行增删改操作。

八、存储过程 关键字:procedure

​  一般在大型的数据库系统中,一组为了完成特定功能的sql语句集,存储在数据库中,经过第

一次编译后,再次调用不需要再次编译,用户只需要指定存储过程名字并给定参数就可以执行

完成任务。

​ 0)、修改结束符号

​ delimiter #

​ 1)、创建存储过程

​ 语法: create procedure 存储过程名(【in/out/inout】 参数 数据类型)

​ begin

​ sql语句1;

​ ……

​ end

​ ①、把学生表中500分以上的学生信息、系部管理表中的所有信息、系主任管理表

​ 中的所有信息 封装成一个存储过程

​ create procedure p_a()

​ begin

​ select * from t_student where sscore>500;

​ select * from t_dept;

​ select * from t_man;

​ end

​ 2)、调用存储过程

​ 语法:call 存储过程名;

​ call p_a();

​ 3)、删除存储过程

​ 语法:drop procedure if exists 存储过程名;

​ drop procedure if exists p_a;

​ 4)、带参数的存储过程

​ create procedure tat(n1 int, n2 int)

​ begin

​ declare n3 int;

​ set n3 = n1+n2;

​ select n3 as sum;

​ end

​ create procedure tat2(n1 int, n2 int)

​ begin

​ declare n3 int;

​ if n1 is null than set n1=10;

​ end if;

​ set n3 = n1+n2;

​ select n3 as sum;

​ end

带参存储过程

​ 分为3类4种:

​ in:表示存储过程需要给它带入一个值(默认)

​ out:表示存储过程会返回一个值

​ inout:表示存储过程即可以带入一个值,有可以带出一个值

​ I、in

​ create procedure p_c(in n1 int)

​ begin

​ select * from t_student where sid=n1;

​ end

​ call p_c(1218);

​ II、out

​ create procedure p_d(out n2 int)

​ begin

​ select max(sscore) into n2 from t_student;

​ end

​ call p_d(@t); //@t:定义的全局变量,用于接受结果

​ select @t;

​ III、in–out

​ create procedure p_e(in n1 int, out n2 int)

​ begin

​ select sscore into n2 from t_student where sid=n1;

​ end

​ call p_e(1218, @i);

​ select @i;

​ IV、inout

​ create procedure p_k(inout n1 int)

​ begin

​ select sscore into n1 from t_student where sid=n1;

​ end

​ set @g=1218;

​ call(@g);

​ select @g;

​ 5)、展示存储过程

​ ①、展示存储过程的名字

​ 语法:select name from mysql.proc where db=’数据库名’;

​ select name from mysql.proc where db=’lianxi’;

​ ②、展示存储过程的详细信息

​ 语法:show procedure status where db=’数据库名’;

​ show procedure status where db=’lianxi’;

九、触发器 关键字:trigger

​ 当某个增删改的sql语句执行时,会触发我们之前保存的某条sql语句

​ 触发器只对表使用,不能对视图,临时表使用。

​ 1、展示表上的触发器

​ 语法:show triggers like ‘表名’;

​ show triggers like ‘t_student’;

​ 2、删除触发器

​ 语法:drop trigger 触发器名;

​ drop trigger tr_student_insert_after;

​ 3、创建触发器

​ create trigger 触发器名 after/before insert/update/delete on 被监听表 for

​ each row sql语句(增删改,没有查);

​ ①、监听insert

​ create trigger tr_student_insert_after after insert on t_student for each

​ row update t_dept set dnum=dnum+1 where did=new.did;

​ 添加一个学生

​ insert into t_student(sid, sname, did) values(201911, ‘张三’, 1);

​ ②、监听delete

​ create trigger tr_student_delete_after after delete on t_student for each row

​ update t_dept set dnum=dnum-1 where did=old.did;

​ 删除一个学生

​ delete from t_student where sid=201911;

​ Insert触发器

           包含一张new虚拟表,这个虚拟表里面放的是刚刚插入的数据
  
           如果使用的before,那我们可以更改insert里面的数据

​ Delete触发器

           包含一张old虚拟表,这个虚拟表里面放的是刚刚删除的数据

​ Update触发器

          包含两张表(new、old)。new表里保存的是更新数据以后的这条记录;old表里

​ 保存的是更新数据以前的这条记录

十、E-R图

十一、三大范式

​ 1、属性列不能在分

​ 2、部分依赖 组合关键字

​ 3、传递依赖 两个实体没有分开

JDBC

​ Java DataBase Connection:Java连接数据库,可以通过Java语言连接数据库,从而对数据

库(MySQL/Oracle/SQLServer…)中的数据进行增删改查操作(数据的持久化存储)

​ SQL指令(DML、DDL、存储过程、触发器、程序包、函数)

一、6步

​ (外键)

​ Connection

​ Statement

​ PreparedStatement

​ ResultSet

二、调用存储过程

​ CallableStatement

三、事务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.shiwu.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;

import com.shiwu.utils.DBManager;

public class Test03_事务完整 {

public static void main(String[] args) {

Connection conn = null;
PreparedStatement psta = null;
Savepoint sp = null;
try {
conn = DBManager.getConn();

//把事务默认的自动提交--改为--手动提交
conn.setAutoCommit(false);

//设置事务的隔离级别
conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);

String sql1 = "update person set money=money-1000 where pname='张三'";
String sql2 = "update person set money=money-1000 where pname='李四'";
String sql3 = "pdate person set money=money-1000 where pname='王五'";

psta = conn.prepareStatement(sql1);
psta.executeUpdate();

//定义回滚的位置
sp = conn.setSavepoint();

psta = conn.prepareStatement(sql2);
psta.executeUpdate();

psta = conn.prepareStatement(sql3);
psta.executeUpdate();


System.out.println("执行成功!");
} catch (SQLException e) {
System.out.println("执行失败!");
try {
// conn.rollback(); //回滚,一般回滚到修改(手动)提交方式的地方
conn.rollback(sp);//回滚到定位处
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally {
try {
conn.commit(); //手动的提交事务
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.close(conn, psta, null);
}


}

}

四、连接池

1、dbcp–主打

2、c3p0–了解

3、阿里连接池

前端

​ 1、html标签 (骨骼、肉体)

​ 搭建页面的结构,标记性语言

​ 格式标签

​ 文本标签

​ 超链接标签

​ 图形、图像标签

​ 音频、视频标签

​ 分区标签

​ 2、CSS样式 (衣服、化妆)

​ 对html结构的装饰、描述性语言

​ 选择器

​ 属性、样式

​ 3、JS (动态效果)

​ 编程语言

​ 数据类型、标识符、关键字、条件语句、循环语句、

​ 函数、内置对象、BOM、DOM

​ 4、jQuery框架

​ js框架、基本框架

​ 选择器+效果

​ 5、Bookstrap框架

​ 给页面给样式或动态效果

​ 6、AngularJS/VueJS

web应用的构成

C/S架构

Client-Server:客户端-服务端模式(比如:QQ、英雄联盟)

​ 优点:体验度比较好

​ 缺点:所有用户都必须下载客户端,并且在应用更新时需要更新,消耗大量网络宽带,

​ 对客户端设备有一点要求

B/S架构

Broser-Server:浏览器-服务端模式(比如:京东、淘宝)

​ 优点:对客户端几乎没有要求,在应用更新时不需要客户端做任何事情,只需要在客户

​ 端进行渲染修改,随着css3+H5的发展,基于浏览器的应用越来越接近于原生态

​ 缺点:用户体验相对不稳定,调用本机功能兼容性问题(不同的浏览器,由不同的生产厂

​ 家生产,内核不一样,所以存在兼容性问题)

HTML

​ 格式标签

​ 文本标签

​ 超链接标签

​ 多媒体标签

​ 图形图像标签

​ 表格标签

​ *表单标签

​ *分区标签(div+span)

​ 语义化标签

CSS

​ CSS属性—-背景

​ CSS属性—-边框

​ CSS属性—-文本

​ CSS布局—-盒子模型

​ CSS布局—-定位与浮动

​ CSS过渡与自定义动画

​ 重叠样式表

JS

​ JavaScript:是一门基于HTML的脚本语言(无法独立运行,一般嵌入到html中运行,自从有了

NodeJS之后,js可以基于node环境运行),js是一门基于对象的语言(不是面向对象,没有类的概

念,但是有对象、有构造器),是一门基于事件驱动(可以通过页面中的可交互控件触发js代码)的语

言。

​ 由三大部分组成:

​ 1、ECMAScript核心语法 (js的基本语法)

​ 2、DOM模型 (Document Object Model)

​ 3、BOM模型 (Browse Object Model)

Java:

​ 面向对象 (任何对象都需要创建)

​ 需要先编译,在运行

​ 强类型语言 (任何数据类型在使用时,都需要先声明类型,并且所有数据类型一旦定义则无法

更改)

​ 四类八种数据数据类型 (引用类型)

JS:

​ 基于对象 (有一系列的内置对象,同时也可以创建)

​ 无需编译,可以直接运行

​ 弱类型语言 (所有数据类型都不必事先声明,并且在使用期间能随时动态的改变数据类型)

​ JS的数据类型都可以通过关键字var表示:

​ 数字类型number、字符串类型string、布尔类型boolean、null、undefined、NaN、

​ 对象类型

​ 数字类型里面都是double类型(没有整数、小数区分)

​ 字符串类型里面都是字符串(没有字符类型char)

JS在页面中使用的三种方式:

​ 1、在HTML页面的头部,使用