MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old May 14th, 2002, 07:02 AM
kruahsohr kruahsohr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Switzerland
Posts: 7 kruahsohr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
mysql referential integrity

Hello,

can someone tell me why this can work:

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> create table ttest(name varchar(20) not null , lastname varchar(25), primary key(name));

Query OK, 0 rows affected (0.01 sec)

mysql> insert into ttest values('','miller');
Query OK, 1 row affected (0.01 sec)

mysql> select * from ttest;
+------+----------+
| name | lastname |
+------+----------+
| | miller |
+------+----------+
1 row in set (0.01 sec)

MySql inserts a space as the name ... and it's not because of the '' i insert (above)

-----------------------------------------------------------------------------------

And this works too:



mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> create table one (myid int(5) not null auto_increment, myname varchar(10)
, primary key(myid));
Query OK, 0 rows affected (0.01 sec)

mysql> create table two (fmyid int(5), myadr varchar(50) not null, primary key(m
yadr));
Query OK, 0 rows affected (0.01 sec)

mysql> alter table two add constraint foreign key(fmyid) references one(myid);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into two values(10, 'Test for integrity');
Query OK, 1 row affected (0.00 sec)

mysql> select * from one;
Empty set (0.01 sec)

------------------------------------------------------------------------------------

What am I doing wrong ???
Where's the referential integrity in mysql ?

Thanks a lot

Last edited by kruahsohr : May 14th, 2002 at 09:32 AM.

Reply With Quote
  #2  
Old May 14th, 2002, 07:04 AM
kruahsohr kruahsohr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Switzerland
Posts: 7 kruahsohr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry...

The question is why can insert an non existent foreign key ???

Reply With Quote
  #3  
Old May 15th, 2002, 01:05 AM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
In the first example your insert query inserts into name then value, yet the name field is empty and the value has the name in it. are you sure you havent mixed up your queries?

as for the second part, you're inserting into a table that has a key setup, but you don't have vals in the first table. MySQL should be reporting an error i'd say if you can add to the second table without a PK in the first... i'm gonna go do some MySQL testing.

Reply With Quote
  #4  
Old May 15th, 2002, 01:34 AM
kruahsohr kruahsohr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Switzerland
Posts: 7 kruahsohr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
In the first example i dont insert the primary key i just insert a value and mysql inserts a space as the key ...
you can try also with:

a,b varchar(20)
a is primary
when the table is empty (or has no primary key with a space)

insert into test (b) values('fnhjdsfnds');
and it will insert a space ...

And for the second example:

Yes that's my problem ... ( i tried with the older and the newer version of mysql ...)

Any ideas ???

Reply With Quote
  #5  
Old May 15th, 2002, 05:48 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
you've just listed 'a' as 'primary'. Are you creating a proper primary key as a unique id, like this?

a int auto_increment not null,
...

primary key(a),
unique id(a)
);

???

Reply With Quote
  #6  
Old May 15th, 2002, 10:17 PM
kruahsohr kruahsohr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Switzerland
Posts: 7 kruahsohr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes sure !
create table ( column def, ...., primary key(colname))
I didn't use unique but that shouldn't be the point ...

Reply With Quote
  #7  
Old May 26th, 2002, 01:45 PM
shunter101 shunter101 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Layton, UT
Posts: 12 shunter101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to shunter101 Send a message via AIM to shunter101 Send a message via Yahoo to shunter101
Why are you not inserting anything into the primary key...you have the following:

create table ttest(name varchar(20) not null , lastname varchar(25), primary key(name));

Then the primary key is name, which cannot be NULL. This always needs to have a value that is diferent for each row. That is why it is a primary key (database theory).

Here is your insert statement:
insert into ttest values('','miller');

MySQL needs to add a value and above you are not suppling a value. So MySQL inserts a space which is not a NULL value because the primary key cannot be NULL.

Your other example works because you have an auto-incrementing field as the primary key. This will always have a value (NOT NULL) and the value will always be diferent (auto-incrementing).

Hope this explains why MySQL added the space. You can also check the docs on this and verify what MySQL does when not suppling a value to a NOT NULL field.

Reply With Quote
  #8  
Old May 26th, 2002, 11:16 PM
kruahsohr kruahsohr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Switzerland
Posts: 7 kruahsohr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
first of all thanks for your reply ...

You said my second example is working 'cause it's an auto-incrementing field, ok, but in database theory a foreign key is a primary key in another table, so it has to exist to do so, but i can insert something with an non-existing key and it works ...

Reply With Quote
  #9  
Old May 26th, 2002, 11:53 PM
shunter101 shunter101 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Layton, UT
Posts: 12 shunter101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to shunter101 Send a message via AIM to shunter101 Send a message via Yahoo to shunter101
Actually you are right about the foreign key constraint. Mitch also stated the same thing. When I do it I get a referencial integrity violation.

So yes, you can only insert into table two if the foreign key that you are adding is in table one.

What table type are you using with MySQL?

Reply With Quote
  #10  
Old May 27th, 2002, 12:10 AM
shunter101 shunter101 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: Layton, UT
Posts: 12 shunter101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to shunter101 Send a message via AIM to shunter101 Send a message via Yahoo to shunter101
Not that in SQL-92 that foreign key are allowed to be declared as either nonnull or null provided that they have not been declared to be nonnull. Here is the trick part: if ANY of the foreign key columns is NULL, the tuple is defined automatically to satisfy the constraint. This defined definition may not always be the right one so SQL also provides constructs that allow you to change the behavior with NULL values...

Does that make sense...basically, if you have a foreign-key that has on tuple or column defined as null, then the database will define that tuple automatically to satisfy the constraint...

This could possibly be the reason for MySQL allowing you to insert into table two.

I would suggest that you verify MySQL take on foreign-key constraints for NULL values...

PostgreSQL does not allow you to do that seeing that it is basically a referential integrity violation...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > mysql referential integrity


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway