1.数据库时代了
多练,要练
2.SQL
structured query language(结构化查询语言)
3.数据库简介:
SQL Server
Oracle
MySQL
DB2 银行用的很多
SyBase
4.MySQL配置
Develop
连接数
端口 3306
Manual UTF-8
远程
MySQL解压版:3306
my-medium.ini 把安装目录改一下
startup.bat
stop.bat
管理--服务--MySQL
mysql -u 用户名 -p
密码
show databases; //看安装成功没有
5.数据在数据库中的存储方式
行 row
列 column
一个数据库服务器可以管理多个数据库
一般一个应用程序对应一个数据库
一个数据库可以管理多个表,每个表对应一个实体
create database mydb1;
指定编码 排序
常规字符集和校对
中文
显示数据库
show databases;
显示数据库创建语句
show create batabase mydb2;
数据库删除
drop database mydb1;
修改数据库
alter database mydb1 character set utf8;
alter database mydb1 collate utf8_general_ci;
quit eixt
数据库建表
建表的时候,看有哪些要进数据库的实体
对象进数据库 对象的属性-->列
建表的时候指定字符集 和 校对规则
MySQL中的数据类型
数值类型
文本类型 char固定长度字符串 varchar可变长度字符串
blob二进制数据 text大文本
对象序列化,存储到文件,数据库 ---> blob
时间日期 DATE 年月日
DATETIME 年月日时分秒
TimeStamp 时间戳 代表某一刻的时间(用于自动记录insert,update操作时间)
VARCHAR BLOB TEXT 变长 每个类型的存储需求取决于列值的实际长度
先用数据库,在里面建表
use 数据库名
创建一个员工表:
employee:
create table employee
(
id int,
name varchar(30),
sex varchar(10),
birthday date,
entry_date date,
job varchar(30),
salary float,
resume text
);
修改表
alter table
追加,修改或删除列
add modify drop
在employee增加image
alter table employee add image blob;
show create table employee;
修改job列,使其长度变成60
alter table employee modify job varchar(60);
删除sex列
alter table employee drop sex;
表名改为user
Rename table 表名 to 新表名
rename table employee to user;
修改表的字符集
alter table user character set utf8 collate utf8_general_ci;
MySQL中的权限 用root用户登陆才能建数据库 不用能建表不能建数据库
数据库CRUD
Insert
Update
Delete
Select
inser语句:
插入的数据应与字段的数据类型相同
位置对应
字符和日期 -->单引号
insert into employee(id,name,birthday,entry_date,job,salary,resume)
values(1,'aaa','1956-10-05','1987-7-6','aa',100,'sdfjsldfjoisdjfljsdf');
insert into employee(id,name,birthday,entry_date,job,salary,resume)
values(2,'bbb','1956-10-05','1987-7-6','aa',100,'sdfjsldfjoisdjfljsdf');
insert into employee(id,name,birthday,entry_date,job,salary,resume)
values(2,'bbb','1956-10-05','1987-7-6','aa',100,'sdfjsldfjoisdjfljsdf');
插入空值 null 或 不指定
Update语句:
where 指定更新哪些行,如果没有更新所有
update employee set sex='男' where id=1;
alter table employee character set utf8 collate utf8_general_ci;
把表中名为'aaa'的用户薪水改为20000
update employee set salary=20000 where name='aaa';
update employee set salary=salary+1000 where id=2;
Delete语句:
删除行,如果不指定where 删除所有行
删除列,表 drop
删除表 drop table user;
注意:insert update 参照完整性
删除表中id=2的记录
delete from employee where id=2;
删除表中的所有记录
delete from employee;
使用truncate删除表中的记录
delete是一行一行的删
truncate是先删表,再重建表的结构
truncate table employee;
Select语句:
执行sql脚本
source XXX.sql
select distinct from student; 过滤重复数据
在所有学生分数上加10分
select chinese+10 from student; 改的是显示的值,原来的值没改变
别名: as '' 或 '' 省略as也可以
select name '姓名' (chinese+math+english) as '总分' from student;
> < = >= <= <>
<>不等于
select * from student where (chinese+math+english)>200;
between and
查询英语分数在80-90之间的同学
select * from student where english between 80 and 90;
in
查询英语分数在89,90,91的同学
select * from student where english in(89,90,91);
like 'pattern'
% 0或多个
_ 一个字符
查询所有姓李德学生成绩
select * from student where name like '李%';
查询所有成绩以什么开头
select * from student where math like '7_';
and
select * from student where math>80 and chinese>80;
or
order by
位于select句尾
desc 降序
合计函数
count
select count(math) as '总分' from student where (chinese+math+english)>=250;
sum
sum仅对数值起作用
select sum(math) as '数学总成绩' from student;
select sum(math)/count(math) as '数学平均分' from student;
select sum(math)/count(math) as '数学',sum(chinese)/count(chinese) as '语文',sum(english)/count(english) as '英语' from student;
select sum(chinese+math+english) from student;
avg
select avg(math) as '数学平均分' from student;
max/min
求班级的最高分和最低分
select max(chinese+math+english) as '最高分', min(chinese+math+english) as '最低分' from student;
合计函数不能出现在where中
数学分大于数学的平均分
select * from student where math > (select avg(math) from student as ' 平均分');
group by对列进行分组
使用having子句过滤
having中可以使用合计函数,而where group by不能
select product,sum(price) from orders group by product having sum(price)>100;
group by将重复的搞到一起
select id,product,sum(price) from orders group by product;
select id,product,sum(price) from orders group by product having sum(price)>100;
时间日期相关函数
select current_date();
select current_time();
select current_timestamp();
now() 当前时间
显示表结构
desc 表明;
show create table 表名;
字符串相关函数
数学相关函数
floor向下取整
定义表的约束
身份证 唯一标示
表中有一个列 唯一标示每一行
primary key
主键 自增
primary key auto_increment
定义非空约束
not null
定义唯一约束
unique
主键就是唯一的
删除约束 alter table tablename drop index 列名
alter table语法
表中某一列增加唯一约束
UUID 主键