|
|
Inserting Data
The INSERT statement is used to insert new rows into existing tables.
Values to be inserted may be specified explicitly, as constants or expressions, or in the form of a subselect, see below.
The data to be inserted must be of a type compatible with the corresponding column definition.
If the length of the inserted data differs from that of the column definition, the data is handled as follows:
Inserting Explicit Values
The explicit INSERT statement has the general form:
INSERT INTO table [(column-list)] VALUES (value-list);Values in the value-list are inserted into columns in the column-list in the order specified.
The order of columns in the column-list need not be the same as the order in the table definition. Any columns in the table definition which are not included in the column-list are assigned NULL values, or the column default value if one is defined.
An explicit INSERT statement can only insert a single row.
Insert the values 'SUTB' and 'SUITE WITH BATH' into the ROOMTYPE and DESCRIPTION columns respectively into the ROOMTYPES table:
INSERT INTO ROOMTYPES (ROOMTYPE,DESCRIPTION) VALUES ('SUTB','SUITE WITH BATH');
ROOMTYPE DESCRIPTION SUTB SUITE WITH BATHIf you insert explicit values into all of the columns in a table, the column list can be omitted from the INSERT statement. The values specified are then inserted into the table in the order that the columns are defined in the table.
Thus the example above could also be written:
INSERT INTO ROOMTYPES VALUES ('SUTB','SUITE WITH BATH');Inserting Results of Expressions
You can also insert the result of an expression into a table:
INSERT INTO ROOM_PRICES VALUES ('LAP', 'SUTB', CURRENT_DATE, CURRENT_DATE + INTERVAL '32' DAY, 500 + 40);Result:
HOTELCODE ROOMTYPE FROM_DATE TO_DATE PRICE LAP SUTB 1997-08-22 1997-09-23 540Inserting with a Subselect
Values to be inserted can also be specified in the form of a subselect, i.e. fetched from another table in the database.
INSERT INTO ROOMSTATUS SELECT DISTINCT ROOMNO, 'KEY OUT' FROM BOOK_GUEST WHERE CHECKIN IS NOT NULL AND CHECKOUT IS NULL;The same table cannot be listed in the subselect's FROM clause that is listed in the INSERT INTO clause - data cannot be selected from a table for insertion into the same table.
Inserting the result of a subselect can insert a number of rows into a table. If any of the rows are rejected (e.g. because of a duplicate primary or unique key), the whole INSERT statement fails and no rows are inserted.
Inserting Sequence Values
The value to be inserted can be the value of a sequence. The constructs that return the current value or next value of a sequence can be used as column values in the INSERT statement:
INSERT INTO BOOKGUEST (ROOMNO,KEYCODE) VALUES ('SKY123',NEXT_VALUE OF KEYCODES_SEQUENCE); INSERT INTO BILL (CHARGE_PERIOD_NO,COST) VALUES (CURRENT_VALUE OF CHARGE_PERIOD_NO_SEQUENCE,400);Inserting NULL Values
The keyword NULL may be used to insert the NULL value into a column, provided that the column is not defined as NOT NULL:
INSERT INTO EXCHANGE_RATE (CURRENCY,RATE) VALUES ('XYZ',NULL);The NULL indicator is implicitly inserted into columns when no value is given for that column and the column definition does not include a default value.
Thus, the following INSERT statement will give the same results as the example above:
INSERT INTO EXCHANGE_RATE (CURRENCY) VALUES ('XYZ');
|
Upright Database Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 dbtechnology@upright.se |
|
|