oracle routine operations (1)

1. Create a table
create table test(
  id varchar2(10),
  age number
);

2. Backup table
create table
as
select * from test group by id;
 
3. Delete table
drop table test;--delete table structure and table data

4. Clear the table
truncate table test;--Clear the data table data, there is no room for return
delete from test;---clear the data table data, there is room for return

 

5. Add fields

alter table test add (
  name varchar2(10),
  interest varchar2(20)
);

6. Delete fields
alter table test drop name;

7. Update data

7.1 Update a piece of data

update test t
set t.id='001'
where t.id is not null;
commit;

 

7.2 Updating multiple pieces of data from other tables

update test t set t.name=(
  select tm.name
    from test_common tm
  where t.id=tm.id
);
commit;
--Note: When updating data, it is best to create a separate table for the sql in the update subquery to improve the update speed.

 

7.3 After querying the data in PL/SQL, directly enter the editing mode to change the data

select * from test for update;
--Note: After the update operation is completed, the data table should be locked, and the commit operation should be performed at the same time.

 

8. Query data

select * from test;
 
9. Query the number of data tables
select count(0) from test;
--Note: count(0) or other numbers save database resources more efficiently and quickly than count(*)

10. Insert data

10.1 Insert multiple fields in a piece of data

insert into test (C1,C2) values(1,'Technical Department');
insert into test(C1,C2) select C1,C2 from test_common;
commit;

 

10.2 Insert multiple pieces of data

insert into test(
  id,
  name,
  age
)
select
  id,
  name,
  age
from test_common;
commit;
--Remarks: 1. When inserting multiple pieces of data, there are no values ​​after the insert into statement, and the data query statement is directly spliced; 2. After insert, update, delete and other operations are performed on the data table in oracle, commit is required, otherwise It is forbidden to operate the database at the system, because the database has been locked at this time. This is a preventive mechanism for the database to prevent confusion caused by multiple people modifying the database data at the same time.




 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481014&siteId=291194637