Insert Into Statement Insert Tab A into Slot
Insert Into Statement Insert Tab A into Slot B. DISCOVER! Texas State Technical College
Insert Into Overview The INSERT INTO statement is used to add new records to a table. One INSERT INTO statement will add ONE record to ONE table. Every time an INSERT INTO is called, all the constraints for the table are checked against the incoming values. If they fail, the record is rejected. DISCOVER! Texas State Technical College
Insert Into Syntax There are two forms of the syntax Default Order This form matches values in the list to fields by the default order as established in the CREATE statement. INSERT INTO table_name VALUES (value 1, value 2, value 3, …); Specified Order This form specifies the order in which to match the fields to the values. INSERT INTO table_name (field 1, field 2, field 3, …) VALUES DISCOVER! (value 1, value 2, value 3, …); Texas State Technical College
Insert Into Default Order INSERT INTO table_name VALUES (value 1, value 2, value 3, …); Advantages • Code is shorter. Disadvantages • Must know default order of fields. • Must supply a value for every field. • If a value is not desired, you must use a NULL (empty) keyword. Using a NULL keyword overrides any default values for the field. DISCOVER! Texas State Technical College
Insert Into Default Order INSERT INTO table_name (field 1, field 2, field 3, …) VALUES (value 1, value 2, value 3, …); Advantages • Only have to specify the values you wish to enter into a record. • You do not have to utilize NULLs, thus, any unlisted field will assume its default value if it has one. • The programmer can see the fields he is inputting data and can easily match them up. Disadvantages • Code is longer. DISCOVER! Texas State Technical College
Insert Into For Example… If my default order in the table engine is as follows: engine. ID, volume, cylinders, max_RPM, max_torque In this example, we know all the values for every field and we must specify them for each field. INSERT INTO engine VALUES (‘SVT-V 8’, 4. 6, 8, 350, 2200); In this example, we don’t know the RPM or torque, but we must specify a value so we are forced to use NULL (meaning there is no value). This would override any default values on the fields. INSERT INTO engine VALUES (‘COPELLAS-m 2’, 3. 2, 4, NULL); DISCOVER! Texas State Technical College
Insert Into For Example… If my default order in the table engine is as follows: engine. ID, volume, cylinders, max_RPM, max_torque In this example, we know all the values for every field and we must specify them for each field. INSERT INTO engine (engine. ID, volume, cylinders, max_RPM, max_torque) VALUES (‘SVT-V 8’, 4. 6, 8, 350, 2200); In this example, we don’t have to specify the RPM and torque. Since we don’t specify NULL, default values will apply. INSERT INTO engine (engine. ID, volume, cylinders) VALUES (‘COPELLAS-m 2’, 3. 2, 4); DISCOVER! Texas State Technical College
Insert Into In Summary… • The INSERT INTO statement adds one record to one table. • It has two forms: • Default Order, where you must match the values list to the table default order. INSERT INTO table_name VALUES (value 1, value 2, value 3…); • Specified Order, where you specify which fields and in what order. INSERT INTO table_name (field 1, field 2, field 3…) VALUES (value 1, value 2, value 3…); DISCOVER! Texas State Technical College
- Slides: 8