473,767 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Foreign key constraint accepted even when not same data type

Is it right for postgres to accept a foreign key constraint when the
type of the field is not the same as that of the foreign key?

For example:

# Create table a (id int primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'a_pkey'
for table 'a'
CREATE TABLE
# Create table b (id2 text references a(id));

NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY
check(s)
CREATE TABLE

# \d a
Table "public.a"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
Indexes: a_pkey primary key btree (id)

# \d b
Table "public.b"
Column | Type | Modifiers
--------+------+-----------
id2 | text |
Foreign Key constraints: $1 FOREIGN KEY (id2) REFERENCES a(id) ON UPDATE
NO ACTION ON DELETE NO ACTION
Jean-Christian Imbeault


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #1
6 2017
On Mon, 22 Sep 2003, Jean-Christian Imbeault wrote:
Is it right for postgres to accept a foreign key constraint when the
type of the field is not the same as that of the foreign key?


IIRC in SQL92 it's said that they need to be the same type, but in SQL99
it says that the two types must be comparable. We basically implement the
latter, basically using the existance of a usable equality operator as the
determination of comparable.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 11 '05 #2
Stephan Szabo <ss****@megazon e.bigpanda.com> writes:
On Mon, 22 Sep 2003, Jean-Christian Imbeault wrote:
Is it right for postgres to accept a foreign key constraint when the
type of the field is not the same as that of the foreign key?
IIRC in SQL92 it's said that they need to be the same type, but in SQL99
it says that the two types must be comparable. We basically implement the
latter, basically using the existance of a usable equality operator as the
determination of comparable.


Note however that performance may be poor with a cross-type foreign key
reference, if the planner is unable to figure out how to use an index
for the check queries.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 11 '05 #3
On Mon, 22 Sep 2003, Stephan Szabo wrote:
it says that the two types must be comparable. We basically implement the
latter, basically using the existance of a usable equality operator as the
determination of comparable.


Is it possible to drop the equality operator when one have FK that needs
it?

--
/Dennis
---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #4
On Tue, 23 Sep 2003, Dennis Bjorklund wrote:
On Mon, 22 Sep 2003, Stephan Szabo wrote:
it says that the two types must be comparable. We basically implement the
latter, basically using the existance of a usable equality operator as the
determination of comparable.


Is it possible to drop the equality operator when one have FK that needs
it?


Actually, right now, I think it is (as are necessary casts). That's
probably not good, but since the actual constraint isn't that you can't
drop the equality operator, but that the types must still be comparable
after doing so, I'm not sure how one would represent that right now (for
example, given an int->foo equality operator and foreign key, if there was
say a numeric->foo equality operator, dropping the int one is probably
okay assuming an implicit int->numeric cast).
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #5
Tom Lane wrote:
Stephan Szabo <ss****@megazon e.bigpanda.com> writes:
On Mon, 22 Sep 2003, Jean-Christian Imbeault wrote:
Is it right for postgres to accept a foreign key constraint when the
type of the field is not the same as that of the foreign key?

IIRC in SQL92 it's said that they need to be the same type, but in SQL99
it says that the two types must be comparable. We basically implement the
latter, basically using the existance of a usable equality operator as the
determination of comparable.


Note however that performance may be poor with a cross-type foreign key
reference, if the planner is unable to figure out how to use an index
for the check queries.


Didn't we agree to throw a NOTICE in cases of a mismatch? (I think
Peter agreed to a NOTICE but not a WARNING) Is that completed?

--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.ph a.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #6
On Wed, 24 Sep 2003, Bruce Momjian wrote:
Tom Lane wrote:
Stephan Szabo <ss****@megazon e.bigpanda.com> writes:
On Mon, 22 Sep 2003, Jean-Christian Imbeault wrote:
> Is it right for postgres to accept a foreign key constraint when the
> type of the field is not the same as that of the foreign key?

IIRC in SQL92 it's said that they need to be the same type, but in SQL99
it says that the two types must be comparable. We basically implement the
latter, basically using the existance of a usable equality operator as the
determination of comparable.


Note however that performance may be poor with a cross-type foreign key
reference, if the planner is unable to figure out how to use an index
for the check queries.


Didn't we agree to throw a NOTICE in cases of a mismatch? (I think
Peter agreed to a NOTICE but not a WARNING) Is that completed?


Did that get decided upon? In any case, I don't think domains were talked
about. Should it be decided upon the base type of the domain(s) involved
or just that the final types are different?

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2321
by: dcp | last post by:
I just installed the 4.1.0-alpha-max-nt version of MySql and have just started playing around with it. My first test was to try to create a couple of tables, one with a foreign key constraint. I can't seem to figure out how to drop the foreign key constraint, I read through the docs and it says I need to do a 'show create table <table_name>' to get the internally generated foreign key id, but this does not show up when I do the show...
0
2635
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get the following = MySQL error: INSERT INTO product_access_level (product_id,access_level_id) VALUES
10
42421
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
31
3382
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific customer subtypes: 1 - business, 2 - home, 3 - university
4
3165
by: teddysnips | last post by:
This is a rather abstract question about data design, but I ask it here because a) the database is SQL Server, and b) you're such a learned bunch! Let's assume the classic relation of Customers and Orders, where an Order may reference a single Customer. If I was designing such a relation from scratch, I would create the Customer table with an Identity column and call it CustomerID. The Order table would contain a column called...
0
486
by: Jean-Christian Imbeault | last post by:
Is it right for postgres to accept a foreign key constraint when the type of the field is not the same as that of the foreign key? For example: # Create table a (id int primary key); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'a_pkey' for table 'a' CREATE TABLE # Create table b (id2 text references a(id));
13
51645
by: Tibor | last post by:
I am using PostgreSQL 7.4.1 (only through psql) I know, that the command ALTER TABLE OFFICES DROP PRIMARY KEY (CITY); and its foreign key equivalent: ALTER TABLE SALESREPS DROP CONSTRAINT
2
6313
by: kal stevens | last post by:
I have been trying to write a database schema in mysql, and I cant figure this out. Here is a database schema DROP DATABASE IF EXISTS d; CREATE DATABASE d;
3
2427
by: moorcroft | last post by:
Hi I am trying to write some sql to because we have a missing foregin key constraint. The following is what I have been asked to do: 1. COMPULSORY_WORK.reference to LISTED_BUILDING.reference 2. The constraint name should be COMPULSORY_WORK_FK 3. Provide data check SQL for records which will invalidate the foreign key I have created the constraint with the code:
0
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10013
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8838
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.