Created
July 6, 2012 19:15
-
-
Save yyl/3062221 to your computer and use it in GitHub Desktop.
learning postgresql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE cities ( | |
| city varchar(80) primary key, | |
| location point | |
| ); | |
| CREATE TABLE weather ( | |
| city varchar(80) references cities(city), | |
| temp_lo int, | |
| temp_hi int, | |
| prcp real, | |
| date date | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| city | temp_lo | temp_hi | prcp | date | name | location | |
| ---------------+---------+---------+------+------------+---------------+----------- | |
| Hayward | 37 | 54 | | 1994-11-29 | | | |
| San Francisco | 46 | 50 | 0.25 | 1994-11-27 | San Francisco | (-194,53) | |
| San Francisco | 43 | 57 | 0 | 1994-11-29 | San Francisco | (-194,53) | |
| (3 rows) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)'); | |
| INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27'); | |
| COPY weather FROM '/home/user/weather.txt'; | |
| COPY weather TO '/home/user/weather.txt'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ psql -U username | |
| $ psql djangodb | |
| Password: | |
| psql (9.1.4) | |
| Type "help" for help. | |
| djangodb=# \d | |
| List of relations | |
| Schema | Name | Type | Owner | |
| --------+-----------------------------------+----------+---------- | |
| public | auth_group | table | djangodb | |
| djangodb=# \du | |
| List of roles | |
| Role name | Attributes | Member of | |
| ------------+------------------------------------------------+----------- | |
| djangodb | Create DB | {} | |
| postgres | Superuser, Create role, Create DB, Replication | {} | |
| djangodb=# \q |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather; | |
| SELECT DISTINCT * FROM weather | |
| WHERE city = 'San Francisco' AND prcp > 0.0; | |
| SELECT city, temp_lo, temp_hi, prcp, date, location | |
| FROM weather, cities | |
| WHERE city = name; | |
| SELECT * | |
| FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name); | |
| SELECT max(temp_lo) FROM weather; | |
| SELECT city FROM weather | |
| WHERE temp_lo = (SELECT max(temp_lo) FROM weather); | |
| UPDATE weather | |
| SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2 | |
| WHERE date > '1994-11-28'; | |
| DELETE FROM weather WHERE city = 'Hayward'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment