前言:
SQL(structure query language)—结构化查询语句。
数据库大体可以分为关系型数据库和非关系型数据库
关系型数据库(RDBMS)
基于SQL实现,是指采用了关系模型(库、表、行、列)来组织数据的数据库。
比如:
oracle:甲骨文公司,适合大型项目,适用于做复杂的业务逻辑。
SQL Server:微软公司,适合中大型项目,部署在windows上。
MySQL:甲骨文公司,不适合做复杂的业务逻辑。
MariaDB:基于MySQL的一个开源产品。
非关系型数据库
更多的值NoSQL数据库,不规定基于SQL实现。
比如:
基于键值对(key-value):如memcached、redis。
基于文档型:如mongodb。
基于列族:如hbase。
基于图形:neo4j
一、SQL语言
1、DDL 数据定义语言(库和表的基本操作)
create、drop、alter、show
2、DML 数据操作语言(数据的基本操作)
insert、delete、update、select
3、DCL数据控制语言(权限管理和事务)
grant、revoke
4、DQL数据库查询语言(数据的查找)
select、from、where
service mysqld start 开启
service mysqld stop 关闭
service mysqld restart 重启服务器
mysqld -u root -p xxx 连接服务器
1、DDL
显示当前数据库
show databases;//每一条数据库操作语句都应该以分号 ; 结尾
创建数据库
//--创建名为db_test的数据库。
create database db_test;
//如果系统没有db_test的数据库,则创建一个名叫db_test的数据库,否则不创建。
create database if not exists db_test;
使用数据库
use db_test;
删除数据库
drop database [if exists] db_test;
2、DML
表的操作
查看表结构
desc tablename;
创建表
create table stu_test(
id int,
name varchar(20),
age int,
amout decimal(13,2),
birthday timestamp,
resume text
);
删除表
drop table [if exists] stu_test;
3、数据库的约束
NULL约束
--创建表时可以指定某列不为空
create table student(
id int not null,
sn int,
name varchar(20)
);
UNIQUE:唯一约束
--创建表时指定sn列为唯一的、不重复的
create table student(
id int not null,
sn int unique,
name varchar(20)
);
DEFAULT:默认值约束
--指定插入数据时,name列为空,默认值为无名氏
create table student(
id int not null,
sn int unique,
name varchar(20) default '无名氏'
);
PRIMARY KEY:主键约束
--指定id列为主键
create table student(
id int primary key,
sn int unique,
name varchar(20) default '无名氏'
);
FOREIGN KEY:外键约束
--外键用于关联其他表的主键或唯一键
create table student(
id int primary key,
sn int unique,
name varchar(20) default '无名氏',
class_id int,
foreign key (class_id) references classes(id)
);
CHECK约束
--mysql使用时不报错,但忽略该约束
create table student(
id int primary key,
name varchar(20),
sex varchar(1),
check (sex = '男' or sex = '女')
);
4、DML
4.1、添加数据
insert into stu values("001","zhangsan","man","19");
插入部分元素
insert into stu(id, name, ssex,)values("001","zhangsan","man");
批量插入
//小批量
insert into stu values("001","zhangsan","man","19"),
("002","lisi","man","17"),
("003","wangwu","man","22"),
...........................
("006","xxx","man","xxxx");
//大批量 load
4.2、删除数据
delete from stu where id = "002"; //where 过滤条件
delete from stu ://删除 stu中的所有数据
4.3修改数据
update stu set ssex ="woman" where id ="006";
update stu set ssex ="woman";
select *from stu;查看
4.4、查询数据:
关键字 | 返回值 |
---|---|
count | 返回查询到数据的数量 |
sum | 返回查询到数据的总量 |
avg | 返回查询到数据的平均值 |
max | 返回查询到数据的最大值 |
min | 返回查询到数据的最小值 |
聚合查询
--count
--统计班级共有多少同学
select count(*) from student;
--统计班级的id有多少个,为NULL不会计入
select count(id) from studentl;
--sum
--统计学号总和
select sum(sn) from student;
--学号小于1002的总和
select sum(sn) from student where sn < 1002;
--avg
--统计平均学号
select avg(sn) from student;
--统计id和学号平均和
select avg(sn + id) from student;
--max
--返回最高的学号
select max(sn) from student;
--min
--返回最低的学号
select min(sn) from student;
分组查询
select中使用group by子句可以对指定列进行分组查询
--测试案例
create table emp(
id int primary key auto_increment,
name varchar(20) not null,
role varchar(20) not null,
salary numeric(11,2)
);
insert into emp(name, role, salary) values
('张三','门卫&#