mysql是关系型数据库
(安装教程请参考主页相关文章https://blog.csdn.net/2401_86120676/article/details/141265678?spm=1001.2014.3001.5502 和mysql Ubuntu安装与远程连接配置-CSDN博客)
mysql相关指令
1.数据库
展示所有的数据库:show databases;
 
创建数据库:create database 数据库名 charset=utf8;
 
删除数据库:drop database 数据名;
 
查看当前使用的数据库:select database();
使用指定数据库:use 数据库名;
 
2.数据表
约束:
主键:可以确定唯一的一行,primary key。
非空:not null,不能为空。
 自增长:auto_increment,每次+1。
 唯一:列中内容不重复,unique。
 默认:default,默认值。
 外键:foreign key,与其他表关联 是其他表的左键。
 检查:check(age > 0),mysql8 才能用。
显示所有表:show tables;
创建表:create table 表名( 列名 类型 约束1 约束2 约束3, 列名 类型 约束1 约束2, 列名 类型 约束1 );
查看表结构:desc 表名;
 
删除数据表:drop table 表名;
                 
修改表 :alter table 表名 drop 列名;(删除指定列)
 
alter table 表名 add 列名 类型 约束1 约束2;
                 
alter table 表名 change 原列名 新列名 类型 约束1 约束2;
 
3.CURD增删改查
外键:
关系型数据库表与表之间存在关联关系
 外键:一个表中的某一列是另外一个表中的主键
 创建、添加:constraint  外键名 foreign key(列名)references  表(列)on update  cascade  on 
  delete   cascade;
 删除:alter table 表名 drop foreign key 外键名;
增:
insert into  表名 values (列1, 列2,列n),(列1, 列2, 列n)....
 列的个数与值的个数一致
insert into  表名  (列1, 列2) values (值1, 值2), (值1, 值2)...
 可以指定 哪些列的数据,没有默认值的列必须插入。

insert into   表名  set  列1=值1,列2=值2;
 插入1行指明插入的列与其值。


 改:
 update 表名 set 列1 = 值1 where 条件;
 如果没有条件就修改整个表

查:
select * from 表名;(慎用,非常耗时)
select 列 ,列,列 from 表;
select 列 as 别名,列 as 别名 from 表;


条件where:
select * from 表名 where 条件;
 比较运算符:=   >   >=   <   <=    !=(<>)。

逻辑运算符:and、or、not
 
判空:is null; is not null。

in:in(a, b, c),精确比较。

between and:范围比较,between 5 and 10。

 like:像,%,多个字符;_,一个字符。


关联查询:
 表与表之前存在关联关系
 嵌套查询:第一次查询的结果作为第二次查询的条件
 表连接:笛卡尔连接,一个表中每一行都和另外一个表中所有行连接
                内连接,inner join;例:select user.id as 用户id, user.username as  用户名, role.nick as 昵称, role.level as 等级 from user inner join role  on user.id = role.user_id;

                外连接,左外连接,以左表为主,左表条件满足 正常显示 不满足 右侧补null,left join;例:select * from user left join role on user.id = role.user_id;

                右外连接,以右表为主,右表条件满足 正常显示 不满足 左侧补null,right join;例:select * from role right join user on user.id = role.user_id;

                全连接,full join,mysql不支持关键字 full join,left join   union  right join;例:select * from user right join role on user.id = role.user_id union  select * from user left join role on user.id = role.user_id; 

分组,排序,分页 ...:
 去重:单独列展示 并对列去重,distinct;例:select  distinct user_id from role;

 分组:group by,一般会结合count (*)进行统计;例: select level as '等级', count(*) as '总数' from role group by level;

 排序:列排序,order by,asc 升序,desc 降序;例:select * from role order by user_id desc , id  desc  ;

 分页:limit,一个数字n 代表 前n个,两个数字start,count,每页显示count个,第n页公式(n-1)* count, count。
删:
如果没有条件就清空表
delete from 表名 where 条件;


4.用户
root系统管理员+ 其他用户,% 代表所有ip都可以访问。
创建用户:create user '用户名'@'%' identified by '123456';

分配权限:grant 权限列举 on 数据名.表名 to ‘用户名’@‘%’; all,所有权限;*.*,所有数据库中的所有表;

刷新权限:flush privileges;

修改密码:alter user ‘用户名’@‘%’ identified by ‘123456’;

删除用户:drop user '用户名'@'%';















![【动态规划、dp】P1091 [NOIP2004 提高组] 合唱队形 题解](https://img-blog.csdnimg.cn/img_convert/e6c2c6a9da18ff3b2254251f173ed7a3.jpeg#pic_center)




