学习SAP HANA SQL 语句(创建 EMP,DEPT,BONUS 和 SALGRADE测试表)--像学Oracle一样学习SAP HANA

时间:2022-03-19 17:09:55

创建 EMP,DEPT,BONUS 和 SALGRADE测试表:

第一部分:创建基于行存储的EMP,DEPT,BONUS 和 SALGRADE测试表

创建表时候使用CREATE  tablename默认是ROW存储

部门表:DEPTNO部门编号唯一编号  DNAME部门名称  LOC部门位置

CREATE TABLE DEPT
 (
     DEPTNO smallint PRIMARY KEY,
     DNAME VARCHAR(14) ,
     LOC VARCHAR(13) ) ;

 雇员表:

 EMPNO 表示雇员编号  ENAME表示雇员姓名  JOB表示工作职位 MGR员领导编号 HIREDATE雇佣日期 SAL工资月薪  COMM表示奖金佣金 DEPTNO 部门编号

CREATE TABLE EMP(

           EMPNO INT PRIMARY KEY,
           ENAME VARCHAR(10),
           JOB VARCHAR(9),
           MGR INT,
           HIREDATE DATE,
           SAL INT,
           COMM INT,
           DEPTNO INT);

 奖金表:ENAME雇员姓名 JOB雇员工作  SAL雇员工资  COMM雇员奖金

CREATE TABLE BONUS
 (
 ENAME VARCHAR(10) ,
 JOB VARCHAR(9)  ,
 SAL INT,
 COMM INT
 ) ;

工资等级表: GRADE等级名称    LOSAL此等级的最低工资   HISAL 此等级的最高工资

CREATE TABLE SALGRADE
      ( GRADE INT,
 LOSAL INT,
 HISAL INT);

 

INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');


