我们所有使用关系数据库的人都知道(或正在学习)SQL是不同的。获得期望的结果,并有效地这样做,涉及到一个乏味的过程,其部分特征是学习不熟悉的范例,并发现一些我们最熟悉的编程模式在这里不起作用。常见的反模式是什么?
当前回答
像这样将冗余表连接到查询中:
select emp.empno, dept.deptno
from emp
join dept on dept.deptno = emp.deptno;
其他回答
1)我不知道这是否是一个“官方的”反模式,但我不喜欢并试图避免在数据库列中使用字符串文字作为魔法值。
MediaWiki表'image'中的一个例子:
img_media_type ENUM("UNKNOWN", "BITMAP", "DRAWING", "AUDIO", "VIDEO",
"MULTIMEDIA", "OFFICE", "TEXT", "EXECUTABLE", "ARCHIVE") default NULL,
img_major_mime ENUM("unknown", "application", "audio", "image", "text",
"video", "message", "model", "multipart") NOT NULL default "unknown",
(我只是注意到不同的大小写,另一个要避免的事情)
我设计了这样的情况,int查找表ImageMediaType和ImageMajorMime与int主键。
2)日期/字符串转换,依赖于特定的NLS设置
CONVERT(NVARCHAR, GETDATE())
没有格式标识符
select some_column, ...
from some_table
group by some_column
假设结果将按some_column排序。我在Sybase上看到过这种情况,其中假设成立(目前)。
我见过太多人死死抓着IN(…),却完全忘记了EXISTS(存在)。有关一个好例子,请参见Symfony Propel ORM。
我最不喜欢的是
Using spaces when creating tables, sprocs etc. I'm fine with CamelCase or under_scores and singular or plurals and UPPERCASE or lowercase but having to refer to a table or column [with spaces], especially if [ it is oddly spaced] (yes, I've run into this) really irritates me. Denormalized data. A table doesn't have to be perfectly normalized, but when I run into a table of employees that has information about their current evaluation score or their primary anything, it tells me that I will probably need to make a separate table at some point and then try to keep them synced. I will normalize the data first and then if I see a place where denormalization helps, I'll consider it. Overuse of either views or cursors. Views have a purpose, but when each table is wrapped in a view it's too much. I've had to use cursors a few times, but generally you can use other mechanisms for this. Access. Can a program be an anti-pattern? We have SQL Server at my work, but a number of people use access due to it's availabilty, "ease of use" and "friendliness" to non-technical users. There is too much here to go into, but if you've been in a similar environment, you know.
过度使用临时表和游标。
推荐文章
- 在MySQL中检测值是否为number
- MySQL中两个日期之间的差异
- 使用SQL查询查找最近的纬度/经度
- 将一列的多个结果行连接为一列,按另一列分组
- 检查MySQL表是否存在而不使用“select from”语法?
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- 从表中选择1是什么意思?
- SQL Server中User和Login的区别
- 如何更改表的默认排序规则?
- 为两列的组合添加唯一的约束
- 设置NOW()为datetime数据类型的默认值?
- 在MySQL中Datetime等于或大于今天
- 删除MySQL中的主键
- 对象'DF__*'依赖于列'*' -将int改为double