Seven Databases in Seven Weeks(七周七数据库)——PostgreSQL

Q1: 

新建table venues:

create table venues (
venue_id serial primary key,
name varchar(255),
street_address text,
type char(7) check (type in ('public','private')) default 'public',
postal_code varchar(9),
country_code char(2),
foreign key (country_code,postal_code) references cities(country_code,postal_code) match full
);

table cities 中已有记录:

select * from  cities;
   name   | postal_code | country_code 
----------+-------------+--------------
 portland | 87200       | us
 shenzhen | 518120      | cn

postal_code 和country_code为复合主键。


往venues中插入不合法记录:

insert into venues(name,postal_code,country_code) values('crystal ballroom','97205','us');
ERROR:  insert or update on table "venues" violates foreign key constraint "venues_country_code_fkey"
DETAIL:  Key (country_code, postal_code)=(us, 97205) is not present in table "cities".

再接着插入合法记录:
insert into venues(name,postal_code,country_code) values('crystal ballroom','87200','us');
INSERT 0 1

查询venues的记录:

select * from venues;
 venue_id |       name       | street_address |  type   | postal_code | country_code 
----------+------------------+----------------+---------+-------------+--------------
        2 | crystal ballroom |                | public  | 87200       | us
(1 row)

发现venue_id属性为2,得知插入不合法记录的时候venue_id也进行了递增。

为什么要这样设计?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
File Size: 1260 KB Print Length: 352 pages Publisher: Pragmatic Bookshelf; 1 edition (January 8, 2013) Sold by: Amazon Digital Services, Inc. Language: English Data is getting bigger and more complex by the day, and so are the choices in handling that data. As a modern application developer you need to understand the emerging field of data management, both RDBMS and NoSQL. Seven Databases in Seven Weeks takes you on a tour of some of the hottest open source databases today. In the tradition of Bruce A. Tate's Seven Languages in Seven Weeks, this book goes beyond your basic tutorial to explore the essential concepts at the core each technology. Redis, Neo4J, CouchDB, MongoDB, HBase, Riak, and Postgres: with each database, you’ll tackle a real-world data problem that highlights the concepts and features that make it shine. You’ll explore the five data models employed by these databases: relational, key/value, columnar, document, and graph. See which kinds of problems are best suited to each, and when to use them. You’ll learn how MongoDB and CouchDB are strikingly different, and discover the Dynamo heritage at the heart of Riak. Make your applications faster with Redis and more connected with Neo4J. Use MapReduce to solve Big Data problems. Build clusters of servers using scalable services like Amazon’s Elastic Compute Cloud (EC2). Understand the tradeoffs between consistency and availability, and when you can use them to your advantage. Use multiple databases in concert to create a platform that’s more than the sum of its parts, or find one that meets all your needs at once. Seven Databases in Seven Weeks will take you on a deep dive into each of the databases, their strengths and weaknesses, and how to choose the ones that fit your needs. What You Need: You’ll need a *nix shell (Mac OSX or Linux preferred, Windows users will need Cygwin), and Java 6 (or greater) and Ruby 1.8.7 (or greater). Each chapter will list the downloads required for that database.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值