1.MySQL概述,安装
概述:
关系型数据库管理系统
关系型数据库存放的既有实体,又有实体之间的关系
数据库服务器--->数据库(应用)--->表(类)--->多条记录(对象)
2.数据库的curd(增删改查)
a)增:create database 数据库名;
例如: create database mydb1;
b)删: 删除某个数据库
drop database 数据库名;
c)改: alter database 数据库名 character set 字符集 collate 校对规则;
d)查: 查询有哪些数据库
show database;
查询当前创建的某个数据库定义的信息
show create database 数据库名;
e)切换数据库:
use 数据库名;
f)查询当前使用的数据库:
select database();
3.表的crud(增删改查)
mysql中的数据类型:
java中的类型 MySQL中的类型
byte/short/int/long tinyint/smallint/int/bigint
float float
double double
boolean bit
char/String char和varchar
date date/time/datetime/timestamp
Flie blob/text
注意:
1.char和varchar的区别
char(8)代表是固定长度的字符或字符串,存入数据的时候如果空余的会用空格补全,如果超过长度会报错
varchar(8)代表的是可变长度的字符串,存入多少是多少
2.datetime和timestamp的区别
datetime就是既有日期又有时间的日期类型,如果没有向这个字段中存值,数据库使用null存入到数据库中
timestamp也是既有日期又有时间的日期类型,如果没有向这个字段中存值,数据库使用当前的系统时间存入到数据库中。
a)增: 语法:
create table 表名(字段名 字段类型(长度) 约束, 字段名 字段类型(长度) 约束)
举例:
create table use(
id int primary key auto_increment,
name varchar(10) not null,
age int not null,
email varchar(30) unique not null,
phone varchar(12) not null,
birthday date
)
b)删: drop table 表名;
c)改: 都是以 alter table 表名 开头的
1.添加列:
alert table 表名 add 列名 字段类型(长度) 约束;
alert table student add image varchar(100);
2.修改表的类型,长度约束
alert table 表名 modify 列名 类型 约束;
alert table student modify image varchar(150);
3.删除列
alert table 表名 drop 列名;
alert table student drop age;
4.修改列名称
alert table 表名 change 旧名 新名 类型 约束;
alert table student image pic carchar(150);
5.修改表名称
rename table 表名 to 新表名;
rename table student to use;
6.修改表的字符集
alter table 表名 character set 字符集;
d)查: 查看所有的表:show tables;
show tables;
查看某个表结构:desc 表名;
desc student;
4.表记录的crud(增删改查)*****重点
a)增: 表中插入某些列:
inset into 表名(列名1,列名2,列名3...) values(值1,值2,值3...);
表中插入所有列:
inset into 表名 values (值1,值2,值3...);
一次插入多行数据:
inset into 表名 values (值1,值2,值3...),(值1,值2,值3...)...;
例如:
插入所有列
inset into user values(null,'aaa','123',23,'1991-01-16');
注意:
1.值的类型与数据库中表列的的类型一致.
2.值的顺序要与列的顺序保持一致.
3.插入的值的长度不能超过列的规定长度.
4.对于字符串和时间的值,要使用引号括起来.
b)删:删除语法:
delete from 表名 where条件;
注意:
1.不带条件是删除表中所有的记录.
2.删除是删除表中的一行.
delete from user;
删除所有记录,属于DML语句,一条记录一条记录删除。事务可以作用在DML语句上的
truncate table user;
删除所有记录,属于DDL语句,将表删除,然后重新创建一个结构一样的表。事务不能控制DDL的
c)改:修改语法:
update 表名 set 列名1=值1,列名2=值2....where条件;
如果没有带条件,就是修改找这个表的所有记录
例如:
update user set password = 'xyz' where username = 'bbb';
d)查********重点:
select * from 表名 --->查询所有信息
select 列1,列2...from 表名--->查询某几列的信息
select distinct 列名 from 表名--->去重复查询
select 列名 as 别名 from 表名 --->别名查询
条件查询:
where 跟上条件
> >= < <= <> != =
例如:
select * from exam where math> 80;
模糊查询 like
like '李_' 名字中必须是两个字 李姓
like '李%' 李字后面有任意个字符
like '%四%' 包含四就可以
例如:
select * from exam where name like '李%'
in:在给定的范围内查询
例如:
select * from exam where math in(83,91,98);
between..and..:取介于两个值之间的数据
例如:
select * from exam where math between 80 and 100;
and , or , not可以连接多个条件
例如:
select * from exam where math>80 and name like '李%';
select * from exam where math not in(83,91,98);
排序查询:
select * from exam where条件 order by asc/desc;
例如:
select * from exam order by chinese;
select * from exam order by chinese desc,english asc;
统计查询:
sum(字段名); 查询某一列数据总和
select sum(english) from exam;
select sum(english),sum(math) from exam;
select sum(english) from exam where name like '李%';
max(字段名),计算最大值
select max(math) from exam;
min(字段名),计算最小值
select min(chinese) from exam;
count(字段名),统计记录条数
select count(*) from exam;
select count(*) from exam where name like '李%';
avg(字段名),计算平均值
select avg(chinese) from exam;
聚合函数(sum(),min(),max(),count(),avg())会自动滤空
函数 ifnull(列名,替代值)可以转换Null值!
分组查询:
group by 字段名;
例如:
select product,count(*) from orderitem group by product;
select product,sum(price) from orderitem group by product;
where的子句后面不能跟着聚合函数。如果现在使用带有聚合函数的条件过滤(分组后条件过滤)需要使用一个关键字having
select product,sum(price) from orderitem group by product having sum(price) > 5000;
注意:
1.用了group by 后, where和having的区别?
i. where一般用于分组前过滤,having用于分组后过滤.
ii. having后面可以使用聚合函数,where后面不能用聚合函数.
总结:
完整的SQL格式:
select 字段名,... from 表名 where 条件 group by 字段名 hanving 条件 order by 字段名 asc/desc
查询中用到的关键词主要包含六个,并且他们的书写顺序为:
select--from--where--group by--having--order by
其中select和from是必须的,其他关键词是可选的,这六个关键词的执行顺序与书写顺序并不是一样的,执行顺序为:
from--where--group by--having--select--order by
语句中有group by关键字时,有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面
概述:
关系型数据库管理系统
关系型数据库存放的既有实体,又有实体之间的关系
数据库服务器--->数据库(应用)--->表(类)--->多条记录(对象)
2.数据库的curd(增删改查)
a)增:create database 数据库名;
例如: create database mydb1;
b)删: 删除某个数据库
drop database 数据库名;
c)改: alter database 数据库名 character set 字符集 collate 校对规则;
d)查: 查询有哪些数据库
show database;
查询当前创建的某个数据库定义的信息
show create database 数据库名;
e)切换数据库:
use 数据库名;
f)查询当前使用的数据库:
select database();
3.表的crud(增删改查)
mysql中的数据类型:
java中的类型 MySQL中的类型
byte/short/int/long tinyint/smallint/int/bigint
float float
double double
boolean bit
char/String char和varchar
date date/time/datetime/timestamp
Flie blob/text
注意:
1.char和varchar的区别
char(8)代表是固定长度的字符或字符串,存入数据的时候如果空余的会用空格补全,如果超过长度会报错
varchar(8)代表的是可变长度的字符串,存入多少是多少
2.datetime和timestamp的区别
datetime就是既有日期又有时间的日期类型,如果没有向这个字段中存值,数据库使用null存入到数据库中
timestamp也是既有日期又有时间的日期类型,如果没有向这个字段中存值,数据库使用当前的系统时间存入到数据库中。
a)增: 语法:
create table 表名(字段名 字段类型(长度) 约束, 字段名 字段类型(长度) 约束)
举例:
create table use(
id int primary key auto_increment,
name varchar(10) not null,
age int not null,
email varchar(30) unique not null,
phone varchar(12) not null,
birthday date
)
b)删: drop table 表名;
c)改: 都是以 alter table 表名 开头的
1.添加列:
alert table 表名 add 列名 字段类型(长度) 约束;
alert table student add image varchar(100);
2.修改表的类型,长度约束
alert table 表名 modify 列名 类型 约束;
alert table student modify image varchar(150);
3.删除列
alert table 表名 drop 列名;
alert table student drop age;
4.修改列名称
alert table 表名 change 旧名 新名 类型 约束;
alert table student image pic carchar(150);
5.修改表名称
rename table 表名 to 新表名;
rename table student to use;
6.修改表的字符集
alter table 表名 character set 字符集;
d)查: 查看所有的表:show tables;
show tables;
查看某个表结构:desc 表名;
desc student;
4.表记录的crud(增删改查)*****重点
a)增: 表中插入某些列:
inset into 表名(列名1,列名2,列名3...) values(值1,值2,值3...);
表中插入所有列:
inset into 表名 values (值1,值2,值3...);
一次插入多行数据:
inset into 表名 values (值1,值2,值3...),(值1,值2,值3...)...;
例如:
插入所有列
inset into user values(null,'aaa','123',23,'1991-01-16');
注意:
1.值的类型与数据库中表列的的类型一致.
2.值的顺序要与列的顺序保持一致.
3.插入的值的长度不能超过列的规定长度.
4.对于字符串和时间的值,要使用引号括起来.
b)删:删除语法:
delete from 表名 where条件;
注意:
1.不带条件是删除表中所有的记录.
2.删除是删除表中的一行.
delete from user;
删除所有记录,属于DML语句,一条记录一条记录删除。事务可以作用在DML语句上的
truncate table user;
删除所有记录,属于DDL语句,将表删除,然后重新创建一个结构一样的表。事务不能控制DDL的
c)改:修改语法:
update 表名 set 列名1=值1,列名2=值2....where条件;
如果没有带条件,就是修改找这个表的所有记录
例如:
update user set password = 'xyz' where username = 'bbb';
d)查********重点:
select * from 表名 --->查询所有信息
select 列1,列2...from 表名--->查询某几列的信息
select distinct 列名 from 表名--->去重复查询
select 列名 as 别名 from 表名 --->别名查询
条件查询:
where 跟上条件
> >= < <= <> != =
例如:
select * from exam where math> 80;
模糊查询 like
like '李_' 名字中必须是两个字 李姓
like '李%' 李字后面有任意个字符
like '%四%' 包含四就可以
例如:
select * from exam where name like '李%'
in:在给定的范围内查询
例如:
select * from exam where math in(83,91,98);
between..and..:取介于两个值之间的数据
例如:
select * from exam where math between 80 and 100;
and , or , not可以连接多个条件
例如:
select * from exam where math>80 and name like '李%';
select * from exam where math not in(83,91,98);
排序查询:
select * from exam where条件 order by asc/desc;
例如:
select * from exam order by chinese;
select * from exam order by chinese desc,english asc;
统计查询:
sum(字段名); 查询某一列数据总和
select sum(english) from exam;
select sum(english),sum(math) from exam;
select sum(english) from exam where name like '李%';
max(字段名),计算最大值
select max(math) from exam;
min(字段名),计算最小值
select min(chinese) from exam;
count(字段名),统计记录条数
select count(*) from exam;
select count(*) from exam where name like '李%';
avg(字段名),计算平均值
select avg(chinese) from exam;
聚合函数(sum(),min(),max(),count(),avg())会自动滤空
函数 ifnull(列名,替代值)可以转换Null值!
分组查询:
group by 字段名;
例如:
select product,count(*) from orderitem group by product;
select product,sum(price) from orderitem group by product;
where的子句后面不能跟着聚合函数。如果现在使用带有聚合函数的条件过滤(分组后条件过滤)需要使用一个关键字having
select product,sum(price) from orderitem group by product having sum(price) > 5000;
注意:
1.用了group by 后, where和having的区别?
i. where一般用于分组前过滤,having用于分组后过滤.
ii. having后面可以使用聚合函数,where后面不能用聚合函数.
总结:
完整的SQL格式:
select 字段名,... from 表名 where 条件 group by 字段名 hanving 条件 order by 字段名 asc/desc
查询中用到的关键词主要包含六个,并且他们的书写顺序为:
select--from--where--group by--having--order by
其中select和from是必须的,其他关键词是可选的,这六个关键词的执行顺序与书写顺序并不是一样的,执行顺序为:
from--where--group by--having--select--order by
语句中有group by关键字时,有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面