Skip to content

Commit 185af3e

Browse files
Merge pull request #259 from oracle-devrel/Ulrike-23c-domains
Ulrike 23c domains
2 parents 890a4dd + f5ec811 commit 185af3e

File tree

8 files changed

+201
-0
lines changed

8 files changed

+201
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
REM Script for 23c: 1-creating-domain.sql
2+
3+
-- optional drop the domain.
4+
-- Using FORCE in contrast to PRESERVE disassociates the domain from all its dependent columns
5+
6+
drop domain myemail_domain;
7+
8+
-- create a domain to describe an email
9+
10+
create domain if not exists myemail_domain AS VARCHAR2(100)
11+
default on null 'XXXX' || '@missingmail.com'
12+
constraint email_c CHECK (regexp_like (myemail_domain, '^(\S+)\@(\S+)\.(\S+)$'))
13+
display substr(myemail_domain, instr(myemail_domain, '@') + 1);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
REM Script for 23c: 2-domains-in-tables.sql
2+
3+
-- optional drop the table person
4+
drop table if exists person;
5+
6+
-- create table person with domain myemail_domain
7+
create table person
8+
( p_id number(5),
9+
p_name varchar2(50),
10+
p_sal number,
11+
p_email varchar2(100) domain myemail_domain
12+
)
13+
annotations (display 'person_table');
14+
15+
-- add some rows
16+
insert into person values (1,'Bold',3000,null);
17+
insert into person values (1,'Schulte',1000, '[email protected]');
18+
insert into person values (1,'Walter',1000,'twalter@t_online.de');
19+
insert into person values (1,'Schwinn',1000, '[email protected]');
20+
insert into person values (1,'King',1000, '[email protected]');
21+
commit;
22+
23+
-- try to add invalid data
24+
insert into person values (1,'Schulte',3000, 'mschulte%gmx.net');
25+
26+
-- query the table person
27+
desc person
28+
29+
select * from person;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
REM script for 23c: 3-using-domain-function-display.sql
2+
3+
-- use domain function domain_display
4+
5+
col p_name format a25
6+
col DISPLAY format a25
7+
select p_name, domain_display(p_email) "Display" from person;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
REM script for 23c: 4-monitor-domains.sql
2+
3+
-- use USER_DOMAINS and USER_DOMAIN_CONSTRAINTS to get information about domains
4+
5+
col owner format a15
6+
col name format a15
7+
select owner, name, data_display
8+
from user_domains;
9+
10+
col domain_owner format a15
11+
col domain_name format a15
12+
select * from user_domain_constraints
13+
where domain_name='MYEMAIL_DOMAIN';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
REM script for 23c: 5-domain-and-dbms-metadata.sql
2+
REM to get the DDL command we use DBMS_METADATA
3+
4+
-- use GET_DDL and SQL_DOMAIN as an object_type argument
5+
6+
set longc 1000
7+
set long 1000
8+
9+
select dbms_metadata.get_ddl('SQL_DOMAIN', 'MYEMAIL_DOMAIN') from dual;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
REM script for 23c: 6-using-builtin-domains.sql
2+
REM Oracle provides built-in domains you can use directly on table columns.
3+
4+
-- query ALL_DOMAINS and filter on SYS
5+
select name from all_domains where owner='SYS';
6+
7+
-- Let's investigate the domain EMAIL_D
8+
9+
set long 1000
10+
set longc 1000
11+
select dbms_metadata.get_ddl('SQL_DOMAIN', 'EMAIL_D','SYS') domain_ddl from dual;
12+
13+
-- Now let's re-create the table PERSON.
14+
-- Please keep in mind that we need to adjust the length of the column P_EMAIL to 4000 - otherwise you will receive the following error:ORA-11517: the column data type does not match the domain column
15+
16+
drop table person;
17+
18+
create table person
19+
( p_id number(5),
20+
p_name varchar2(50),
21+
p_sal number,
22+
p_email varchar2(4000) domain EMAIL_D
23+
)
24+
annotations (display 'person_table');
25+
26+
--let's insert valid and invalid data
27+
28+
insert into person values (1,'Bold',3000,null);
29+
30+
insert into person values (1,'Schulte',1000, '[email protected]');
31+
32+
insert into person values (1,'Walter',1000,'twalter@t_online.de');
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
REM script for 23c: 7-validate-json-with-domains.sql
2+
3+
-- create a domain p_recorddomain to validate a given JSON schema
4+
5+
drop domain p_recorddomain;
6+
7+
create domain p_recorddomain AS JSON VALIDATE USING '{
8+
"type": "object",
9+
"properties": {
10+
"first_name": { "type": "string" },
11+
"last_name": { "type": "string" },
12+
"birthday": { "type": "string", "format": "date" },
13+
"address": {
14+
"type": "object",
15+
"properties": {
16+
"street_address": { "type": "string" },
17+
"city": { "type": "string" },
18+
"state": { "type": "string" },
19+
"country": { "type" : "string" }
20+
}
21+
}
22+
}
23+
}' ;
24+
25+
-- a check constraint is automatically created.
26+
27+
set long 1000
28+
col name format a20
29+
select name, generated, constraint_type, search_condition
30+
from user_domain_constraints where domain_name like 'P_RECORD%';
31+
32+
33+
-- create a table person
34+
35+
drop table person;
36+
37+
create table person (id NUMBER,
38+
p_record JSON DOMAIN p_recorddomain);
39+
40+
41+
-- insert valid and invalid data
42+
43+
insert into person values (1, '{
44+
"first_name": "George",
45+
"last_name": "Washington",
46+
"birthday": "1732-02-22",
47+
"address": {
48+
"street_address": "3200 Mount Vernon Memorial Highway",
49+
"city": "Mount Vernon",
50+
"state": "Virginia",
51+
"country": "United States"
52+
}
53+
}');
54+
55+
-- try to insert invalid data
56+
57+
insert into person values (2, '{
58+
"name": "George Washington",
59+
"birthday": "February 22, 1732",
60+
"address": "Mount Vernon, Virginia, United States"
61+
}');
62+
63+
64+
65+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SQL DOMAINS
2+
3+
A SQL domain (also called usage domain or domain) is a dictionary object that belongs to a schema and encapsulates a set of optional properties and constraints for common values. After you define a SQL domain, you can define table columns to be associated with that domain,
4+
thereby explicitly applying the domain's optional properties and constraints to those columns. SQL domains are used to provide additional information to a stored column (JSON or relational), and therefore, used to define and validate data.
5+
Oracle's SQL domain implementation provides a wider range of functions than the one specified in the SQL standard (see Part 2 Foundation (SQL Foundation)).
6+
7+
8+
## Useful Links
9+
10+
### Documentation
11+
12+
- [SQL Language Reference 23c](https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/create-domain.html#GUID-17D3A9C6-D993-4E94-BF6B-CACA56581F41)
13+
- [Database Development Guide](https://docs.oracle.com/en/database/oracle/oracle-database/23/adfns/registering-application-data-usage-database.html#GUID-6F630041-B7AE-4183-9F97-E54682CA6319)
14+
15+
16+
### Team Publications
17+
18+
- [Less coding using new SQL Domains in 23c](https://blogs.oracle.com/coretec/post/less-coding-with-sql-domains-in-23c)
19+
- [Oracle Database 23c Free Developer Release - 10 features you should know](https://blogs.oracle.com/coretec/post/oracle-database-23c-free-developer-sql)
20+
21+
22+
### LiveLabs Workshops
23+
24+
- [coming soon]
25+
26+
27+
# License
28+
29+
Copyright (c) 2023 Oracle and/or its affiliates.
30+
31+
Licensed under the Universal Permissive License (UPL), Version 1.0.
32+
33+
See LICENSE for more details.

0 commit comments

Comments
 (0)