|
| 1 | +--example table showing default behavior of NULL |
| 2 | +create table public.example |
| 3 | +(id int, |
| 4 | +somevalue varchar(50)); |
| 5 | + |
| 6 | + |
| 7 | +--this script will fail |
| 8 | +insert |
| 9 | + into |
| 10 | + radio.radios |
| 11 | +(manufacturer_id) |
| 12 | +values |
| 13 | +(2); |
| 14 | + |
| 15 | + |
| 16 | +--how to list constraints on a table |
| 17 | +select |
| 18 | + c.conname, |
| 19 | + ccu.table_schema, |
| 20 | + ccu.table_name, |
| 21 | + ccu.column_name, |
| 22 | + c.contype, |
| 23 | + pg_get_constraintdef(c.oid) |
| 24 | +from |
| 25 | + pg_constraint as c |
| 26 | +join pg_namespace as ns on |
| 27 | + ns.oid = c.connamespace |
| 28 | +join pg_class as cl on |
| 29 | + c.conrelid = cl.oid |
| 30 | +left join information_schema.constraint_column_usage as ccu |
| 31 | + on |
| 32 | + c.conname = ccu.constraint_name |
| 33 | + and ns.nspname = ccu.constraint_schema |
| 34 | +where |
| 35 | + ccu.table_name = 'radios'; |
| 36 | + |
| 37 | + |
| 38 | +--listing column names in a table that are nullable |
| 39 | +select column_name |
| 40 | +from information_schema.columns |
| 41 | +where table_catalog = 'hamshackradio' |
| 42 | + and table_schema = 'radio' |
| 43 | + and table_name = 'radios' |
| 44 | + and is_nullable = 'YES' |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +-- example 1 unique constraint |
| 51 | +CREATE TABLE IF NOT EXISTS public.uniqueval1 |
| 52 | + (id int not null, |
| 53 | + myuniquevalue varchar(50) unique); |
| 54 | + |
| 55 | + |
| 56 | +--get a list of indexes from a table |
| 57 | +select |
| 58 | + i.indexname, |
| 59 | + i.indexdef |
| 60 | +from |
| 61 | + pg_indexes as i |
| 62 | +where |
| 63 | + i.tablename = 'uniqueval1'; |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +--creating a filtered unique index |
| 69 | +CREATE UNIQUE INDEX uniqueval_notnull_idx ON public.nullunique (ComputedUniqueeval) |
| 70 | +WHERE uniqueval IS NULL; |
| 71 | + |
| 72 | + |
| 73 | +drop table if exists public.uniqueval1; |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +--example unique column constraint |
| 80 | +create table if not exists public.uniqueval2 |
| 81 | + (id int not null, |
| 82 | + myuniquevalue varchar(50) not null, |
| 83 | + unique(myuniquevalue)); |
| 84 | + |
| 85 | +drop table if exists public.uniqueval2; |
| 86 | + |
| 87 | +--With the naming syntax in place |
| 88 | +create table if not exists public.uniqueval3 |
| 89 | + (id int not null, |
| 90 | + myuniquevalue varchar(50) constraint uniqueval3_inameit unique not null); |
| 91 | + |
| 92 | +create table if not exists public.uniqueval4 |
| 93 | + (id int not null, |
| 94 | + myuniquevalue varchar(50), |
| 95 | + constraint inameit unique(myuniquevalue)); |
| 96 | + |
| 97 | +drop table if exists public.uniqueval3; |
| 98 | +drop table if exists public.uniqueval4; |
| 99 | + |
| 100 | +--using alter table to add a constraint |
| 101 | +--create a table without a constraint |
| 102 | +create table if not exists public.uniqueval5 |
| 103 | + (id int not null, |
| 104 | + myuniquevalue varchar(50) null); |
| 105 | + |
| 106 | +--now add the constraint |
| 107 | +alter table public.uniqueval5 |
| 108 | +add constraint mynewconstraint unique(myuniquevalue); |
| 109 | + |
| 110 | +dropt table if exists public.uniqueval5 |
| 111 | + |
| 112 | + |
| 113 | +--dealing with null values |
| 114 | +create table if not exists public.nullnotunique |
| 115 | + (id int not null, |
| 116 | + nonuniqueval varchar(50) constraint notunique unique null); |
| 117 | + |
| 118 | +insert |
| 119 | + into |
| 120 | + public.nullnotunique |
| 121 | +(id, |
| 122 | + nonuniqueval) |
| 123 | +values |
| 124 | +(1, |
| 125 | +null), |
| 126 | +(2, |
| 127 | +null), |
| 128 | +(3, |
| 129 | +'one'); |
| 130 | + |
| 131 | + |
| 132 | +create table if not exists public.nullunique |
| 133 | + (id int not null, |
| 134 | + uniqueval varchar(50) constraint nowunique unique nulls not distinct null); |
| 135 | + |
| 136 | +insert |
| 137 | + into |
| 138 | + public.nullunique |
| 139 | +(id, |
| 140 | + uniqueval) |
| 141 | +values |
| 142 | +(1, |
| 143 | +null), |
| 144 | +(2, |
| 145 | +null), |
| 146 | +(3, |
| 147 | +'one'); |
| 148 | + |
| 149 | + |
| 150 | +drop table if exists public.nullnotunique; |
| 151 | +drop table if exists public.nullunique; |
| 152 | + |
| 153 | + |
| 154 | +--Primary key |
| 155 | +create table if not exists public.pkexample |
| 156 | +(id int primary key not null, |
| 157 | +somevalue varchar(50)); |
| 158 | + |
| 159 | + |
| 160 | +--bad example |
| 161 | +create table if not exists public.badpkexample |
| 162 | +(id int null, |
| 163 | +somevalue varchar(50)); |
| 164 | +Check the results of the following metadata query: |
| 165 | +select is_nullable |
| 166 | +from information_schema.columns |
| 167 | +where table_schema = 'public' |
| 168 | +and table_name = 'badpkexample' |
| 169 | +and column_name = 'id'; |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +alter table public.badpkexample |
| 174 | + add constraint PKbadpkexample |
| 175 | + primary key (id); |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +--clean up |
| 181 | +DROP TABLE IF EXISTS public.badpkexample; |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | +--Foreign key |
| 186 | +ALTER TABLE radio.radios |
| 187 | +ADD foreign key (manufacturer_id) references radio.manufacturers; |
| 188 | + |
| 189 | +--remove and readd the foreign key |
| 190 | +alter table radio.radios drop constraint radios_manufacturer_id_fkey; |
| 191 | + |
| 192 | +alter table radio.radios add constraint |
| 193 | +radios_fk_manufacturer foreign key (manufacturer_id) references radio.manufacturers; |
| 194 | + |
| 195 | +--defining a foreign key as part of the table definition |
| 196 | +create table if not exists radio.radios |
| 197 | + (radio_id int constraint pkradios primary key generated always as identity, |
| 198 | + radio_name varchar(100) not null, |
| 199 | + manufacturer_id int not null, |
| 200 | + picture BYTEA null, |
| 201 | + connectortype_id int not null, |
| 202 | + digitalmode_id int null, |
| 203 | + constraint radios_fk_manufacturer foreign key (manufacturer_id) references radio.manufacturers |
| 204 | + ); |
| 205 | + |
| 206 | +--adding RESTRICT |
| 207 | +create table if not exists radio.radios |
| 208 | + (radio_id int constraint pkradios primary key generated always as identity, |
| 209 | + radio_name varchar(100) not null, |
| 210 | + manufacturer_id int not null, |
| 211 | + picture BYTEA null, |
| 212 | + connectortype_id int not null, |
| 213 | + digitalmode_id int null, |
| 214 | + constraint radios_fk_manufacturer foreign key (manufacturer_id) references radio.manufacturers on delete restrict |
| 215 | + ); |
| 216 | + |
| 217 | +--changing it to cascade |
| 218 | +create table if not exists radio.radios |
| 219 | + (radio_id int constraint pkradios primary key generated always as identity, |
| 220 | + radio_name varchar(100) not null, |
| 221 | + manufacturer_id int not null, |
| 222 | + picture BYTEA null, |
| 223 | + connectortype_id int not null, |
| 224 | + digitalmode_id int null, |
| 225 | + constraint radios_fk_manufacturer foreign key (manufacturer_id) references radio.manufacturers on delete cascade |
| 226 | + ); |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +--Check constraints |
| 234 | +CREATE TABLE IF NOT EXISTS radio.bands |
| 235 | +(band_id int CONSTRAINT pkbands PRIMARY KEY GENERATED ALWAYS AS IDENTITY, |
| 236 | + band_name varchar(100) NOT NULL, |
| 237 | + frequency_start_khz numeric(9,2) NOT NULL, |
| 238 | + frequency_end_khz numeric(9,2) NOT NULL, |
| 239 | + country_id int NOT NULL); |
| 240 | + |
| 241 | +alter table radio.bands |
| 242 | +add constraint minfrequency check (frequency_start_khz >= 135.7); |
| 243 | + |
| 244 | + |
| 245 | +--another constraint comparing columns |
| 246 | +ALTER TABLE radio.bands |
| 247 | +ADD CONSTRAINT startlessthanend CHECK (frequency_start_khz < frequency_end_khz); |
| 248 | + |
| 249 | + |
| 250 | +--null constraint |
| 251 | +create table public.nullconstraintcheck |
| 252 | +( |
| 253 | + id int not null, |
| 254 | + value int NULL |
| 255 | +); |
| 256 | + |
| 257 | +--adding constraint |
| 258 | +alter table public.nullconstraintcheck |
| 259 | + add constraint valueEquals1 |
| 260 | + check (value = 1); |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | +--code wil fail |
| 265 | +insert into public.nullconstraintcheck(id, value) |
| 266 | +values (1,1); |
| 267 | +insert into public.nullconstraintcheck(id, value) |
| 268 | +values (2,2); |
| 269 | + |
| 270 | + |
| 271 | +--code will succeed |
| 272 | +insert into public.nullconstraintcheck(id, value) |
| 273 | +values (3,NULL); |
| 274 | + |
| 275 | + |
| 276 | +--changing the table to get better behavior |
| 277 | +alter table public.nullconstraintcheck |
| 278 | + drop constraint valueEquals1; |
| 279 | +alter table public.nullconstraintcheck |
| 280 | + add constraint valueEquals1 |
| 281 | + check ((value is not null and value = 1) |
| 282 | + OR (id < 5 and value = 1)); |
| 283 | + |
| 284 | +insert into public.nullconstraintcheck(id,value) |
| 285 | +values (4,NULL); |
| 286 | + |
| 287 | +insert into public.nullconstraintcheck(id,value) |
| 288 | +values (5,NULL); |
| 289 | + |
| 290 | + |
| 291 | + |
| 292 | +--exclusion constraint |
| 293 | +CREATE EXTENSION btree_gist; |
| 294 | + |
| 295 | + |
| 296 | + |
| 297 | + |
| 298 | +alter table logging.logs |
| 299 | +add constraint uniquecontact exclude |
| 300 | + using gist (log_date with =, |
| 301 | + log_callsign with =, |
| 302 | +log_location with ~=); |
| 303 | + |
| 304 | + |
| 305 | +INSERT INTO logging.logs |
| 306 | +(log_date,log_callsign,log_location) |
| 307 | +VALUES |
| 308 | +('12/21/2022','KC1KCE','35.952, -96.152'), |
| 309 | +('12/21/2022','KC1KCE','35.957, -96.127'); |
| 310 | + |
| 311 | +INSERT INTO logging.logs |
| 312 | +(log_date,log_callsign,log_location) |
| 313 | +VALUES |
| 314 | +('12/21/2022','KC1KCE','35.952, -96.152'); |
| 315 | + |
| 316 | + |
| 317 | + |
| 318 | + |
| 319 | + |
0 commit comments