服务器做jsp网站教程视频播放,做网站需要走公司吗,所有外包网站,苍南做网站哪里找一、数据库基本概念
数据库#xff1a;DateBase#xff0c;简称#xff1a;DB
数据库特点#xff1a;
持久化存储数据 - 文件系统方便存储和管理数据使用了统一方式管理数据库 - SQL
常见的数据库软件#xff1a;
OracleMySQLMicrosoft SQL ServerDB2SQLite …一、数据库基本概念
数据库DateBase简称DB
数据库特点
持久化存储数据 - 文件系统方便存储和管理数据使用了统一方式管理数据库 - SQL
常见的数据库软件
OracleMySQLMicrosoft SQL ServerDB2SQLite
二、MySQL
默认端口号3306
配置环境变量(Mac)$PATH:/usr/local/mysql/bin
本地登录
mysql -uroot -p密码指定IP登陆
mysql -hIP地址 -uroot -p密码登出
exit
quitMySQL目录结构
安装目录 bin*.exemy.iniMySQL的配置文件 数据目录数据库(文件夹) - 表(文件) - 数据
三、SQL语句
1、基本概念
SQL结构化查询语言 - 定义了操作所有关系型数据库的规则
方言每种数据库操作的方式存在差异
2、SQL语法
SQL语句能单行或多行书写以分号结尾
不区分大小写关键字建议大写
注释
-- 注释内容(必须加空格)#注释内容MySQL特有/*注释内容*/
1DDL操作数据库和表
1. 操作数据库CRUD
Create创建
-- 创建数据库
create database 数据库名称;
-- 若不存在创建数据库
create database if not exists 数据库名称;
-- 创建数据库并指定gbk编码
create database 数据库名称 character set gbk;Retrieve查询
-- 1.查询所有数据库的名称
show databases;
-- 2.查询某个数据库的创建语句(字符集)
show create database 数据库名称;Update修改
-- 修改数据库的字符集
alter database 数据库名称 character set 字符集名称;
alter database db character set utf8;Delete删除
-- 删除数据库
drop database 数据库名称;
-- 判断存在再删除数据库
drop database if exists 数据库名称;2. 使用数据库
-- 1.查询当前正在使用的数据库名称;
select database();
-- 2.使用数据库
use 数据库名称;3. 操作表
Create创建
-- 创建表 最后一个不写逗号
create table student(id int,name varchar(32),age int,score double(4,1),birthday date,insert_time timestamp
);Retrieve查询
-- 1.查询数据库下表的名称
show 数据库名称;
-- 2.查询表结构
desc 表名;Update修改
-- 1.修改表名
alter table 表名 rename to 新的表名;
-- 2.修改表的字符集
alter table 表名 character set 字符集名;
-- 3.添加一列
alter table 表名 add 列名 数据类型;
-- 4.修改列名称 类型
alter table 表名 change 列名 新列名 新数据类型;
-- 5.删除列
alter table 表名 drop 列名;Delete删除
drop table 表名;
drop table if exists 表名4. SQL的数据类型
int整数类型double(最大位数, 小数点后位数)小数类型date日期类型yyyy-MM-dddatetime 日期时间类型yyyy-MM-dd HH:mm:sstimestamp时间戳类型yyyy-MM-dd HH:mm:ss不给赋值会自动用系统时间varchar(最大字符数)字符串
2DML数据的增删改
1. 添加数据
insert into 表名(列名1,列名2,...列名n) values (值1,值2,...值n);列名要和值一一对应 可以不写列名进行完全添加
insert into 表名 value (值1,值2,...值n);除了数字类型均需要引号
2. 删除数据
delete from 表名 [where 条件];删除所有元素
delete from 表名; -- 删除表的所有数据
truncate table 表名; -- 删除表并创建新表3. 修改数据
update 表名 set 列名1 值1,列名2 值2,...[where 条件];不加条件会改所有行的数据
3DQL数据的查询
1. 基础查询
多个字段的查询
select 字段名1,字段名2... from 表名;
select * from 表名;去除重复distinct
select distinct 字段名 from 表名;计算列
一般使用四则运算计算列的数值ifnull(表达式1, 表达式2)表达式1可能出现null的列的字段名表达式2null的替换值
select name,math,chinese,math chinese from stu;
select name,math,chinese,ifnull(math chinese) from stu;起别名as
select name as 姓名 from stu;
select name 姓名 from stu; -- 可以省略2. 条件查询
where子句后跟条件
运算符
####### !
select * from stu where age 20; -- 大于
select * from stu where age 20; -- 等于
select * from stu where age ! 20; -- 不等于
select * from stu where age 20; -- 不等于####### (and) || (or) ! (not)
select * from stu where age 20 age 30;
select * from stu where age 20 and age 30;####### between ... and ...
select * from stu where age between 20 and 30;####### in(集合)
select * from stu where age 22 || age 18 or age 19;
select * from stu where age in (22,18,19);####### is null
null值不能使用判断
select * from stu where math is null;
select * from stu where math is not null;模糊查询like
占位符
_单个任意字符%多个任意字符
select * from stu where name like 王%;
select * from stu where name like _铭%;3. 排序查询
select * from stu order by 排序字段1 排序方式1,排序字段2 排序方式2 ...;排序方式
ASC升序默认的DESC降序
4. 聚合函数
将一列作为整体进行纵向计算
计算个数count
选非空的列主键count(*)
select count(name) from student;计算最大值max
select max(math) from student;计算最小值min
select min(math) from student;计算求和sum
select sum(math) from student;计算平均值avg
select avg(math) from student;ifnull
聚合函数会排除null值
选不包含null的列ifnull函数
select count(ifnull(name,0)) from student;计算结果特点单行单列
5. 分组查询
group by 分组字段
分组之后查询的字段分组字段、聚合函数 where和having的区别 限定时间 where在分组前限定不满足条件不参与分组having在分组后限定不满足条件不被查询 聚合函数 where后面能加聚合函数having后面不能加聚合函数 select sex,avg(math),count(id) from stu group by sex;
select sex,avg(math),count(id) from stu where math 70 group by sex;
select sex,avg(math),count(id) from stu where math 70 group by sex having count(id) 2;
select sex,avg(math),count(id) 人数 from stu where math 70 group by sex having 人数 2;6. 分页查询
语法 limit 开始的索引, 每页的条数
select * from stu limit 0,3;开始的索引 (当前的页码 - 1) * 每页显示的页数
limit是MySQL的方言
3、约束
对表的数据进行限定保证数据的正确性、有效性和完整性
1非空约束
非空约束not null值不为null
1. 创建表时添加约束
create table stu(id int,name varchar(20) not null
);2. 删除表的约束
alter table stu modify name vachar(20);3. 后期添加约束
alter table stu modify name vachar(20) not null;2唯一约束
唯一约束unique值不重复能保存null值但是只能保存一个
1. 创建表时添加约束
create table stu(id int,phone varchar(20) unique
);2. 删除表的约束
alter table stu drop index phone;3. 后期添加约束
只有在没有重复数据才能添加成功
alter table stu modify phone vachar(20) unique;3主键约束
主键约束primary key非空且唯一
一张表只能有一个主键是表中记录的唯一标识
1. 创建表时添加约束
create table stu(id int primary key,phone varchar(20)
);2. 删除表的约束
alter table stu drop primary key;3. 后期添加约束
只有在没有重复数据才能添加成功
alter table stu modify id int primary key;4自动增长auto_increment
1. 创建表时添加约束
create table stu(id int primary key auto_increment,phone varchar(20)
);2. 添加数据
insert into stu value(null,wmh);3. 删除自动增长(不影响主键)
alter table stu modify id int;5外键约束
外键约束forign key让表于表产生关系从而保证数据的正确性
1. 在创建表时可以添加外键
create table 表名(...外键列constraint 外键名称 foreign key (外键列名称) references 主表名称(主表列名称)
);2. 删除外键
alter table 表名 drop foreign key 外键名称;3. 创建表之后添加外键
alter table 表名 add constraint 外键名称 foreign key (外键字段名称) references 主表名称(主表列名称);4. 级联操作
添加级联操作
alter table 表名 add constraint 外键名称 foreign key (外键字段名称) on update cascade on delete cascade;分类
级联更新on update cascade
级联删除on delete cascade