Oracle database usage notes

Commonly used SQL statements

Involved system tables user_tables, user_col_comments, user_tab_comments
Note: The table name is case sensitive, otherwise it is impossible to find out
the table information of the database connected through the system table query

  • Query all table names: select t.table_name from user_tables t;
  • Query all field names: select t.column_name from user_col_comments t;
  • Query all the field names of the specified table: select t.column_name from user_col_comments t where t.table_name ='table name';
  • Query all field names and field descriptions of the specified table: select t.column_name, t.column_name from user_col_comments t where t.table_name ='table name';
  • Query the table name and table description of all tables: select t.table_name, f.comments from user_tables t inner join user_tab_comments f on t.table_name = f.table_name;
  • 查询模糊表名的表名和表说明:select t.table_name from user_tables t where t.table_name like ‘ACCOU%’;
    或者select t.table_name,f.comments from user_tables t inner join user_tab_comments f on t.table_name = f.table_name where t.table_name like ‘ACCOU%’;
  • Query table data number, table name, Chinese table name: select a.num_rows, a.TABLE_NAME, b.COMMENTS from user_tables a, user_tab_comments b WHERE a.TABLE_NAME = b.TABLE_NAME order by TABLE_NAME;

SQL INSERT INTO statement

  • INSERT INTO table name VALUES (value 1, value 2, value 3, value)
  • Copy all the columns from one table and insert them into another existing table: INSERT INTO table2 SELECT * FROM table1
  • Copy only the desired column and insert it into another existing table: INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

SELECT data record filter

  • Query a single value: select * from data table where field name = field value order by field name
  • Fuzzy query: select * from data table where field name like'% field value' order by field name
  • Query the first 10 data that meet the conditions: select * from table name where rownum<=10;
  • select * from table name where rownum<=10 order by field name desc
  • Query multiple eligible values: select * from data table where field name in ('value 1','value 2','value 3')
  • Query data within the range of field values: select * from table name where field name between'value 1'and'value 2'
  • Filter field length The length (field) function can determine the length of a field: select * from tbName where length(nvl(alias,''))<= 4 length( nvl(str1,'')) When using the nvl function to filter Filter out the value that results in null
    Example: Actually use SELECT * FROM SYS_EMP WHERE length(EMP_NO)<9;
  • Query data information that is not within a certain range in oracle: select * from table name where field not in ('value'); null values ​​are filtered out in the not in clause, in order to avoid null values ​​in the not in subquery and affect the query results , You can perform non-empty filtering on the results of this part of the subquery. As follows: Select * From A where A.col not in (Select B.col from B where B.col is not null)

UPDATE data update record

  • update data table set field name = field value where conditional expression
  • update data table set field name 1 = field value 1, field name 2 = field value 2 where conditional expression

DELETE delete statement

  • delete from data table where conditional expression
  • delete from data table (delete all records in the database table)
  • drop table test is to remove the entire table. The data inside disappears

INSERT insert statement

  • insert into data table (field 1, field 2, field 3...) values ​​(value 1, value 2, value 3...)
  • insert into the target data table select * from the source data table (add the records of the source data table to the target data table)

Data recording statistical functions

  • AVG (field name) to get the average value of a table column
  • COUNT (**|Field name) Statistics on the number of rows of data or statistics on data with a value in a column. Note: Enter a *
    select count(*) from table name in parentheses ; count the number of rows queried
  • MAX (field name) to obtain the largest value of a table column
  • SUM (field name) adds the value of the data column
  • MIN (field name) obtains the smallest value of a table column.
    Example: Other use consistent
    select sum (NEWVENDOR) from ANDY where NEWVENDOR between '1000345' and '1000366'; Add the NEWVENDOR value in this range and output the total

Multi-table query, join query

  • Inner join query: implicit inner join select * from A, B where conditional implicit join uses alias: select * from A alias 1, B alias 2 where alias 1.xx=alias 2.xx; display inner join select * from A The inner join B on condition (inner can be omitted) shows the alias used for the connection: select * from A alias 1 inner join B alias 2 on alias 1.xx=alias 2.xx
    Example: SELECT * FROM category c,product p WHERE c.cid =p.category_id;

  • External connection: There are two ways of external connection, one is the left outer connection and the other is the right outer connection. External connection problem: When the condition is not satisfied, then any desirable to include records in results is not satisfied
    left outer connection: where d.deptno = e.deptno When not satisfied, then any of the information representative of the left operand table is included
    wording : where d.deptno=e.deptno(+)
    Right outer join: where d.deptno=e.deptno When not true, the information of the table represented on the right side of the equal sign is still included.
    Writing: where d.deptno(+)= e.deptno

Date problem when querying data in TOAD

  • If the type of your time column is date type, just
    select to_char(time degree,'yyyy-mm-dd hh24:mm:ss') from table
  • If it is a character type, you can only split the time, use substr
    select substr(time,7,2)||'-'||substr(time,4,2)||'-'

update two tables associated update

  • update table1 a set a.c = (select b.b from table2 b where a.a=b.a)
    例如:update BAT_TOOLING_STORE B set B.STORE_CODE=(select s.STORECODE from STORE s where s.STORENAME=B.STORE_CODE)

Find whether there are duplicate values ​​in a certain condition in Oracle

  • select field value from table group by field value having(count(field value))>1
    Example:
    select STORENAME from STORE group by STORENAME having(count(STORENAME))>1

Create table and delete fields, etc.
To add a column to the table, please use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in the table, please use the following syntax:
ALTER TABLE table_name
DROP COLUMN column_name
To change the column in the table Data type, please use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
to add a field to the
table 1, alter table table name ADD field type baidu NOT NULL Default 0

2、ALTER TABLE employee ADD spbh varchar(20) NOT NULL Default 0

3. Add spbh type to the table employee, varchar size 20 is not empty, the default value is zhi0

ALTER TABLE G_LITEON_FIFO_REELID ADD AREA varchar(20)

Change the type length of the Oracle database table field
Add a temporary field of varchar2 type
alter table A add new_temp varchar2(10);
backup the field a
update A set new_temp = a;
transaction
commit commit;
clear the field a data
update A set a = ' ';
Transaction
commit commit;
At this time the a field is no data
alter table A modify a varchar2(10);
move the data of the temporary field over
update A set a = new_temp;
transaction
commit commit;
drop the temporary field
alter table appr_control_info_ex drop column new_temp;

Guess you like

Origin blog.csdn.net/caoguanghui0804/article/details/105834099