Skip to content

Commit e368fde

Browse files
committed
correct & update example to add/drop constraint
1 parent f9093b7 commit e368fde

File tree

9 files changed

+383
-366
lines changed

9 files changed

+383
-366
lines changed

src/current/v23.2/alter-table.md

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,71 +1153,66 @@ By default, referenced columns must be in the same database as the referencing f
11531153

11541154
#### Drop and add a primary key constraint
11551155

1156-
Suppose that you want to add `name` to the composite primary key of the `users` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
1156+
Suppose that you want to add `creation_time` to the composite primary key of the `promo_codes` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
11571157

11581158
{% include_cached copy-clipboard.html %}
11591159
~~~ sql
1160-
> SHOW CREATE TABLE users;
1160+
SHOW CREATE TABLE promo_codes;
11611161
~~~
11621162

11631163
~~~
1164-
table_name | create_statement
1165-
-------------+--------------------------------------------------------------
1166-
users | CREATE TABLE users (
1167-
| id UUID NOT NULL,
1168-
| city VARCHAR NOT NULL,
1169-
| name VARCHAR NULL,
1170-
| address VARCHAR NULL,
1171-
| credit_card VARCHAR NULL,
1172-
| CONSTRAINT users_pkey PRIMARY KEY (city ASC, id ASC)
1173-
| )
1164+
table_name | create_statement
1165+
--------------+------------------------------------------------------------
1166+
promo_codes | CREATE TABLE public.promo_codes (
1167+
| code VARCHAR NOT NULL,
1168+
| description VARCHAR NULL,
1169+
| creation_time TIMESTAMP NULL,
1170+
| expiration_time TIMESTAMP NULL,
1171+
| rules JSONB NULL,
1172+
| CONSTRAINT promo_codes_pkey PRIMARY KEY (code ASC)
1173+
| )
11741174
(1 row)
11751175
~~~
11761176

