--创建schema:
--语法:CREATE SCHEMA [OWNED BY ]
/*OWNED BY:指定schema的拥有者,如果省略。当前用户将是这个shema的拥有者*/
create schema my_schema;
create schema my_schema OWNED BY system.
--删除schema:
--语法:DROP SCHEMA []
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下的所有表
HANA数据库可以存储ROW TABLE(行表)和COLUMN TABLE(列表),分别适用于不同的场景。
行存储表适用于场景:
列存储表适用场景:
--行存储表示例:
--例1
CREATE TABLE accounts
( act_no CHAR(18),
act_name NVARCHAR(100),
balance DECIMAL(15,2),
PRIMARY KEY (act_no)
) ;
--例2
CREATE TABLE R
(A INT PRIMARY KEY,
B NVARCHAR(10)
);
--列存储表示例:
--例1
CREATE COLUMN TABLE tpch.nation
(n_nationkey INT NOT NULL,
n_name CHAR(25),
n_regionkey INT,
n_comment VARCHAR(152),
PRIMARY KEY(n_nationkey)
) ;
--例2(分区,只适用于列表,分区键必须是主键的一部分)
CREATE COLUMN TABLE tpch.lineitem
(l_orderkey INT NOT NULL,l_partkey INT,
l_suppkey INT,l_linenumber INT NOT NULL,
l_quantity DECIMAL(10,2),l_extendedprice DECIMAL(10,2),
l_discount DECIMAL(10,2),l_tax DECIMAL(10,2),
l_returnflag CHAR(1),l_linestatus CHAR(1),
l_shipdate DATE,l_commitdate DATE,l_receiptdate DATE,
l_shipinstruct CHAR(25),l_shipmode CHAR(10),
l_comment VARCHAR(44),
PRIMARY KEY INVERTED VALUE (l_orderkey,l_linenumber)
)
PARTITION BY HASH (l_orderkey,l_linenumber) PARTITIONS 4,
RANGE (l_shipdate) (
PARTITION '2011/01/01'<= VALUES <'2011/04/01',
PARTITION '2011/04/01'<= VALUES <'2011/07/01',
PARTITION '2011/07/01'<= VALUES <'2011/10/01',
PARTITION OTHERS) ;
--HANA查看特定表的分区情况
select * from sys.M_CS_PARTITIONS where table_name = 'TABLE_NAME';
在上面的创建列表的例子中,我们指定的“UNIQUE”或“PRIMARY KEY”约束,HANA会自动创建相应的索引。这些索引分为两种类型(仅适用于列表)
如果不指定,缺省是 INVERTED VALUE。上面的INVERTED VALUE和INVERTED HASH 仅在UNIQUE和PRIMARY KEY约束中使用。下面是指定的示例
PRIMARY KEY INVERTED VALUE (l_orderkey,l_linenumber)
PRIMARY KEY INVERTED HASH (l_orderkey,l_linenumber)
参考链接:SAP HANA 学习系列——SQL开发(5) | SAP Blogs
HANA除了直接建表外,也可以通过已存在的表创建新表,示例如下:
/*依据某个已经存在的表创建另外的新表。HANA SQL提供了两类方法*/
--方法一:创建的表与源表数据类型、约束完全相同。例如:
CREATE TABLE T_ACCOUNTS LIKE ACCOUNTS WITH NO DATA
--方法二:创建的表字段类型和NULL/NOT NULL属性相同
CREATE TABLE T_ACCOUNTS1 AS (SELECT * FROM ACCOUNTS) WITH DATA
--01.语法:RENAME TABLE TO
--02.描述:RENAME TABLE 语句在同一个Schema下,将表名修改为 new_table_name。
--03.示例:
CREATE TABLE mySchema.A (A INTPRIMARYKEY, B INT);
--显示模式 mySchema 下表名的列表:
SELECT TABLE_NAME FROM TABLES WHERE SCHEMA_NAME = 'MYSCHEMA';
--表 mySchema.A 重命名为 B:
RENAME TABLE mySchema.A TO B;--注:修改后B还是在mySchema里
HANA支持行表转换为列表,或者列表转换为行表。示例如下:
ALTER TABLE accounts COLUMN THREADS 10 BATCH 10000; --行存储表转为列存储表
ALTER TABLE accounts ROW THREADS 10; --列存储表转为行存储表
参数说明:
关于THREADS和BATCH两个参数,官方文档《SAP_HANA_SQL_and_System_Views_Reference_en》说明如下:
Specifies how many parallel execution threads should be used for the table conversion. The optimal value for the number of threads is the number of available CPU cores. If THREADS is not provided the default value of the number of CPU cores specified in the indexserver.ini file will be used.
Specifies the number of rows to be inserted in a batch. If BATCH is not specified the default value of 2,000,000 will be used. Inserts into column tables will be immediately committed after every
records have been inserted. BATCH option can be used only when a table is converted from ROW to COLUMN
storage.
--删除/增加主键
ALTER TABLE "SCHEMA1"."TABLE1" DROP PRIMARY KEY;
ALTER TABLE "SCHEMA1"."TABLE1" ADD PRIMARY KEY(TID, TNAME);
--增加或删除字段
ALTER TABLE "SCHEMA1"."TABLE1" ADD("CREATETIME" DATE NULL) ;
ALTER TABLE "SCHEMA1"."TABLE1" DROP (COL1);
--修改字段类型
ALTER TABLE "SCHEMA1"."TABLE1" ALTER ("TYPE" NVARCHAR (10) NULL);
--创建非分区表
CREATE COLUMN TABLE "TEST"."TABLE1" (
"WHENDATE" DAYDATE CS_DAYDATE,
"COL1" VARCHAR(4),
"COL2" VARCHAR(13),
PRIMARY KEY ("WHENDATE","COL1")) UNLOAD PRIORITY 5 AUTO MERGE ;
--修改为分区表(使用WHENDATE作为分区)
alter table TEST.TABLE1 partition by range (month(WHENDATE))(
PARTITION '190001'<= values < '201801' ,
PARTITION '201801'<= values < '201802' ,
PARTITION '201802'<= values < '201803' ,
PARTITION '201803'<= values < '201804' ,
PARTITION '201804'<= values < '201805' ,
PARTITION '201805'<= values < '201806' ,
PARTITION '201806'<= values < '201807' ,
PARTITION '201807'<= values < '201808' ,
PARTITION OTHERS)
最后需要带PARTITION OTHERS,否则会出现如下报错信息:
Could not execute 'alter table ******* PARTITION BY ‘RANGE month(WHENDATE) …’ in 162 ms 210 µs .
SAP DBTech JDBC: [2048]: column store error: fail to alter partition: [2593] Error during split/merge operation;A specified Range partitioning does not have a rest part.,object=********Een
--增加分区
ALTER TABLE TEST.TABLE1 ADD PARTITION (month(WHENDATE)) '201808' <= VALUES < '201809';
ALTER TABLE TEST.TABLE1 ADD PARTITION (month(WHENDATE)) '201809' <= VALUES < '201810';
--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
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 索引队列的状态
--1.时间相关
--查看当前日期和时间
select current_date "date",current_time "time" from dummy;
--查看当前日期零时零分
select to_timestamp(to_char(CURRENT_TIMESTAMP,'yyyy-mm-dd')) from dummy;
--2.查找雇员的姓名及雇员的年薪
select ename,sal*12 from emp;
--3.像oracle一样任何含空值的数学表达式是空值:
select ename,sal*12+comm from emp;
--4.其中||相当于把sal全部转换成字符串
select ename||sal from emp;
--5.表示字符串的方法
select ename||' ename' from emp;
--6.如果字符串中包含有' 用''替换它
select ename||''' ename' from emp;
--7.取出emp表的前5条记录
select top 5 * from emp;
--8.取出emp表的全部记录
select all * from emp;
--9.去除两个字段重复的数据
select distinct deptno,job from emp;
--10.取ASCII码
SELECT ASCII('Ant') "ascii" FROM DUMMY;