Question on INSERT statement

Поиск
Список
Период
Сортировка
Искать

Question on INSERT statement

От:
Geoffrey KRETZ <gk@4js.com>
Дата:
Hello,

I'm wondering if the following behaviour is the correct one for 
PostGreSQL (7.4 on UNIX).

I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching 
the following request :

INSERT INTO temp_tab VALUES (1,2,3)

It  will insert the values in the three first row whereas with informix 
or db2 for exemple, it will return an error.

So is that normal ?

Geoffrey Kretz - Four J's Development Tools

Re: Question on INSERT statement

От:
Geoffrey KRETZ <gk@4js.com>
Дата:
Richard Huxton wrote:

> Geoffrey KRETZ wrote:
>
>>
>> I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a 
>> launching the following request :
>>
>> INSERT INTO temp_tab VALUES (1,2,3)
>>
>> It  will insert the values in the three first row whereas with 
>> informix or db2 for exemple, it will return an error.
>>
>> So is that normal ?
>
>
> Well, it's normal in the sense that other installations of PG will do 
> the same thing (and it's documented in the INSERT page of the 
> manuals). Whether it is desirable or according to the SQL standards is 
> another matter.
>
> Anyone with a copy of the specs know what they say?


I think it's that, isn't it :

        3) (...)If the  is omitted, then an 
            that identifies all columns of T in the ascending sequence of
            their ordinal positions within T is implicit.

Here's the complete SQL92 specifications about the insert clause, I 
don't know if sthing has change with SQL99 spec

"13.8 

         Function

         Create new rows in a table.

         Format

          ::=
              INSERT INTO 
                

          ::=
                [    ]
              
              | DEFAULT VALUES

          ::= 


         Syntax Rules

         1) The table T identified by the 
shall not be a read- only table. 2) An that specifies DEFAULT VALUES is equivalent to an that specifies a of the form VALUES (DEFAULT, . . . ) where the number of "DEFAULT" entries is equal to the number of columns of T. 3) No of T shall be identified more than once. If the is omitted, then an that identifies all columns of T in the ascending sequence of their ordinal positions within T is implicit. 4) A column identified by the is an object column. 5) Let QT be the table specified by the . The degree of QT shall be equal to the number of s in the . The column of table T identified by the i-th in the corresponds with the i-th column of QT. 6) The Syntax Rules of Subclause 9.2, "Store assignment", apply to corresponding columns of T and QT as TARGET and VALUE, respec- tively. Access Rules 1) Case: a) If an is specified, then the applicable shall include INSERT for each in the . b) Otherwise, the applicable privileges shall include INSERT for each in T. Note: The applicable privileges for a
are defined in Subclause 10.3, "". 2) Each in the shall identify a column of T. General Rules 1) If the access mode of the current SQL-transaction is read-only and T is not a temporary table, then an exception condition is raised: invalid transaction state. 2) Let B be the leaf generally underlying table of T. 3) The is effectively evaluated before inserting any rows into B. 4) Let Q be the result of that . Case: a) If Q is empty, then no row is inserted and a completion con- dition is raised: no data. b) Otherwise, for each row R of Q: i) A candidate row of B is effectively created in which the value of each column is its default value, as specified in the General Rules of Subclause 11.5, "". The candidate row includes every column of B. ii) For every object column in the candidate row, the value of the object column identified by the i-th in the is replaced by the i-th value of R. iii) Let C be a column that is represented in the candidate row and let SV be its value in the candidate row. The General Rules of Subclause 9.2, "Store assignment", are applied to C and SV as TARGET and VALUE, respectively. iv) The candidate row is inserted into B. Note: The data values allowable in the candidate row may be constrained by a WITH CHECK OPTION constraint. The effect of a WITH CHECK OPTION constraint is defined in the General Rules of Subclause 11.19, "". Leveling Rules 1) The following restrictions apply for Intermediate SQL: a) The leaf generally underlying table of T shall not be gen- erally contained in the immediately contained in the except as the of a . 2) The following restrictions apply for Entry SQL in addition to any Intermediate SQL restrictions: a) The that is contained in an shall be a or it shall be a that contains exactly one of the form " ", and each of that shall be a . b) If the data type of the target identified by the i-th is an exact numeric type, then the data type of the i- th item of the shall be an exact numeric type. c) If the data type of the target C identified by the i-th is character string, then the length in characters of the i-th item of the shall be less than or equal to the length of C. d) The shall immediately contain a ."

Re: Question on INSERT statement

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Geoffrey KRETZ  writes:
> I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching 
> the following request :
> INSERT INTO temp_tab VALUES (1,2,3)
> It  will insert the values in the three first row whereas with informix 
> or db2 for exemple, it will return an error.

This is an extension to the spec - per SQL92 it would be right to throw
an error.  However Postgres has always behaved this way and we're not
likely to change it.  (I think it's a hangover from PostQUEL.)  Many
people find it to be a convenient behavior.

If you want an error then specify an explicit column list:

	INSERT INTO temp_tab (f1,f2,f3,f4,f5) VALUES (1,2,3)

			regards, tom lane
FAQ