1177-
1. Add a [`NOT NULL`]({% link {{ page.version.version }}/not-null.md %}) constraint to the `name` column with [`ALTER COLUMN`](#alter-column).
1177+
1. Add a [`NOT NULL`]({% link {{ page.version.version }}/not-null.md %}) constraint to the `creation_time` column with [`ALTER COLUMN`](#alter-column):
11781178

11791179
{% include_cached copy-clipboard.html %}
11801180
~~~ sql
1181-
> ALTER TABLE users ALTER COLUMN name SET NOT NULL;
1181+
ALTER TABLE promo_codes ALTER COLUMN creation_time SET NOT NULL;
11821182
~~~
11831183

11841184
1. In the same transaction, `DROP` the old `"primary"` constraint and [`ADD`](#add-constraint) the new one:
11851185

11861186
{% include_cached copy-clipboard.html %}
11871187
~~~ sql
1188-
> BEGIN;
1189-
> ALTER TABLE users DROP CONSTRAINT "primary";
1190-
> ALTER TABLE users ADD CONSTRAINT "primary" PRIMARY KEY (city, name, id);
1191-
> COMMIT;
1192-
~~~
1193-
1194-
~~~
1195-
NOTICE: primary key changes are finalized asynchronously; further schema changes on this table may be restricted until the job completes
1188+
BEGIN;
1189+
ALTER TABLE promo_codes DROP CONSTRAINT promo_codes_pkey;
1190+
ALTER TABLE promo_codes ADD CONSTRAINT promo_codes_pkey PRIMARY KEY (code, creation_time);
1191+
COMMIT;
11961192
~~~
11971193

1198-
1. View the table structure:
1194+
1. View the updated table structure:
11991195

12001196
{% include_cached copy-clipboard.html %}
12011197
~~~ sql
1202-
> SHOW CREATE TABLE users;
1198+
SHOW CREATE TABLE promo_codes;
12031199
~~~
12041200

12051201
~~~
1206-
table_name | create_statement
1207-
-------------+---------------------------------------------------------------------
1208-
users | CREATE TABLE users (
1209-
| id UUID NOT NULL,
1210-
| city VARCHAR NOT NULL,
1211-
| name VARCHAR NOT NULL,
1212-
| address VARCHAR NULL,
1213-
| credit_card VARCHAR NULL,
1214-
| CONSTRAINT "primary" PRIMARY KEY (city ASC, name ASC, id ASC),
1215-
| FAMILY "primary" (id, city, name, address, credit_card)
1216-
| )
1217-
(1 row)
1202+
table_name | create_statement
1203+
--------------+----------------------------------------------------------------------------
1204+
promo_codes | CREATE TABLE public.promo_codes (
1205+
| code VARCHAR NOT NULL,
1206+
| description VARCHAR NULL,
1207+
| creation_time TIMESTAMP NOT NULL,
1208+
| expiration_time TIMESTAMP NULL,
1209+
| rules JSONB NULL,
1210+
| CONSTRAINT promo_codes_pkey PRIMARY KEY (code ASC, creation_time ASC)
1211+
| )
1212+
(1 row)
12181213
~~~
12191214

1220-
Using [`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key) would have created a `UNIQUE` secondary index called `users_city_id_key`. Instead, there is just one index for the primary key constraint.
1215+
Using [`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key) would have created a `UNIQUE` secondary index called `promo_codes_code_key`. Instead, there is just one index for the primary key constraint.
12211216

12221217
#### Add a unique index to a `REGIONAL BY ROW` table
12231218

@@ -1828,10 +1823,6 @@ To unhide the column, run:
18281823
18291824
### Alter a primary key
18301825
1831-
#### Demo
1832-
1833-
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
1834-
18351826
#### Alter a single-column primary key
18361827
18371828
Suppose that you are storing the data for users of your application in a table called `users`, defined by the following `CREATE TABLE` statement:

src/current/v24.1/alter-table.md

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,71 +1142,73 @@ By default, referenced columns must be in the same database as the referencing f
11421142

11431143
#### Drop and add a primary key constraint
11441144

1145-
Suppose that you want to add `name` to the composite primary key of the `users` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
1145+
Suppose that you want to add `creation_time` to the composite primary key of the `promo_codes` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
11461146

11471147
{% include_cached copy-clipboard.html %}
11481148
~~~ sql
1149-
> SHOW CREATE TABLE users;
1149+
SHOW CREATE TABLE promo_codes;
11501150
~~~
11511151

11521152
~~~
1153-
table_name | create_statement
1154-
-------------+--------------------------------------------------------------
1155-
users | CREATE TABLE users (
1156-
| id UUID NOT NULL,
1157-
| city VARCHAR NOT NULL,
1158-
| name VARCHAR NULL,
1159-
| address VARCHAR NULL,
1160-
| credit_card VARCHAR NULL,
1161-
| CONSTRAINT users_pkey PRIMARY KEY (city ASC, id ASC)
1162-
| )
1153+
table_name | create_statement
1154+
--------------+------------------------------------------------------------
1155+
promo_codes | CREATE TABLE public.promo_codes (
1156+
| code VARCHAR NOT NULL,
1157+
| description VARCHAR NULL,
1158+
| creation_time TIMESTAMP NULL,
1159+
| expiration_time TIMESTAMP NULL,
1160+
| rules JSONB NULL,
1161+
| CONSTRAINT promo_codes_pkey PRIMARY KEY (code ASC)
1162+
| )
11631163
(1 row)
11641164
~~~
11651165

1166-
1. Add a [`NOT NULL`]({% link {{ page.version.version }}/not-null.md %}) constraint to the `name` column with [`ALTER COLUMN`](#alter-column).
1166+
1. Add a [`NOT NULL`]({% link {{ page.version.version }}/not-null.md %}) constraint to the `creation_time` column with [`ALTER COLUMN`](#alter-column):
11671167

11681168
{% include_cached copy-clipboard.html %}
11691169
~~~ sql
1170-
> ALTER TABLE users ALTER COLUMN name SET NOT NULL;
1170+
ALTER TABLE promo_codes ALTER COLUMN creation_time SET NOT NULL;
11711171
~~~
11721172

1173+
1. Turn off autocommit for DDL for the current session, so that multiple DDL statements can execute within one explicit transaction:
1174+
1175+
{% include_cached copy-clipboard.html %}
1176+
~~~ sql
1177+
SET autocommit_before_ddl = false;
1178+
~~~
1179+
11731180
1. In the same transaction, `DROP` the old `"primary"` constraint and [`ADD`](#add-constraint) the new one:
11741181

11751182
{% include_cached copy-clipboard.html %}
11761183
~~~ sql
1177-
> BEGIN;
1178-
> ALTER TABLE users DROP CONSTRAINT "primary";
1179-
> ALTER TABLE users ADD CONSTRAINT "primary" PRIMARY KEY (city, name, id);
1180-
> COMMIT;
1184+
BEGIN;
1185+
ALTER TABLE promo_codes DROP CONSTRAINT promo_codes_pkey;
1186+
ALTER TABLE promo_codes ADD CONSTRAINT promo_codes_pkey PRIMARY KEY (code, creation_time);
1187+
COMMIT;
11811188
~~~
11821189

1183-
~~~
1184-
NOTICE: primary key changes are finalized asynchronously; further schema changes on this table may be restricted until the job completes
1185-
~~~
1186-
1187-
1. View the table structure:
1190+
1. View the updated table structure:
11881191

11891192
{% include_cached copy-clipboard.html %}
11901193
~~~ sql
1191-
> SHOW CREATE TABLE users;
1194+
SHOW CREATE TABLE promo_codes;
11921195
~~~
11931196

11941197
~~~
1195-
table_name | create_statement
1196-
-------------+---------------------------------------------------------------------
1197-
users | CREATE TABLE users (
1198-
| id UUID NOT NULL,
1199-
| city VARCHAR NOT NULL,
1200-
| name VARCHAR NOT NULL,
1201-
| address VARCHAR NULL,
1202-
| credit_card VARCHAR NULL,
1203-
| CONSTRAINT "primary" PRIMARY KEY (city ASC, name ASC, id ASC),
1204-
| FAMILY "primary" (id, city, name, address, credit_card)
1205-
| )
1206-
(1 row)
1198+
table_name | create_statement
1199+
--------------+----------------------------------------------------------------------------
1200+
promo_codes | CREATE TABLE public.promo_codes (
1201+
| code VARCHAR NOT NULL,
1202+
| description VARCHAR NULL,
1203+
| creation_time TIMESTAMP NOT NULL,
1204+
| expiration_time TIMESTAMP NULL,
1205+
| rules JSONB NULL,
1206+
| CONSTRAINT promo_codes_pkey PRIMARY KEY (code ASC, creation_time ASC)
1207+
| )
1208+
(1 row)
12071209
~~~
12081210

1209-
Using [`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key) would have created a `UNIQUE` secondary index called `users_city_id_key`. Instead, there is just one index for the primary key constraint.
1211+
Using [`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key) would have created a `UNIQUE` secondary index called `promo_codes_code_key`. Instead, there is just one index for the primary key constraint.
12101212

12111213
#### Add a unique index to a `REGIONAL BY ROW` table
12121214

@@ -1817,10 +1819,6 @@ To unhide the column, run:
18171819
18181820
### Alter a primary key
18191821
1820-
#### Demo
1821-
1822-
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
1823-
18241822
#### Alter a single-column primary key
18251823
18261824
Suppose that you are storing the data for users of your application in a table called `users`, defined by the following `CREATE TABLE` statement:

src/current/v24.2/alter-table.md

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,71 +1115,73 @@ By default, referenced columns must be in the same database as the referencing f
11151115

11161116
#### Drop and add a primary key constraint
11171117

1118-
Suppose that you want to add `name` to the composite primary key of the `users` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
1118+
Suppose that you want to add `creation_time` to the composite primary key of the `promo_codes` table, [without creating a secondary index of the existing primary key](#changing-primary-keys-with-add-constraint-primary-key).
11191119

11201120
{% include_cached copy-clipboard.html %}
11211121
~~~ sql
1122-
> SHOW CREATE TABLE users;
1122+
SHOW CREATE TABLE promo_codes;
11231123
~~~
11241124

11251125
~~~
1126-
table_name | create_statement
1127-
-------------+--------------------------------------------------------------
1128-
users | CREATE TABLE users (
1129-
| id UUID NOT NULL,
1130-
| city VARCHAR NOT NULL,
1131-
| name VARCHAR NULL,
1132-
| address VARCHAR NULL,
1133-
| credit_card VARCHAR NULL,
1134-
| CONSTRAINT users_pkey PRIMARY KEY (city ASC, id ASC)
1135-
| )
1126+
table_name | create_statement
1127+
--------------+------------------------------------------------------------
1128+
promo_codes | CREATE TABLE public.promo_codes (
1129+
| code VARCHAR NOT NULL,
1130+
| description VARCHAR NULL,
1131+
| creation_time TIMESTAMP NULL,
1132+
| expiration_time TIMESTAMP NULL,
1133+
| rules JSONB NULL,
1134+
| CONSTRAINT promo_codes_pkey PRIMARY KEY (code ASC)
1135+
| )
11361136
(1 row)
11371137
~~~
11381138

1139-
1. Add a [`NOT NULL`]({% link {{ page.version.version }}/not-null.md %}) constraint to the `name` column with [`ALTER COLUMN`](#alter-column).
1139+
1. Add a [`NOT NULL`]({% link {{ page.version.version }}/not-null.md %}) constraint to the `creation_time` column with [`ALTER COLUMN`](#alter-column):
11401140

11411141
{% include_cached copy-clipboard.html %}
11421142
~~~ sql
1143-
> ALTER TABLE users ALTER COLUMN name SET NOT NULL;
1143+
ALTER TABLE promo_codes ALTER COLUMN creation_time SET NOT NULL;
11441144
~~~
11451145

1146+
1. Turn off autocommit for DDL for the current session, so that multiple DDL statements can execute within one explicit transaction:
1147+
1148+
{% include_cached copy-clipboard.html %}
1149+
~~~ sql
1150+
SET autocommit_before_ddl = false;
1151+
~~~
1152+
11461153
1. In the same transaction, `DROP` the old `"primary"` constraint and [`ADD`](#add-constraint) the new one:
11471154

11481155
{% include_cached copy-clipboard.html %}
11491156
~~~ sql
1150-
> BEGIN;
1151-
> ALTER TABLE users DROP CONSTRAINT "primary";
1152-
> ALTER TABLE users ADD CONSTRAINT "primary" PRIMARY KEY (city, name, id);
1153-
> COMMIT;
1157+
BEGIN;
1158+
ALTER TABLE promo_codes DROP CONSTRAINT promo_codes_pkey;
1159+
ALTER TABLE promo_codes ADD CONSTRAINT promo_codes_pkey PRIMARY KEY (code, creation_time);
1160+
COMMIT;
11541161
~~~
11551162

1156-
~~~
1157-
NOTICE: primary key changes are finalized asynchronously; further schema changes on this table may be restricted until the job completes
1158-
~~~
1159-
1160-
1. View the table structure:
1163+
1. View the updated table structure:
11611164

11621165
{% include_cached copy-clipboard.html %}
11631166
~~~ sql
1164-
> SHOW CREATE TABLE users;
1167+
SHOW CREATE TABLE promo_codes;
11651168
~~~
11661169

11671170
~~~
1168-
table_name | create_statement
1169-
-------------+---------------------------------------------------------------------
1170-
users | CREATE TABLE users (
1171-
| id UUID NOT NULL,
1172-
| city VARCHAR NOT NULL,
1173-
| name VARCHAR NOT NULL,
1174-
| address VARCHAR NULL,
1175-
| credit_card VARCHAR NULL,
1176-
| CONSTRAINT "primary" PRIMARY KEY (city ASC, name ASC, id ASC),
1177-
| FAMILY "primary" (id, city, name, address, credit_card)
1178-
| )
1179-
(1 row)
1171+
table_name | create_statement
1172+
--------------+----------------------------------------------------------------------------
1173+
promo_codes | CREATE TABLE public.promo_codes (
1174+
| code VARCHAR NOT NULL,
1175+
| description VARCHAR NULL,
1176+
| creation_time TIMESTAMP NOT NULL,
1177+
| expiration_time TIMESTAMP NULL,
1178+
| rules JSONB NULL,
1179+
| CONSTRAINT promo_codes_pkey PRIMARY KEY (code ASC, creation_time ASC)
1180+
| )
1181+
(1 row)
11801182
~~~
11811183

1182-
Using [`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key) would have created a `UNIQUE` secondary index called `users_city_id_key`. Instead, there is just one index for the primary key constraint.
1184+
Using [`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key) would have created a `UNIQUE` secondary index called `promo_codes_code_key`. Instead, there is just one index for the primary key constraint.
11831185

11841186
#### Add a unique index to a `REGIONAL BY ROW` table
11851187

@@ -1790,10 +1792,6 @@ To unhide the column, run:
17901792
17911793
### Alter a primary key
17921794
1793-
#### Demo
1794-
1795-
{% include_cached youtube.html video_id="MPx-LXY2D-c" %}
1796-
17971795
#### Alter a single-column primary key
17981796
17991797
Suppose that you are storing the data for users of your application in a table called `users`, defined by the following `CREATE TABLE` statement:

0 commit comments

Comments
 (0)