(摘)Postgresql数据库学习备录(二)
这一部分描述的是PostgreSQL里面SQL的使用:
一、数据定义
drop table products; create table products( product_no integer, --产品标号 name text, --产品名称 price numeric --价格 );
有一个小限制:一个表能包含的字段数目。根据字段类型的不同,这个数目可能在250到1600之间。不过,不管是哪一端的数字,如果你设计的表包含那么多的字段都很不可能发生,否则就是你设计有问题。
2、缺省值
一个字段可以赋予缺省值,如果没有明确声明缺省值,那么缺省值是空。
create table products( product_no integer, name text, price numeric default 9.99 );
3、约束
3.1检查约束
检查约束是最常见的约束类型。它允许你声明在某个字段里的数值必须满足一个布尔表达式。
create table products( product_no integer, name text, price numeric constraint positive_price check(price > 0), --字段约束 check (price > 2) --表约束 );
3.2非空约束
非空约束只是简单地声明一个字段必须不能是空值。
create table products( product_no integer no null, name text not null, price numeric );
3.3唯一约束
唯一约束保证在一个字段或者一组字段里的数据与表中其它行的数据相比是唯一的。
create table products( product_no integer unique, --字段唯一约束 name text, price numeric ); create table products( product_no integer, name text, price numeric, unique(product_no) -- 表唯一约束 );
3.4主键
从技术上讲,主键约束只是非空约束和唯一约束的组合。所以下面两种方式定义接受相同数据:
create talbe products( product_no integer unique not null, name text, price numeric ); || create table products( product_no integer primary key, name text, price numeric );
3.5外键
外键约束声明一个字段(或者一组字段)的数值必须匹配另外一个表中某些行出现的数值。我们把这个行为称做两个相关表之间的参考完整性。
create table products( product_no integer primary key, name text, price numeric ); create table orders( order_id integer primary key, product_no integer references products(product_no), quantity integer );
4、修改表
4.1增加字段
alter table products add column description text;
4.2删除字段
alter table products drop column description;
4.3增加约束
alter table products alter column product_no set not null; --添加字段约束 alter table products add check(name <> ''); --添加表约束
4.4删除约束
要删除一个约束,你需要知道它的名字。如果你给了它一名字,那么事情就好办了。否则系统会分配一个生成的名字,这样你就需要把它找出来:psql的命令\d tablename在这儿可以帮忙;其它接口可能也提供了检查表的细节的方法。然后就是这条命令:
alter table products drop constraint some_name; (如果你在处理一个生成的约束名,比如$2,别忘了你需要给它添加双引号,让它成为一个有效的标识符)
和删除字段一样,如果你想删除有着被依赖关系地约束,你需要用cascade.一个例子是某个外键约束依赖被引用字段上的唯一约束或者主键约束。
除了非空约束外,所有约束类型都这么用。要删除非空类型,用
alter table products alter column product_no drop not null;
要记得非空约束没有名字。
4.5改变一个字段的缺省值
要给一个字段设置缺省值,命令:
alter table products alter column price set default 7.77;
请注意这么做不会影响表中现有的数据行,它只是为将来 insert 命令改变缺省值。
要删除缺省值,用
alter table products alter column price drop default;
4.6修改一个字段的数据类型
alter table products alter column price type numeric(10,2);
注:只有在字段里现有的每个项都可以用一个隐含的类型转换转换城新的类型时才可能成功。
4.7给字段改名字
alter table products rename column product_no to product_number;
4.8给表改名字
alter table products rename to items;
5、权限
PostgreSQL支持的权限有:select、insert、update、delete、rule、references、trigger、create、temporary、execute、usage。它们因对象类型(表、函数等)的不同而不同。
要赋予一个权限,我们使用grant命令。因此,如果joe是一个现存的用户,而accounts是一个已经存在的表,更新表的权限可以用下面的命令赋予:
grant update on accounts to joe;
要撤销一个权限,使用合适的revoke命令:
revoke all on accounts from public;
6、模式
一个数据库包含一个或多个命名的模式,模式又包含表。模式类似于操作系统层次的目录,但是不能嵌套。
6.1创建一个模式
create schema myschema;
在模式中创建新表,用命令:
create table myschema.mytable( --如果省略模式名,则默认为public模式 ... );
如果要删除一个空模式,用命令:
drop schema myschema;
删除一个非空模式,则用命令:
drop schema myschema cascade;