Detailed oracle

Oracle database management system

1. Introduction to Database

Data warehousing, data management software. Oracle database users with different rights management of
benefits:
support data type
document storage security
support multi-user access to
support large volumes of data
services to :( instance orcl)
OracleServiceORCL Oracle core services
OracleORCLTNSLinstener Oracle serviced external access
Oracle popular clients:
①SQL PLUS
②iSQL PLUS (HTTP: // localhost: 1158 / EM)
③ third-party clients such as: PLSQL, Navicat

2. sql - simple queries, de-emphasis, sort, query conditions

Structure Query Language
① simple queries.
Noun: table (Table), field (Column), row (Row), the primary key (Primary key), user (Account)
syntax: select the column name 1, column name 2, ... from the table;

a, part of the column query:
SELECT field 1, field 2 from the table;

b, query all columns:
the SELECT field 1, field n from table ...; (actual development application and readable)
the SELECT * from table;

C, rename query result fields (alias)
SELECT 1 AS column name alias 1, 2 as the column name alias table 2 from;

d, query results string concatenation ( '||')
SELECT FIRST_NAME || '·' || last_name from the Employees;

e, the results of the query do arithmetic (+ - * /)
SELECT * 10 from the Employees the salary;

f, increases filters
SELECT the salary form the Employees WHERE the employee_id = 100;
② deduplication.
SELECT name from table distinct columns;
. ③ sorting
select * from table order by field 1 asc / desc, field 2 asc / desc; (first pair of field Sort 1, if the same, then for the field sort 2)
. ④ query condition
a, the equivalent query
select * form employees where salary = 7000 ;

b, 多条件查询
    select * form employees where employee_id = 100 and salary = 7000;
    select * form employees where salary = 2300 or salary = 7000;

c, 不等值查询(逻辑判断:>  <  >=  <=  <>不等于  !=不等于)
    select * from employees where salary > 1000;        

d, 区间查询( 闭区间 [临近小值, 临近大值] )
    select * from employees where salary >= 1000 and salary <= 10000;
    select * from employees where salary between 1000 and 10000;

e, is null 、is not null
    select * form employees where commission_pct is null;

select * form employees where commission_pct is not null;

f, 枚举查询(in)
    select * from employees where employee_id in(100, 103, 104);
    也可以用or语句
g, 模糊查询 (like 、 not like ) 、(‘_’占位符, ‘%’通配符)
    select * from employees where first_name is not like ‘%L%’; (名字不含L)

select * from employees where first_name is like '_ _ _ _%'; (name length is at least 4)

3. sql - dual, sysdate, systimestamp, a single row (group) function, sql writing (execution) sequentially

① special keyword Dual, SYSDATE, SYSTIMESTAMP.
Dual: virtual table, a row of Table
1. The standing angle data, meaningless
2. The effect is to maintain the integrity of the sql statement

    例如:
    select 1000*1000 from dual;

sysdate: 当前系统时间(年 月 日 时 分 秒)
    select sysdate from dual;
systimestamp: 时间戳,也是当前系统时间 (年 月 日 时 分 秒 毫秒)
    select systimestamp form dual;

② function - one-way function
of each piece of data to process a query function

常用:
    to_char( 被转化的日期,’日期格式关键词’);             将日期转化为字符串
    to_date( ‘被转化的日期字符串’,’日期格式关键词’);    将字符串转化为日期

Date format Keywords: yyyy (years), mm (month), dd (day), hh24 (when the 24-hour clock), mi (points), ss (seconds), day (week)

用法:
    select to_char(sysdate, ‘yyyy-mm-dd’) from dual;        查询日期转化为日期字符串:年 月 日
    select to_date(‘2001-3-23’, ‘yyyy-mm-dd’) from dual;    将日期字符串转化为日期
    select to_char(to_date(‘2000-12-2’, ‘yyyy-mm-dd’), ‘day’) form dual;    将日期字符串最后转为日期:星期
    

③ Function - function group
query table data packet, generating a result for each set of data processing

常见组函数:
    max(字段)         所查询字段的最大值
    min(字段)         所查询字段的最小值
    avg(字段)             所查询字段的平均值
    sum(字段)         所查询字段的总和
    count(字段 或 *)       所查询字段的行数
注意:组函数对null不做任何统计,自动忽略

分组:
    语法:group by 字段名1,字段名2;
    注意: ①group by 在where条件筛选之后
            ②select后可以查询分组的依据字段,组函数的统计(其他不能查询)

分组的过滤:
    语法:group by 字段 having 条件;
    注意: ①where是在分组前对数据过滤
            ②having是在分组后对数据惊醒过滤
            ③如果where和having同时满足查询要求时,优先选择where

. ④ sql statement order
① the order written: SELECT ... ... WHERE from HAVING ... ... ... Group by Order by [ASC / desc]
② execution order: from (determination table) ---> WHERE (before packet filter) ---> group by (packet) ---> having (after packet filter) ---> select (query) ---> order by (sort)

4. sql - subqueries pseudo column paging query, connection tables

①. Subqueries
subqueries instead of a table, the attribute value

① subquery result is a line and first column
For example: query highest wage employee information
the SELECT * from the Employees the WHERE salary = (the SELECT max (salary) from the Employees);
② sub-query results is n rows and one column
for example: query and "king" and a name in the employee information in the same department
SELECT * from DEPARTMENT_ID in the employees WHERE (SELECT WHERE last_name = DEPARTMENT_ID from the employees 'King');
③ subquery result n rows and n columns
Example: query pay before five to ten employee information between the bit
SELECT * from (SELECT the employee_id, FIRST_NAME, last_name, AS rownum from RN (SELECT * from the employees Order by the salary desc)) WHERE RN> = RN. 5 and <= 10
②. pseudo column paging query
dummy column:
physical address rowid row, does not exist in any form, can query
rownum result data query satisfying conditions numbered (starting from 1)
usage:
SELECT rowid, rownum, other fields from table;
page:
using the sub-queries, Discover all table data together with sequence (rownum) as a temporary table

For example:
① salary fifth to tenth place query employee information from
** to press wage in descending order, form a temporary table plus the number field
select * from (select employees_id, first_name , last_name, salary, rownum as rn from (select * from employees order by salary desc) ) where rn> = 5 and rn <= 10

② adding table number
SELECT * the Employees, AS rownum RN from the Employees;.
③ connection table.

4. The multiple tables connected to a processing table (on the rear from)

  1. Inner connecting
    Keywords: ... (inner) join ... on inner omitted
    Syntax: Table 1 (inner) join in Table 2. Table 1.xxx = ON TABLE 2.xxx
    features: ① consolidated Table 1 and Table 2 also exists corresponding present ② does not retain data in table 1 corresponding to the presence and absence of data tuples (outer connector comparative) in table 2
  2. An outer connector
    ① The left even
    Keywords: left (outer) join ... on outer omitted
    syntax: Table 1 left join Table 2. Table 1.xxx = ON TABLE 2.xxx
    characteristics: Table 1 and Table 2 were combined, and tables retained 1 and table 2 corresponds to the presence tuple does not exist, and its complement is empty
    ② right outer even
    syntax: table 1 right join table 2 ON conditions
  3. Extended
    if there are more than two tables are connected, a first connection table becomes two, and then connected to a next turn is connected

    5. Table (data types, constraints), modified table structure

    Create a table: create table table name (field name data type [constraints]);
    delete tables: drop table table name;

oracle table comprising: a table name, the field, the data type, [bound]
wherein:
(1) data type
① digital
number default length is 38, the fractional length 0
number (n-) the maximum length of n-
number (n-, m) maximum length It is n, which is the fractional part of the length m
Integer corresponds Number (38 is)
② string
char (n) fixed length character string is n, supplemented by whitespace insufficient
varchar2 (n) the maximum length of the string is n, the length of fill the string length
Note: high char data management efficiency, but whitespace wasted space, varchar2 space saving
Example: name varchar2
phone number char (. 11) (because they do not make and compare)
mailbox varchar2
③ date
date accurate to the second
embodiment: insert into table (date attribute) values (to_date ( '1999-8-9' , 'yyyy-mm-dd'));

(2) constraint
① Key primary key constraint Primary
(non-null, unique)

②唯一约束   unique
    (表明列值不重复,例如手机号)

③非空约束   not null
    (表明列值不为空)

④检查约束   check(约束语法)
    例如: --- phone_num char(11) check(length(phone_num) = 11)
            --- email varchar2(100) check(email like ‘%@%’)
            --- sex char(2) check(sex in(‘男’, ‘女’))
    
⑥外键约束(forienkey)        references 其他表名(字段名)
    例如:  --- stu_class char(20) references classes(class_id),

(3) modify the table structures:
① editing table fields:
Syntax: ALTER TABLE table name the MODIFY (column name, data type);
  eg1a: ALTER Modify Table skate_test (author Number (10,0))
the length of time the modified column, only edit existing fields than the length of the actual deposit larger
eg2: alter table skate_test modify (author varchar2 (10))
in the modified data type of the column when the column must be modified blank
to increase a column ②:
syntax: ALTER tABLE table name ADD (column name, data type);
EG1: ALTER tABLE skate_test ADD (author NUMBER (38,0) Not null);
③ reclassified name:
syntax: ALTER tABLE table name RENAME cOLUMN TO new column current column name;
EG1: ALTER tABLE skate_test RENAME the cOLUMN author the TO authorer_new
④ delete a column:
syntax: ALTER tABLE table dROP cOLUMN column name;
EG1: the ALTER the table skate_test drop column author
⑤ to change the name of the table:
syntax: ALTER tABLE current table name RENAME TO The new table name;
EG1: ALTER Table skate_test to the rename test_sakte
⑥ footnote to Table:
Comment column ON table column name is 'footnotes'; // note column modified table.
the COMMENT the ON TABLE MOVO_NEW.TEST_SAKTE the IS 'footnotes'; // Notes modify the table

6. view, sequence index

Delete: drop view / sequence / index view name / sequence name / index

① view: query results as a view for queries using the virtual tables
create a view: create view view name as select statement;
see the view: select column names from the view name;
advantages: ① simplify SQL statements ② security can be achieved on development who shielded the original table and field names
Note: view does not improve query performance, is essentially stored select statement, basically do not take up hard disk space

② Sequence: oracle tools provided, resulting in a series of consecutive non-repeating value;
creating a sequence: create sequence sequence name [start with digital] // not write start, the default from the beginning;
obtain sequence values: sequence name .nextval

例如:
    create sequence seq_keyValue start whith 100001;
    …
    insert into employee(id, name) values(seq_keyValue.nextval, ‘xyz’);

③ index:
create index: create index index name on table name (field names);
the use of sequence: do not need a manual to use, the default will use the index query field conditions, then the data will automatically use the index to find data
features:
① itself disk space occupied
② to add, delete, change, there will be maintenance, reducing the efficiency of DML
⑤ primary key (primary key) and the only (unique) database will automatically add an index

7. affairs

Concept: is the smallest unit of the database, there are multiple sql statements in a whole
the results: the implementation of a sql statement to a function, either all succeed, or all fail
successfully concluded: commit submit
ended in failure: rollback Rollback
realization of the principle:
① the database is assigned a separate rollback (temporary space - rollback seagment) for each client
②commit is the result of the implementation of the rollback sql-time synchronized to the database; rollback rollback is to give up in the results

Transaction characteristics:
A (Atomic) atomic: sql as a whole to a
C (Consistency) Consistency: Whether or not executed successfully, the data are reasonably
I (Isolation) Isolation: between the transaction and the transaction is independent of
D ( Durability) persistence: end of the transaction, data modification is permanent

8. Copy table structure, designed to add bulk, data replication

(1) copy the table structure
Create Table NewTable AS select * from TableName WHERE 1 = 2;
(2) adding bulk design
. Insert into table select sequence name .nextval, T * from
(select field 1, field 2, ... from dual
union select field 1, field 2, ... from Dual
union select field 1, field 2, ... from Dual
...
) T;
added: copying data t_text1 table into t_text2 table
insert into t_text2 (id2, name2, sex2) select id, name, sex from t_text1;

Guess you like

Origin www.cnblogs.com/linanana/p/12107399.html