First day of PostgreSQL-relationships, crud, and joins

Source: Internet
Author: User
Tags joins postgresql


To create a table:

CREATE TABLE  Char(2PRIMARYKEYtextUNIQUE);

Insert data:

INSERT  intocountries (Country_code,country_name)VALUES('US','states'),('MX','Mexico'),('au','Australia'),('GB','Kingdom'),('de','Gemany'),('ll','Loompaland');

Or

INSERT  into countries VALUES ('US','states'), ('mx ','Mexico');

Query data:

SELECT *  from countries;

Delete data:

DELETE  from countries WHERE = ' ll ';

FOREIGN key: In order to ensure that all the country_code in cities have appeared in countries,
So add the references foreign KEY constraint to cities's country_code.

CREATE TABLE  text notNULLvarchar (9 CHECK <>     ' char (2 REFERENCES countries,  PRIMARYKEY  (Country_code,postal_code));

But the Country_code reference to the cities table can be null, representing a value that is vacant,
If Cities.country_code is not allowed to be empty, you can define this:

Char (2REFERENCESnotNULL

Insert FOREIGN KEY data:

INSERT  into Cities VALUES ('Portland','87200','US ');

Update the above data:

UPDATE Cities SET = ' 97205 ' WHERE = ' Portland ';

To use a join query:

SELECT cities. * , Country_name  from INNER JOIN countries  on = Countries.country_code;

Foreign keys reference two primary keys:

CREATE TABLEvenues (venue_id SERIALPRIMARY KEY, namevarchar(255), Street_addresstext, typeChar(7)CHECK(Typeinch(' Public','Private'))DEFAULT ' Public', Postal_CodeChar(2), Country_codeChar(2),FOREIGN KEY(Country_code,postal_code)REFERENCESCities (Country_code,postal_code) MATCH Full);

Left outer joins:

SELECT E.title,v.name  from  Left JOIN Venues v  on = v.venues_id;

When you create a new table, PostgreSQL indexes the primary key by default.
Use the unique force to create an index on a column.
To build an index:

CREATE INDEX Envents_title  on envents USING Hash (title);

For matching queries that have an operator greater than/less than/equal to this, use the B-tree index, which is equivalent to the assembly's segment register.

CREATE INDEX Events_starts  on Events USING btree (starts);

Use the B-Tree index query:

SELECT *  from Events WHERE >= ' 2012-04-01 ';

First day of PostgreSQL-relationships, crud, and joins

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.