Postgres SQL约束字符类型

时间:2015-06-15 12:21:15

标签: postgresql constraints

我在Postgres中有一个表定义。我想为一个Character数据类型的列添加一个约束,使其只有3个允许的值:

CREATE TABLE my_table
(
  id character varying(255) NOT NULL,
  uid character varying(255) NOT NULL,
  my_text text NOT NULL,
  is_enabled boolean NOT NULL
);

所以我希望my_text列只包含' A',' B'或者' C'作为价值观。

我在哪里可以找到关于此的文档?

2 个答案:

答案 0 :(得分:3)

使用检查约束:

CREATE TABLE my_table
(
  id character varying(255) NOT NULL,
  uid character varying(255) NOT NULL,
  my_text text NOT NULL,
  is_enabled boolean NOT NULL,
  constraint check_allowed check (my_text in ('A', 'B', 'C'))
);

手册中的更多详细信息:http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS

答案 1 :(得分:1)

如果您希望能够在不修改条件的情况下添加caracters:

my_text

您将无法在my_ref中添加不在<div class="row"> <?php $post_type = 'product'; $tax = 'productcat'; $args = array( 'type' => 'product', 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 0, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'productcat', 'pad_counts' => false ); $categories = get_categories( $args ); foreach($categories as $category): $term_id = $category->term_id; $variable = get_field('category_image', 'productcat_'.$term_id); $imageURL = $variable['sizes']['medium']; ?> <div class="col-sm-4 alt-no-padding-r-x"> <div class="products_item"> <div class="product_head"> <p><?php echo $category->name; ?></p> </div> <div class="top-img"> <img src="<?php echo $imageURL; ?>" /> </div> <div class="product_desc"> <p><?php echo $category->description; ?></p> </div> <div class="product_list text-left no-decor"> <ul> <?php $args=array( 'post_type' => $post_type, "$tax" => $category->slug, 'post_status' => 'publish', 'posts_per_page' => -1, 'caller_get_posts'=> 1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li> <?php endwhile; } ?> </ul> </div> <div class="readmore-product no-decor"> <a href="#">Read More</a> </div> </div> <div class="plan-shadow"></div> </div> <?php endforeach; wp_reset_query(); ?> </div> 表中的值。