oracle insert, update, delete data

Insert, update, delete data

oracle provides a feature-rich database management statement
contains valid insert data into database insert statements
to update the data update statement
, and delete data when the data no longer use the delete statement

After you change the data must be submitted using the operation, or just kept in memory, others can not view the change

  • Rollback;
    • Rollback
  • commit;
    • submit

An insert data

Preparation Table

SQL> create table person(
  2  id number(9) not null,
  3  name varchar2(40) not null,
  4  age number(9) not null,
  5  info varchar2(50) null,
  6  primary key(id)
  7  );
表已创建。

All fields of the table insert data 1.1

A directed insertion of all values ​​in the table section two kinds of buildings

  • One is to specify all the field names,
  • Another is completely specified field name

grammar

INSERT INTO table_name (column_list) VALUES (value_list);

1 specifies that all field names

When inserting data, do not require the insertion sequence table definition, as long as the values ​​of the same order to ensure the order of the columns can Yu segments

SQL> insert into person(id,name,age,info)
  2  values(1,'Green',21,'lawyer');

已创建 1 行。

2 completely specified field name

A list of values ​​for each field specified column insert, and the order of these values ​​must be defined in order person table in the same field

SQL> insert into person
  2  values(2,'Mary',24,'Musician');

已创建 1 行。

When the need to insert a null value, or can use null keyword '

SQL> insert into person
  2  values(3,'meditation',24,null);

已创建 1 行。

1.2 insert data for the specified field of the table

In the person table, insert a new record, name value Willam, age 20

SQL> insert into person(id,name,age)
  2  values(4,'Willam',20);

已创建 1 行。

1.3 while inserting multiple rows of data

SQL> insert into person(id,name,age)
  2  select 5,'Harry',21 from dual
  3  union all
  4  select 6,'harriet',19 from dual;

已创建2行。

1.4 query results into data

grammar

insert into table_name1(column_list1)
select (column_list2) from table_name2 where (condition)
  • table_name1 specified table data to be inserted
  • column_list1 specify which data to be inserted in the list to be inserted
  • table_name2 specified data is inserted into the table from which the query out
  • column_list2 specified data source query column table. The number of fields in the list must list column_list1 same, the same data type
  • condition specified in the query SELECT clause

Ready person_old table

SQL> create table person_old(
  2  id number(9) not null,
  3  name varchar2(40) not null,
  4  age number(9) not null,
  5  info varchar2(50) null,
  6  primary key(id)
  7  );
表已创建。

Insert data

SQL> insert into person_old(id,name,age)
  2  select 7,'Evans',23 from dual
  3  union all
  4  select 8,'Dale',29 from dual;

已创建2行。

Adding the two data person_old person in

SQL> insert into person(id,name,age)
  2  select id,name,age from person_old;

已创建2行。
SQL> select id,name,age from person;

        ID NAME               AGE
---------- ----------- ----------
         1 Green               21
         2 Mary                24
         3 meditation          24
         4 Willam              20
         5 Harry               21
         6 harriet             19
         7 Evans               23
         8 Dale                29
  • It can be seen from the results, person more than two records in the table
  • id is the primary key column is generally from growing, upon insertion to ensure uniqueness of the field
  • If you are unsure, you can insert of the neglect of the field, just insert the values ​​of other fields

2 Update Data

oracle record update statements using the updated table,
to update a particular row, or to update all the rows

grammar

update table_name
set column_name1=value1,...column_namen=valuen
where (condition);

Update record id value of 6, 25 to the age field, the name field to LiMing

SQL> update person
  2  set age=25,name='LiMing'
  3  where id=6;

已更新 1 行。

Update record value age 19 to 23, the info field value to student

SQL> update person
  2  set info='student'
  3  where age between 19 and 23;

已更新4行。
SQL> select * from person;

        ID NAME                 AGE INFO
---------- ------------- ---------- ----
         1 Green                 21 student
         2 Mary                  24 Musician
         3 meditation            24
         4 Willam                20 student
         5 Harry                 21 student
         6 LiMing                25
         7 Evans                 23 student
         8 Dale                  29

2.2 update contingency table

The person tables and table id column person_old same data info to the person table columns info column

SQL> select * from person_old;

        ID NAME                    AGE INFO
---------- ---------------- ---------- -----------
         7 Evans                    23
         8 Dale                     29

update

SQL> update person_old p1
  2  set info=(select info from person p2 where p1.id=p2.id)
  3  where exists(select 1 from person p2 where p1.id=p2.id);

已更新2行。
SQL> select id,name,info from person_old;

        ID NAME          INFO
---------- ------------- ---------
         7 Evans         student
         8 Dale

SQL>

merge into

The person tables and table id column person_old same data info to the person table columns info column

SQL> Rollback;
回退已完成。

SQL> select * from person_old;
        ID NAME           AGE INFO
---------- ------- ---------- ------
         7 Evans           23
         8 Dale            29
SQL> merge into person_old p1
  2  using person p2
  3  on(p1.id=p2.id)
  4  when matched then
  5    update
  6    set
  7    p1.info=p2.info;

2 行已合并。
SQL> select * from person_old;

        ID NAME        AGE INFO
---------- -------- ------ ----
         7 Evans        23 student
         8 Dale         29
SQL>
Statement description
merge into: Keyword, followed by the name of the main exemplar
using: Used, followed by the data source can be a table, the result may be a set of
on: Keywords, within parentheses after the condition is associated with;
when matched then: I.e., when the condition is satisfied on the updating operation is executed, i.e. with the rear Update;
when not matched then: That is, when the operating condition is not satisfied on, to be performed

3 Delete Data

delete statements were allowed where clause to specify delete conditions

3.1 Delete a single row

SQL> delete from person
  2  where id=7;
已删除 1 行。
SQL> select * from person where id=7;
未选定行

3.2 delete multiple rows of data

SQL> delete from person
  2  where info is null;
已删除3行。
SQL> select * from person where info is null;
未选定行
SQL> delete from person
  2  where id between 4 and 8;
已删除2行。
SQL> select * from person
  2  where id=4;
未选定行

3.3 Delete all data

SQL> select count(1) from person;
  COUNT(1)
----------
         2
SQL> delete from person;
已删除2行。
SQL> select count(1) from person;
  COUNT(1)
----------
         0

3.4truncate clear all the data in the table

SQL> Rollback;
回退已完成。

SQL> select count(1) from person;
  COUNT(1)
----------
         8
SQL> truncate table person;
表被截断。

SQL> select count(1) from person;
  COUNT(1)
----------
         0

3.5truncate and delete the difference

  1. delete a DML statement to be submitted; truncate is DDL statements, you do not need to submit;
  2. delete to delete one or more rows of data, truncate data is for the whole table;
  3. When the large amount of data, delete will be slower, because to write the log, easy data recovery .truncate it quickly, because only the small amount of logging, it is also difficult to restore;

Guess you like

Origin www.cnblogs.com/inmeditation/p/11949651.html