Oracle Database Learning Essay

1. Oracle database tablespace
1. Create tablespace:
create tablespace HZT datafile 'D:/ora/oradata/orcl/HZT.dbf'
size 15M autoextend on next 5M maxsize 50M;
2. Delete tablespace:
drop tablespace HZT including contents and datafiles
2. Oracle user
1. Create user – Create the user
create user HZT
identified by “”
default tablespace HZT
temporary tablespace TEMP
profile DEFAULT
password expire;
2. Assign user rights

Visual operation:
insert image description here
Assign link permissions connector Link roles:
– Grant/Revoke role privileges
grant connect to HZT;
insert image description here
Assign system permissions
– Grant/Revoke system privileges
grant create any index to HZT;
grant create any sequence to HZT;
grant create any synonym to HZT ;
grant create any table to HZT;
grant create any view to HZT;
grant unlimited tablespace to HZT;
insert image description here
Three data tables
Create table
create table TEST
(
ID NUMBER,
NAME VARCHAR2(10),
AGE NUMBER
)
delete data table
drop table table_name;
commit ;
View table structure command
desc table_name;

4. Constraints
primary key constraints
unique constraints
non-null constraints
foreign key constraints

5. SQL learning
1. SELECR basic query statement
select * from table_name
where ... limit the rows that meet the query conditions
group by ...
keywords cannot be split, and the format is not case-sensitive. *
2. Arithmetic expressions in the select statement:
* / + - ( )
Calculate from left to right, first calculate the value in the brackets, then multiply and divide, and then add and subtract.
Note: The multiplication and division of the select statement can only calculate numeric characters, and cannot directly multiply and divide date types;
column names cannot be abbreviated;
column aliases: can be named with as or spaces, and if special characters such as @ spaces are included, double quotes need to be added on both sides of the alias ;
distinct deduplication;
|| connector;
3. String and date
String and date values ​​should be placed in single quotes
, character values ​​are case-sensitive, date value format is sensitive, and
the default format of the date is DD-MON-RR
Chinese version Compared with the English version of Oracle, there is a difference in the format of the month of the date. The Chinese version uses January, and the English version uses the abbreviation of the month. For example: January Jan.
4. Comparison conditions
< less than, = equal to, > greater than, <= less than or equal to, >= greater than or equal to, <> not equal to, != not equal to, ^= not equal to.
BETWEEN... AND ... between two values ​​(inclusive); [equivalent to >= lower limit and <= upper limit].
IN(set) matches an arbitrary list of values, which is actually transformed into a set of or conditions by the Oracle server: a in (value1, value2) is equivalent to a=value1 or a=value2.
LIKE matches a character template, use % _ for fuzzy query, '% condition %', escape escape characters, such as like 'SA_%' escape ''.
IS NULL is a null value.
IS NOT NULL is not a null value.
The select statement cannot directly query percentages, and percentages need to be converted to decimals.

5. The logical operation
AND parallel conditions are true at the same time
OR, if one condition is met, then
NO can be reversed

6. Priority rules
insert image description here
7. Order by sorting
The result set is sorted, and the sorting of the columns not included in the select clause can also be used in the sorting.
order by clause query, ASC ascending order by default, DESC descending order.
order by at the end of the select statement, followed by the sorted columns
For null values, ascending order is last and descending order is first.
You can sort by the serial number of the column, you can also sort by the alias of the column, and you can also specify multiple columns for sorting;

Six, multi-table query
Cartesian product:
Cartesian product of two sets X and Y, also known as direct product, expressed as X × Y, is the first object is a member of X and the second Objects are all possible ordered pairs of a member of Y.
Suppose set A={a,b}, set B={0,1,2}, then the Cartesian product of the two sets is {(a,0),(a,1),(a,2),( b,0),(b,1),(b,2)}.

Similar example: If A represents the set of students in a school, and B represents the set of all courses in the school, then the Cartesian product of A and B represents all possible course selections.
1. Inner join (inner join)
Equivalent connection
select table1.column1, table2.column2 from table1, table2 where table1.column1 = table2.column2
Non-equivalent connection
Non-equivalent connection is a connection that does not use equality (=) conditional query. Such as !=, >, <, >=, <=, between... and... etc.;
select * from table1, table2 where table1.column1 > a
self-connection
Use a table to connect itself.
select * from table1 a, table1 b where a.column1 = b.column2
[ select worker.last_name “manager name”, manager.last_name “worker name” from employees worker, employees manager where
manager.manager_id =worker.employee_id ]

The performance of CROSS JOIN and INNER JOIN in MySQL is exactly the same, both can specify the ON condition
2. Outer join (outer join)
Outer join means to query the data that meets the join conditions and also includes orphan data. (Orphan data refers to the data whose column value is empty)
Left (outer) join, left (outer) join on:
contains the orphan data of the left table, that is, the left table is fully displayed, and the right table contains null values
​​select * from table1 t1 left outer join table2 t2 on t1.column1=t2.column2
* right (outer) join, right (outer) join on:
contains the orphan data of the right table, that is, the right table is fully displayed, and the left table contains null values
​​select * from table1 t1 right outer join table2 t2 on t1.column1=t2.column2
full outer join, full (outer) join on: contains orphan data of all tables
select * from table1 t1 full outer join table2 t2 on t1.column1=t2.column2

Oracle's extended outer join
is extended to the left outer and right outer joins in the outer join in the Oracle database, which can simplify the syntax of the outer join, and use (+) on the back side of the join condition to indicate whether to display orphan data, with ( +) means that orphan data is not displayed, while orphan data is displayed on the other side, but this writing method can only be used in Oracle database, and cannot be used in MySQL.
Which table should display orphan data, which table will not add (+)

select last_name, d.department_name from employees e, departments d
where e.department_id = d.department_id (+)

select last_name, d.department_name from employees e, departments d
where e.department_id(+) = d.department_id

3. Cross join (cross join)
is the cross product of two tables, which is the same as the Cartesian product of two tables.
4. Natural join (natural join)
performs an equivalence join of the same columns in two tables. The same columns must have the same value and type. If the data types of the columns are inconsistent, an error will be returned. It is equivalent to an inner connection, and its performance is the same as that of an equivalent connection, without some connection conditions.
select * from table1 natural join table2
5. Using the Using clause
When there are multiple columns matching, use the Using clause to match the only column.
If a column is used in USING, do not use the table name or alias when referring to the column.
natural join and using sub-bureau are mutually exclusive and cannot be used at the same time.
select * from table1 join table2 using (column1) Note: column1 is the same column that matches table 1 and table 2.

6. Subquery
Nested query, that is, one query is the condition of another query.
Subqueries can be placed in many SQL clauses, where sub-bureau, from sub-bureau, having sub-bureau.
select * from table1
where column1 = ( select colmun2 from table2 where… ) The
subquery is placed in parentheses;
the subquery is placed on the right side of the comparison condition; a
single row operator is used in the single row subquery, and the single row subquery means that the query result returns one data.
Use multi-line operators in multi-line subqueries, and multi-line subqueries refer to query results that return multiple pieces of data.
Multi-line subquery:
in ( ),
any( ), <any is less than any one, and is less than the maximum value; >any is greater than any one, and is greater than the minimum value.
all( ), <all is less than all, less than the minimum value> all is greater than any one, greater than the maximum value.

Seven, data manipulation language (DML)
insert statement
insert into table_name (colmun1, colmun2, colmun3 ) values ​​(values1, values2, values3 );
insert null column data
insert into table_name (colmun1, colmun2, colmun3 ) values ​​(values1, values2 , null );
or insert into table_name (colmun1, colmun2 ) values ​​(values1, values2 );

Oracle data import method:
1. sql file import
Open PLSQL Developer, create a command window, edit @d:/[Shang Silicon Valley]_Song Hongkang_oracle_sql_plsqlcourseware_chapter practice_data/2.data table file/01_del_data.sql
and 02_hr_cre. sql and 03_hr_popul.sql. The sql file is successfully imported and the statement is executed.

insert image description here

Guess you like

Origin blog.csdn.net/huangtao_1995/article/details/129288011