/*
https://dev.mysql.com/downloads/windows/installer/8.0.html
Navicat 链接数据库
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
FLUSH PRIVILEGES;
数据库的关系模型 :多张表通过 key关联起来。
表的每一行 :称为记录(record)
表的每一列:称为字段(列)column
类型
int
float
datetime 插入 now()
char[30];
varchar(30);
text 最大长度为65535(65535)//固定长度没用
enum
set
primary key :
alter table 表名 add primary key(列名);
alter table 表名 drop primary key;
foreign key
alter table 表名1 add foreign key(列名)references表名2(列名2);
alter table 表名 drop foreign key 外键名;
auto_increment 一个表只能有一个
alter table 表名 modify 列名 类型 auto_increment;
not null
alter table 表名 modify 列名 类型 not null;
default 默认值
alter table 表名 modify 列名 类型 default 值;
普通索引
alter table 表名 add index 别名 (列名) ;
唯一索引
alter table 表名 add unique 别名 (列名);
全文索引
alter table 表名 add fulltext 别名 (列名) ;
alter table 表名 drop index 别名;
//删除索引
show index from 表名;
对表的操作
c s d a a i u d
create table 表名(id int comment 'xxxx');
show create table 表名;
desc 表名
alter table 表名 add id int;
alter table 表名 drop id;
insert into 表名 values(10);
update 表名 set id =20 where xx= yyy;
delete from 表名 where id = 10;
1 铅笔 * d
select * from 表名;
select distinct from 表名;
2 鸭子 = l b i
select * from 表名 where 列名 = 值;
select * from 表名 where 列名 like ‘M% ’ / ‘M _ ’;
select * from 表名 where 列名 between 值1 and 值2 ;
select * from 表名 where 列名 in(值,值,值);
3 耳朵 o g
select * from 表名 order by 列名; //排序
select group_concat(列名) from 表名 group by(列名);//写出合并的组的列
4 小旗 c m m s a
select count(列名)/ max(列名)/min(列名)/sum(列名)/avg(列名)/from 表名;
5 钩子 i r l
//内连接
select * from 表名1 inner join 表名2 on 表名1.列名 = 表名2.列名;
//外连接
select * from 表名1 right/left join 表名2 on 表名1.列名 = 表名2.列名;
6 哨子 u
//合并查询
select 列名1 from 表名1
union all/union(去重)
select 列名2 from 表名2;
//把对应的两列组合成一列
7 镰刀 = i a a a e
//子查询
select * from 表名1 where 列名1
= / in / >ALL/ >any /and exists(select 列名2 from 表名2);
1 * d
2 = l b i
3 o g
4 c m m s a
5 i r l
6 u
7 = i a a a e
视图:
create view 视图名 as select
h1.xx,h2.yy from h1 h2;
desc 视图名;
drop view 视图名;
触发器:
create trigger 名字
after insert/delete on 表名1 for each row
update 表名2 set 列名2 = 值
where 表名2.id = new.id /OLD.id; // new/old 指的是表名1
delimiter $$
create trigger 名字 after delete on 表名1 for each row
begin
delete from 表名2 where 表名2.id = OLD.id;
update 表名3 set count = count - 1 where 表名3.id = OLD.id;
end;
$$
delimiter;
drop trigger 触发器名;//删除触发器
函数存储
in 输入 / out 输出 /inout
delimiter $$
create procedure aa(in a int,out b varchar(20))
begin
declare c int default 0;
Select id into c from xx where id = a;
set c=20;
select c; //打印
set b=c;
select b;
end;
$$
delimiter ;
call 函数名(2, @name);//name用户变量
select @name;
光标的使用
begin
declare done int default 0;
declare c_xx cursor for
select id from xx where id = sid;// c_xx=id
declare continue handler for not found set done = 1;
//当光标 fetch c_xx into tmp_name 没有值时done 才被设置成1
open c_xx;
select done;
fetch c_xx into tmp_name ;//done才有效
select done;
select tmp_name, tmp_cid;
close c_xx;
end;
if 不能用leave 跳出
if ... then ....; 执行了下面的不执行
......可以写代码
elseif ... then ...;
......可以写代码
else ...;
......可以写代码
end if;
case a
when 20 then set a=a+1;
...........
when 30 then ....;
...........
else ...;
end case ;
xx:loop 相当于 while
leave xx;
iterate xx; 相当于continue
end loop xx;
xx: repeat
SET @count=@count+1;
until @count=100 直到等于100时结束
end repeat xx;
xx :while @count<100
do
SET @count = @count + 1;
end while xx;
*/
/*
c++ 访问数据库
1 在 mysql 安装的文件夹下找到
include 和 lib 文件夹的目录
VC++ 目录--->包含目录--->包含 include目录
C:\Program Files\MySQL\MySQL Server 8.0\include
VC++目录----->库目录----->包含 lib 目录
C:\Program Files\MySQL\MySQL Server 8.0\lib
2
连接器----->输入------ > 附加依赖项------ >
libmysql.lib
3
把 libmysql.dll 文件
复制到 C:\Windows\System32 系统下面。。。
4
https://downloads.mysql.com/archives/c-cpp/
找libssl-3-x64.dll 和 libcrypto-3-x64.dll 文件
放到程序生成exe的同级目录下面
*/
//#include <mysql.h>
//MYSQL mysql;
//mysql_init(&mysql);//1.初始化数据库句柄
//mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");//2.设置字符编码
//mysql_real_connect(&mysql, _127001, ROOT, LZ123456, LZ, _3306, NULL, 0);
//3.连接数据库
//char sql[256];
//bool ret = mysql_query(&mysql, sql); //数据库查询
//MYSQL_RES* res = mysql_store_result(&mysql);//获取结果集
//MYSQL_ROW row = mysql_fetch_row(res); //获取行
//int id = atoi(row[0]);
//mysql_error(&mysql);//数据库出错时打印信息
//mysql_free_result(res);//释放结果集
//mysql_close(&mysql);//关闭数据库