为什么我们关心数据类型? [英] Why do we care about data types?

查看:434
本文介绍了为什么我们关心数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,在关系数据库管理系统中,为什么我们需要在创建时知道列的数据类型(更可能是对象的属性)?

Specifically, in relational database management systems, why do we need to know the data type of a column (more likely, the attribute of an object) at creation time?

对我来说,数据类型感觉像是一个优化,因为一个数据点可以以任何数量的方式实现。不是更好地为数据点分配语义角色和约束,然后让引擎在内部检查和优化哪种数据类型最适合用户?

To me, data types feel like an optimization, because one data point can be implemented in any number of ways. Wouldn't it be better to assign semantic roles and constraints to a data point and then have the engine internally examine and optimize which data type best serves the user?

我怀疑这是重要的提升,为什么它更容易询问用户,而不是做工作。

I suspect this is where the heavy lifting is and why it's easier to just ask the user rather than to do the work.

你觉得怎么样?我们在哪里?这是一个现实的期望吗?或者我有一个误导的假设?

What do you think? Where are we headed? Is this a realistic expectation? Or do I have a misguided assumption?

推荐答案

你说得对:为列分配数据类型是一个实现细节,与数据库引擎背后的集合理论或演算无关。作为一个理论模型,一个数据库应该是typeless,并能够存储任何我们抛出它。

You're right: assigning a data type to a column is an implementation detail and has nothing to do with the set theory or calculus behind a database engine. As a theoretical model, a database ought to be "typeless" and able to store whatever we throw at it.

但是我们必须在真实的​​计算机上实现数据库真实约束。从性能的角度来看,让计算机动态尝试找出如何最好地存储数据是不切实际的。

But we have to implement the database on a real computer with real constraints. It's not practical, from a performance standpoint, to have the computer dynamically try to figure out how to best store the data.

例如,假设您有一个表,它存储了几百万个整数。计算机可以正确地确定它应该将每个数据存储为一个整数值。但是如果你有一天突然尝试在该表中存储一个字符串,数据库引擎是否应该停止一切,直到它将所有数据转换为更一般的字符串格式?

For example, let's say you have a table in which you store a few million integers. The computer could -- correctly -- figure out that it should store each datum as an integral value. But if you were to one day suddenly try to store a string in that table, should the database engine stop everything until it converts all the data to a more general string format?

不幸的是,指定数据类型是一个必要的邪恶。

Unfortunately, specifying a data type is a necessary evil.

这篇关于为什么我们关心数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