数据库
-
简称
DB(database),与数据库管理系统配合使用(DBMS)配合使用,是长期存储在计算机内有组织可共享的结构化数据集合 -
设计目的:消除冗余数据,保证数据一致性,提供标准化接口,支持多用户,多应用,高效的访问
-
作用:
- 数据存储:将数据实现结构化持久性的存储,避免数据混乱
- 数据管理:通过
DBMS实现数据的增删改查 - 数据共享:多用户、多程序可同时访问一个数据库
- 数据分析:提供高效的查询能力
-
数据库的核心是数据库管理系统(DBMS),所有的数据操作都通过DBMS
- 数据:数据库对象(库,表,索引),数据库结构(字段类型、长度、约束)
- 操作:增删改查
- 数据保护:设置约束规则,防止无效数据储存,提供备份及恢复
- 权限:DCL管理用户权限
SQL
-
结构化查询语言,数据库标准化语言
-
DDL: 数据定义语言,定义、修改、删除数据库对象CREATE DROP ALTER
-
DML:数据操作语言,对表中的数据进行增删改INSERT UPDATE DELETE
-
DQL:数据查询语言,在数据库中查询数据SELECT
-
DCL:数据控制语言,管理数据库权限 -
数据库采用"库 - 表- 字段 - 记录" 的分层结构管理数据,层层向下包容
-
库
(database)- 不同场景的数据集合,数据库的顶层容器
-
表
(table)- 库中的下层容器,表是存储数据的 集合,归类相同类型的业务数据
-
字段
- 表中的列称为字段,用于定义数据的类型和属性
-
记录
- 表中的行称为数据
命令
- 所有命令需要使用
;结束 [if not exists]如果不存在
数据库
-
创建数据库
create database [if not exists] dbname;- 名称必须合法不能重名,名字长度不超过64个字符,字母数字下划线
$,不能数字开头
-
删除数据库
drop database dbname;
-
显示数据库
show database
-
切换数据库
use dbname;
表
-
查看表
show tables;
-
创建表
create table tbname(字段名1 类型 [约束] , 字段名2 类型 [约束] ***);
-
数据类型:
- 数值型:
tinyintSAMLLINTMEDIUMINTINTBIGINTFLOATDOUBLE - 字符串型:
charvarchar(不定长)单引号 - 日期:
datetimedatetime
- 数值型:
-
约束条件
- 主键
primary key:用于唯一标识记录,区别其他的数据 - 非空约束
not null:当前字段不允许为空 - 唯一约束
unique key:当前字段中仅出现一个相同 - 自增约束:在基础之上自增(修饰整型字段)
- 外键约束
foreign key:设置其他表的外键 - 默认约束:设置默认值
- 主键
sql
create table if not exists person (id int,name varchar(20),age int,address varchar(20),charset = utf8);
create table if not exists person //navicat
(
id int pimary key,
name varchar(20) not null,
age int default '18',
address varchar(20)
);
- 查看表结构:
descibe name;show create table name;describe name; - 删除表:
drop table name; - 修改表名:
alter table oldname rename newname;
字段
- 添加字段:
alter table tbname add 字段 类型 [约束条件]; - 在指定指定位置添加:
alter table tbname add 新字段 类型 [约束条件] after 字段; - 修改字段:
alter table tbname change 旧字段 新字段 字段类型; - 修改类型:
alter table modify 字段 新字段类型; - 将字段移动指定位置:
alter table tbname modify 1字段名 类型 after 2字段名; - 删除字段:
alter table tbname drop 字段名;
c
alter table tbname add id int primary key after name;
alter table tbname change age number int; //把age改number
alter table tbname modify name valchar; //
alter table tbname drop name;
-
增加约束条件
- 增加主键:
alter table tbname add primary key(字段); - 增加非空:
alter table tbname modify 字段 类型 not null; - 设置默认:
alter table tbname alter 字段 set default 'val'; - 设置外键:
alter table tbname add constraint 外键名 foreign key(字段); - 设置自增约束:
alter table tbname add column 新字段 类型 auto_increment primary key;
- 增加主键:
-
取消约束
- 取消主键:
alter table tbname drop primary key; - 取消唯一约束:
alter table tbname drop index 字段名; - 取消默认约束:
alter table tbname drop default; - 取消非空:
alter table tbname modify 字段 类型
- 取消主键:
-
修改字符集
- 修改所有字符集:
alter table tbname convert to character set utf8;
- 修改所有字符集:
数据操作
-
增加:
- 增加一条数据:非空和主键数据不可省略,不添加字段选项时,后面的val按顺序匹配,缺少位置的字段需要指定val插入的位置
insert into tbname [字段,字段....] values(val1,val2...);- 增加多条数据:
insert into tbname values (val1,vla2..),(val1,val2),...
-
删除数据:
delect from tbname where id == 5;delect * from tbname;
-
修改更新数据:
update tbname set 字段1 = val,字段2 = val where id = 5;
查找
-
查询所有:
select * from tbname; -
查询指定字段:
select id··· from tbname; -
去重查询:
select distinct age··· from tbname; -
四则运算:
select name pid + uid from tbnameselect name pid + uid as id from tbname; -
设置显示格式:显示时会在id和name之间添加字符串
select concat(id,"-->",name) as man from tbname; -
限制输出个数:
select name from tbname limit 起始,个数; -
逻辑判断:
select age from tbname where age >=0 or age <=100;select age from tbname where age <0 || age >100;select age from tbname where age (not)between 0 and 100;闭区间
-
满足集合查找:
select name from tbname where id (not)in(1,2,3);select id from tbname where class like '二年级_班';select name from tbname where class like '二年级%
-
排序查询:
select name from tbname order by id asc;asc 升序 desc 降序 -
分组查询:
select id from tbname group by id ;select name id from tbname group by name id ; -
函数查询
- count函数:统计行数
select count(id) from tbname; - sum函数、avg函数、max函数、min函数
- count函数:统计行数
-
多表查询
- 内连接:
select id from tbname1 join tbname2 on tbname1.id = tbname2.tid;,- 两张表的每一个发生比较,成立则组成一行
- 外连接:以某一张表为基础,当表中的数据不满足条件时,另一张表显示NULL
select id from tbname1 left join tbname2 on tbname1.id = tbname2.tid;select id from tbname1 right join tbname2 on tbname1.id = tbname2.tid;- 笛卡尔积:每一个都匹配另一个表的数据
c
#include <mysql/mysql.h>
MYSQL *sq = mysql_init(NULL);
if(sq == NULL)
{
perror("");
}
//登陆数据库
sq = mysql_real_connect(MYSQL* mysql,"127.0.0.1","root","1",NULL,0,NULL,0);
//1.mysql 连接对象
//2.主机名 IP地址
//3.用户名 root
//4.密码 1
//5.数据库名称 不指定则是NULL
//6.端口 0
//7.套接字 NULL
//8.0
if(sq ==NULL)
{
perror();
}
//创建数据库
int res = my_query(sq,"create database sys_stu;");//创建sys_stu 的数据库
if(res != 0)
{
perror;
}
//切换数据库
int res = my_query(sq,"use sys_stu;");//创建sys_stu 的数据库
if(res != 0)
{
perror;
mysql_close(sq); //关闭数据库
}
//创建表
int res = my_query(sq,"create table if not exists tbname (id int,name varchar(20),age int,address varchar(20),charset = utf8);");
if(res != 0)
{
perror;
mysql_close(sq); //关闭数据库
}
//插入数据
int res = my_query(sq,"insert into tbname [id,name,age] values(1,'lu',18);");
if(res != 0)
{
perror;
mysql_close(sq); //关闭数据库
}
//查询
int res = my_query(sq,"select * from tbname;");
if(res != 0)
{
perror;
mysql_close(sq); //关闭数据库
}
//获取查询结果
MYSQL_RES* dat吗a = mysql_store_result(sq);
if(data != 0)
{
perror;
mysql_close(sq); //关闭数据库
}
//获取字段数量
unsigned int fieldnamenum = mysql_fetch_fields(res);
//打印字段名称
for(int i = 0;i < fieldnamenum ;i++)
{
printf("%s",fieldnamenum[i],name);
}
//释放结果集
mysql_free_result(data);
mysql_close(sq);
gcc mysql.c -lmysqlclient
底层实现
- SQL 底层实现的核心是 "翻译 + 优化 + 执行":先将 SQL 解析为结构化的语法树,再通过优化器选择最优执行策略,最后由执行引擎调用存储引擎完成实际数据操作。整个过程的效率依赖于优化器的智能决策和存储引擎的性能。
- 解析:将语句拆分验证关键字是否错误,然后根据 SQL 语法规则, 组合成抽象语法树
- 优化:优化器根据数据的特性,选择成本最低的方式进行选择
- 执行:按计划步骤执行,通过存储引擎InnoDB提供的接口读写数据