SQL建模错误--逗号分隔值
最近帮一个客户分析SQL语句的问题,大致经过如下
场景:
委托方有一个用于追踪他们产品的系统,每个产品都会卖给许多客户;但是客户又被从业务上分成两类,一类是带有合作伙伴性质的,这个
合作伙伴通常会给予产品一些建设性的意见,和问题反馈;还有一类就是普通客户了。
程序的第一个版本
create table customer(
id int not null auto_increment, -- 客户id
name varchar(16),-- 客户名
constraint pk__customer__id primary key(id));
-- customer表用于记录客户的信息 create table product(
id int not null auto_increment, -- 产品id
name varchar(32), -- 产品名
partner int not null, -- 合作伙伴id
constraint fk__product__partner foreign key(id) references customer(id),
constraint pk__product__id primary key(id));
-- product表用于记录产品信息 -- 比如说目前的客户有google,facebook,apple; 它们都用了atlas-1.23这个产品;但只有google作为atlas-1.23的合作伙伴
insert into customer(name) values('google'),('facebook'),('apple'); -- 查询customer表中的信息
select * from customer;
+----+----------+
| id | name |
+----+----------+
| 1 | google |
| 2 | facebook |
| 3 | apple |
+----+----------+ -- google 作为atlas-1.23这个产品的合作伙伴
insert into product(name,partner) values('atlas-1.23',1);
这个数据库的逻辑结构有一个问题,就是一个产品是有一个合作伙伴,为了迎合业务数据库的逻辑结构有了第二个版本
程序的第二版
create table customer(
id int not null auto_increment, -- 客户id
name varchar(16),-- 客户名
constraint pk__customer__id primary key(id));
-- customer表用于记录客户的信息 create table product(
id int not null auto_increment, -- 产品id
name varchar(32), -- 产品名
partner varchar(32), -- 合作伙伴名
constraint pk__product__id primary key(id));
-- product表用于记录产品信息 -- 比如说目前的客户有google,facebook,apple; 它们都用了atlas-1.23这个产品;但只有google作为atlas-1.23的合作伙伴
insert into customer(name) values('google'),('facebook'),('apple'); -- 查询customer表中的信息
select * from customer;
+----+----------+
| id | name |
+----+----------+
| 1 | google |
| 2 | facebook |
| 3 | apple |
+----+----------+ -- google 作为atlas-1.23这个产品的合作伙伴
insert into product(name,partner) values('atlas-1.23','google'); --
select * from product;
+----+------------+---------+
| id | name | partner |
+----+------------+---------+
| 1 | atlas-1.23 | google |
+----+------------+---------+ -- 把facebook也设置成atlas-1.23这个产品的合作伙伴
update product set partner=concat(partner,',','facebook') where name='atlas-1.23'; -- 查看atlas-1.23中是否包涵有google & facebook
select * from product;
+----+------------+-----------------+
| id | name | partner |
+----+------------+-----------------+
| 1 | atlas-1.23 | google,facebook |
+----+------------+-----------------+
这个看是完成了业务上的要求,但是它招来了魔鬼
select * from product;
+----+------------+------------------------+
| id | name | partner |
+----+------------+------------------------+
| 1 | atlas-1.23 | google,facebook |
| 2 | alano | facebook,google,Google |
+----+------------+------------------------+
1、这会引起常用的b-tree索引,hash索引失去作用如:select name from product where partner like '%google%';
2、数据的准确性有问题,因为你无法保证不出现google,Google这样的值存在;
3、可扩展性并不强,也就是说如果合作伙伴足够多那么它就会超过varchar(32)的范围;
那么这个要怎么改进呢?
程序的第三个版本就出来了
create table customer(
id int not null auto_increment, -- 客户id
name varchar(16),-- 客户名
constraint pk__customer__id primary key(id));
-- customer表用于记录客户的信息 create table product(
id int not null auto_increment, -- 产品id
name varchar(32), -- 产品名
constraint pk__product__id primary key(id));
-- product表用于记录产品信息 create table product_partner(
id int not null auto_increment primary key,
product_id int not null,
customer_id int not null,
constraint fk__product_id foreign key(id) references product(id),
constraint fk__customer_id foreign key(id) references customer(id));
-- product_partner 表用于保存一个product对应的partner。 insert into customer(name) values('googl');
insert into product(name) values('atlas-1.23'); select * from product;
+----+------------+
| id | name |
+----+------------+
| 1 | atlas-1.23 |
+----+------------+
select * from customer;
+----+-------+
| id | name |
+----+-------+
| 1 | googl |
+----+-------+ insert into product_partner(product_id,customer_id) values(1,1); select * from product_partner;
+----+------------+-------------+
| id | product_id | customer_id |
+----+------------+-------------+
| 1 | 1 | 1 |
+----+------------+-------------+
SQL建模错误--逗号分隔值的更多相关文章
- sql server中单引号拼接字符串(书写错误会出现错误"浮点值 XXXX 超出了计算机表示范围(8 个字节)。“XX”附近有语法错误。")
" ' "(单引号)的运用:在sql server中,两个" ' "(单引号)在拼接字符串的情况下运用,就是表示拼接上了一个" ' "单引号 ...
- SQL Server 错误日志过滤(ERRORLOG)
一.背景 有一天我发现SQL Server服务器的错误日志中包括非常多关于sa用户的登陆错误信息:“Login failed for user 'sa'. 原因: 评估密码时出错.[客户端: XX.X ...
- MS SQL 监控错误日志的告警信息
SQL Server的错误消息(Error Message)按照消息的严重级别一共划分25个等级,级别越高,表示严重性也越高.但是如果你统计sys.messages,你会发现,实际上只有16(SQL ...
- SQL Server自动化运维系列——监控磁盘剩余空间及SQL Server错误日志(Power Shell)
需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在所有的自检流程中最基础的一个就是磁盘剩余空间检测. ...
- SQL Server代理(5/12):理解SQL代理错误日志
SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 如我们在这个系列的前几篇文章所见,SQL ...
- SQL Server自动化运维系列 - 监控磁盘剩余空间及SQL Server错误日志(Power Shell)
需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在所有的自检流程中最基础的一个就是磁盘剩余空间检测. ...
- 获取动态SQL查询语句返回值(sp_executesql)
在写存储过程时经常会遇到需要拼接SQL语句的情况,一般情况下仅仅是为了执行拼接后的语句使用exec(@sql)即可. 而今天的一个存储过程却需要获取动态SQL的查询结果. 需求描述:在某表中根据Id值 ...
- sql server 错误日志errorlog
一 .概述 SQL Server 将某些系统事件和用户定义事件记录到 SQL Server 错误日志和 Microsoft Windows 应用程序日志中. 这两种日志都会自动给所有记录事件加上时间戳 ...
- Oracle 在函数或存储过程中执行sql查询字符串并将结果值赋值给变量
请看黄色部分 --区县指标 THEN TVALUE_SQL := 'SELECT TO_CHAR(' || CUR_ROW.MAIN_FIELD || ') FROM ' || CUR_ROW.END ...
随机推荐
- Android利用百度地图定位
百度地图照着百度的教程做的总是出现报错 请帮我看看错误在那 2013-12-13 15:16168海军 | 分类:百度地图 | 浏览1252次 java.lang.RuntimeException: ...
- Entity Framewor 学习笔记 (Enum)
EF 6 支持Enum 的处理 首先说一下 mysql 和 sql server 的区别 mysql 是有 Enum 这个类型的 , insert 时是放入 int , 出来的时候它会是 string ...
- ural 1353. Milliard Vasya's Function
http://acm.timus.ru/problem.aspx?space=1&num=1353 #include <cstdio> #include <cstring&g ...
- 实现Rsync同步Nginx前端配置
近期,由于我们的阿里前端服务器频频受到恶意的流量攻击,导致前端NGINX进入黑洞而无法正常访问公司网站. 按之前的预计方法,采用加速乐及备用全配置前端的作法,将恶意短时流量攻击的损失时间降到最短.现将 ...
- 俄罗斯方块:Python实现
网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的 主程序: #coding:utf8 #! /usr/bin/env python # ...
- 【HDOJ】3518 Boring Counting
后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10 ...
- crontab,at命令,常见问题
crontab命令 前 一天学习了 at 命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由 cron (crond) 这个系统服务来控制的. Linux 系统上面原本就有非常 ...
- COJ 0036 数数happy有多少个?
数数happy有多少个? 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 图图是个爱动脑子.观察能力很强的好学生.近期他正学英语 ...
- CSS3新特性罗列
接触CSS3这么久了,总是到要用的时候直接拿来用,却没有好好地总结归纳一下,那就在这里好好梳理一下吧. CSS3边框: 圆角边框: 关键:border-radius <!DOCTYPE html ...
- 【爬虫问题】爬取tv.sohu.com的页面, 提取视频相关信息
尝试解决下面的问题 问题: 爬取tv.sohu.com的页面, 提取视频相关信息,不可用爬虫框架完成 何为视频i关信息?属性有哪些? 需求: 做到最大可能的页面覆盖率 *使用httpClient 模拟 ...