Phoenix SQL语法

数据库

CREATE SCHEMA

CREATE SCHEMA [IF NOT EXISTS] 数据库名字
0: jdbc:phoenix:CentOS> create schema if not exists jiangzz;
Error: ERROR 725 (43M08): Cannot create schema because config phoenix.schema.isNamespaceMappingEnabled for enabling name space mapping isn't enabled. schemaName=jiangzz (state=43M08,code=725)
java.sql.SQLException: ERROR 725 (43M08): Cannot create schema because config phoenix.schema.isNamespaceMappingEnabled for enabling name space mapping isn't enabled. schemaName=jiangzz

提示必须同时修改$HASE_HOME/conf/hbase-site.xml文件和 $PHOENIX_HOME/bin/hbase-site.xml文件,修改完成重启Hbase服务

<property>
  <name>phoenix.schema.isNamespaceMappingEnabledname>
  <value>truevalue>
property>
<property>
  <name>phoenix.schema.mapSystemTablesToNamespacename>
  <value>truevalue>
property>
0: jdbc:phoenix:CentOS> create schema if not exists jiangzz;
No rows affected (0.052 seconds)

USE

USE 数据库名
0: jdbc:phoenix:CentOS> use jiangzz;
No rows affected (0.003 seconds)

DROP SCHEMA

DROP SCHEMA IF EXISTS 数据库名
0: jdbc:phoenix:CentOS> drop schema if exists jiangzz;
No rows affected (0.005 seconds)

用户表

CREATE TABLE

CREATE TABLE IF NOT EXISTS [SCHEMA.]表名(
 字段名 约束,
 ...
)
表选项
create table if not exists t_user(
 id integer primary key ,
 name varchar(128),
 sex boolean,
 birthDay date,
 salary decimal(7,2)
)

DESC TABLE

 !DESC [SCHEMA.]表名;
0: jdbc:phoenix:CentOS> !desc jiangzz.t_user;
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
| TABLE_CAT  | TABLE_SCHEM  | TABLE_NAME  | COLUMN_NAME  | DATA_TYPE  | TYPE_NAME  | COLUMN_SIZE  | BUFF |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
|            | JIANGZZ      | T_USER      | ID           | 4          | INTEGER    | null         | null |
|            | JIANGZZ      | T_USER      | NAME         | 12         | VARCHAR    | 128          | null |
|            | JIANGZZ      | T_USER      | SEX          | 16         | BOOLEAN    | null         | null |
|            | JIANGZZ      | T_USER      | BIRTHDAY     | 91         | DATE       | null         | null |
|            | JIANGZZ      | T_USER      | SALARY       | 3          | DECIMAL    | 7            | null |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+

DROP TABLE

DROP TABLE	IF EXISTS 表名 [CASCADE]	
0: jdbc:phoenix:CentOS> drop table if exists  t_user cascade;
No rows affected (0.003 seconds)

cascade表示会将该表以及该表的所有依赖都给删除。

ALTER TABLE

ALTER	(TABLE|VIEW)	表名 ADD [IF NOT EXISTS] 列名
ALTER	(TABLE|VIEW)	表名 DROP COLUMN [IF EXISTS] 列名 
ALTER	(TABLE|VIEW)	表名 SET options
0: jdbc:phoenix:CentOS> !desc t_user;
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
| TABLE_CAT  | TABLE_SCHEM  | TABLE_NAME  | COLUMN_NAME  | DATA_TYPE  | TYPE_NAME  | COLUMN_SIZE  | BUFF |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
|            | JIANGZZ      | T_USER      | ID           | 4          | INTEGER    | null         | null |
|            | JIANGZZ      | T_USER      | NAME         | 12         | VARCHAR    | 128          | null |
|            | JIANGZZ      | T_USER      | SEX          | 16         | BOOLEAN    | null         | null |
|            | JIANGZZ      | T_USER      | BIRTHDAY     | 91         | DATE       | null         | null |
|            | JIANGZZ      | T_USER      | SALARY       | 3          | DECIMAL    | 7            | null |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
0: jdbc:phoenix:CentOS> alter table t_user add if not exists age integer; 
No rows affected (6.082 seconds)
0: jdbc:phoenix:CentOS> !desc t_user;
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
| TABLE_CAT  | TABLE_SCHEM  | TABLE_NAME  | COLUMN_NAME  | DATA_TYPE  | TYPE_NAME  | COLUMN_SIZE  | BUFF |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
|            | JIANGZZ      | T_USER      | ID           | 4          | INTEGER    | null         | null |
|            | JIANGZZ      | T_USER      | NAME         | 12         | VARCHAR    | 128          | null |
|            | JIANGZZ      | T_USER      | SEX          | 16         | BOOLEAN    | null         | null |
|            | JIANGZZ      | T_USER      | BIRTHDAY     | 91         | DATE       | null         | null |
|            | JIANGZZ      | T_USER      | SALARY       | 3          | DECIMAL    | 7            | null |
|            | JIANGZZ      | T_USER      | AGE          | 4          | INTEGER    | null         | null |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
0: jdbc:phoenix:CentOS> alter table t_user drop column if  exists age ; 
20/01/10 19:17:32 WARN query.ConnectionQueryServicesImpl: Unable to update meta data repo within 1 seconds for T_USER
No rows affected (1.093 seconds)
0: jdbc:phoenix:CentOS> alter table t_user drop column if  exists age ; 
No rows affected (0.013 seconds)
0: jdbc:phoenix:CentOS> !desc t_user;
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
| TABLE_CAT  | TABLE_SCHEM  | TABLE_NAME  | COLUMN_NAME  | DATA_TYPE  | TYPE_NAME  | COLUMN_SIZE  | BUFF |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
|            | JIANGZZ      | T_USER      | ID           | 4          | INTEGER    | null         | null |
|            | JIANGZZ      | T_USER      | NAME         | 12         | VARCHAR    | 128          | null |
|            | JIANGZZ      | T_USER      | SEX          | 16         | BOOLEAN    | null         | null |
|            | JIANGZZ      | T_USER      | BIRTHDAY     | 91         | DATE       | null         | null |
|            | JIANGZZ      | T_USER      | SALARY       | 3          | DECIMAL    | 7            | null |
+------------+--------------+-------------+--------------+------------+------------+--------------+------+
0: jdbc:phoenix:CentOS> alter table t_user set TTL=100;
No rows affected (5.897 seconds)
0: jdbc:phoenix:CentOS> upsert into t_user(id,name,sex,birthDay,salary) values(1,'jiangzz',true,'1990-12-16',5000.00);
1 row affected (0.025 seconds)
0: jdbc:phoenix:CentOS> select * from t_user;
+-----+----------+-------+--------------------------+---------+
| ID  |   NAME   |  SEX  |         BIRTHDAY         | SALARY  |
+-----+----------+-------+--------------------------+---------+
| 1   | jiangzz  | true  | 1990-12-16 00:00:00.000  | 5E+3    |
+-----+----------+-------+--------------------------+---------+
1 row selected (0.125 seconds)
0: jdbc:phoenix:CentOS>

数据管理

插入&更新

0: jdbc:phoenix:CentOS> upsert into t_user(id,name,sex,birthDay,salary) values(1,'jiangzz',true,'1990-12-16',5000.00);
1 row affected (0.014 seconds)

删除数据

0: jdbc:phoenix:CentOS> delete from t_user where id=1;
1 row affected (0.007 seconds)

查询数据

0: jdbc:phoenix:CentOS> select * from t_user;
+-----+-----------+--------+--------------------------+---------+
| ID  |   NAME    |  SEX   |         BIRTHDAY         | SALARY  |
+-----+-----------+--------+--------------------------+---------+
| 1   | jiangzz   | true   | 1990-12-16 00:00:00.000  | 5E+3    |
| 2   | zhangsan  | false  | 1990-12-16 00:00:00.000  | 6E+3    |
+-----+-----------+--------+--------------------------+---------+
2 rows selected (0.055 seconds)
0: jdbc:phoenix:CentOS> select * from t_user where name like '%an%' order by salary desc limit 10;
+-----+-----------+--------+--------------------------+---------+
| ID  |   NAME    |  SEX   |         BIRTHDAY         | SALARY  |
+-----+-----------+--------+--------------------------+---------+
| 2   | zhangsan  | false  | 1990-12-16 00:00:00.000  | 6E+3    |
| 1   | jiangzz   | true   | 1990-12-16 00:00:00.000  | 5E+3    |
+-----+-----------+--------+--------------------------+---------+
2 rows selected (0.136 seconds)

你可能感兴趣的:(Phoenix)