1. Insert Insertion Statement: Syntax : INSERT into Tablename[column1[,column2 ...] VALUES (Value1[,value2 ...]); Description INSERT INTO: the keyword to insert. TableName: Represents the table to be inserted. Column1: Optional parameter that represents the column to be inserted, with multiple columns used, delimited. VALUES (value1.) : The inserted value, value1 here, must correspond to the preceding column, and if the column is not written, it must be consistent with the structure of the table. Cases:
| --Create a Learner information table and add constraintsCREATETABLEStuinfo (stunameVARCHAR2 (20)NotNullCONSTRAINT Pk_namePRIMARYKEY, StupassVARCHAR2 (20)not null constraint Ck_ Pass check (LENGTH (stupass) > 3), stuage number (3,not null constraint ck_age CHECK (Stuage>18), birthday DATE default Sysdate) |
inserting data into the Learner information table
--Insert a record into the student tableINSERTIntoStuinfo (Stuname,stupass,stuage,birthday)VALUES (‘Tom‘,‘123123 ", 20,) -- Add data for a custom date type insert into Stuinfo values ( John Doe " 123456 ', 25,to_date ( 1999-9-20 ", |
Precautions: 1) The inserted column must correspond to the inserted value one by one, including the data type, number, and order must be identical. 2) If the data table contains default values, you can use the default keyword to insert defaults. 3) The inserted data must meet the data constraints. Otherwise, the insert fails. 4) inserted columns can be omitted, but data must be inserted in the order of the columns in the table. 5) Complete the addition of date function data through to_date. 6) Insert String type must be used ' included. To insert multiple rows of records: (1, insert: Into...select ... Generate Custom Data
| --Insert multiple rows of data at one time and query through Union keywordINSERTIntoStuinfo (Stuname,stupass,stuage,birthday)SELECT‘Harry‘,‘888888‘,20,to_date ( ' 1999-8-8 ' 25,to_date ( 195-8-20 ", from dual |
Splicing multiple query results one at a time into the database, where the type data queried must be exactly the same as the type and order of data inserted. (2, insert: Into...select ... Inserting records that already exist in a table into a new table
| INSERT into stuinfo (stuname,stupass,stuage,birthday) SELECT name,pass,age,sysdate from backuser |
Inserts the result of the query into an existing table and an error if the table does not exist. (3, Select...into ...
--Generate a new table for the data you are querying CREATE TABLE backuser as SELECT stuname,stupass,stuage,birthday from stuinfo |
Insert the result of the query into a new table, the table must not exist, if there is an error. |