The basic operation of Oracle database table and the solution to the problem of null value

1. Oracle database language classification:

1) DDL: data definition language, which plays a role in data structure

  • create: create an object

  • alter: modify an object

  • drop: delete an object

  • truncate: clear data + implicit submission

    2) DCL: data control language

  • grant: grant

  • revoke: recycling

    3) DQL: data query language

  • select: query

    4) DML: data manipulation language, working on data

  • insert: add data

  • delete: delete data

  • update: modify data + submit explicitly

    5) TCL: transaction control language (working on DML)

  • commit: commit

  • savepoint: save point, used with rollback

  • rollback: rollback

2. Distinguish between delete, drop and truncate? (Interview question)

1) Language classification: delete is data manipulation language (DML), drop and truncate are data definition language (DDL)

2) Delete content: drop is to delete the entire object, truncate is to clear the data in the object, delete is to delete the data

3) Is it possible to roll back: delete is rollable, drop and truncate are not rollable

3. Common commands:

1) Create database objects:

Syntax:

create table 表名(字段1 数据类型,字段2 数据类型,.......);

2)) Add data:

grammar:

insert into 表名 values(1,值2...); //表中有几个字段,对应就有几个值

insert into 表名(字段1,字段2values(1,值2);//给必须赋值的字段添加数据

3) Query data:

grammar:

select * from 表名;

select 字段1,字段2 from 表名;

select 表达式 from dual; //dual是虚表(一行一列的虚表)

4) Query table structure:

grammar:

  desc 表名;

5) Modify the data:

grammar:

  update 表名 set 字段 = '值' where 字段 = '值';

6) Delete data:

grammar:

  delete from 表名 where 字段='值';

4. The data type of Oracle database:

1) Number type:

  • number (n): number (the longest is n digits)
  • number (n, m): the longest n, with m digits after the decimal point

For example: What is the maximum value of number (5,2)? 999.99

2) Character type: (Interview question: distinguish char, varchar, varchar2)

  • char (n): fixed length n, if the inserted data length is less than n, it must be filled with spaces
  • varchar (n): variable length, if the length of the inserted data is less than n, the last displayed character length is the length of the inserted data
  • varchar2 (n): usage is consistent with varchar, varchar2 is a unique type of Oracle database

3) Date type:

  • date

After learning the common commands and data types of Oracle, let's take an example:

create table dept_jinli( deptno number(2) , dname char(20) , location char(20)) ;
insert into dept_jinli values(10 , 'developer' , 'beijing') ; 
insert into dept_jinli values(20 , 'account' , 'shanghai') ;
insert into dept_jinli values(30 , 'sales' , 'guangzhou') ; 
insert into dept_jinli values(40 , 'operations' , 'tianjin') ; 
commit ;


create table emp_jinli( 
empno number(4) , 
ename varchar2(20) , 
job varchar2(15) , 
salary number(7 , 2) , 
bonus number(7 , 2) , 
hiredate date, 
mgr number(4) ,
deptno number(10));

insert into emp_jinli values( 1001 , '张无忌' , 'Manager' , 10000 , 2000 ,
                                 to_date( '2010-12-12' , 'YYYY-MM-DD' ) , 1005 , 10) ; 
insert into emp_jinli values( 1002 , '小苍' , 'Analyst' , 8000 , 1000 , 
                                 to_date( '2011-01-01' , 'YYYY-MM-DD' ) , 1001, 10) ; 
insert into emp_jinli values( 1003 , '李怡' , 'Analyst' , 9000 , 1000 ,
                                 to_date( '2010-01-11' , 'YYYY-MM-DD' ) , 1001, 10) ;
insert into emp_jinli values( 1004 , '郭芙蓉' , 'Programmer' , 5000 , null , 
                                 to_date( '2011-07-01' , 'YYYY-MM-DD' ) , 1001 , 10) ; 
insert into emp_jinli values( 1005 , '张三丰' , 'President' , 15000 , null ,
                                 to_date( '2008-05-15' , 'YYYY-MM-DD' ) , null , 20) ; 
insert into emp_jinli values( 1006 , '燕小六' , 'Manager' , 5000 , 400 , 
                                 to_date( '2009-02-01' , 'YYYY-MM-DD' ) , 1005 , 20) ;
insert into emp_jinli values( 1007 , '陆无双' , 'clerk' , 3000 , 500 ,
                                 to_date( '2009-02-01' , 'YYYY-MM-DD' ) , 1006 , 20) ;
insert into emp_jinli values( 1008 , '黄蓉' , 'Manager' , 5000 , 500 , 
                                 to_date( '2009-05-01' , 'YYYY-MM-DD' ), 1005 , 30) ; 
insert into emp_jinli values( 1009 , '韦小宝' , 'salesman' , 4000 , null , 
                                 to_date( '2009-02-20' , 'YYYY-MM-DD' ) , 1008 , 30) ; 
insert into emp_jinli values( 1010 , '郭靖' , 'salesman' , 4500 , 500 , 
                                 to_date( '2009-05-10' , 'YYYY-MM-DD' ) , 1008 , 30) ;

Query employee table and department table:

select * from emp_jinli;
select * from dept_jinli;

Query the monthly and annual salary of employees in the employee table

select ename ,salary ,salary*12+bonus from emp_jinli;

There is a problem with the above formula. As shown in the figure, some people's annual salary is null:
Insert picture description here

5.null:

1) Any data can be empty

2) If null participates in the operation, the result is empty

3) The null value and the string are spliced, then null is equivalent to no (splicing operation in Oracle database uses '||', plus sign in Java)

6.nvl (field 1, value 1):

Function to handle null, if the field 1 is null, then display the value 1

Note :

  • nvl (d1, d2) must ensure that the data types of d1 and d2 are consistent

  • nvl2 (expression, d1, d2): judge the expression, if the expression holds, then execute d1, otherwise execute d2, which is equivalent to the trinocular operation expression? Value 1: Value 2

So the situation above can be changed like this:

select ename,salary ,salary*12+nvl(bonus,0) from emp_jinli;

The rendering at this time is:
Insert picture description here

Published 24 original articles · praised 41 · visits 2778

Guess you like

Origin blog.csdn.net/abc701110/article/details/105532384