forked from mougiakouel/QGIS-Training-Manual
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpg_orig.t2t
2442 lines (1528 loc) · 57.5 KB
/
pg_orig.t2t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
= Introduction to PostgreSQL =
== What is a Database? ==
"A database consists of an organized collection of data for one or more uses,
typically in digital form." - Wikipedia
"A database management system (DBMS) consists of software that operates
databases, providing storage, access, security, backup and other facilities." -
Wikipedia
=== Tables ===
"In relational databases and flat file databases, a table is a set of data
elements (values) that is organized using a model of vertical columns (which
are identified by their name) and horizontal rows. A table has a specified
number of columns, but can have any number of rows. Each row is identified by
the values appearing in a particular column subset which has been identified as
a candidate key." - Wikipedia
```
id | name | age
----+-------+-----
1 | Tim | 20
2 | Horst | 88
(2 rows)
```
In SQL databases a table is also known as a **relation.**
=== Columns / Fields ===
"A column is a set of data values of a particular simple type, one for each row
of the table. The columns provide the structure according to which the rows are
composed. The term field is often used interchangeably with column, although
many consider it more correct to use field (or field value) to refer
specifically to the single item that exists at the intersection between one row
and one column." - Wikipedia
A column:
```
| name |
+-------+
| Tim |
| Horst |
```
A field:
```
| Horst |
```
=== Records ===
A record is the information stored in a table row. Each record will have a
field for each of the columns in the table.
```
2 | Horst | 88 <-- one record
```
=== Datatypes ===
"Datatypes restrict the kind of information that can be stored in a column." -
Tim and Horst
There are many kinds of datatypes. Let's focus on the most common:
- String - to store free-form text data
- Integer - to store whole numbers
- Real - to store decimal numbers
- Date - to store Horst's birthday so no one forgets
- Boolean - to store simple true/false values
-
You can tell the database to allow you to also store nothing in a field - this
data is referred to as 'Null'.
```
insert into person (age) values (40);
```
```
INSERT 0 1
test=# select * from person;
id | name | age
----+-------+-----
1 | Tim | 20
2 | Horst | 88
4 | | 40 <-- null for name
(3 rows)
```
There are many more datatypes you can use - [check the PostgreSQL manual! http://www.postgresql.org/docs/current/static/datatype.html]
=== Modelling an Address Database ===
Let's use a simple case study to see how a database is constructed. We want to
create an address database. What kind of information should we store?
```
Write some address properties here:
```
The properties that describe an address are the columns. The type of
information stored in each column is its datatype. In the next section we will
analyse our conceptual address table to see how we can make it better!
== Database Theory ==
The process of creating a database involves creating a model of the real world
- taking real world concepts and representing them in the database as entities.
-
=== Normalisation ===
One of the main ideas in a database is to avoid data duplication / redundancy.
The process of removing redundancy from a database is called Normalisation.
"Normalization is a systematic way of ensuring that a database structure is
suitable for general-purpose querying and free of certain undesirable
characteristics -insertion, update, and deletion anomalies- that could lead to a
loss of data integrity." - Wikipedia
There are different kinds of normalisation 'forms'.
Let's take a look at a simple example:
```
Table "public.people"
Column | Type | Modifiers
----------+------------------------+------------------------------------------------------
id | integer | not null default
| | nextval('people_id_seq'::regclass)
| |
name | character varying(50) |
address | character varying(200) | not null
phone_no | character varying |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
```
```
select * from people;
id | name | address | phone_no
--+---------------+-----------------------------+-------------
1 | Tim Sutton | 3 Buirski Plein, Swellendam | 071 123 123
2 | Horst Duester | 4 Avenue du Roix, Geneva | 072 121 122
(2 rows)
```
Imagine you have many friends with the same street name or city. Every time
this data is duplicated, it consumes space. Worse still, if a city name
changes, you have to do a lot of work to update your database.
Discussion:
```
Try to redesign our people table to reduce duplication!
```
You can read more about database normalisation [here http://en.wikipedia.org/wiki/Database_normalization]
=== Indexes ===
"A database index is a data structure that improves the speed of data retrieval
operations on a database table." - Wikipedia
Imagine you are reading a textbook and looking for the explanation of a concept
- and the textbook has no index! You will have to start reading at one cover
and work your way through the entire book until you find the information you
need. The index at the back of a book helps you to jump quickly to the page
with the relevant information.
```
create index person_name_idx on people (name);
```
Now searches on name will be faster:
```
Table "public.people"
Column | Type | Modifiers
----------+------------------------+------------------------------------------------------
id | integer | not null default
| | nextval('people_id_seq'::regclass)
| |
name | character varying(50) |
address | character varying(200) | not null
phone_no | character varying |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
"person_name_idx" btree (name)
```
=== Sequences ===
A sequence is a unique number generator. It is normally used to create a unique
identifier for a column in a table.
In this example, id is a sequence - the number is incremented each time a
record is added to the table:
```
id | name | address | phone_no
---+--------------+-----------------------------+-------------
1 | Tim Sutton | 3 Buirski Plein, Swellendam | 071 123 123
2 | Horst Duster | 4 Avenue du Roix, Geneva | 072 121 122
```
=== Entity Relationship Diagramming ===
In a normalised database, you typically have many relations (tables). The
entity-relationship diagram (ER Diagram) is used to design the logical
dependencies between the relations. Remember our un-normalised people table?
```
test=# select * from people;
id | name | address | phone_no
----+--------------+-----------------------------+-------------
1 | Tim Sutton | 3 Buirski Plein, Swellendam | 071 123 123
2 | Horst Duster | 4 Avenue du Roix, Geneva | 072 121 122
(2 rows)
```
With a little work we can split it into two tables, removing the need to repeat
the street name for individuals who live in the same street:
```
test=# select * from streets;
id | name
----+--------------
1 | Plein Street
(1 row)
```
and
```
test=# select * from people;
id | name | house_no | street_id | phone_no
----+--------------+----------+-----------+-------------
1 | Horst Duster | 4 | 1 | 072 121 122
(1 row)
```
We can then link the two tables using the 'keys' streets.id and people.streets_id
If we draw an ER Diagram for these two tables it would look something like
this:
[er-beispiel.png]
The ER Diagram helps us to express 'one to many' relationships. In this case
the arrow symbol show that one street can have many people living on it.
Our people model still has some normalisation issues - try to see if you can
normalise it further and show your thoughts by means of an ER Diagram.
```
```
=== Constraints, Primary Keys and Foreign Keys ===
A database constraint is used to ensure that data in a relation matches the
modeller's view of how that data should be stored. For example a constraint on
your postal code could ensure that the number falls between 1000 and 9999.
A Primary key is one or more field values that make a record unique. Usually
the primary key is called id and is a sequence.
A Foreign key is used to refer to a unique record on another table (using that
other table's primary key).
In ER Diagramming, the linkage between tables is normally based on Foreign keys
linking to Primary keys.
If we look at our people example, the table definition shows that the street
column is a foreign key that references the primary key on the streets table:
```
Table "public.people"
Column | Type | Modifiers
-----------+-----------------------+--------------------------------------
id | integer | not null default
| | nextval('people_id_seq'::regclass)
name | character varying(50) |
house_no | integer | not null
street_id | integer | not null
phone_no | character varying |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"people_street_id_fkey" FOREIGN KEY (street_id) REFERENCES streets(id)
```
=== Transactions ===
When adding, changing, or deleting data in a database, it is always important
that the database is left in a good state if something goes wrong. Most
databases provide a feature called transaction support. Transactions allow you
to create a rollback position that you can return to if your modifications to
the database did not run as planned.
Take a scenario where you have an accounting system. You need to transfer funds
from one account and add them to another. The sequence of steps would go like
this:
- remove R20 from Joe
- add R20 to Anne
-
If something goes wrong during the process (e.g. power failure), the transaction
will be rolled back.
== Implementing the Data Model ==
=== Install PostgreSQL ===
Under Ubuntu:
```
sudo apt-get install postgresql-8.4
```
You should get a message like this:
```
[sudo] password for timlinux:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
postgresql-client-8.4 postgresql-client-common postgresql-common
Suggested packages:
oidentd ident-server postgresql-doc-8.4
The following NEW packages will be installed:
postgresql-8.4 postgresql-client-8.4 postgresql-client-common postgresql-common
0 upgraded, 4 newly installed, 0 to remove and 5 not upgraded.
Need to get 5,012kB of archives.
After this operation, 19.0MB of additional disk space will be used.
Do you want to continue [Y/n]?
```
Press 'Y' and 'Enter' and wait for the download and installation to finish.
===Help===
PostgreSQL has very good [local file:///usr/share/doc/postgresql-doc-8.4/html/index.html] and [online http://www.postgresql.org/docs/8.4/static/index.html] documentation.
=== Create a database user ===
Under Ubuntu:
After the installation is complete, run this command to become the postgres
user and then create a new database user:
```
sudo su - postgres
```
Type in your normal log in password when prompted (you need to have sudo rights).
Now, at the postgres user's bash prompt, create the database user. Make sure
the user name matches your unix login name: it will make your life much easier,
as postgres will automatically authenticate you when you are logged in as that
user.
```
createuser -d -E -i -l -P -r -s timlinux
```
Enter a password when prompted. I normally use a different password to my usual
unix login.
What do those options mean?
```
-d, --createdb role can create new databases
-E, --encrypted encrypt stored password
-i, --inherit role inherits privileges of roles it is a member of (default)
-l, --login role can login (default)
-P, --pwprompt assign a password to new role
-r, --createrole role can create new roles
-s, --superuser role will be superuser
```
Now you should should leave the postgres user's bash shell environment by
typing:
```
exit
```
=== Verify the new account ===
```
psql -l
```
Should return something like this:
```
timlinux@linfiniti:~$ psql -l
List of databases
Name | Owner | Encoding | Collation | Ctype |
----------+----------+----------+------------+------------+
postgres | postgres | UTF8 | en_ZA.utf8 | en_ZA.utf8 |
template0 | postgres | UTF8 | en_ZA.utf8 | en_ZA.utf8 |
template1 | postgres | UTF8 | en_ZA.utf8 | en_ZA.utf8 |
(3 rows)
```
Type 'q' to exit
=== Create a database ===
The createdb command is used to create a new database. It should be run from
the bash shell prompt.
```
createdb address
```
You can verify the existence of your new database by using this command:
```
psql -l
```
Which should return something like this:
```
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
----------+----------+----------+------------+------------+-----------------------
address | timlinux | UTF8 | en_ZA.utf8 | en_ZA.utf8 |
postgres | postgres | UTF8 | en_ZA.utf8 | en_ZA.utf8 |
template0 | postgres | UTF8 | en_ZA.utf8 | en_ZA.utf8 | =c/postgres: postgres=CTc/postgres
template1 | postgres | UTF8 | en_ZA.utf8 | en_ZA.utf8 | =c/postgres: postgres=CTc/postgres
(4 rows)
```
Type 'q' to exit.
=== Starting a database shell session ===
You can connect to your database easily like this:
```
psql address
```
To exit out of the psql database shell, type:
```
\q
```
For help in using the shell, type:
```
\?
```
For help in using sql commands, type:
```
\help
```
To get help on a specific command, type (for example):
```
\help create table
```
See also the [Psql cheat sheet psql_cheatsheet.pdf] - available online [here http://www.postgresonline.com/special_feature.php?sf_name=postgresql83_psql_cheatsheet&outputformat=html].
=== Make Tables in SQL ===
Let's start making some tables! We will use our ER Diagram as a guide. First,
let's create a streets table:
```
create table streets (id serial not null primary key, name varchar(50));
```
'serial' and 'varchar' are ''data types''. 'serial' tells PostgreSQL to start
an integer sequence (autonumber) to populate the id automatically for every new
record. 'varchar(50)' tells PostgreSQL to create a character field of 50
characters in length.
You will notice that the command ends with a ```;``` - all SQL commands should
be terminated this way. When you press enter, psql will report something like
this:
```
NOTICE: CREATE TABLE will create implicit sequence "streets_id_seq" for serial
column "streets.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit
index "streets_pkey" for table "streets"
CREATE TABLE
```
That means your table was created successfully, with a primary key 'streets_pkey'
using streets.id.
Note: If you hit return without entering a ';', then you will get a prompt like
this: 'address-#'. This is because PG is expecting you to enter more. Enter
';' to run your command.
To view your table schema, you can do this:
```
\d streets
```
Which should show something like this:
```
Table "public.streets"
Column | Type | Modifiers
--------+-----------------------+--------------------------------------
id | integer | not null default
| | nextval('streets_id_seq'::regclass)
name | character varying(50) |
Indexes:
"streets_pkey" PRIMARY KEY, btree (id)
```
To view your table contents, you can do this:
```
select * from streets;
```
Which should show something like this:
```
id | name
---+------
(0 rows)
```
As you can see, our table is empty!
Use the approach shown above to make a table called people:
Add fields such as phone number, home address, name, etc. (these aren't all valid names: change them to make them valid).
```
Write the SQL you create here:
```
Solution:
```
create table people (id serial not null primary key,
name varchar(50),
house_no int not null,
street_id int not null,
phone_no varchar null );
```
The schema for the table looks like this:
```
Table "public.people"
Column | Type | Modifiers
-----------+-----------------------+-------------------------------------
id | integer | not null default
| | nextval('people_id_seq'::regclass)
name | character varying(50) |
house_no | integer | not null
street_id | integer | not null
phone_no | character varying |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
```
**Note:** for advanced users, we have purposely omitted the fkey constraint.
=== Create Keys in SQL ===
The problem with our solution above is that the database doesn't know that
people and streets have a logical relationship. To express this relationship,
we have to define a foreign key that points to the primary key of the streets
table.
[er-beispiel.png]
There are two ways to do this:
- adding the key after the table has been created
- defining the key at time of table creation
-
Our table has already been created, so let's do it the first way:
```
alter table people
add constraint people_streets_fk foreign key (street_id) references streets(id);
```
That tells the people table that its street_id fields must match a valid street
id from the streets table.
The more usual way to create a constraint is to do it when you create the table:
```
create table people (id serial not null primary key,
name varchar(50),
house_no int not null,
street_id int references streets(id) not null,
phone_no varchar null);
```
After adding the constraint, our table schema looks like this now:
```
\d people
Table "public.people"
Column | Type | Modifiers
-----------+-----------------------+---------------------------------
id | integer | not null default
| | nextval('people_id_seq'::regclass)
name | character varying(50) |
house_no | integer | not null
street_id | integer | not null
phone_no | character varying |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"people_streets_fk" FOREIGN KEY (id) REFERENCES streets(id)
```
=== Create Indexes in SQL ===
We want lightning fast searches on peoples names. To provide for this, we can
create an index on the name column of our people table:
```
create index people_name_idx on people(name);
```
```
address=# \d people
Table "public.people"
Column | Type | Modifiers
-----------+-----------------------+-----------------------------------
id | integer | not null default nextval
| | ('people_id_seq'::regclass)
name | character varying(50) |
house_no | integer | not null
street_id | integer | not null
phone_no | character varying |
Indexes:
"people_pkey" PRIMARY KEY, btree (id)
"people_name_idx" btree (name) <-- new index added!
Foreign-key constraints:
"people_streets_fk" FOREIGN KEY (id) REFERENCES streets(id)
```
=== Dropping Tables in SQL ===
If you want to get rid of a table you can use the 'drop' command:
```
drop table streets;
```
In our example, this would not work - why?
```
Some deep and inspired thoughts as to why...
```
Sometimes you just can't stand having a table any more. Maybe you are sick of
all your friends. How can you get rid of them all in one easy step? Drop the
table of course! Of course, right now too much hard work has gone into our
table to get rid of it on a whim, but if you really hate your friends that
much, nothing's stopping you from ridding yourself of them forever:
```
drop table people;
```
This time it works fine! Why? Are people less important than streets?
```
Some thoughts on why you could drop people:
```
Note: If you actually did enter that command and dropped the people table,
now would be a good time to rebuild it, as you will need it in the next
exercises.
=== A word on PG Admin III ===
We are showing you the SQL commands from the psql prompt because it's a very
useful way to learn about databases. However, there are quicker and easier ways
to do a lot of what we are showing you. Install PGAdminIII and you can create,
drop, alter etc tables using 'point and click' in a GUI.
Under ubuntu you can install it like this:
```
sudo apt-get install pgadmin3
```
== Adding Data to the Model ==
=== Insert statement ===
How do you add data to a table? The sql INSERT statement provides the
functionality for this:
```
insert into streets (name) values ('High street');
```
A couple of things to note:
- after the table name (streets), you list the column names that you will be
populating (in this case only the 'name' column).
- after the 'values' keyword, place the list of field values.
- strings should be quoted using single quotes.
- you will note that I did not insert a value for the id column - that is
because it is a sequence and will be autogenerated.
- if you do manually set the id, you may cause serious problems with the
integrity of your database.
-
You should see 'INSERT 0 1' if it is successful.
You can see the result of your insert action by selecting all the data in the table:
```
select * from streets;
```
result:
```
select * from streets;
id | name
----+-------------
1 | High street
(1 row)
```
Now you try:
Use the INSERT command to add a new street to the streets table.
```
Write the sql you used here:
```
=== Sequencing data addition according to constraints ===
Try to add a person to the people table with the following details:
```
Name: Joe Smith
House Number: 55
Street: Main Street
Phone: 072 882 33 21
```
Remember, we defined phone numbers as strings.
What problems did you encounter?
```
```
You should have an error report if you try to do this without first creating a
record for Main Street in the streets table.
What error did you get?
You should have noticed that:
- You can't add the street using its name
- You can't add a street using a street id before first
creating the street record on the streets table
-
Remember that our two tables are linked via a Primary/Foreign Key pair. This
means that no valid person can be created without there also being a valid
corresponding street record.
Here is how we made our friend:
```
insert into streets (name) values('Main Street');
insert into people (name,house_no, street_id, phone_no)
values ('Joe Smith',55,2,'072 882 33 21');
```
If you look at the streets table again (using a select statement as before),
you'll see that the id for the 'Main Street' entry is 2. That's why we could
merely enter the number 2 above. Even though we're not seeing 'Main Street'
written out fully in the entry above, the database will be able to associate
that with the street_id value of 2.
=== Select data ===
We have already shown you the syntax for selecting records. Lets look at a few
more examples: