1. Naming rules for table names and columns
must start with a letter
The length cannot exceed 30 characters
Cannot use oracle reserved words
Only the following characters AZ, az, 0-9, $, #, etc. can be used
2. Data types supported by oracle
a. Character type
char: fixed length, maximum 2000 characters
Example: char(10) 'XiaoXiao' put 'XiaoXiao' in the first four characters, and then add 6 spaces to complete, but this query efficiency is higher than varchar2
varchar2(20): variable length, up to 4000 characters
Example: varchar(10) 'Little' oracle allocates four characters, which saves space
clob(character large object): character large object, up to 4G
b. digital
number: The range is -10 to the 38th power of 10 to the 38th power of 10, which can represent integers or decimals
number(5,2): Indicates that a decimal has 5 significant digits, 2 decimal places, and the range is -999.99~999.99
number(5): Represents a five-digit integer in the range -99999~99999
c. Date type
date: contains the year, month, day and hour, minute and second
timestamp: This is an extension of oracle9i to the date data type
d. pictures
blob: binary data, can store pictures/sounds 4G
For security reasons, put the pictures in the database, which can generally be stored in a folder, and the path can be stored in the database.
3. Create the table
create table student(--表名
stuNo number(4),--Student number
stuName varchar2(20),--name
sex char(2),--sex
birthday date,--date of birth
sal number(7,2) – Scholarship
);
4. Modify the table
add a field
alter table student add(classid number(2));
Modify the length of the field
alter table student modify (stuName varchar2(30));
Modify the type/name of the field (no data)
alter table student modify (stuName char(30));
delete a field
alter table student drop column sal;
Change the name of the table
rename student to stu;
delete table
drop table student;
5. Inquiry
set time on: Turn on the switch to display the operation time
a. Simple query statement
1). View the table structure: desc dept;
2). Query all columns: select * from dept;
3). Query the specified column: select ename,sal,job,deptno from emp;
4). How to cancel duplicate rows: select distinct deptno, job from emp;
Additional knowledge points:
疯狂复制:insert into users(userid,username,userpss) select * from users;
5). Inquire about SMITH's salary, job and department
select deptno,job,sal from emp where ename=’SMITH’;
6). Use arithmetic expressions
Display the annual salary of each employee:
select sal*13 + nvl(comm.,0)*13 "annual salary", ename, comm. from emp;
7). Use column aliases
select ename "name",sal*12 as "annual income" from emp;
8). Handling null values
Use nvl functions to process
9). If the connection string (||)
select ename || ‘is a ‘ || job from emp;
10). Use where clause
Display employees with salary higher than 3000: select ename,sal from emp where sal>3000;
Find employees who joined after January 1, 1982: select ename, hiredate from emp where hiredate>'January-January-1982';
Show employees with salary from 2000 to 2500: select ename,sal from emp where sal>=2000 and sal<=2500;
11). How to use the like operator
%: represents any 0 or more characters
_: Represents any single character
How to display the name and salary of the employee whose first character is S: select ename,sal from emp where ename like 'S%';
How to display all employees and salaries whose third character is capital O: select ename,sal from emp where ename like '__O%';
12). Use in in where condition
How to display the situation of employees whose empno is 7844,234,456: select * from emp where empno in(7844,234,456);
13). Use the is null operator
How to display the situation of employees without superiors: select * from emp where mgr is null;
14). Inquire about employees whose salary is higher than 500 or whose position is MANAGER, and also satisfy their initials with a capital J.
select * from emp where (sal>500 or job=’MANAGER’) and ename like ‘J%’;
15). Use the order by clause
How to display employee information in order of salary from lowest to highest
select * from emp order by sal asc;
Sort by department number in ascending order and employee salary in descending order
select * from emp order by deptno asc,sal desc;
Sort by department number in ascending order and employees' entry time in descending order
select * from emp order by deptno asc,hiredate desc;
16). Sort using column aliases
select ename,(sal+nvl(comm,0))*12 "annual salary" from emp order by "annual salary" asc;
Aliases need to be circled with ""
17). Paging query
Sort by employee id in ascending order
select * from (select a1.*,rownum rn from (select ename,sal from emp order by sal desc) a1 where rownum<=10) where rn>=5;
Oracle paging query:
A. According to ROWID
select * from test where rowid in( select rid from ( select rownum rn,rid from( select rowid rid,cid from test order by cid desc ) where rownum<10000 where rn>9980 ) order by cid desc;
B. According to the analysis function
select * from (select t.*, row_number() over(order by cid desc) rk from test t) where rk < 10000 and rk > 9980;
C. According to ROWNUM to divide
select * from (select t.*, rownum rn from (select * from test order by cid desc) t where rownum < 10000) where rn > 9980;
Among them, test is the name of the table, and cid is the key field of the table. Take the 9981-9999th records sorted by CID in descending order. There are more than 70,000 records in the test table. A has the best efficiency, C is the next, and B is the worst.
b.oracle table complex query
Description: In practical applications, it is often necessary to perform complex data statistics, and it is often necessary to display data from multiple tables.
A. Data grouping - max, min, avg, sum, count
1). How to display the highest salary and the lowest salary among all employees
select max(sal),min(sal) from emp;
2) Display the average salary and sum of salary for all employees
select avg(sal),sum(sal) from emp;
3) Calculate how many employees there are
select count(*) from emp;
4) Display the name of the highest paid employee, job title
select ename,job from emp where sal=(select max(sal) from emp);
5) Display information about employees whose salary is higher than the average salary
select * from emp where sal>(select avg(sal) from emp);
group by and having clauses: group by is used to group statistics on the results of the query, and the having clause is used to limit the grouped display results.
1). If the average salary and maximum salary of each department are displayed
select avg(sal),max(sal),deptno from emp group by deptno;
2). Display the average salary and minimum salary of each position in each department
select avg(sal),max(sal),deptno,job from emp group by deptno,job;
3). Display the department number with average salary higher than 2000 and its average salary
select avg(sal),max(sal),deptno from emp group by deptno having avg(sal)>2000;
Summary of data groupings:
Grouping functions can only appear in select lists, having, and order by clauses;
If group by, having, order by are included in the select statement at the same time, then their order is group by, having, order by;
In the selection column, if there are columns, expressions, and grouping functions, then one of these columns and expressions must appear in the group by clause, otherwise an error will occur;
Such as select deptno, avg(sal), max(sal) from emp group by deptno having avg(sal)<2000; here deptno must appear in group by.
B. Multi-table query
Note: Multi-table query refers to a query based on two or more tables or views. In practical applications, querying a single table may not meet your needs. If the location of the sales department and the names of its employees are displayed, this kind of In this case, you need to use the dept table and the emp table.
1) Display employee name, employee salary and the name of the department
The condition of multi-table query is at least not less than the number of tables -1
select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno;
2) How to display the department name, employee name and salary with department number 10
select a1.dname,a2.ename,a2.sal from dept a1,emp a2 where a1.deptno=a2.deptno and a1.deptno=10;
3) Display the name, salary and salary level of each employee
select a1.ename,a1.sal,a2.grade from emp a1,salgrade a2 where a1.sal between a2.losal and a2.hisal;
4) Display employee name, employee salary and the name of the department, and sort by department
select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno order by a1.deptno;
Self-join: Self-join refers to a join query on the same table
5). Display the name of an employee's superior, such as the superior of 'FORD'
select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno and worker.ename=’FORD’;
C. Subqueries
A subquery refers to a select statement embedded in other SQL statements, also called a nested query.
A single-row subquery refers to a subquery that returns only one row of data
1) Show all employees of the same department also SMITH
select * from emp where deptno=(select deptno from emp where ename=’SMITH’);
Multi-row subqueries are subqueries that return multiple rows of data
2) Query the name, position, salary, department number of the employee who has the same job as department 10
select * from emp where job in (select distinct job from emp where deptno=10);
Using the all operator in a multiline subquery
3) How to display the name, salary and department number of the employee whose salary is higher than that of all employees in department 30
select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);
Another way:
select * from emp where sal>(select max(sal) from emp where deptno=30);
Using the any operator in a multi-row subquery
4) Display the name, salary and department number of the employee whose salary is higher than that of any employee in department 30
select ename,sal,deptno from emp where sal>any (select sal from emp where deptno=30);
Another way:
select * from emp where sal>(select min(sal) from emp where deptno=30);
Multi-column subqueries
Note: A single-row subquery refers to a subquery that returns only a single column and a single row of data, a multi-row subquery refers to returning a single column and multiple rows of data, all for a single column, and a multi-column subquery refers to a query that returns multiple columns of data subquery statement.
5) Query all employees with the exact same department and position as SMITH
select * from emp where (deptno,job)=(select deptno,job from emp where ename=’SMITH’);
6) Use subqueries in the from clause
Display the name, salary, department number and the average salary corresponding to the department of the employee whose salary is higher than the average salary of their own department
select a2.ename,a2.sal,a2.deptno,a1.mysal from emp a2,(select deptno,avg(sal) mysal from emp group by deptno) a1 where a1.deptno=a2.deptno and a2.sal>a1.mysal;
When a subquery is used in the from clause, the subquery is treated as a view, so it is also called an embedded view. When a subquery is used in the from clause, an alias must be specified for the subquery.
7) Create new table with query result
This command is a quick way to create a table
create table mytable (id,name,sal,job,deptno) as select empno,ename,sal,job,deptno from emp;
c. Merge queries
Sometimes in practical applications, in order to combine the results of multiple select statements, you can use the set operator symbols union, union all, intersect, minus
A.union
This operator is used to obtain the union of two result sets. When this operator is used, duplicate rows are automatically removed from the result set.
select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job=’MANAGER’;
B.union all
This operation is similar to union, but it does not cancel duplicate rows, and it does not sort.
select ename,sal,job from emp where sal>2500 union all select ename,sal,job from emp where job=’MANAGER’;
This operator is used to obtain the union of two result sets. When this operator is used, duplicate rows are automatically removed from the result set.
C.intersect
Use this operator to obtain the intersection of two result sets.
select ename,sal,job from emp where sal>2500 intersect select ename,sal,job from emp where job=’MANAGER’;
D.minus
Use this operator to get the difference of two result sets, it will only show the data that exists in the first set, but not in the second set.
select ename,sal,job from emp where sal>2500 minus select ename,sal,job from emp where job=’MANAGER’;
6. Add data
a. All fields are inserted : insert into student values('A001','Zhang San','Male','01-May-05',10);
Default date format 'DD-MON-YY' in Oracle
dd: day (day)
mon: month
yy: 2-digit year, such as '09-June-99' means June 9, 1999
Change the default format of the date: alter session set nls_date_format = 'yyyy-mm-dd';
After modification, the date type can be added in the following format:
insert into student values(‘A002’,’MIKE’,’男’,’1905-05-06’,10);
b. Insert some fields
insert into student(stuNo,stuName,sex) values(‘A003’,’JOHN’,’女’);
c. Insert a null value
insert into student(stuNo,stuName,sex,birthday) values(‘A004’,’MARTIN’,’男’,null);
7. Modify data
a. Change a field
update student set sex=’女’ where stuNo=’A001’;
b. Modify multiple fields
update student set sex=’男’,birthday=’1980-04-01’ where stuNo=’A001’;
c. Modify data containing null values (is null)
8. Deletion of data
delete from student;
Delete all records, the table structure is still there, write logs, recoverable, slow
drop table student;
delete table structure and data
delete from student where stuNo=’A001’;
delete a record
truncate table student;
Delete all records in the table, the table structure is still there, no log is written, the deleted records cannot be retrieved, and the speed is fast
9. Create a new database
There are two ways to create a database:
a. Through the wizard tool provided by oracle
oracle->Configuration and Migratin Tools->Database Configuration Assistant
b. Can be created directly with manual steps