null foreign key column
null foreign key column
От:
"Arunachalam Jaisankar" <jsankar@xtra.co.nz>
Дата:
Hi all,
I would like to have a foreign key column in my table which allows null value also.
But the below create table sql command doesn't accept null value for person_id.
How to do in postgres?
create table event
(
event_id serial not null,
event_description char(255) ,
person_id serial ,
primary key (event_id),
foreign key (person_id)
references person (person_id)
);
(
event_id serial not null,
event_description char(255) ,
person_id serial ,
primary key (event_id),
foreign key (person_id)
references person (person_id)
);
regards
Jai
Re: null foreign key column
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Tue, Feb 11, 2003 at 16:55:28 +1300, Arunachalam Jaisankar wrote: > Hi all, > > I would like to have a foreign key column in my table which allows null value also. > But the below create table sql command doesn't accept null value for person_id. > How to do in postgres? > > create table event > ( > event_id serial not null, > event_description char(255) , > person_id serial , > primary key (event_id), > foreign key (person_id) > references person (person_id) > ); I don't think you want to use serial type for a value that references another table. Use integer instead. Serial also implies not null which is why you couldn't stick null values into that field.
Re: null foreign key column
От:
Dmitry Tkach <dmitry@openratings.com>
Дата:
You don't want it to be serial - just make it 'person_id in' I hope, it helps... Dima Arunachalam Jaisankar wrote: > This is a multi-part message in MIME format. > > ------=_NextPart_000_0005_01C2D1EE.61998D70 > Content-Type: text/plain; > charset="iso-8859-1" > Content-Transfer-Encoding: quoted-printable > > Hi all, > > I would like to have a foreign key column in my table which allows null val= > ue also. > But the below create table sql command doesn't accept null value for person= > _id. > How to do in postgres? > > create table event > ( > event_id serial not null, > event_description char(255) , > person_id serial , > primary key (event_id), > foreign key (person_id) > references person (person_id) > ); > > regards > Jai > ------=_NextPart_000_0005_01C2D1EE.61998D70 > Content-Type: text/html; > charset="iso-8859-1" > Content-Transfer-Encoding: quoted-printable > > > > > > > > >Hi all,>>I would like to have a foreign key column = > in my=20 > table which allows null value also.>But the below create table sql command doe= > sn't=20 > accept null value for person_id.>How to do in postgres?>= >>create table event>
(
&nb= > sp;=20 > event_id &= > nbsp; =20 > serial &nb= > sp; =20 > not null,
event_description &n= > bsp;=20 > char(255) = > =20 > ,
=20 > person_id = > =20 > serial &nb= > sp; =20 > ,
primary key (event_id),
forei= > gn=20 > key (person_id)
references pe= > rson=20 > (person_id)
);>regards>Jai> > ------=_NextPart_000_0005_01C2D1EE.61998D70-- >
Re: null foreign key column
От:
Vernon Wu <vernonw@gatewaytech.com>
Дата:
12/02/2003 2:24:49 PM, Dmitry Tkach wrote: >You don't want it to be serial - just make it 'person_id in' Any reasons? > >I hope, it helps... > >Dima > >Arunachalam Jaisankar wrote: >> This is a multi-part message in MIME format. >> >> ------=_NextPart_000_0005_01C2D1EE.61998D70 >> Content-Type: text/plain; >> charset="iso-8859-1" >> Content-Transfer-Encoding: quoted-printable >> >> Hi all, >> >> I would like to have a foreign key column in my table which allows null val= >> ue also. >> But the below create table sql command doesn't accept null value for person= >> _id. >> How to do in postgres? >> >> create table event >> ( >> event_id serial not null, >> event_description char(255) , >> person_id serial , >> primary key (event_id), >> foreign key (person_id) >> references person (person_id) >> ); >> >> regards >> Jai >> ------=_NextPart_000_0005_01C2D1EE.61998D70 >> Content-Type: text/html; >> charset="iso-8859-1" >> Content-Transfer-Encoding: quoted-printable >> >> >> >> >> >> >> >> >>Hi all,>>>>I would like to have a foreign key column = >> in my=20 >> table which allows null value also.>>But the below create table sql command doe= >> sn't=20 >> accept null value for person_id.>>How to do in postgres?>>= >>>>create table event>>
(
&nb= >> sp;=20 >> event_id &= >> nbsp; =20 >> serial &nb= >> sp; =20 >> not null,
event_description &n= >> bsp;=20 >> char(255) = >> =20 >> ,
=20 >> person_id = >> =20 >> serial &nb= >> sp; =20 >> ,
primary key (event_id),
forei= >> gn=20 >> key (person_id)
references pe= >> rson=20 >> (person_id)
);>>regards>>Jai>> >> ------=_NextPart_000_0005_01C2D1EE.61998D70-- >> > > >---------------------------(end of broadcast)--------------------------- >TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) >
Re: null foreign key column
От:
Dmitry Tkach <dmitry@openratings.com>
Дата:
Vernon Wu wrote: >12/02/2003 2:24:49 PM, Dmitry Tkach wrote: > > > >>You don't want it to be serial - just make it 'person_id in' >> >> > >Any reasons? > > Yeah... Two of them: - It does not make sense for a serial column to reference other tables - the only purpose of serial is to generate unique keys, in your case you do not want them generated, but rather copied from the entries in the referenced table. - The other reason is that 'serial' implies 'not null' - that is why your insert statement fails. Dima > > >>I hope, it helps... >> >>Dima >> >>Arunachalam Jaisankar wrote: >> >> >>>This is a multi-part message in MIME format. >>> >>>------=_NextPart_000_0005_01C2D1EE.61998D70 >>>Content-Type: text/plain; >>> charset="iso-8859-1" >>>Content-Transfer-Encoding: quoted-printable >>> >>>Hi all, >>> >>>I would like to have a foreign key column in my table which allows null val= >>>ue also. >>>But the below create table sql command doesn't accept null value for person= >>>_id. >>>How to do in postgres? >>> >>>create table event >>>( >>> event_id serial not null, >>> event_description char(255) , >>> person_id serial , >>> primary key (event_id), >>> foreign key (person_id) >>> references person (person_id) >>>); >>> >>>regards >>>Jai >>>------=_NextPart_000_0005_01C2D1EE.61998D70 >>>Content-Type: text/html; >>> charset="iso-8859-1" >>>Content-Transfer-Encoding: quoted-printable >>> >>> >>> >>> >>> >>> >>> >>> >>>Hi all,>>>>>>I would like to have a foreign key column = >>>in my=20 >>>table which allows null value also.>>>But the below create table sql command doe= >>>sn't=20 >>>accept null value for person_id.>>>How to do in postgres?>>>= >>>>>>create table event>>>
(
&nb= >>>sp;=20 >>>event_id &= >>>nbsp; =20 >>>serial &nb= >>>sp; =20 >>>not null,
event_description &n= >>>bsp;=20 >>>char(255) = >>> =20 >>>,
=20 >>>person_id = >>> =20 >>>serial &nb= >>>sp; =20 >>>,
primary key (event_id),
forei= >>>gn=20 >>>key (person_id)
references pe= >>>rson=20 >>>(person_id)
);>>>regards>>>Jai>>> >>>------=_NextPart_000_0005_01C2D1EE.61998D70-- >>> >>> >>> >>---------------------------(end of broadcast)--------------------------- >>TIP 2: you can get off all lists at once with the unregister command >> (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) >> >> >> > > > >
Re: null foreign key column
От:
Wei Weng <wweng@kencast.com>
Дата:
On Wed, 12 Feb 2003, Vernon Wu wrote: > > 12/02/2003 2:24:49 PM, Dmitry Tkach wrote: > > >You don't want it to be serial - just make it 'person_id in' > > Any reasons? serial is an auto-increment datatype. In your table, you probably want to control what person_id is, rather than letting them mindlessly auto-increment row after row, given it is a foreign key to a Person table. > > > > >I hope, it helps... > > > >Dima > > > >Arunachalam Jaisankar wrote: > >> This is a multi-part message in MIME format. > >> > >> ------=_NextPart_000_0005_01C2D1EE.61998D70 > >> Content-Type: text/plain; > >> charset="iso-8859-1" > >> Content-Transfer-Encoding: quoted-printable > >> > >> Hi all, > >> > >> I would like to have a foreign key column in my table which allows null val= > >> ue also. > >> But the below create table sql command doesn't accept null value for person= > >> _id. > >> How to do in postgres? > >> > >> create table event > >> ( > >> event_id serial not null, > >> event_description char(255) , > >> person_id serial , > >> primary key (event_id), > >> foreign key (person_id) > >> references person (person_id) > >> ); > >> > >> regards > >> Jai