SAP In-Memory Database SQL Reference Guide
SAP In-Memory Database SQL Reference Guide
0 SPS 02
Copyright 2011 SAP AG. All rights reserved. No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Copyright 2011 Sybase, Inc. All rights reserved. Unpublished rights reserved under U.S. copyright laws.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used
Sybase, the Sybase logo, Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. All other trademarks are the property of their respective owners. Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation.
under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. in the United States and in other countries. Sybase and Adaptive Server, iAnywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. Sybase is an SAP company. All other product and service names mentioned are the trademarks of
Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation.
their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express
warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. Disclaimer Some components of this product are based on Java. Any code change in these components may cause unpredictable and severe malfunctions and is therefore expressively prohibited, as is any decompilation of these components. Any Java Source Code delivered with this product is only to be used by SAPs Support Services and may not be modified or altered in any way. Documentation in the SAP Service Marketplace You can find this documentation at the following address:
http://service.sap.com/hana
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3. SAP License Agreement for STLport SAP License Agreement for STLPort between SAP Aktiengesellschaft Systems, Applications, Products in Data Processing Neurottstrasse 16 69190 Walldorf, Germany (hereinafter: SAP) and you (hereinafter: Customer) a) Subject Matter of the Agreement A) SAP grants Customer a non-exclusive, non-transferrable, royaltyfree license to use the STLport.org C++ library (STLport) and its documentation without fee. B) By downloading, using, or copying STLport or any portion thereof Customer agrees to abide by the intellectual property laws, and to all of the terms and conditions of this Agreement. C) The Customer may distribute binaries compiled with STLport (whether original or modified) without any royalties or restrictions. D) Customer shall maintain the following copyright and permissions notices on STLport sources and its documentation unchanged: Copyright 2001 SAP AG E) The Customer may distribute original or modified STLport sources, provided that: o The conditions indicated in the above permissions notice are met; o The following copyright notices are retained when present, and conditions provided in accompanying permission notices are met: Coypright 1994 Hewlett-Packard Company Copyright 1996,97 Silicon Graphics Computer Systems Inc. Copyright 1997 Moscow Center for SPARC Technology. Copyright 1999,2000 Boris Fomitchev Copyright 2001 SAP AG Permission to use, copy, modify, distribute and sell this software and its documentation for any purposes is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Hewlett-Packard Company makes no representations about the suitability of this software for any purpose. It is provided as is without express or implied warranty. Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Silicon Graphics makes no representations about the suitability of this software for any purpose. It is provided as is without express or implied warranty. Permission to use, copy, modify, distribute and sell this software and its documentation for any purposes is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Moscow Center for SPARC makes no representations about the suitability of this software for any purpose. It is provided as is without express or implied warranty. Boris Fomitchev makes no representations about the suitability of this software for any purpose. This material is provided "as is", with absolutely no warranty expressed or implied. Any use is at your own risk. Permission to use or copy this software for any purpose is hereby granted without fee, provided the above notices are retained on all copies. Permission to modify the code and to distribute modified code is granted, provided the above notices are retained, and a notice that the code was modified is included with the above copyright notice. Permission to use, copy, modify, distribute and sell this software and its documentation for any purposes is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. SAP makes no representations about the suitability of this software for any purpose. It is provided with a limited warranty and liability as set forth in the License Agreement distributed with this copy. SAP offers this liability and warranty obligations only towards its customers and only referring to its modifications. b) Support and Maintenance SAP does not provide software maintenance for the STLport. Software maintenance of the STLport therefore shall be not included. All other services shall be charged according to the rates for services quoted in the SAP List of Prices and Conditions and shall be subject to a separate contract. c) Exclusion of warranty As the STLport is transferred to the Customer on a loan basis and free of charge, SAP cannot guarantee that the STLport is error-free, without material defects or suitable for a specific application under third-party rights. Technical data, sales brochures, advertising text and
quality descriptions produced by SAP do not indicate any assurance of particular attributes. d) Limited Liability A) Irrespective of the legal reasons, SAP shall only be liable for damage, including unauthorized operation, if this (i) can be compensated under the Product Liability Act or (ii) if caused due to gross negligence or intent by SAP or (iii) if based on the failure of a guaranteed attribute. B) If SAP is liable for gross negligence or intent caused by employees who are neither agents or managerial employees of SAP, the total liability for such damage and a maximum limit on the scope of any such damage shall depend on the extent to which its occurrence ought to have anticipated by SAP when concluding the contract, due to the circumstances known to it at that point in time representing a typical transfer of the software. C) In the case of Art. 4.2 above, SAP shall not be liable for indirect damage, consequential damage caused by a defect or lost profit. D) SAP and the Customer agree that the typical foreseeable extent of damage shall under no circumstances exceed EUR 5,000. E) The Customer shall take adequate measures for the protection of data and programs, in particular by making backup copies at the minimum intervals recommended by SAP. SAP shall not be liable for the loss of data and its recovery, notwithstanding the other limitations of the present Art. 4 if this loss could have been avoided by observing this obligation. F) The exclusion or the limitation of claims in accordance with the present Art. 4 includes claims against employees or agents of SAP. 4. Adobe Document Services Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and / or other countries. For information on Third Party software delivered with Adobe document services and Adobe LiveCycle Designer, see SAP Note 854621
Table of Contents
About this Guide ........................................................................................................................................... 7 SAP HANA Guides.......................................................................................................................................... 7 Notation ........................................................................................................................................................ 8 Functions ....................................................................................................................................................... 9 Data type conversion functions ................................................................................................................ 9 CAST Function ....................................................................................................................................... 9 TO_BIGINT Function.............................................................................................................................. 9 TO_BINARY Function ............................................................................................................................. 9 TO_BLOB Function .............................................................................................................................. 10 TO_CHAR Function .............................................................................................................................. 10 TO_CLOB Function .............................................................................................................................. 10 TO_DATE Function .............................................................................................................................. 10 TO_DATS Function .............................................................................................................................. 11 TO_DECIMAL Function ........................................................................................................................ 11 TO_DOUBLE Function ......................................................................................................................... 11 TO_INT Function ................................................................................................................................. 12 TO_INTEGER Function ......................................................................................................................... 12 TO_NCHAR Function ........................................................................................................................... 12 TO_NCLOB Function ............................................................................................................................ 12 TO_NUMBER Function ........................................................................................................................ 13 TO_REAL Function ............................................................................................................................... 13 TO_SMALLINT Function ...................................................................................................................... 13 TO_TINYINT Function .......................................................................................................................... 13 TO_TIME Function............................................................................................................................... 14 TO_TIMESTAMP Function ................................................................................................................... 14 DateTime Functions ................................................................................................................................ 15 ADD_DAYS Function ............................................................................................................................ 15 ADD_MONTHS Function ..................................................................................................................... 15 2
SAP In-Memory Database SQL Reference Manual ADD_YEARS Function .......................................................................................................................... 15 ADD_SECONDS Function ..................................................................................................................... 15 DAYS_BETWEEN Function ................................................................................................................... 16 DAYNAME Function ............................................................................................................................ 16 DAYOFMONTH Function ..................................................................................................................... 16 DAYOFYEAR Function .......................................................................................................................... 16 EXTRACT Function ............................................................................................................................... 17 HOUR Function.................................................................................................................................... 17 LAST_DAY Function ............................................................................................................................. 17 MINUTE Function ................................................................................................................................ 17 MONTH Function ................................................................................................................................ 18 MONTHNAME Function ...................................................................................................................... 18 NEXT_DAY Function ............................................................................................................................ 18 NOW Function ..................................................................................................................................... 18 SECOND Function ................................................................................................................................ 19 SECONDS_BETWEEN Function ............................................................................................................ 19 WEEK Function .................................................................................................................................... 19 WEEKDAY Function ............................................................................................................................. 20 YEAR Function ..................................................................................................................................... 20 Miscellaneous Functions ......................................................................................................................... 21 COALESCE Function ............................................................................................................................. 21 IFNULL Function .................................................................................................................................. 21 NULLIF Function .................................................................................................................................. 21 CURRENT_CONNECTION Function ...................................................................................................... 22 CURRENT_SCHEMA Function .............................................................................................................. 22 CURRENT_USER Function ................................................................................................................... 22 DATABASE Function ............................................................................................................................ 23 USER Function ..................................................................................................................................... 23 GROUPING_ID Function ...................................................................................................................... 23 SESSION_CONTEXT Function............................................................................................................... 24 SYSUUID Function ............................................................................................................................... 25 Number Functions................................................................................................................................... 26 3
SAP In-Memory Database SQL Reference Manual ABS Function ....................................................................................................................................... 26 ACOS Function..................................................................................................................................... 26 ASIN Function ...................................................................................................................................... 26 ATAN Function .................................................................................................................................... 26 ATAN2 Function .................................................................................................................................. 27 BITAND Function ................................................................................................................................. 27 CEIL / CEILING Function ...................................................................................................................... 27 COS Function ....................................................................................................................................... 28 COSH Function .................................................................................................................................... 28 COT Function ....................................................................................................................................... 28 EXP Function ....................................................................................................................................... 28 FLOOR Function .................................................................................................................................. 29 GREATEST Function ............................................................................................................................. 29 LEAST Function .................................................................................................................................... 29 LN Function ......................................................................................................................................... 30 LOG Function....................................................................................................................................... 30 MOD Function ..................................................................................................................................... 30 POWER Function ................................................................................................................................. 31 ROUND Function ................................................................................................................................. 31 SIGN Function...................................................................................................................................... 31 SIN Function ........................................................................................................................................ 31 SINH Function...................................................................................................................................... 32 SQRT Function ..................................................................................................................................... 32 TAN Function....................................................................................................................................... 32 TANH Function .................................................................................................................................... 32 String Functions ...................................................................................................................................... 34 ASCII Function ..................................................................................................................................... 34 CONCAT Function ................................................................................................................................ 34 LEFT Function ...................................................................................................................................... 34 LCASE Function.................................................................................................................................... 34 LENGTH Function ................................................................................................................................ 35 LOCATE Function ................................................................................................................................. 35 4
SAP In-Memory Database SQL Reference Manual LOWER Function ................................................................................................................................. 35 LPAD Function ..................................................................................................................................... 36 LTRIM Function ................................................................................................................................... 36 REPLACE Function ............................................................................................................................... 36 RIGHT Function ................................................................................................................................... 37 RPAD Function .................................................................................................................................... 37 RTRIM Function ................................................................................................................................... 37 SUBSTRING Function ........................................................................................................................... 37 SUBSTR_AFTER Function ..................................................................................................................... 38 SUBSTR_BEFORE Function .................................................................................................................. 38 TRIM Function ..................................................................................................................................... 38 UCASE Function................................................................................................................................... 39 UNICODE Function .............................................................................................................................. 39 UPPER Function................................................................................................................................... 39 SQL Statements ........................................................................................................................................... 41 ALTER TABLE........................................................................................................................................ 41 ALTER USER ......................................................................................................................................... 44 CREATE CALCULATION SCENARIO ....................................................................................................... 45 CREATE COLUMN VIEW....................................................................................................................... 47 CREATE INDEX ..................................................................................................................................... 52 CREATE ROLE ....................................................................................................................................... 53 CREATE SCHEMA ................................................................................................................................. 54 CREATE SEQUENCE.............................................................................................................................. 55 CREATE SYNONYM .............................................................................................................................. 57 CREATE TABLE ..................................................................................................................................... 58 CREATE TYPE ....................................................................................................................................... 62 CREATE USER....................................................................................................................................... 63 CREATE VIEW ...................................................................................................................................... 64 DELETE................................................................................................................................................. 65 DROP CALCULATION SCENARIO .......................................................................................................... 66 DROP INDEX ........................................................................................................................................ 67 DROP ROLE .......................................................................................................................................... 68 5
SAP In-Memory Database SQL Reference Manual DROP SCHEMA .................................................................................................................................... 69 DROP SEQUENCE................................................................................................................................. 70 DROP SYNONYM ................................................................................................................................. 71 DROP TABLE ........................................................................................................................................ 72 DROP TYPE .......................................................................................................................................... 73 DROP TABLE ........................................................................................................................................ 74 DROP USER .......................................................................................................................................... 75 DROP VIEW.......................................................................................................................................... 76 RENAME INDEX ................................................................................................................................... 77 GRANT ................................................................................................................................................. 78 INSERT ................................................................................................................................................. 81 LOAD ................................................................................................................................................... 82 MERGE DELTA ..................................................................................................................................... 83 RENAME COLUMN .............................................................................................................................. 84 RENAME INDEX ................................................................................................................................... 85 RENAME TABLE ................................................................................................................................... 86 REVOKE ............................................................................................................................................... 87 SELECT ................................................................................................................................................. 88 SET TRANSACTION .............................................................................................................................. 93 UNLOAD .............................................................................................................................................. 94 UPDATE ............................................................................................................................................... 95 UPSERT / REPLACE .............................................................................................................................. 96
Notation
This reference use BNF (Backus Naur Form) which is the notation technique used to define programming languages, to describe SQL. BNF describes the syntax of a grammar using a set of production rules using a set of symbols. Table 1 presents meta symbols of BNF and descriptions. The Meta symbols of BNF Meta-symbol <> Description Angle brackets are placeholders for syntactical units explained in this document. Production rule has a left hand side (LHS) and a right hand side (RHS) separated by the meta-symbol "::=". The symbol on the left-hand side of the ::= is defined by the right hand side. Enclose optional element. Element between [ ] can be specified or omitted. Groups items. Elements between {} must be specified. Possible option elements in the formula. Elements follow by | can be chosen differently than front of | parts Displays several repeated item in the formula
::=
[] {} | ...
Functions
Functions are used to return information from the database. They are allowed anywhere an expression is allowed. Functions use the same syntax conventions used by SQL statements.
Description: Returns the value of an expression converted to a supplied data type. Parameters: expression - The expression to be converted. data type The target data type.
BIGINT | BINARY | BLOB | CHAR | CLOB | DATE | DECIMAL | DOUBLE | NCHAR | NCLOB | REAL | TIME | TIMESTAMP
Example:
SELECT TOP 1 CAST(7 AS CHAR(10)) "cast" FROM users;
Retrieves: cast
7
TO_BIGINT Function
Syntax: TO_BIGINT(expr) Description: Converts the expr of a data type into a value of bigint data type. Example: SELECT TOP 1 TO_BIGINT('10') "to bigint" FROM users; Retrieves:
to bigint 10
TO_BINARY Function
Syntax: TO_BINARY(expr) Description: Converts the expr of a data type into a value of binary string type.
to binary 6162636465
TO_BLOB Function
Syntax: TO_BLOB(expr) Description: Converts the expr of a data type into a value of blob type. expr must be a binary string. Example: SELECT TOP 1 TO_BLOB(TO_BINARY('abcde')) "to blob" FROM users; Retrieves:
to blob abcde
TO_CHAR Function
Syntax: TO_CHAR(expr [,format]) Description: Converts the expr of a data type into a value of character data type. Example: SELECT TOP 1 TO_CHAR(TO_DATE('2009-12-31'), 'YYYY/MM/DD') "to char" FROM users; Retrieves:
to char 2009/12/31
TO_CLOB Function
Syntax: TO_CLOB(expr) Description: Converts the expr of a data type into a value of CLOB data type. Example: SELECT TOP 1 TO_CLOB ('TO_CLOB converts an expression into a value of CLOB data type') "to clob" FROM users; Retrieves:
TO_DATE Function
Syntax: TO_DATE(expr [, format]) Description:
10
to date 2010-01-12
TO_DATS Function
Syntax: TO_DATS(expr) Description: Converts the expr of a data type into a value of ABAP DATE data type. Example: SELECT TOP 1 TO_DATS('2010-01-12') "abap date" FROM users; Retrieves:
TO_DECIMAL Function
Syntax: TO_DECIMAL(expr[, precision, scale]) Description: Converts the expr of a data type into a value of DECIMAL(precision, scale) data type. Example: SELECT TOP 1 TO_DECIMAL(7654321.89, 9, 2) "to decimal" FROM users; Retrieves:
to decimal 7654321.89
TO_DOUBLE Function
Syntax: TO_DOUBLE(expr) Description: Converts the expr of a data type into a value of DOUBLE (double precision) data type. Example: SELECT TOP 1 3*TO_DOUBLE ('15.12') "to double" FROM users; Retrieves:
to double 45.36
11
to int 10
TO_INTEGER Function
Syntax: TO_INTEGER(expr) Description: Converts the expr of a data type into a value of INTEGER data type. Example: SELECT TOP 1 TO_INTEGER('10') "to int" FROM users; Retrieves:
to int 10
TO_NCHAR Function
Syntax: TO_NCHAR(expr [,format]) Description: Converts the expr of a data type into a value of national character data type. If format is omitted, it converts to the corresponding format using the date format model. Example: SELECT TOP 1 TO_NCHAR(TO_DATE('2009/12/31'), 'YY-MM-DD') "to nchar" FROM users; Retrieves:
to nchar 09-12-31
TO_NCLOB Function
Syntax: TO_NCLOB(expr) Description: Converts the expr of a data type into a value of NCLOB data type. Example: SELECT TOP 1 TO_NCLOB ('TO_NCLOB converts an expression into a value of NCLOB data type') "to nclob" FROM users;
12
TO_NUMBER Function
Syntax: TO_NUMBER(expr) Description: Converts the expr of a data type into a numeric data type. Example: SELECT TOP 1 3.1*TO_NUMBER ('15') "to number" FROM users; Retrieves:
to number 46.5
TO_REAL Function
Syntax: TO_REAL(expr) Description: Converts the expr of a data type into the value of REAL (single precision) data type. Example: SELECT TOP 1 3*TO_REAL ('15.12') "to real" FROM users; Retrieves:
to real 45.36
TO_SMALLINT Function
Syntax: TO_SMALLINT(expr) Description: Converts the expr of a data type into a value of SMALLINT data type. Example: SELECT TOP 1 TO_SMALLINT('10') "to smallint" FROM users; Retrieves:
to smallint 10
TO_TINYINT Function
Syntax: TO_TINYINT(expr) Description: Converts the expr of a data type into a value of TINYINT data type. Example: SELECT TOP 1 TO_TINYINT('10') "to tinyint" FROM users;
13
Retrieves:
to tinyint 10
TO_TIME Function
Syntax: TO_TIME(expr [, format]) Description: Converts the expr of a data type into a value of TIME data type. If format is omitted, it converts expr into the corresponding format using the date format model. Example: SELECT TOP 1 TO_TIME ('08:30 AM', HH12:MI AM) "to time" FROM users; Retrieves:
to time 08:30:00
TO_TIMESTAMP Function
Syntax: TO_TIMESTAMP(expr [, format]) Description: Converts the expr of a data type into the TIMESTAMP data type. If format is omitted, it converts expr into the corresponding format using the date format model. Example: SELECT TOP 1 TO_TIMESTAMP ('2010-01-11 13:30:00', 'YYYY-MM-DD HH24:MI:SS') "to timestamp" FROM users; Retrieves:
14
DateTime Functions
ADD_DAYS Function
Syntax: ADD_DAYS(d, n) Description: Computes the date d plus n days. Example: SELECT TOP 1 ADD_DAYS(TO_DATE('2009-12-05', 'YYYY-MM-DD'), 30) "add days" FROM users; Retrieves:
ADD_MONTHS Function
Syntax: ADD_MONTHS(d, n) Description: Computes the date d plus n months. Example: SELECT TOP 1 ADD_MONTHS(TO_DATE('2009-12-05', 'YYYY-MM-DD'), 1) "add months" FROM users; Retrieves:
ADD_YEARS Function
Syntax: ADD_YEARS(d, n) Description: Computes the date d plus n years. Example: SELECT TOP 1 ADD_YEARS(TO_DATE('2009-12-05', 'YYYY-MM-DD'), 1) "add years" FROM users; Retrieves:
ADD_SECONDS Function
Syntax: ADD_SECONDS(t, n) Description: Computes the time t plus n seconds. Example: SELECT TOP 1 ADD_SECONDS(TO_TIME('23:30:45'), 60*30) "add seconds" FROM users;
15
DAYS_BETWEEN Function
Syntax: DAYS_BETWEEN (date1, date2) Description: Computes the number of days between date1 and date2. Example: SELECT TOP 1 DAYS_BETWEEN(TO_DATE('2009-12-05', 'YYYY-MM-DD'), TO_DATE('2010-01-05', 'YYYYMM-DD')) "days between" FROM users;
Retrieves:
days between 31
DAYNAME Function
Syntax: DAYNAME(expr) Description: Returns the name of the weekday in English. Example: SELECT TOP 1 DAYNAME('2011-05-30') "dayname" FROM users; Retrieves:
dayname MONDAY
DAYOFMONTH Function
Syntax: DAYOFMONTH(expr) Description: Returns the day of the month in integer. Example: SELECT TOP 1 DAYOFMONTH ('2011-05-30') "dayofmonth" FROM users; Retrieves:
dayofmonth 30
DAYOFYEAR Function
Syntax: DAYOFYEAR(expr) Description: Returns the day of the year in integer.
16
dayofyear 150
EXTRACT Function
Syntax: EXTRACT({YEAR | MONTH | DAY | HOUR | MINUTE | SECOND} FROM datetime_value) Description: Finds and returns the value of a specified datetime field from a datetime_value. Example: SELECT TOP 1 EXTRACT(YEAR FROM TO_DATE('2010-01-04', 'YYYY-MM-DD')) "extract" FROM users; Retrieves:
extract 2010
HOUR Function
Syntax: HOUR(expr) Description: Extract hour from expr. Example: SELECT TOP 1 HOUR ('12:34:56) "hour" FROM users; Retrieves:
hour 12
LAST_DAY Function
Syntax: LAST_DAY(d) Description: Returns the date of the last day of the month that contains d.
Example: SELECT TOP 1 LAST_DAY(TO_DATE('2010-01-04', 'YYYY-MM-DD')) "last day" FROM users; Retrieves:
MINUTE Function
Syntax: MINUTE(expr) Description:
17
minute 34
MONTH Function
Syntax: MONTH(expr) Description: Returns the number of the month from a given date. Example: SELECT TOP 1 MONTH ('2011-05-30) "month" FROM users; Retrieves:
month 5
MONTHNAME Function
Syntax: MONTHNAME(expr) Description: Returns the name of the month in English. Example: SELECT TOP 1 MONTHNAME ('2011-05-30) "monthname" FROM users; Retrieves:
monthname MAY
NEXT_DAY Function
Syntax: NEXT_DAY(d) Description: Returns the date of the next day of d. Example: SELECT TOP 1 NEXT_DAY(TO_DATE('2009-12-31', 'YYYY-MM-DD')) "next day" FROM users; Retrieves:
NOW Function
Syntax:
18
SECOND Function
Syntax: SECOND(t) Description: Returns the second number of a given time t. Example: SELECT TOP 1 SECOND(12:34:56) "second" FROM users; Retrieves:
second 56
SECONDS_BETWEEN Function
Syntax: SECONDS_BETWEEN(d1, d2) Description: Computes the number of seconds between d1 and d2, which is semantically equal to d2 d1. Example: SELECT TOP 1 SECONDS_BETWEEN('2009-12-05', '2010-01-05') "seconds between" FROM users; Retrieves:
WEEK Function
Syntax: WEEK(d) Description: Returns the week number of a given date d.
week 22
19
week day 4
YEAR Function
Syntax: YEAR(d) Description: Returns the year number of a given date d. Example: SELECT TOP 1 YEAR(TO_DATE('2011-05-30', 'YYYY-MM-DD')) "year" FROM users; Retrieves:
year 2011
20
Miscellaneous Functions
COALESCE Function
Syntax : COALESCE(expr_list) Description:
Returns the first non-NULL expression from a list. At least two expressions must be passed into the function, and all expressions must be comparable.The result will be NULL if all the arguments are NULL.
Example: SELECT * FROM tab; Retrieves:
ID 1 2 3
ID 1 2 3
IFNULL Function
Syntax: IFNULL(expr1, expr2) Description: Returns expr1 if expr1 is not NULL and expr2 if expr1 is NULL. Example: SELECT TOP 1 IFNULL('diff', 'same') "ifnull" FROM users; Retrieves:
ifnull diff
NULLIF Function
Syntax: NULLIF(expr1, expr2) Description: NULLIF compares the values of the two expressions. If the first expression equals the second expression, NULLIF returns NULL. If the first expression does not equal the second expression, or if the second expression is NULL, NULLIF returns the first expression.
21
nullif diff
SELECT TOP 1 NULLIF('same', 'same') "nullif" FROM users; Retrieves:
nullif NULL
CURRENT_CONNECTION Function
Syntax: CURRENT_CONNECTION Description: Returns the current connection id. Example: SELECT TOP 1 CURRENT_CONNECTION "current connection" FROM users; Retrieves:
current connection 2
CURRENT_SCHEMA Function
Syntax: CURRENT_SCHEMA Description: Returns the current schema name in the string. Example: SELECT TOP 1 CURRENT_SCHEMA "current schema" FROM users; Retrieves:
CURRENT_USER Function
Syntax: CURRENT_USER Description: Returns the current user name in the string. Example: SELECT TOP 1 CURRENT_USER "current user" FROM users; Retrieves:
current user
22
DATABASE Function
Syntax: DATABASE() Description: Returns the current schema name. Example: SELECT TOP 1 DATABASE() FROM users; Retrieves:
CURRENT_SCHEMA SYSTEM
USER Function
Syntax: USER() Description: Returns the current user name. Example: SELECT TOP 1 USER() "current user" FROM users; Retrieves:
GROUPING_ID Function
Syntax: GROUPING_ID(column_name_list) Description: GROUPING_ID function can be used with GROUPING SETS to return multiple levels of aggregations in a single result set. GROUPING_ID returns an integer value to identify which grouping set each row belongs to. Each column in GROUPING_ID must be an element of the GROUPING SETS. GROUPING_ID is assigned by converting the bit vector generated from GROUPING SETS to a decimal number by treating the bit vector as a binary number. When a bit vector is composed, 0 is assigned to each column specified in the GROUPING SETS and 1 otherwise in the order it appears in the GROUPING SETS. By treating the bit vector as a binary number, this function returns an integer value as the output. Example: SELECT customer, year, product, SUM(sales), GROUPING_ID(customer, year, product) FROM guided_navi_tab GROUP BY GROUPING SETS ( (customer, year, product), (customer, year), (customer, product),
23
CUSTOMER YEAR 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 C1 C1 C2 C2 C1 C1 C2 C2 C1 C1 C2 C2 C1 C2 C1 C2 ? ? ? ? C1 C2 ? ? ? ? ? ? ? ? ? ? ? ?
PRODUCT SUM(SALES) 100 50 200 100 200 150 300 150 300 200 500 250 150 300 350 450 300 150 500 300 500 750 800 450 450 800
GROUPING_ID(CUSTOMER,YEAR,PRODUCT) 0 0 0 0 0 0 0 0 1 1 1 1 2 2 2 2 4 4 4 4 3 3 5 5 6 6
2009 P1 2010 P1 2009 P1 2010 P1 2009 P2 2010 P2 2009 P2 2010 P2 2009 ? 2010 ? 2009 ? 2010 ? P1 P1 P2 P2 2009 P1 2010 P1 2009 P2 2010 P2 ? ? 2009 ? 2010 ? P1 P2
SESSION_CONTEXT Function
Syntax: SESSION_CONTEXT(session_parameter) Description:
24
session context 4
SYSUUID Function
Syntax: SYSUUID Description: Returns the SYSUUID. Example: SELECT TOP 1 SYSUUID FROM users; Retrieves:
SYSUUID 4DE3CD576C79511BE10000000A3C2220
25
Number Functions
Number functions take numeric values or strings with numeric characters as inputs and returns numeric values. When strings with numeric characters are given as inputs, implicit conversion from string to number is performed automatically before computing the result values.
ABS Function
Syntax : ABS ( n ) Description:
Retrieves:
absolute 1
ACOS Function
Syntax: ACOS ( n ) Description:
acos 1.0471975511965979
ASIN Function
Syntax: ASIN ( n ) Description:
asin 0.5235987755982989
ATAN Function 26
Returns the arc-tangent, in radians, of a number n. The argument is a number and the range of n is
unlimited. Example: SELECT TOP 1 ATAN(0.5) "atan" FROM users; Retrieves:
atan 0.4636476090008061
ATAN2 Function
Syntax : ATAN2 (n, m) Description:
Returns the arc-tangent, in radians, of the ratio of two numbers n and m. This is the same result as
ATAN(n/m). Example: SELECT TOP 1 ATAN2(1.0, 2.0) "atan2" FROM users; Retrieves:
atan2 0.4636476090008061
BITAND Function
Syntax: BITAND (expr1, expr2) Description: Calculates an AND operation on the bits of expr1 and expr2. Both expr1 and expr2 must be non-negative integers. The BITAND function returns an integer. Example: SELECT TOP 1 BITAND(255, 123) "bitand" FROM users; Retrieves:
bitand 123
Returns the first integer that is greater or equal to a given value n. For positive numbers, this is known as rounding up. 27
ceiling 15
COS Function
Syntax: COS ( n ) Description:
cos 1.0
COSH Function
Syntax: COSH ( n ) Description: Computes the hyperbolic cosine of n. Example: SELECT TOP 1 COSH(0.5) "cosh" FROM users; Retrieves:
cosh 1.1276259652063807
COT Function
Syntax: COT ( n ) Description: Computes the cotangent of n. Example: SELECT TOP 1 COT(40) "cot" FROM users; Retrieves:
Cot 1.5423510453569202
EXP Function
Syntax: EXP ( n )
28
Description:
Returns the result of the base of natural logarithms e raised to the power of the given argument n.
Example: SELECT TOP 1 EXP(1.0) "exp" FROM users; Retrieves:
exp 2.718281828459045
FLOOR Function
Syntax: FLOOR ( n ) Description:
Returns the largest integer not greater than the given number n.
Example: SELECT TOP 1 FLOOR(14.5) "floor" FROM users; Retrieves:
floor 14
GREATEST Function
Syntax: GREATEST ( n1 [, n2]...) Description: Returns the greatest value among the arguments: n1, n2, ... Example: SELECT TOP 1 GREATEST ('aa', 'ab', 'ba', 'bb') "greatest" FROM users; Retrieves:
greatest bb
LEAST Function
Syntax: LEAST ( n1 [, n2]...) Description: Returns the least value among the arguments: n1, n2... Example: SELECT TOP 1 LEAST('aa', 'ab', 'ba', 'bb') "least" FROM users; Retrieves:
least aa
29
ln
2.1972245773362196
LOG Function
Syntax : LOG(m, n) Description: Returns the natural logarithm of n base m. The base m must be a positive value other than 1 and n must be any positive value. Example: SELECT TOP 1 LOG(10, 2) "log" FROM users; Retrieves:
log 0.30102999566398114
MOD Function
Syntax: MOD(n, d) Description:
modulus
30
POWER Function
Syntax: POWER(b, e) Description:
power 1024.0
ROUND Function
Syntax: ROUND(n [, pos ]) Description:
Rounds n to the specified pos amount of places after the decimal point.
Example: SELECT TOP 1 ROUND(16.16, 1) "round" FROM users; Retrieves:
round 16.2
SELECT TOP 1 ROUND(16.56) "round" FROM users; Retrieves:
round 17
SIGN Function
Syntax: SIGN ( n ) Description:
Returns the sign (positive or negative) of the given number.n. Returns 1 if n is a positive value, -1
if n is a negative value, and 0 if n is equal to zero. Example: SELECT TOP 1 SIGN(-15) "sign" FROM users; Retrieves:
sign -1
SIN Function
Syntax:
31
sine 1.0
SINH Function
Syntax: SINH (n) Description: Returns the hyperbolic sine of n, where
sinh 0.0
SQRT Function
Syntax: SQRT ( n ) Description:
sqrt 1.4142135623730951
TAN Function
Syntax: TAN ( n ) Description:
tan 0.0
TANH Function 32
Syntax: TANH ( n ) Description: Returns the hyperbolic tangent of a number n. Example: SELECT TOP 1 TANH(1.0) "tanh" FROM users; Retrieves:
tanh 0.7615941559557649
33
String Functions
ASCII Function
Syntax: ASCII(c) Description:
ascii 65
CONCAT Function
Syntax : CONCAT(str1, str2) Description: Returns a combined string which consists of str1 followed by str2. The concatenation operator (||) is identical to this function. Example: SELECT TOP 1 CONCAT('C', 'at') "concat" FROM users; Retrieves:
concat Cat
LEFT Function
Syntax : LEFT(str, n) Description:
left Hel
LCASE Function
Syntax: LCASE(s) Description:
Converts all characters in a string s to lowercase. The LCASE function is identical to the LOWER function. 34
lcase test
LENGTH Function
Syntax: LENGTH(s) Description:
Returns the number of characters in the specified string.s. For CLOB or NCLOB, it returns the
length in bytes. Example: SELECT TOP 1 LENGTH('length in char') "length" FROM users; Retrieves:
length 14
LOCATE Function
Syntax: LOCATE(haystack, needle) Description:
Returns the position of sub-string needle within string haystack. Returns 0 if needle is not found
from haystack. Example: SELECT TOP 1 LOCATE('length in char', char) "locate" FROM users; Retrieves:
Locate 11
LOWER Function
Syntax LOWER(s) Description
Converts all characters in a string s to lowercase. The LOWER function is identical to the LCASE function.
Example SELECT TOP 1 LOWER('Ant') "lower" FROM users; Retrieves:
lower ant
35
Retrieves:
lpad 123451234512end
LTRIM Function
Syntax: LTRIM(target [, remove_set]) Description: Removes from the leftmost of target all of the characters that appear in remove_set until reaching a character not in remove_set and then returns the result. If remove_set is not specified, a single blank space is used. Please note that remove_set is treated as a set of characters and not a search string. Example: SELECT TOP 1 LTRIM('babababAabend','ab') "ltrim" FROM users; Retrieves:
ltrim Aabend
REPLACE Function
Syntax: REPLACE (original_string, search_string, replace_string) Description: Searches in original_string for all occurrences of search_string and replaces them with replace_string. If original_string is an empty string, then the result will also be an empty string. If two overlapping substrings match the search_string in the original_string, then only the first occurrence will be replaced with the replace_string. If original_string does not contain any occurrence of search_string, then the function returns the original_string unchanged. If original_string, search_string, or replace_string is NULL, then the function returns NULL.
Example: SELECT TOP 1 REPLACE ('DOWNGRADE DOWNWARD','DOWN', 'UP') "replace" FROM users; Retrieves:
36
right 789
RPAD Function
Syntax: RPAD (target, n[, pattern]) Description: Pad the right side of a target string with spaces or pattern to make the target n characters in length. Example: SELECT TOP 1 RPAD('end', 15, '12345') "right padded" FROM users; Retrieves:
RTRIM Function
Syntax: RTRIM (target [,remove_set ]) Description: Removes from the rightmost of target all of the characters that appear in remove_set until reaching a character not in remove_set and then returns the result. If remove_set is not specified, a single blank space is used. Please note that remove_set is treated as a set of characters and not a search string.
rtrim endabA
SUBSTRING Function
Syntax: SUBSTRING (target, start_position [, string_length]) Description:
Returns a substring of a string target starting from the start_position of the string. SUBSTRING can
either return the remaining part of a string from the start_position or, optionally, a number of characters set by the string_length parameter.
37
substring 45
SUBSTR_AFTER Function
Syntax: SUBSTR_AFTER(target, pattern) Description: Returns a substring of the target string that follows the first occurrence of the pattern argument in the target string. If target does not contain a substring which is the same as pattern, then an empty string is returned. If pattern is an empty string, then target is returned. If target or pattern is NULL, then NULL is returned.
Example: SELECT TOP 1 SUBSTR_AFTER('Hello My Friend','My ') "substr after" FROM users; Retrieves:
SUBSTR_BEFORE Function
Syntax: SUBSTR_BEFORE(target, pattern) Description: Returns a substring of the target string before the first occurrence of the pattern argument in the target string. If target does not contain a substring which is the same as pattern, then an empty string is returned. If pattern is an empty string, then target is returned. If target or pattern is NULL, then NULL is returned. Example: SELECT TOP 1 SUBSTR_BEFORE('Hello My Friend','My') "substr before" FROM users; Retrieves:
TRIM Function
Syntax: TRIM ([[LEADING | TRAILING | BOTH] trim_char FROM] target_string ) Description: Returns a string after trimming a trim_char from trim_source string. The trimming operation is carried out either from the start (LEADING), end (TRAILING) or both(BOTH) ends of the target_string.
38
Example: SELECT TOP 1 TRIM (a FROM aaa123456789aa) "trim both" FROM users; Retrieves:
UCASE Function
Syntax : UCASE(target) Description:
ucase ANT
UNICODE Function
Syntax : UNICODE(c) Description:
Returns an integer containing the Unicode code point of the first character in the string, or NULL if the first character is not a valid encoding.
Example: SELECT TOP 1 UNICODE('#') "unicode" FROM users; Retrieves:
unicode 54620
UPPER Function
Syntax: UPPER(target) Description:
39
uppercase ANT
40
SQL Statements
This chapter describes the SQL statements that are supported by the HDB.
ALTER TABLE
SQL Syntax: ALTER TABLE table_name { <add_column_clause> | < drop_column_clause> | <add_primary_key_clause> | < drop_primary_key_clause> | < preload_clause> | <table_conversion_clause> | < move_clause> } [WITH PARAMETERS ( parameter_key_value, ... ) ] Parameters: add_column_clause::= ADD ( column_definition, ... ) drop_column_clause ::=DROP ( column_name, ... ) add_primary_key_clause ::= ADD [CONSTRAINT constraint_name] PRIMARY KEY ( column_name, ... ) CONSTRAINT Specifies the name of a constraint. PRIMARY KEY A primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constraint. It prohibits multiple rows from having the same value in the same column. drop_primary_key_clause ::= DROP PRIMARY KEY preload_clause ::= PRELOAD ALL | PRELOAD ( column_name ) | PRELOAD NONE PRELOAD sets/removes the preload flag of the given tables or columns. As a consequence, these tables are automatically loaded into memory after an index server start. The current status of the preload flag is visible in the system table TABLES, column PRELOAD, possible values ('FULL', 'PARTIALLY', 'NO' ) and in system table TABLE_COLUMNS, column PRELOAD, possible values ('TRUE', 'FALSE').
table_conversion_clause ::= [ MODIFY TYPE ] [ ROW | COLUMN ] [ THREADS int_constr ] [ BATCH batch_size ] MODIFY TYPE ROW | COLUMN This command is used to convert the table storage from ROW to COLUMN or from COLUMN to ROW. THREADS int_const Specifies how many threads should be used in parallel for table conversion. Default: The default value is TABLE_CONVERSION_PARALLELISM that is, the number of CPU cores specified in the indexserver.ini file. BATCH int_const
41
WITH PARAMETERS ( <parameter_key_value>, ... ) Column store-specific options can be passed in using the "WITH PARAMETERS" clause. Keys and single values can be any string constant Duplicate keys are allowed Keys are automatically mapped into their uppercase representation parameter_key_value ::= <string_literal> = <string_literal> | <identifier> = <string_literal> | <string_literal> = (<string_literal>, ... ) Current parameters AUTO_MERGE = 'ON' | 'OFF' Default=ON Automatic delta merge triggered by memwatcher. CONCAT_ATTRIBUTE = (new_att, exist_att_list) Creating concatenated attributes: new_att:= string_literal exist_att_list = 'string_literal' | exist-att-list ',' 'string_literal' DELETE_CONCAT_ATTRIBUTE := exist_att Deleting concatenated attributes: exist_att := 'string_literal' TREX_FLAGS = ('column_name','flags'), Flags are the implementation flags; the integer value is currently between 0 and 67108864. INDEX_PROPERTY = ('prop#', 'value') Changing column table properties ALTER TABLE <table_name> WITH PARAMETERS (INDEX_PROPERTY = ('prop#', 'value') ) PARTITION_SPEC = 'partition spec',
42
43
44
45
46
47
48
49
Description: The CREATE COLUMN VIEW statement is used to create a column view. TYPE clause can be omitted and default type is JOIN_INDEX. SQL expression and PARAMETERS cannot be defined together. SQL expression only, or PARAMETERS only, is allowed. A column view can be created when the kernel has metadata information about all physical indexes of the column view. Creating a column view referencing row tables, row views or other column views is not allowed. SQL Join with column views is allowed. When a PARAMETERS clause is defined, Column list can't be defined. Object names of view attributes will be field names. At least one viewAttribute must be defined. If there is a conflict between TYPE clause and indexType parameter, an error will be thrown. When a column table is dropped, all column views referencing the table are dropped at the same time.
Example 1: Creating a simple JOIN view DROP TABLE CUSTOMER; DROP TABLE ORDERS; CREATE CREATE INSERT INSERT INSERT INSERT INSERT INSERT COLUMN TABLE CUSTOMER ( ID INT PRIMARY KEY, NAME VARCHAR(30) ); COLUMN TABLE ORDERS ( ID INT PRIMARY KEY, CID INT, AMOUNT DECIMAL(10,2) ); INTO CUSTOMER VALUES (1, 'Cust01'); INTO ORDERS VALUES (1, 1, 10); INTO ORDERS VALUES (2, 1, 110); INTO ORDERS VALUES (3, 1, 120); INTO CUSTOMER VALUES (2, 'Cust02'); INTO ORDERS VALUES (4, 2, 110);
CREATE COLUMN VIEW CUSTORDERS TYPE JOIN WITH PARAMETERS ( joinIndex=CUSTOMER,joinIndexType=0, joinIndex=ORDERS,joinIndexType=0, joinCondition=('JC01', CUSTOMER, ID, ORDERS, CID, '', 0, 0), joinPath=('JP01', 'JC01'), view=('V_CUSTORDERS', CUSTOMER), defaultView='V_CUSTORDERS', viewAttribute=('NAME', CUSTOMER, NAME, 'JP01', 'default', 'attribute'), viewAttribute=('AMOUNT', ORDERS, AMOUNT, 'JP01', 'default', 'attribute'), constraint=('CONSTR1', 0, '', CUSTOMER, ID, 'eq', '$$CID$$'), optimizemetamodel=1 ); SELECT * FROM CUSTORDERS WITH PARAMETERS ('PLACEHOLDER' = ('$$CID$$' , '1'));
50
Example 4: Creating a hierarchy view CREATE COLUMN VIEW h17$hli TYPE hierarchy WITH PARAMETERS ( hierarchyDefinitionType' = 'JSON', hierarchyDefinition = '{"sourceType":"recursive", "sourceQuery":"SELECT pred,succ FROM h17$hli", "rootNode":-1}');
51
52
53
54
CREATE SEQUENCE
SQL Syntax: CREATE SEQUENCE sequence_name [ <sequence_parameter_list> ] [RESET BY <subquery>] Parameters: sequence_parameter_list ::= sequence_parameter, ... sequence_parameter ::= INCREMENT BY integer | START WITH integer | MAXVALUE integer | NOMAXVALUE | MINVALUE integer | NOMINVALUE | CYCLE | NOCYCLE INCREMENT BY
Defines the amount the next sequence value is incremented from the last value assigned. The default is 1. Specify a negative value to generate a descending sequence. An error is returned if the INCREMENT BY value is 0
START WITH
Defines the starting sequence value. If you do not specify a value for the START WITH clause, MINVALUE is used for ascending sequences and MAXVALUE is used for descending sequences.
MAXVALUE
Defines the largest value generated by the sequence and must be between 0 and (2^63)-1 = 9223372036854775807.
NOMAXVALUE When MAXVALUE is not specified, the maximum value for an ascending sequence is the maximum value for a descending sequences is -1.
(2^63)-1 and
MINVALUE The minimum value of a sequence can be specified after MINVALUE and is between 0 and 4611686018427387903. NOMINVALUE When MINVALUE is not specified, the minimum value for an ascending sequence is 1 and the minimum value for a descending is -4611686018427387903. CYCLE The sequence number will be reused after it reaches its maximum or minimum value. NOCYCLE Default option. The sequence number will not be reused after it reaches its maximum or minimum value.
55
During the rebooting of the database, database automatically executes the RESET BY statement and returns the result to assign the sequence value. Therefore, no additional effort is needed to manage a sequence. Example 2: If the sequence s is used to create a unique key on column A in the table R, then after a database is restarted, a UNIQUE key value can be created by automatically assigning the maximum value of column A to the sequence value using a RESET BY statement as follows: CREATE SEQUENCE s RESET BY SELECT IFNULL(MAX(a), 0) + 1 FROM r;
56
57
58
NULL | NOT NULL The NOT NULL constraint prohibits a column value from being NULL. If NULL is specified it is not considered a constraint, it represents a column that may contain a null value. The default is NULL. UNIQUE Specifies a column as a unique key. A composite unique key enables the specification of multiple columns as a unique key. With a unique constraint, multiple rows cannot have the same value in the same column. PRIMARY KEY A primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constraint. It prohibits multiple rows from having the same value in the same column. BTREE | CPBTREE Specifies the index type. When column data types are character string types, binary string types, decimal types, or when the constraint is a composite key, or non-unique constraint, the default index type is CPBTREE. Otherwise, BTREE is used. BTREE keyword has to be used in order to use B+-tree index and the CPBTREE keyword has to be used for the CPB+-tree index. If the index type is omitted, the HDB chooses the appropriate index considering the column data types. table_constraint ::=UNIQUE [BTREE | CPBTREE] | PRIMARY KEY [BTREE | CPBTREE] This defines a table constraint which can be used on one or more columns of a table. There are two kinds of a table constraint. They are: UNIQUE Specifies a uniqueness constraint for a column. This prevents multiple rows from having the same values in the same column list. PRIMARY KEY A primary key constraint is a combination of the NOT NULL and UNIQUE constraints. It creates a unique column that can be always be used to locate rows uniquely within a table. BTREE | CPBTREE Specifies the index type. When column data types are character string types, binary string types, decimal types, or when the constraint is a composite key, or non-unique constraint, the default index type is CPBTREE, BTREE is used in all other cases. BTREE keyword has to be used in order to use the B+-tree index and the CPBTREE keyword has to be used for the CPB+-tree index. If the index type is omitted, the HDB chooses the appropriate index considering the column data types. as_table_subquery ::=AS TABLE as_table_name | AS ( <select_query>)
59
parameter_key_value 'AUTO_MERGE' = 'ON' | 'OFF' Default=ON Automatic delta merge triggered by memwatcher. 'SESSION_TYPE' = 'SIMPLE' | 'HISTORY' Default = 'SIMPLE' Creates a table with a particular transaction session type. Tables with session type HISTORY support time travel. 'COMPRESSION' = ('column_name', 'compression_type'): Sets the compression for the specified column. ('compression_type' = 'PREFIXED' | 'SPARSE' | 'CLUSTERED' | 'INDIRECT' | 'RLE') 'INDEX' = ('column_name', 'index_type') Sets the inverted index for the specified column. ('index_type' 'FULL' | 'SIGNATURE') 'FAST_PREPROCESS' = 'ON' | 'OFF' Turns fast preprocessing on or off. This feature is used for column tables that have attributes that should support text-search on attribute of type 'AETEXT'. 'LOCATION' = 'host:port' Specifies the target node for table creation with host:port. If not provided, the table will be automatically assigned to one node. This option can be used for both row store and column store tables in a distributed environment. 'PARTITION_SPEC' = 'partition spec', For example 'HASH 2 a, b' where 2 is the number of parts to create and 'a' and 'b' are the columns that are used to create that hash value for each row. 'TREX_FLAGS' = ('column_name','flags'), Flags are the implementation flags, integer value is currently between 0 and 67108864. Description: The CREATE TABLE statement creates a table. Tables are created without data except when as_table_subquery is used with the WITH DATA option.
60
61
62
63
64
65
66
67
68
DROP SCHEMA
SQL Syntax: DROP SCHEMA schema_name [ <drop_option> ] Parameters: drop_option ::= CASCADE | RESTRICT Default = RESTRICT Restrict drop behavior will drop the object when there is no dependent object. If there is a dependent object, an error will be thrown. Cascaded drop drops the object and dependent objects. Non-Cascaded drop option is not supported for dropping SCHEMA. Description: The DROP SCHEMA statement removes a schema. Example: DROP SCHEMA my_schema;
69
70
71
72
73
74
75
76
77
78
Analytics CREATE SCENARIO: This privilege controls the creation of calculation scenarios and cubes (CalcEngine) via the following SQL commands: o ADD SCENARIO, o CREATE CALCULATION SCENARIO. SCENARIO ADMIN: This privilege authorizes all calculation scenario-related activities (including creation), which can be performed using the following SQL commands: o ADD SCENARIO, FLATTEN SCENARIO o CREATE CALCULATION SCENARIO, DROP CALCULATION SCENARIO CREATE STRUCTURED PRIVILEGE: This privilege authorizes the creation of structured privileges (analytical privileges) via the SQL command ADD STRUCTURED PRIVILEGE. Note that currently, only the owner of an analytical privilege can drop it. Therefore, there is no additional privilege for dropping structured privileges. ADMIN: This privilege controls the execution of auditing-related SQL commands: CREATE AUDIT POLICY DROP AUDIT POLICY ALTER AUDIT POLICY
Auditing AUDIT o o o
System Management These privileges authorize the various system activities. Due to the potentially high level of impact on the system, these privileges are not relevant to a normal database user and should only be granted with care (for example, only to a support user/role.) Data Import and Export Two system privileges are available for authorization of the data import/export in the database: IMPORT: This privilege authorizes the import activity in the database via the commands IMPORT or LOAD TABLE. Note that beside this privilege, the user still needs the INSERT privilege on the target tables to be imported. EXPORT: This privilege authorizes the export activity in the database via the commands EXPORT or LOAD TABLE. Note that beside this privilege, the user still needs the INSERT privilege on the source tables to be exported Object Privileges Object privileges are bound to objects they provide authorization for. The following table lists the different object privileges supported by SAP HANA Database and the object types for which the privileges can be employed (OK: applicable, -: not applicable, ?: dynamically evaluated - see Notes):
79
CREATE ANY: This privilege allows the creation of all kinds of objects, in particular tables, views, sequences, synonyms, SQL Script functions, or DB procedures in a schema. Obviously, this privilege can only be granted on a schema. ALL PRIVILEGES: This is actually a collection of all privileges, which a) the grantor currently has and is allowed to grant further, and b) can be granted on this particular object. This collection is dynamically evaluated for the given grantor and object. ALL PRIVILEGES is not applicable to a schema, but only a table, view, or table type. DROP and ALTER are DDL privileges and authorize the DROP and ALTER SQL commands. While the DROP privilege is valid for all kinds of objects, the ALTER privilege is not valid for sequences and synonyms as their definitions cannot be changed after creation. INDEX is a special DDL privilege and authorizes the creation/changing/dropping of indexes for an object via the commands CREATE INDEX, DROP INDEX, and ALTER INDEX. This privilege can only be applied to schema, table, and table type. SELECT, INSERT, UPDATE and DELETE are DML privileges and authorize respective SQL commands. While SELECT is valid for all kinds of objects, except for functions/procedures, INSERT, UPDATE, and DELETE are valid for schemas, tables, and table types only. EXECUTE is a special DML privilege and authorizes the execution of a SQL Script function or a DB procedure via the commands CALL.
Notes: All privileges are valid for schemas in the sense that they authorize applicable activities on the objects created in this schema. Synonyms can be created for tables, views, sequences, and database procedures, but not for schemas and SQL Script functions. The privileges which can be granted and revoked on a synonym will be determined by the actual object the synonym stands for.
80
81
82
WITH PARAMETERS (parameter_key_value): Column store-specific options can be passed in using the "WITH PARAMETERS" clause. Keys and single values can be any string constant Duplicate keys are allowed Keys are automatically mapped into their uppercase representation parameter_key_value ::= <string_literal> = <string_literal> | <identifier> = <string_literal> | <string_literal> = (<string_literal>, ... ) Current parameters 'SMART_MERGE' = 'ON' | 'OFF' When SMART_MERGE is ON, the database does a smart merge, this means database decides whether to merge or not based on merge criteria specified in automerge section of indexserver configuration PASSPORT=<string> Merge request call tag to identify the request in related traces MEMORY_MERGE=ON|OFF Database merges delta index in memory only, it will not be persisted.
Description The MERGE DELTA statement merges deltas to a main column store table. Since column stores are read optimized and compressed, deltas are introduced to optimize insert or updates. For this to function, insertions are passed stored in a delta to the original table data. The MERGE DELTA command can then be used to merge the delta changes back into the main table store. HISTORY Can be specified to merge history deltas into the history main on column store history tables. PART Can be specified to merge a delta of a partition to main column table. Example: MERGE DELTA OF A; MERGE DELTA OF A PART 1; MERGE HISTORY DELTA OF A; MERGE HISTORY DELTA OF A PART 1;
83
84
85
86
87
88
Description: select_clause ::= SELECT [ TOP expr ] [ ALL | DISTINCT ] < select_columns > select_columns::= { <select_item> , } select_item ::= [ [schema_name.] table_name.]* | expr [ AS ] column_alias The SELECT statement retrieves data from one or more tables in the database. TOP Returns the fist expr records from the SQL statement. DISTINCT, ALL DISTINCT returns only one instance per duplicated row in the selected rows. ALL (the default) will return all rows selected including duplicates. select_columns With select columns you can specify exactly which columns you wish to return * [asterisk] selects all columns the from all tables or views listed in the FROM clause column_alias, table_alias Using an alias you can rename an existing column or table name. from_clause ::= FROM <table>, table ::= table_name [ [AS] table_alias ] | <subquery> [ [AS] table_alias ] | <joined_table> joined_table ::= <table> [ <join_type> ] JOIN <table> ON <predicate> | <table> CROSS JOIN <table> | <joined_table> join_type::=INNER | {LEFT | RIGHT | FULL} OUTER The join_type specifies the type of join to be created. RIGHT indicates a right outer join, LEFT indicates a left outer join, and FULL indicates a full outer join. The OUTER keyword may or may not be used when an outer join is being performed. ON predicate The ON clause specifies a join predicate. CROSS JOIN You can use a CROSS JOIN to combine rows from each of the tables in the join as a Cartesian product. where_clause ::=WHERE <predicate> The WHERE predicate is used to specify a predicate on the records to be selected. By using WHERE you can filter the results set and only retrieve required rows.
89
90
91
92
93
94
95
96