|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi all,
I have create a table A with a foreign key for a relation to table B. The relation must be benot null so the corresponding attribute is set as not null. If I create a new A with the foreign key null, the record is saved with a 0. I would expect that MySql would send me an exception without saving. How can I get the correct behaviour ? Thanks in advance for any . Pierre MySQl 4.0.18 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Wed, 19 Dec 2007 14:12:31 +0100, Pierre Gilquin <inconnue@bluewin.ch>
wrote: > Hi all, > > > I have create a table A with a foreign key for a relation to table B. > The relation must be benot null so the corresponding attribute is set as > not null. > > If I create a new A with the foreign key null, the record is saved with > a 0. > I would expect that MySql would send me an exception without saving. > How can I get the correct behaviour ? It does limit the insert here. Can you show use the CREATE TABLE syntax for both tables? (and does a key with 0 exist in B, just to be sure?) -- Rik Wasmus |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Thanks Rik,
It's something like that : CREATE TABLE A( ID BIGINT(9) NOT NULL, linkOnTableBId BIGINT(9) NOT NULL, .... ); ALTER TABLE interaction ADD PRIMARY KEY (ID); This is very simple as it is generated by a Object - Relationnal Modelling tools. The constraint for the relation is not managed in the database. There is no attempt to create a B with id = 0. The problem is that the default value for linkOnTableBId is 0 and I cannot get rid of this. Pierre "Rik Wasmus" <luiheidsgoeroe@hotmail.com> a écrit dans le message de news: op.t3k3ll1z5bnjuv@metallium.lan... > On Wed, 19 Dec 2007 14:12:31 +0100, Pierre Gilquin <inconnue@bluewin.ch> > wrote: > >> Hi all, >> >> >> I have create a table A with a foreign key for a relation to table B. >> The relation must be benot null so the corresponding attribute is set as >> not null. >> >> If I create a new A with the foreign key null, the record is saved with >> a 0. >> I would expect that MySql would send me an exception without saving. >> How can I get the correct behaviour ? > > It does limit the insert here. > Can you show use the CREATE TABLE syntax for both tables? (and does a key > with 0 exist in B, just to be sure?) > -- > Rik Wasmus |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Wed, 19 Dec 2007 14:38:34 +0100, Pierre Gilquin <inconnue@bluewin.ch>
wrote: > It's something like that : 'something like' is not what I'm looking for. Could you post the full definitions? Just run SHOW CREATE TABLE <tablename>. It really saves time to see the definitions and pinpoint it directly instead of trying to list all possible reasons why this could/should fail. > ALTER TABLE interaction ADD PRIMARY KEY (ID); This is never in a CREATE TABLE syntax. -- Rik Wasmus |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
This is the real one :
The main table is Interaction SHOW CREATE TABLE interaction CREATE TABLE `interaction` ( `audit_id` mediumint(9) default NULL, `category` char(3) default NULL, `ID` bigint(9) NOT NULL default '0', `impact` char(1) default NULL, `substance1Id` bigint(9) NOT NULL default '0', `substance2Id` bigint(9) NOT NULL default '0', `type` char(1) default NULL, `pourcent` decimal(5,2) default NULL, `Ki` decimal(15,5) default NULL, `Km` decimal(15,5) default NULL, `heriteAtcFromSubstance` varchar(5) default 'true', `infoImportId` bigint(9) default NULL, `impactValue` decimal(15,5) default NULL, PRIMARY KEY (`ID`), UNIQUE KEY `SUB1_SUB2_IDX` (`substance1Id`,`substance2Id`,`category`,`type`) ) TYPE=MyISAM You can see the 2 attributes used for a relation on table Substance ( `substance1Id` bigint(9) NOT NULL default '0', `substance2Id` bigint(9) NOT NULL default '0',) if at least one of theses attributes is null, I would like an exception ... CREATE TABLE `substance` ( `AUC` decimal(15,5) default NULL, `Cmax` decimal(15,5) default NULL, `DCI` blob, `Q0` decimal(15,5) default NULL, `TDemi` decimal(15,5) default NULL, `Vd` decimal(15,5) default NULL, `id` mediumint(9) NOT NULL default '0', `biodisponibilite` decimal(15,5) default NULL, `category` char(3) NOT NULL default '', `idEMediat` mediumint(9) default NULL, `invisible` tinyint(1) default NULL, `isomere` char(3) default NULL, `VERROU` tinyint(1) default NULL, `metabolite` tinyint(1) default NULL, `metaboliteActif` tinyint(1) default NULL, `nomLocal` varchar(255) NOT NULL default '', `poidsMoleculaire` decimal(15,5) default NULL, `fu` decimal(15,5) default NULL, `ka` decimal(15,5) default NULL, `cl` decimal(15,5) default NULL, `CmaxSs` decimal(15,5) default NULL, `tauRefCmaxSs` decimal(15,5) default NULL, `doseRefCmax` decimal(15,5) default NULL, `tempsRefAUC` decimal(15,5) default NULL, PRIMARY KEY (`id`), KEY `nomSubstance` (`nomLocal`) ) TYPE=MyISAM Pierre "Rik Wasmus" <luiheidsgoeroe@hotmail.com> a écrit dans le message de news: op.t3k4ca0j5bnjuv@metallium.lan... > On Wed, 19 Dec 2007 14:38:34 +0100, Pierre Gilquin <inconnue@bluewin.ch> > wrote: >> It's something like that : > > 'something like' is not what I'm looking for. Could you post the full > definitions? Just run SHOW CREATE TABLE <tablename>. It really saves time > to see the definitions and pinpoint it directly instead of trying to list > all possible reasons why this could/should fail. > >> ALTER TABLE interaction ADD PRIMARY KEY (ID); > > This is never in a CREATE TABLE syntax. > -- > Rik Wasmus |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
You are right, I tryed a more simple :
CREATE TABLE A( ID BIGINT(9) NOT NULL, linkOnTableBId BIGINT(9) NOT NULL); ALTER TABLE A ADD PRIMARY KEY (ID); and in this case, I cannot save with a null value for linkOnTableBId ! The problem is on the complete definition of my table Pierre "Rik Wasmus" <luiheidsgoeroe@hotmail.com> a écrit dans le message de news: op.t3k4ca0j5bnjuv@metallium.lan... > On Wed, 19 Dec 2007 14:38:34 +0100, Pierre Gilquin <inconnue@bluewin.ch> > wrote: >> It's something like that : > > 'something like' is not what I'm looking for. Could you post the full > definitions? Just run SHOW CREATE TABLE <tablename>. It really saves time > to see the definitions and pinpoint it directly instead of trying to list > all possible reasons why this could/should fail. > >> ALTER TABLE interaction ADD PRIMARY KEY (ID); > > This is never in a CREATE TABLE syntax. > -- > Rik Wasmus |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On 19 Dec, 13:53, "Pierre Gilquin" <incon...@bluewin.ch> wrote:
> This is the real one : > > The main table is Interaction > > SHOW CREATE TABLE interaction > CREATE TABLE `interaction` ( > `audit_id` mediumint(9) default NULL, > `category` char(3) default NULL, > `ID` bigint(9) NOT NULL default '0', > `impact` char(1) default NULL, > `substance1Id` bigint(9) NOT NULL default '0', > `substance2Id` bigint(9) NOT NULL default '0', > `type` char(1) default NULL, > `pourcent` decimal(5,2) default NULL, > `Ki` decimal(15,5) default NULL, > `Km` decimal(15,5) default NULL, > `heriteAtcFromSubstance` varchar(5) default 'true', > `infoImportId` bigint(9) default NULL, > `impactValue` decimal(15,5) default NULL, > PRIMARY KEY (`ID`), > UNIQUE KEY `SUB1_SUB2_IDX` > (`substance1Id`,`substance2Id`,`category`,`type`) > ) TYPE=MyISAM > > You can see the 2 attributes used for a relation on table Substance > > ( `substance1Id` bigint(9) NOT NULL default '0', > `substance2Id` bigint(9) NOT NULL default '0',) > > if at least one of theses attributes is null, I would like an exception .... > > CREATE TABLE `substance` ( > `AUC` decimal(15,5) default NULL, > `Cmax` decimal(15,5) default NULL, > `DCI` blob, > `Q0` decimal(15,5) default NULL, > `TDemi` decimal(15,5) default NULL, > `Vd` decimal(15,5) default NULL, > `id` mediumint(9) NOT NULL default '0', > `biodisponibilite` decimal(15,5) default NULL, > `category` char(3) NOT NULL default '', > `idEMediat` mediumint(9) default NULL, > `invisible` tinyint(1) default NULL, > `isomere` char(3) default NULL, > `VERROU` tinyint(1) default NULL, > `metabolite` tinyint(1) default NULL, > `metaboliteActif` tinyint(1) default NULL, > `nomLocal` varchar(255) NOT NULL default '', > `poidsMoleculaire` decimal(15,5) default NULL, > `fu` decimal(15,5) default NULL, > `ka` decimal(15,5) default NULL, > `cl` decimal(15,5) default NULL, > `CmaxSs` decimal(15,5) default NULL, > `tauRefCmaxSs` decimal(15,5) default NULL, > `doseRefCmax` decimal(15,5) default NULL, > `tempsRefAUC` decimal(15,5) default NULL, > PRIMARY KEY (`id`), > KEY `nomSubstance` (`nomLocal`) > ) TYPE=MyISAM > > Pierre > > "Rik Wasmus" <luiheidsgoe...@hotmail.com> a écrit dans le message de news: > op.t3k4ca0j5bn...@metallium.lan... > > > On Wed, 19 Dec 2007 14:38:34 +0100, Pierre Gilquin <incon...@bluewin.ch> > > wrote: > >> It's something like that : > > > 'something like' is not what I'm looking for. Could you post the full > > definitions? Just run SHOW CREATE TABLE <tablename>. It really saves time > > to see the definitions and pinpoint it directly instead of trying to list > > all possible reasons why this could/should fail. > > >> ALTER TABLE interaction ADD PRIMARY KEY (ID); > > > This is never in a CREATE TABLE syntax. > > -- > > Rik Wasmus You have defined the fields with a default value of 0. So if the fields are not supplied for the insert or the supplied values are NULL, the default will be used. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Wed, 19 Dec 2007 14:53:35 +0100, Pierre Gilquin <inconnue@bluewin.ch>
wrote: > This is the real one : > > The main table is Interaction > > SHOW CREATE TABLE interaction > CREATE TABLE `interaction` ( > `audit_id` mediumint(9) default NULL, ........ > `impactValue` decimal(15,5) default NULL, > PRIMARY KEY (`ID`), > UNIQUE KEY `SUB1_SUB2_IDX` > (`substance1Id`,`substance2Id`,`category`,`type`) > ) TYPE=MyISAM > > CREATE TABLE `substance` ( > `AUC` decimal(15,5) default NULL, .......... > `tempsRefAUC` decimal(15,5) default NULL, > PRIMARY KEY (`id`), > KEY `nomSubstance` (`nomLocal`) > ) TYPE=MyISAM You have no foreign key constraints (what makes you think you do?), and further more foreign key constraints are not supported in the MyISAM engine, you'll probably want to convert them to InnoDB. -- Rik Wasmus |
|
![]() |
| Outils de la discussion | |
|
|