Re: [SQL] create table with default value...
От | Frederic boucher |
---|---|
Тема | Re: [SQL] create table with default value... |
Дата | |
Msg-id | 19991105183316.21030.qmail@hotmail.com обсуждение исходный текст |
Список | pgsql-sql |
Hi, First, thanks a lot for your answer... I had this possibility in mind but I wasn't sure about the security of this... Is it possible for an other thread to request NEXTVAL('u_id_seq') in the middle of the creation of a record? Because if this is possible the unique id would be let's say 1 and the concatenation would be 2 (because the "new" last_value as been changed)...?!?! I'm not sure I'm quite clear ... Second, to answer your question "Why are you wanting this?" it's because, like the create string says, it a DEFAULT value... for lazy, or stupid user that would put anything unusefull in there... But it can be changed too an must me remebered... Isnt it the right way to do this? Third, A simple one : How can I get the current datetime? like a oracle's "select sysdate from dual;" Thanks PS: By the way, where could I find difference, or exact definition, of data type... Like what's the difference between "text" and "varchar" or between "integer" and "int8" (in this case I suppose that integer is "int4" > >Salut Frederic, > >Not quite sure why you'd want to do what you're asking to do but the >following works: > >DROP TABLE prova1; >CREATE TABLE prova1 ( >u_id int8 DEFAULT nextval('u_id_seq'), >name varchar(50) DEFAULT 'NT_' || currval('u_id_seq'), >extra_field text >); > >test=> select * from prova1; >u_id|name|extra_field >----+----+----------- >(0 rows) > >test=> insert into prova1 (extra_field) values ('look'); >INSERT 2217966 1 >test=> insert into prova1 (extra_field) values ('no'); >INSERT 2217967 1 >test=> insert into prova1 (extra_field) values ('hands!'); >INSERT 2217968 1 >test=> select * from prova1; >u_id|name|extra_field >----+----+----------- > 1|NT_1|look > 2|NT_2|no > 3|NT_3|hands! >(3 rows) > >test=> > >The reason why I'm not quite sure why you'd want to do this is that the >field name would be redundant. You could simply obtain it using a select. >e.g.: > >test=> SELECT u_id, 'NT_'::text || u_id::text AS name, extra_field FROM >prova1; >u_id|name|extra_field >----+----+----------- > 1|NT_1|look > 2|NT_2|no > 3|NT_3|hands! >(3 rows) > >test=> > >HTH, > >Regards, > >Stuart. > ______________________________________________________ Get Your Private, Free Email at http://www.hotmail.com
В списке pgsql-sql по дате отправления: