设置具有不同数据类型的外键 [英] Setting up foreign key with different datatype

查看:474
本文介绍了设置具有不同数据类型的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建了两个表,并且想将一列设置为另一表列的外键,则为什么要允许我设置外键列的数据类型?



没有任何意义,或者我错过了什么吗?



在任何情况下,带有外键的列都故意具有不同的数据类型吗?



关于我的担忧,我尝试使用pgadmin来构建一些简单的Postgres DB。我用主键 serial 数据类型制作了第一个表。然后我尝试制作外键,但是什么数据类型?我曾看到序列 bigint unsigned 的地方。但是这个选项甚至在pgadmin中都不存在。当然我可以使用sql,但是为什么我要使用gui?所以我尝试了Navicat,同样的问题。我觉得每当选择我的数据库设计时都会犯另一个错误...



编辑:



也许我以错误的方式问了这个问题。
我被允许做构建结构:

  CREATE TABLE用户

id bigint NOT NULL,
约束user_pkey主键(id)

WITH(
OIDS = FALSE
);

创建表

用户整数,
CONSTRAINTdependent_user_fkey外键(user)
参考用户(id)匹配简单
ON用(
OIDS = FALSE
)更新不删除不采取行动

;

我向表用户插入一些数据:

 插入用户(id)
值(5000000000);

但是我不能在插入后进行强制转换:

 将书插入(用户)
值(5000000000);

错误:整数超出范围其中是可以理解的,但是明显的设计错误。



我的问题是:为什么当我们设置 CONSTRAINT ,数据类型未得到验证。如果我错了,答案应该包含以下场景:使用不同的数据类型会很有用。

解决方案

实际上,这确实是有原因的:



在一个表,实际上您可以将任何列设置为其主键。因此,它可以是整数,双精度数,字符串等。即使在当今,我们仍然主要使用整数或最近使用字符串作为表中的主键。



外键指向另一个表的主键,这就是为什么您需要指定外键的数据类型的原因。显然,它必须是相同的数据类型。



编辑:



SQL我们可以看到,这种情况的实现方式不严格:它们确实允许兼容类型(INT和BIG INT,Float或DECIMAL和DOUBLE),但后果自负。就像我们在您的示例中看到的那样。



但是,SQL规范确实指定两个数据类型必须相同。
如果数据类型是字符,则它们必须具有相同的长度,否则,如果它是整数,则它们必须具有相同的大小,并且两者都必须已签名或未签名



您可以自己查看在这里,这是MySQL图书于2003年出版的一章。



希望这能回答您的问题问题。


If I create two tables and I want to set one column as foreign key to another table column why the hell am I allowed to set foreign key column datatype?

It just doesn't make any sense or am I missing something? Is there any scenario where column with foreign keys has different datatype on purpose?

Little more deeper about my concerns, I tried to use pgadmin to build some simple Postgres DB. I made first table with primary key serial datatype. Then I tried to make foreign key but what datatype? I have seen somewhere serial is bigint unsigned. But this option doesn't even exists in pgadmin. Of course I could use sql but then why am I using gui? So I tried Navicat instead, same problem. I feel like with every choice I do another mistake in my DB design...

EDIT:

Perhaps I asked the question wrong way. I was allowed to do build structure:

CREATE TABLE user
(
  id bigint NOT NULL,
  CONSTRAINT user_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);

CREATE TABLE book
(
  user integer,
  CONSTRAINT dependent_user_fkey FOREIGN KEY (user)
      REFERENCES user (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

I insert some data to table user:

INSERT INTO user(id)
    VALUES (5000000000);

But I can't cast following insert:

INSERT INTO book(user)
    VALUES (5000000000);

with ERROR: integer out of range which is understandable, but obvious design error.

And my question is: Why when we set CONSTRAINT, data types are not being validated. If I'm wrong, answer should contain scenario where it is useful to have different data types.

解决方案

Actually it does make sense here is why:

In a table, you can in fact set any column as its primary key. So it could be integer, double, string, etc. Even though nowadays, we mostly use either integers or, more recently, strings as primary key in a table.

Since the foreign key is pointing to another table's primary key, this is why you need to specify the foreign key's datatype. And it obviously needs to be the same datatype.

EDIT:

SQL implementations are lax on this case as we can see: they do allow compatible types (INT and BIG INT, Float or DECIMAL and DOUBLE) but at your own risk. Just as we can see in your example, below.

However, SQL norms do specify that both datatypes must be the same. If datatype is character, they must have the same length, otherwise, if it is integer, they must have the same size and must both be signed or both unsigned.

You can see by yourself over here, a chapter from a MySQL book published in 2003.

Hope this answers your question.

这篇关于设置具有不同数据类型的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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