创建表

启动数据库
hawq start cluster --with_magma

启动 hdfs
start-dfs.sh

进入数据库
psql -d postgres

\l 或者 select dataname from pg_databse;

修改系统时间
hwclock --systohc
hwclock --set --date="12/20/22 09:52"
clock --set --date="12/20/22 09:52"

创建表
CREATE TABLE my_first_table(
first_column text,
second_column integer
);

查看表字段描述
\dS+ my_first_table

OushuDB 现在支持多种存储格式:AO、ORC、Magma
行/列存储 存储格式 是否支持新执行器 压缩 是否 UPDATE/DELETE 是否 INDEX
AO: 行 自定义存储格式 不支持 支持 SNAPPY,ZLIB 不支持 不支持
ORC: 行列混合存储 兼容标准 ORC 格式 支持 支持 SNAPPY,LZ4 支持 不支持
MAGMA: 行列混合存储 自定义存储格式 支持 自动压缩算法 支持 支持

删除表
DROP TABLE my_first_table;

缺省值
CREATE TABLE products(
product_no integer,
name text,
price numeric DEFAULT 9.99
);

--表达式缺省值
CREATE TABLE purchaseHistory(
purchase_id integer,
product_no integer,
time timestamp DEFAULT now()
);

MAGMA 格式的表不支持为列设置缺省值;

约束
检查约束 非空约束 主键

检查约束:
CREATE TABLE product(
product_no integer,
name text,
price numeric CHECK(price > 0)
);

有独立名字的约束

CREATE TABLE product(
product_no integer,
name text,
price numeric(10,2) constraint positive_price CHECK(price > 0)
);

多列约束采用表约束
CREATE TABLE product(
product_no integer,
name text,
price numeric check(price > 0),
discounted_price numeric check(discounted_price > 0),
check (price > discounted_price)
);

执行之后

NOTICE: Using DECIMAL(10,0) for ORC DECIMAL instead
NOTICE: Using DECIMAL(10,0) for ORC DECIMAL instead
CREATE TABLE
testdb=#

插入一条记录
insert into product
select 1001,'ASFASD',9.99,8.99;

查看结果
select * from product;

testdb-# ;
product_no | name | price | discounted_price
------------+---------+-------+------------------
1001 | ASDFADS | 10 | 9
(1 row)

--数字设置精度之后执行
CREATE TABLE product(
product_no integer,
name text,
price numeric(10,2) check(price > 0),
discounted_price numeric(10,2) check(discounted_price > 0),
check (price > discounted_price)
);

CREATE TABLE
testdb=# insert into product
select 1001,'ASDFADS',9.99,8.99;
INSERT 0 1

--查看结果
testdb=# select * from product
;
product_no | name | price | discounted_price
------------+---------+-------+------------------
1001 | ASDFADS | 9.99 | 8.99
(1 row)

testdb=#

--有独立名字的表约束
CREATE TABLE product(
product_no integer,
name text,
price numeric(10,2) check(price > 0),
discounted_price numeric(10,2) check(discounted_price > 0),
constraint valid_discount check (price > discounted_price)
);

注意:当约束表达式计算结果为真或 NULL 的时候,检查约束会被认为是满足条件的,
因为大多数表达式在含有 NULL 操作数的时候结果都是 NULL,所以这些约束不能阻止列值为 NULL;

非空约束

CREATE TABLE product(
product_no integer NOT NULL,
name text NOT NULL,
price numeric(10,2)
);

一个列可以有多个约束,只要一个接着一个写就可以了,例如价格设置为非空且大于零
顺序无所谓,顺序并不影响约束检查的顺序

CREATE TABLE product(
product_no integer NOT NULL,
name text NOT NULL,
price numeric(10,2) NOT NULL CHECK(price > 0)
);

主键约束:
AO、ORC、MAGMA 中 只有 MAGMA 表支持主键约束,而且在创建 MAGMA 表时可以声明主键列,
且主键约束的列里数据类型为非定长时需要将该列放置最后一列

CREATE TABLE products(
product_no integer,
name text,
price numeric,
primary key(product_no)
) FORMAT 'Magma';

testdb=# CREATE TABLE products(
testdb(# product_no integer,
testdb(# name text,
testdb(# price numeric,
testdb(# primary key(product_no)
testdb(# ) FORMAT 'Magma';
ERROR: invalid parameter value for "orientation": "Magma"
报错

magma 的表是不支持列约束和非空约束的,但支持主键约束;

评论
    test