INSERT INTO EMP VALUES    (7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO EMP VALUES    (7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600,300,30);
INSERT INTO EMP VALUES    (7521,'WARD','SALESMAN',7698,'1981-2-22',1250,500,30);
INSERT INTO EMP VALUES    (7566,'JONES','MANAGER',7839,'1981-4-2',2975,NULL,20);
INSERT INTO EMP VALUES    (7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250,1400,30);
INSERT INTO EMP VALUES    (7698,'BLAKE','MANAGER',7839,'1981-5-1',2850,NULL,30);
INSERT INTO EMP VALUES    (7782,'CLARK','MANAGER',7839,'1981-6-9',2450,NULL,10);
INSERT INTO EMP VALUES    (7788,'SCOTT','ANALYST',7566,'87-7-13',3000,NULL,20);
INSERT INTO EMP VALUES    (7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES    (7844,'TURNER','SALESMAN',7698,'1981-9-8',1500,0,30);
INSERT INTO EMP VALUES    (7876,'ADAMS','CLERK',7788,'87-7-13',1100,NULL,20);
INSERT INTO EMP VALUES    (7900,'JAMES','CLERK',7698,'1981-12-3',950,NULL,30);
INSERT INTO EMP VALUES    (7902,'FORD','ANALYST',7566,'1981-12-3',3000,NULL,20);
INSERT INTO EMP VALUES    (7934,'MILLER','CLERK',7782,'1982-1-23',1300,NULL,10);
 
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
 

SQL 语句:

dummy是系统的一个空表,类似oracle的dual

1. select current_date "date" from dummy; 查看当前日期。

2.select ename,sal*12 from  P1526659201.emp;  查找雇员的姓名及雇员的年薪。P1526659201为schema

3.像oracle一样任何含空值的数学表达式是空值:select ename,sal*12+comm  from  P1526659201.emp;

4.select ename||sal from emp; 其中||相当于把sal全部转换成字符串。

5.select ename||' ename' from  emp; 表示字符串的方法。

6.select ename||''' ename' from emp; 如果字符串中包含有' 用''替换它。

7.select distinct deptno from emp; 去除部门中重复的数据。关键字distinct

8.select distinct deptno,job from emp;去除两个字段重复的数据。

9.select * from emp where deptno <> 10; 查出部门编号部位10的雇员。

10.select * from  emp where sal>=800 and sal <=1500; 查出薪水在800到1500之间的雇员。

11.select *  from emp where comm is null;查询空值

12.select *  from emp where comm is not null;查询非空值

13.select * from emp where sal in(800,1500,1200);关键字 in

14.查询符合条件的日期:

      select ename,sal,hiredate from emp where hiredate > '1981-12-17'  

      select ename,sal,hiredate from emp where hiredate < '1981-12-17'

      select ename,sal,hiredate from emp where hiredate = '1980-12-17'

      select ename,sal,hiredate from emp where hiredate > '1980-12-17' and hiredate < '1981-11-17'

      select ename,sal,hiredate from emp where hiredate <> '1980-12-17'

15.select top 5 * from emp; 取出emp表的前5条记录。

      select all * from emp; 取出emp表的全部记录

16.select *from dept order by deptno 按照部门信息,编号进行排序,默认为升序排序

     select *from dept order by deptno desc  用desc按照降序进行排序。

     select ename,sal from emp where sal <>1000 order by sal desc

17. .select avg(sal) from emp where sal>2000 group by deptno having avg(sal)>1500 order by avg(sal) desc;

 

14. SELECT  SUBSTRING ('1234567890',4,2) "substring" FROM DUMMY; 第四个字符开始截取2个字符。 ---> 结果 45

15.  SELECT SUBSTR_BEFORE ('Hello My Friend','My') "substr before" FROM DUMMY; 截取"My “字符串之前的字符串 ---->结果 Hello

16,SELECT SUBSTR_AFTER ('Hello My Friend','My ') "substr after" FROM DUMMY; 截取"My “字符串之后的字符串 ---->结果 Friend

17.select round(2.3456,3) from  dummy; 四舍五入小数点后三位--->结果 2.346

18.select round(2.3456) from  dummy; 四舍五入--->结果2

19.SELECT ASCII('Ant') "ascii" FROM DUMMY; 取ASCII码

 

索引:

SAP HANA的索引都是保存在内存中。

创建索引:

语法:CREATE [UNIQUE] [BTREE | CPBTREE] INDEX <index_name> ON <table_name> (<column_name_order>, ...) [ASC | DESC]

创建测试表:

create row table test_index (id INT,name nvarchar(10), remark nvarchar(10));

create index indextest1 on test_index(name);

CREATE CPBTREE INDEX indextest2 ON test_index(id, name DESC);

创建唯一键索引:

create unique index indextest4 on test_index(id);

create unique index indextest3 on test_index(name,remark);

删除索引:

 DROP INDEX <index_name>

drop index indextest2;删除索引indextest2

查询索引:

select * from indexes where table_name ='EMP';  查询员工表中使用的索引。

select * from index_columns  where table_name ='EMP'; 查询索引列

select * from m_rs_indexes  where table_name = 'EMP' ;查询索引的统计信息(B-tree and CPB-tree);

select * from FULLTEXT_INDEXES where table_name = 'EMP'; 查询Fulltext 索引。

select * from M_FULLTEXT_QUEUES; 查看fulltext 索引队列的状态。

Schema:

创建schema.

语法:CREATE SCHEMA <schema_name> [OWNED BY <user_name>]

OWNED BY:指定schema的拥有者,如果省略。当前用户将是这个shema的拥有者

create schema my_schema;

create schema my_schema OWNED BY system.

删除schema:

语法:DROP SCHEMA <schema_name> [<drop_option>]

drop_option: CASCADE | RESTRICT

默认的drop_option为:RESTRICT(限制约束)

RESTRICT:直接删除没有依赖的对象,如果对象有依赖关系,会抛出错误信息。

CASCADE:直接删除所有对象。

CREATE SCHEMA my_schema;

 CREATE TABLE my_schema.t (a INT);

DROP SCHEMA my_schema CASCADE;

select * from tables  where schema_name='P1526659201'  查询schema:P1526659201下的所有表

 

第二部分:创建基于列存储的EMP_COLUMN,DEPT_COLUMN,BONUS_COLUMN 和 SALGRADE_COLUMN测试表

CREATE COLUMN TABLE DEPT_COLUMN
(
     DEPTNO smallint PRIMARY KEY,
     DNAME VARCHAR(14) ,
     LOC VARCHAR(13)

 ) ;

CREATE COLUMN TABLE EMP_COLUMN
(

     EMPNO INT PRIMARY KEY,
     ENAME VARCHAR(10),
     JOB VARCHAR(9),
     MGR INT,
     HIREDATE DATE,
     SAL INT,
     COMM INT,
     DEPTNO INt

);
 
 
 CREATE COLUMN TABLE BONUS_COLUMN
 (
         ENAME VARCHAR(10) ,
         JOB VARCHAR(9)  ,
         SAL INT,
         COMM INT
 ) ;
 
 
 CREATE COLUMN TABLE SALGRADE_COLUMN
  (

        GRADE INT,
        LOSAL INT,
        HISAL INT

);