-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_columns.sql
59 lines (46 loc) · 2.04 KB
/
virtual_columns.sql
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
-- Let's create the `test` datavase, if it doesn't already exist,
-- and a site_user table. Examples will be based on that table.
CREATE DATABASE IF NOT EXISTS test
DEFAULT CHARACTER SET utf8;
USE test;
-- OR REPLACE is a MariaDB syntax, which increases usability and
-- makes replication more robust.
CREATE OR REPLACE TABLE site_user
(
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
email CHAR(200) NOT NULL,
first_name CHAR(50) NOT NULL,
last_name CHAR(50) NOT NULL,
birth_date DATETIME NOT NULL,
PRIMARY KEY (id)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET utf8
;
-- sometimes we want to extract users fullname or birth year,
-- but we don't want to repeat the SQL expressions every time
ALTER TABLE site_user
ADD COLUMN full_name CHAR(100)
AS (CONCAT(first_name, ' ', last_name)) VIRTUAL;
ALTER TABLE site_user
ADD COLUMN birth_year TINYINT UNSIGNED
AS (YEAR(birth_date)) VIRTUAL;
-- users may not know how a last name is writte. for example:
-- dell'uomo or delluomo? digiovanni or di giovanni?
-- remove apostrophs and spaces and search normalized_name
ALTER TABLE site_user
ADD COLUMN normalized_last_name CHAR(50) AS (
REPLACE(REPLACE(last_name, ' ', ''), '''', '')
) PERSISTENT,
ADD INDEX idx_normalized_last_name (normalized_last_name);
INSERT INTO site_user (email, first_name, last_name, birth_date)
VALUES ('[email protected]', 'Pico', 'de Paperis', DATE '1950-12-01');
SELECT * FROM site_user WHERE normalized_last_name LIKE 'depaperis';
-- make sure that [email protected] is recognized as a duplicate of [email protected]
-- and thus rejected
ALTER TABLE site_user
ADD COLUMN normalized_email CHAR (200) AS (LOWER(email)) PERSISTENT,
ADD UNIQUE unq_normalized_email (normalized_email);
-- should return a duplicate key error (1062)
INSERT INTO site_user (email, first_name, last_name, birth_date)
VALUES ('[email protected]', 'Pico', 'de Paperis', DATE '1950-12-01');