Skip to content

Commit 72d1fc3

Browse files
committed
Add example of renaming a table with reference
1 parent 01c6316 commit 72d1fc3

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

renaming/rename_table.sql

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
-- rename a table
2+
3+
begin;
4+
5+
create table ingredient_types (
6+
name varchar primary key
7+
);
8+
9+
create table ingredients (
10+
id serial primary key,
11+
name varchar not null,
12+
ingredient_type varchar not null references ingredient_types (name)
13+
);
14+
15+
16+
-- > \d ingredient_types
17+
-- Table "public.ingredient_types"
18+
-- Column | Type | Modifiers
19+
-- --------+-------------------+-----------
20+
-- name | character varying | not null
21+
-- Indexes:
22+
-- "ingredient_types_pkey" PRIMARY KEY, btree (name)
23+
-- Referenced by:
24+
-- TABLE "ingredients" CONSTRAINT "ingredients_ingredient_type_fkey" FOREIGN KEY (ingredient_type) REFERENCES ingredient_types(name)
25+
--
26+
-- > \d ingredients
27+
-- Table "public.ingredients"
28+
-- Column | Type | Modifiers
29+
-- -----------------+-------------------+----------------------------------------------------------
30+
-- id | integer | not null default nextval('ingredients_id_seq'::regclass)
31+
-- name | character varying | not null
32+
-- ingredient_type | character varying | not null
33+
-- Indexes:
34+
-- "ingredients_pkey" PRIMARY KEY, btree (id)
35+
-- Foreign-key constraints:
36+
-- "ingredients_ingredient_type_fkey" FOREIGN KEY (ingredient_type) REFERENCES ingredient_types(name)
37+
38+
39+
alter table ingredient_types rename to item_types;
40+
41+
42+
-- > \d item_types
43+
-- Table "public.item_types"
44+
-- Column | Type | Modifiers
45+
-- --------+-------------------+-----------
46+
-- name | character varying | not null
47+
-- Indexes:
48+
-- "ingredient_types_pkey" PRIMARY KEY, btree (name)
49+
-- Referenced by:
50+
-- TABLE "ingredients" CONSTRAINT "ingredients_ingredient_type_fkey" FOREIGN KEY (ingredient_type) REFERENCES item_types(name)
51+
--
52+
-- > \d ingredients
53+
-- Table "public.ingredients"
54+
-- Column | Type | Modifiers
55+
-- -----------------+-------------------+----------------------------------------------------------
56+
-- id | integer | not null default nextval('ingredients_id_seq'::regclass)
57+
-- name | character varying | not null
58+
-- ingredient_type | character varying | not null
59+
-- Indexes:
60+
-- "ingredients_pkey" PRIMARY KEY, btree (id)
61+
-- Foreign-key constraints:
62+
-- "ingredients_ingredient_type_fkey" FOREIGN KEY (ingredient_type) REFERENCES item_types(name)

0 commit comments

Comments
 (0)