2018年1月27日土曜日

■スキーマについて学習してみた その2

再度、ちょっとした練習

①Ownerがtestuserでtestdb2を作成
$ createuser -d -r -l -P testuser
$ createdb -O testuser testdb2

②DBに接続
$pslq -U testuser -d testdb
testdb2=> \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 dvdrental | postgres | UTF8     | C       | C     |
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C       | C     |
 testdb2   | testuser | UTF8     | C       | C     |

③スキーマを作成
testdb2=> create schema testuser;

④テーブルを作成
testdb2=> create schema testuser;
CREATE SCHEMA
testdb2=> create table TripType(
testdb2(> TripTypeID                  INTEGER NOT NULL,
testdb2(> Name                        VARCHAR(15),
testdb2(> Description                 VARCHAR(50),
testdb2(> LastUpdated                 TIMESTAMP
testdb2(> );
CREATE TABLE
testdb2=> \dt+
                           List of relations
  Schema  |       Name       | Type  |  Owner   |  Size   | Description
----------+------------------+-------+----------+---------+-------------
 public   | pgbench_accounts | table | testuser | 13 MB   |
 public   | pgbench_branches | table | testuser | 64 kB   |
 public   | pgbench_history  | table | testuser | 1152 kB |
 public   | pgbench_tellers  | table | testuser | 72 kB   |
★testuser | triptype         | table | testuser | 0 bytes |
→スキーマがtestuserでtriptypeテーブルが作成された

⑤publicスキーマのテーブルを作成
testdb2=> create table public.TripType(
testdb2(> TripTypeID                  INTEGER NOT NULL,
testdb2(> Name                        VARCHAR(15),
testdb2(> Description                 VARCHAR(50),
testdb2(> LastUpdated                 TIMESTAMP
testdb2(> );
CREATE TABLE

testdb2=> \dt+
                           List of relations
  Schema  |       Name       | Type  |  Owner   |  Size   | Description
----------+------------------+-------+----------+---------+-------------
 public   | pgbench_accounts | table | testuser | 13 MB   |
 public   | pgbench_branches | table | testuser | 64 kB   |
 public   | pgbench_history  | table | testuser | 1152 kB |
 public   | pgbench_tellers  | table | testuser | 72 kB   |
 testuser | triptype         | table | testuser | 0 bytes |
(5 rows)
→publicスキーマのtriptypeテーブルは表示されない

⑥データの挿入
testdb2=> insert into TripType values( 1, 'TRNG', 'Training', NULL);
INSERT 0 1
testdb2=> select count(*) from triptype;
 count
-------
     1
(1 row)

testdb2=> select count(*) from public.triptype;
 count
-------
     0
→publicのtriptypeテーブルにはデータは格納されず。

⑦サーチパスの確認
testdb2=> show search_path;
   search_path
-----------------
 "$user", public
→自分のユーザに紐づいたスキーマを1番最初にリストするから。

⑧テーブル情報の確認
estdb2=> select * from pg_tables where tableowner='testuser';
 schemaname |    tablename     | tableowner | tablespace | hasindexes | hasrules
 | hastriggers | rowsecurity
------------+------------------+------------+------------+------------+---------
-+-------------+-------------
 public     | pgbench_tellers  | testuser   |            | t          | f
 | f           | f
 public     | pgbench_branches | testuser   |            | t          | f
 | f           | f
 public     | pgbench_accounts | testuser   |            | t          | f
 | f           | f
 public     | pgbench_history  | testuser   |            | f          | f
 | f           | f
 testuser   | triptype         | testuser   |            | f          | f
 | f           | f
 public     | triptype         | testuser   |            | f          | f
 | f           | f
(6 rows)

うーん、ややこしいね。

0 件のコメント:

コメントを投稿

■証明書の流れ

証明書の流れ