2021年12月03日问题汇总

MySQL问题

如何创建联合主键

联合主键是什么

数据库的每张表只能有一个主键,不可能有多个主键。所谓的一张表多个主键,我们称之为联合主键。联合主键就是用多个字段一起作为一张表的主键。主键的主键的作用是保证数据的唯一性和完整性,同时通过主键检索表能够增加检索速度。

联合主键怎么用

{% note success %} 建表时候已经指定联合主键 {% endnote %}

create table if not exists `course`(
 `course_no` varchar(128)  comment '课程号' not null,
 `teacher_no` varchar(128)  comment '教师号' not null,
 `course_name` varchar(128) comment '课程名称' not null,
 primary key(teacher_no,course_no)
)engine=InnoDB default charset = utf8 ;

mysql插入单条,多条数据


-- 创建课程表 course
create table if not exists `course`(
 `course_no` varchar(128)  comment '课程号' not null,
 `teacher_no` varchar(128)  comment '教师号' not null,
 `course_name` varchar(128) comment '课程名称' not null,
 primary key(`teacher_no`,`course_no`)
)engine=InnoDB default charset = utf8 ;

-- 插入数据

-- 插入多条数据
insert into `course`(course_no,teacher_no,course_name) values
('0001','语文','0002'),
('0002','数学','0001'),
('0003','英语','0003');

-- 插入单条数据
insert into `course` value ('0004','编程学习','0004') ;

-- 查询数据
select * from score ;

删除数据

有三种方案用于删除数据库信息,drop,truncate,delete,三者用于不同场景
drop table table_name 删除表结构数据
truncate table table_name : 删除表全部数据,保留表结构,立刻释放磁盘空间 ,存储引擎不管是 Innodb 和 MyISAM;
delete from table_name : 删除表全部数据,表结构不变,对于 MyISAM 会立刻释放磁盘空间,InnoDB 不会释放磁盘空间;
总结

  1. 当你不再需要该表时, 用 drop;
  2. 当你仍要保留该表,但要删除所有记录时, 用 truncate;
  3. 当你要删除部分记录时, 用 delete。

distinct用法

简单说明

distinct一般是用来去除查询结果中的重复记录的,
而且这个语句在select、insert、delete和update中只可以在select中使用

具体用法
-- 这里的expressions可以是多个字段
select distinct expression[,expression...] from tables [where conditions];
实例
-- 测试表
-- SET FOREIGN_KEY_CHECKS=0;执行sql脚本时,不让外键受影响导致出错
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for test_distinct
-- ----------------------------
DROP TABLE IF EXISTS `test_distinct`;
CREATE TABLE `test_distinct` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `province` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of test_distinct
-- ----------------------------
INSERT INTO `test_distinct` VALUES ('1', 'BJ', 'BJ', 'houchenggong');
INSERT INTO `test_distinct` VALUES ('2', 'LN', 'DL', 'zhenhuasun');
INSERT INTO `test_distinct` VALUES ('3', 'LN', 'DL', 'yueweihua');
INSERT INTO `test_distinct` VALUES ('4', 'BJ', 'BJ', 'sunzhenhua');
INSERT INTO `test_distinct` VALUES ('5', 'LN', 'TL', 'fengwenquan');
INSERT INTO `test_distinct` VALUES ('6', 'LN', 'DL', 'renquan');
INSERT INTO `test_distinct` VALUES ('7', 'LN', 'DL', 'wuxin');

作用于单列

-- 测试表
-- SET FOREIGN_KEY_CHECKS=0;执行sql脚本时,不让外键受影响导致出错
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for test_distinct
-- ----------------------------
DROP TABLE IF EXISTS `test_distinct`;
CREATE TABLE `test_distinct` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `province` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of test_distinct
-- ----------------------------
INSERT INTO `test_distinct` VALUES ('1', 'BJ', 'BJ', 'houchenggong');
INSERT INTO `test_distinct` VALUES ('2', 'LN', 'DL', 'zhenhuasun');
INSERT INTO `test_distinct` VALUES ('3', 'LN', 'DL', 'yueweihua');
INSERT INTO `test_distinct` VALUES ('4', 'BJ', 'BJ', 'sunzhenhua');
INSERT INTO `test_distinct` VALUES ('5', 'LN', 'TL', 'fengwenquan');
INSERT INTO `test_distinct` VALUES ('6', 'LN', 'DL', 'renquan');
INSERT INTO `test_distinct` VALUES ('7', 'LN', 'DL', 'wuxin');

 -- 作用于单列 
select * from test_distinct ;
select distinct city from test_distinct;

20211204115854

作用于多列:对多列操作,表示选取 多列都不重复的数据,相当于 多列拼接的记录 的整个一条记录 , 不重复的记录。

-- 测试表
-- SET FOREIGN_KEY_CHECKS=0;执行sql脚本时,不让外键受影响导致出错
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for test_distinct
-- ----------------------------
DROP TABLE IF EXISTS `test_distinct`;
CREATE TABLE `test_distinct` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `province` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of test_distinct
-- ----------------------------
INSERT INTO `test_distinct` VALUES ('1', 'BJ', 'BJ', 'houchenggong');
INSERT INTO `test_distinct` VALUES ('2', 'LN', 'DL', 'zhenhuasun');
INSERT INTO `test_distinct` VALUES ('3', 'LN', 'DL', 'yueweihua');
INSERT INTO `test_distinct` VALUES ('4', 'BJ', 'BJ', 'sunzhenhua');
INSERT INTO `test_distinct` VALUES ('5', 'LN', 'TL', 'fengwenquan');
INSERT INTO `test_distinct` VALUES ('6', 'LN', 'DL', 'renquan');
INSERT INTO `test_distinct` VALUES ('7', 'LN', 'DL', 'wuxin');

 -- 作用于单列 
select * from test_distinct ;
select distinct province, city from test_distinct;

20211204120419
20211204120441

注意

{% label primary @distinct 必须放在第一个参数 %}

错误实例用来说明

SELECT  province, DISTINCT city FROM test_distinct;

20211204121336

{% label primary @DISTINCT 表示对后面的所有参数的拼接取 不重复的记录,相当于 把 SELECT 表达式的项 拼接起来选唯一值 %}

SELECT DISTINCT   province,city FROM test_distinct;
-- 期望值:  只对 第一个参数  province 取唯一值。如下图

20211204120441

解决方法

{% label primary @前提说明 %}

问:mysql出现which is not functionally dependent on columns in GROUP BY clause报错

原因:输出的结果是叫target list,就是select后面跟着的字段,还有一个地方group by column,就是 group by后面跟着的字段。由于开启了ONLY_FULL_GROUP_BY的设置,所以如果一个字段没有在target list 和group by字段中同时出现,或者是聚合函数的值的话,那么这条sql查询是被mysql认为非法的,会报错误。 SELECT province , city FROM test_distinct GROUP BY province;


解答:SELECT any_value(province) , any_value(city) FROM test_distinct GROUP BY province;

方法一: 利用 group_concat 函数

SELECT  group_concat(distinct any_value(province)) AS province, any_value(city) FROM test_distinct GROUP BY province;

方法二: 不利用DISTINCT , 而是利用group by (我认为第一种方法 其实就是 第二种方法, 第一种方法也就是第二种方法)

SELECT any_value(province) , any_value(city) FROM test_distinct GROUP BY province;

count统计
注意:注意 COUNT( ) 会过滤掉为NULL 的项


select count(distinct username) from test_distinct ;

20211204230120

针对于null处理

-- 测试表
-- SET FOREIGN_KEY_CHECKS=0;执行sql脚本时,不让外键受影响导致出错
SET FOREIGN_KEY_CHECKS=0;
 
-- ----------------------------
-- Table structure for test_distinct
-- ----------------------------
DROP TABLE IF EXISTS `test_distinct`;
CREATE TABLE `test_distinct` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `province` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;
 
-- ----------------------------
-- Records of test_distinct
-- ----------------------------
INSERT INTO `test_distinct` VALUES ('1', 'BJ', 'BJ', 'houchenggong');
INSERT INTO `test_distinct` VALUES ('2', 'LN', 'DL', 'zhenhuasun');
INSERT INTO `test_distinct` VALUES ('3', 'LN', 'DL', 'yueweihua');
INSERT INTO `test_distinct` VALUES ('4', 'BJ', 'BJ', 'sunzhenhua');
INSERT INTO `test_distinct` VALUES ('5', 'LN', 'TL', 'fengwenquan');
INSERT INTO `test_distinct` VALUES ('6', 'LN', 'DL', 'renquan');
INSERT INTO `test_distinct` VALUES ('7', 'LN', 'DL', 'wuxin');

 -- 空值不过滤 
select * from test_distinct ;
select distinct username from test_distinct ;

20211204225322

与all不能同时使用

默认情况下,查询时返回所有的结果,此时使用的就是all语句,这是与distinct相对应的
20211204225811

搭配group by 使用

20211204230344

where和having区别

1、having在聚合(max(target),min(target),sum(target),avg(target),count(*))之后进行过滤,having在分组的时候会用,对分组结果进行过滤,通常分组里面包含聚合函数
2、where搜索条件在分组操作之前应用,having搜索条件在进行分组操作之后应用
!!!having子语句与where子语句区别:
  where子句在分组前对记录进行过滤;
  having子句在分组后对记录进行过滤;


select any_value(id),any_value(province),any_value(username),any_value(city) from test_distinct where id > 3 group by province having province like 'B%' ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值