Project: ShardingSphere (App Definition and Development / Database)
File: create-encrypt-rule.en.md

+++ title = "CREATE ENCRYPT RULE" weight = 1 +++ The `CREATE ENCRYPT RULE` syntax is used to create encrypt rules. {{< tabs >}} {{% tab name="Grammar" %}} ```sql CreateEncryptRule ::= 'CREATE' 'ENCRYPT' 'RULE' ifNotExists? encryptDefinition (',' encryptDefinition)* ifNotExists ::= 'IF' 'NOT' 'EXISTS' encryptDefinition ::= ruleName '(' 'COLUMNS' '(' columnDefinition (',' columnDefinition)* ')' ')' columnDefinition ::= '(' 'NAME' '=' columnName ',' 'CIPHER' '=' cipherColumnName (',' 'ASSISTEDQUERY' '=' assistedQueryColumnName)? (',' 'LIKEQUERY' '=' likeQueryColumnName)? ',' encryptAlgorithmDefinition (',' assistedQueryAlgorithmDefinition)? (',' likeQueryAlgorithmDefinition)? ')' encryptAlgorithmDefinition ::= 'ENCRYPT_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' algorithmType (',' propertiesDefinition)? ')' assistedQueryAlgorithmDefinition ::= 'ASSISTEDQUERYALGORITHM' '(' 'TYPE' '(' 'NAME' '=' algorithmType (',' propertiesDefinition)? ')' likeQueryAlgorithmDefinition ::= 'LIKEQUERYALGORITHM' '(' 'TYPE' '(' 'NAME' '=' algorithmType (',' propertiesDefinition)? ')' propertiesDefinition ::= 'PROPERTIES' '(' key '=' value (',' key '=' value)* ')' ruleName ::= identifier columnName ::= identifier cipherColumnName ::= identifier assistedQueryColumnName ::= identifier likeQueryColumnName ::= identifier algorithmType ::= string key ::= string value ::= literal ``` {{% /tab %}} {{% tab name="Railroad diagram" %}} <iframe frameborder="0" name="diagram" id="diagram" width="100%" height="100%"></iframe> {{% /tab %}} {{< /tabs >}} `CIPHER` specifies the cipher column, `ASSISTEDQUERY` specifies the assisted query column`LIKEQUERY` specifies the like query column; `algorithmType` specifies the encryption algorithm type, please refer to ; Duplicate `ruleName` will not be created; `ifNotExists` clause used for avoid `Duplicate encrypt rule` error. ```sql CREATE ENCRYPT RULE t_encrypt ( COLUMNS( (NAME=userid,CIPHER=usercipher,ASSISTEDQUERY=assistedqueryuser,LIKEQUERY=likequeryuser,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))), (NAME=orderid,CIPHER =ordercipher,ASSISTEDQUERY=assistedqueryorder,LIKEQUERY=likequeryorder,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))) )), tencrypt2 ( COLUMNS( (NAME=userid,CIPHER=usercipher,ASSISTEDQUERY=assistedqueryuser,LIKEQUERY=likequeryuser,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))), (NAME=orderid, CIPHER=ordercipher,ASSISTEDQUERY=assistedqueryorder,LIKEQUERY=likequeryorder,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))) )); ``` ```sql CREATE ENCRYPT RULE IF NOT EXISTS t_encrypt ( COLUMNS( (NAME=userid,CIPHER=usercipher,ASSISTEDQUERY=assistedqueryuser,LIKEQUERY=likequeryuser,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))), (NAME=orderid,CIPHER =ordercipher,ASSISTEDQUERY=assistedqueryorder,LIKEQUERY=likequeryorder,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))) )), tencrypt2 ( COLUMNS( (NAME=userid,CIPHER=usercipher,ASSISTEDQUERY=assistedqueryuser,LIKEQUERY=likequeryuser,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))), (NAME=orderid,CIPHER=ordercipher,ASSISTEDQUERY=assistedqueryorder,LIKEQUERY=likequeryorder,ENCRYPTALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc'))),ASSISTEDQUERYALGORITHM(TYPE(NAME='MD5')),LIKEQUERYALGORITHM(TYPE(NAME='CHARDIGEST_LIKE'))) )); ``` `CREATE`, `ENCRYPT`, `RULE`, `COLUMNS`, `NAME`, `CIPHER`, `ASSISTEDQUERY`, `LIKEQUERY`, `ENCRYPTALGORITHM`, `ASSISTEDQUERYALGORITHM`, `LIKEQUERY_ALGORITHM`, `TYPE`, `TRUE`, `FALSE`